blob: 460dd512eebd56290c0ad9736ef20eb94150aa9f [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"
Chris Mason39279cc2007-06-12 06:35:45 -040053
54struct btrfs_iget_args {
55 u64 ino;
56 struct btrfs_root *root;
57};
58
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070059static const struct inode_operations btrfs_dir_inode_operations;
60static const struct inode_operations btrfs_symlink_inode_operations;
61static const struct inode_operations btrfs_dir_ro_inode_operations;
62static const struct inode_operations btrfs_special_inode_operations;
63static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070064static const struct address_space_operations btrfs_aops;
65static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070066static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050067static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040068
69static struct kmem_cache *btrfs_inode_cachep;
70struct kmem_cache *btrfs_trans_handle_cachep;
71struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040072struct kmem_cache *btrfs_path_cachep;
73
74#define S_SHIFT 12
75static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
76 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
77 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
78 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
79 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
80 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
81 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
82 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
83};
84
Josef Bacik7b128762008-07-24 12:17:14 -040085static void btrfs_truncate(struct inode *inode);
Chris Masonc8b97812008-10-29 14:49:59 -040086static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
Chris Mason771ed682008-11-06 22:02:51 -050087static noinline int cow_file_range(struct inode *inode,
88 struct page *locked_page,
89 u64 start, u64 end, int *page_started,
90 unsigned long *nr_written, int unlock);
Josef Bacik7b128762008-07-24 12:17:14 -040091
Yan, Zhengf34f57a2009-11-12 09:35:27 +000092static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
93 struct inode *inode, struct inode *dir)
Jim Owens0279b4c2009-02-04 09:29:13 -050094{
95 int err;
96
Yan, Zhengf34f57a2009-11-12 09:35:27 +000097 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -050098 if (!err)
Yan, Zhengf34f57a2009-11-12 09:35:27 +000099 err = btrfs_xattr_security_init(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500100 return err;
101}
102
Chris Masond352ac62008-09-29 15:18:18 -0400103/*
Chris Masonc8b97812008-10-29 14:49:59 -0400104 * this does all the hard work for inserting an inline extent into
105 * the btree. The caller should have done a btrfs_drop_extents so that
106 * no overlapping inline items exist in the btree
107 */
Chris Masond3977122009-01-05 21:25:51 -0500108static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400109 struct btrfs_root *root, struct inode *inode,
110 u64 start, size_t size, size_t compressed_size,
111 struct page **compressed_pages)
112{
113 struct btrfs_key key;
114 struct btrfs_path *path;
115 struct extent_buffer *leaf;
116 struct page *page = NULL;
117 char *kaddr;
118 unsigned long ptr;
119 struct btrfs_file_extent_item *ei;
120 int err = 0;
121 int ret;
122 size_t cur_size = size;
123 size_t datasize;
124 unsigned long offset;
125 int use_compress = 0;
126
127 if (compressed_size && compressed_pages) {
128 use_compress = 1;
129 cur_size = compressed_size;
130 }
131
Chris Masond3977122009-01-05 21:25:51 -0500132 path = btrfs_alloc_path();
133 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400134 return -ENOMEM;
135
Chris Masonb9473432009-03-13 11:00:37 -0400136 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400137 btrfs_set_trans_block_group(trans, inode);
138
139 key.objectid = inode->i_ino;
140 key.offset = start;
141 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400142 datasize = btrfs_file_extent_calc_inline_size(cur_size);
143
144 inode_add_bytes(inode, size);
145 ret = btrfs_insert_empty_item(trans, root, path, &key,
146 datasize);
147 BUG_ON(ret);
148 if (ret) {
149 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400150 goto fail;
151 }
152 leaf = path->nodes[0];
153 ei = btrfs_item_ptr(leaf, path->slots[0],
154 struct btrfs_file_extent_item);
155 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
156 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
157 btrfs_set_file_extent_encryption(leaf, ei, 0);
158 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
159 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
160 ptr = btrfs_file_extent_inline_start(ei);
161
162 if (use_compress) {
163 struct page *cpage;
164 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500165 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400166 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500167 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400168 PAGE_CACHE_SIZE);
169
Chris Masonb9473432009-03-13 11:00:37 -0400170 kaddr = kmap_atomic(cpage, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400171 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Chris Masonb9473432009-03-13 11:00:37 -0400172 kunmap_atomic(kaddr, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400173
174 i++;
175 ptr += cur_size;
176 compressed_size -= cur_size;
177 }
178 btrfs_set_file_extent_compression(leaf, ei,
179 BTRFS_COMPRESS_ZLIB);
180 } else {
181 page = find_get_page(inode->i_mapping,
182 start >> PAGE_CACHE_SHIFT);
183 btrfs_set_file_extent_compression(leaf, ei, 0);
184 kaddr = kmap_atomic(page, KM_USER0);
185 offset = start & (PAGE_CACHE_SIZE - 1);
186 write_extent_buffer(leaf, kaddr + offset, ptr, size);
187 kunmap_atomic(kaddr, KM_USER0);
188 page_cache_release(page);
189 }
190 btrfs_mark_buffer_dirty(leaf);
191 btrfs_free_path(path);
192
Yan, Zhengc21677542009-11-12 09:34:21 +0000193 /*
194 * we're an inline extent, so nobody can
195 * extend the file past i_size without locking
196 * a page we already have locked.
197 *
198 * We must do any isize and inode updates
199 * before we unlock the pages. Otherwise we
200 * could end up racing with unlink.
201 */
Chris Masonc8b97812008-10-29 14:49:59 -0400202 BTRFS_I(inode)->disk_i_size = inode->i_size;
203 btrfs_update_inode(trans, root, inode);
Yan, Zhengc21677542009-11-12 09:34:21 +0000204
Chris Masonc8b97812008-10-29 14:49:59 -0400205 return 0;
206fail:
207 btrfs_free_path(path);
208 return err;
209}
210
211
212/*
213 * conditionally insert an inline extent into the file. This
214 * does the checks required to make sure the data is small enough
215 * to fit as an inline extent.
216 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400217static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400218 struct btrfs_root *root,
219 struct inode *inode, u64 start, u64 end,
220 size_t compressed_size,
221 struct page **compressed_pages)
222{
223 u64 isize = i_size_read(inode);
224 u64 actual_end = min(end + 1, isize);
225 u64 inline_len = actual_end - start;
226 u64 aligned_end = (end + root->sectorsize - 1) &
227 ~((u64)root->sectorsize - 1);
228 u64 hint_byte;
229 u64 data_len = inline_len;
230 int ret;
231
232 if (compressed_size)
233 data_len = compressed_size;
234
235 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400236 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400237 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
238 (!compressed_size &&
239 (actual_end & (root->sectorsize - 1)) == 0) ||
240 end + 1 < isize ||
241 data_len > root->fs_info->max_inline) {
242 return 1;
243 }
244
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000245 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
Chris Masona1ed8352009-09-11 12:27:37 -0400246 &hint_byte, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400247 BUG_ON(ret);
248
249 if (isize > actual_end)
250 inline_len = min_t(u64, isize, actual_end);
251 ret = insert_inline_extent(trans, root, inode, start,
252 inline_len, compressed_size,
253 compressed_pages);
254 BUG_ON(ret);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400255 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400256 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400257 return 0;
258}
259
Chris Mason771ed682008-11-06 22:02:51 -0500260struct async_extent {
261 u64 start;
262 u64 ram_size;
263 u64 compressed_size;
264 struct page **pages;
265 unsigned long nr_pages;
266 struct list_head list;
267};
268
269struct async_cow {
270 struct inode *inode;
271 struct btrfs_root *root;
272 struct page *locked_page;
273 u64 start;
274 u64 end;
275 struct list_head extents;
276 struct btrfs_work work;
277};
278
279static noinline int add_async_extent(struct async_cow *cow,
280 u64 start, u64 ram_size,
281 u64 compressed_size,
282 struct page **pages,
283 unsigned long nr_pages)
284{
285 struct async_extent *async_extent;
286
287 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
288 async_extent->start = start;
289 async_extent->ram_size = ram_size;
290 async_extent->compressed_size = compressed_size;
291 async_extent->pages = pages;
292 async_extent->nr_pages = nr_pages;
293 list_add_tail(&async_extent->list, &cow->extents);
294 return 0;
295}
296
Chris Masonc8b97812008-10-29 14:49:59 -0400297/*
Chris Mason771ed682008-11-06 22:02:51 -0500298 * we create compressed extents in two phases. The first
299 * phase compresses a range of pages that have already been
300 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400301 *
Chris Mason771ed682008-11-06 22:02:51 -0500302 * This is done inside an ordered work queue, and the compression
303 * is spread across many cpus. The actual IO submission is step
304 * two, and the ordered work queue takes care of making sure that
305 * happens in the same order things were put onto the queue by
306 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400307 *
Chris Mason771ed682008-11-06 22:02:51 -0500308 * If this code finds it can't get good compression, it puts an
309 * entry onto the work queue to write the uncompressed bytes. This
310 * makes sure that both compressed inodes and uncompressed inodes
311 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400312 */
Chris Mason771ed682008-11-06 22:02:51 -0500313static noinline int compress_file_range(struct inode *inode,
314 struct page *locked_page,
315 u64 start, u64 end,
316 struct async_cow *async_cow,
317 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400318{
319 struct btrfs_root *root = BTRFS_I(inode)->root;
320 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400321 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -0400322 u64 orig_start;
323 u64 disk_num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400324 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400325 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500326 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400327 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400328 struct page **pages = NULL;
329 unsigned long nr_pages;
330 unsigned long nr_pages_ret = 0;
331 unsigned long total_compressed = 0;
332 unsigned long total_in = 0;
333 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500334 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400335 int i;
336 int will_compress;
Chris Masonb888db22007-08-27 16:49:44 -0400337
Chris Masonc8b97812008-10-29 14:49:59 -0400338 orig_start = start;
Chris Masonbe20aa92007-12-17 20:14:01 -0500339
Chris Mason42dc7ba2008-12-15 11:44:56 -0500340 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400341again:
342 will_compress = 0;
343 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
344 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
345
Chris Masonf03d9302009-02-04 09:31:06 -0500346 /*
347 * we don't want to send crud past the end of i_size through
348 * compression, that's just a waste of CPU time. So, if the
349 * end of the file is before the start of our current
350 * requested range of bytes, we bail out to the uncompressed
351 * cleanup code that can deal with all of this.
352 *
353 * It isn't really the fastest way to fix things, but this is a
354 * very uncommon corner.
355 */
356 if (actual_end <= start)
357 goto cleanup_and_bail_uncompressed;
358
Chris Masonc8b97812008-10-29 14:49:59 -0400359 total_compressed = actual_end - start;
360
361 /* we want to make sure that amount of ram required to uncompress
362 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500363 * of a compressed extent to 128k. This is a crucial number
364 * because it also controls how easily we can spread reads across
365 * cpus for decompression.
366 *
367 * We also want to make sure the amount of IO required to do
368 * a random read is reasonably small, so we limit the size of
369 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400370 */
371 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400372 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500373 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400374 disk_num_bytes = num_bytes;
375 total_in = 0;
376 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400377
Chris Mason771ed682008-11-06 22:02:51 -0500378 /*
379 * we do compression for mount -o compress and when the
380 * inode has not been flagged as nocompress. This flag can
381 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400382 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200383 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500384 (btrfs_test_opt(root, COMPRESS) ||
385 (BTRFS_I(inode)->force_compress))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400386 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400387 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400388
Chris Masonc8b97812008-10-29 14:49:59 -0400389 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
390 total_compressed, pages,
391 nr_pages, &nr_pages_ret,
392 &total_in,
393 &total_compressed,
394 max_compressed);
395
396 if (!ret) {
397 unsigned long offset = total_compressed &
398 (PAGE_CACHE_SIZE - 1);
399 struct page *page = pages[nr_pages_ret - 1];
400 char *kaddr;
401
402 /* zero the tail end of the last page, we might be
403 * sending it down to disk
404 */
405 if (offset) {
406 kaddr = kmap_atomic(page, KM_USER0);
407 memset(kaddr + offset, 0,
408 PAGE_CACHE_SIZE - offset);
409 kunmap_atomic(kaddr, KM_USER0);
410 }
411 will_compress = 1;
412 }
413 }
414 if (start == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500415 trans = btrfs_join_transaction(root, 1);
416 BUG_ON(!trans);
417 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400418 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500419
Chris Masonc8b97812008-10-29 14:49:59 -0400420 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500421 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400422 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500423 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400424 */
425 ret = cow_file_range_inline(trans, root, inode,
426 start, end, 0, NULL);
427 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500428 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400429 ret = cow_file_range_inline(trans, root, inode,
430 start, end,
431 total_compressed, pages);
432 }
433 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500434 /*
435 * inline extent creation worked, we don't need
436 * to create any more async work items. Unlock
437 * and free up our temp pages.
438 */
Chris Masonc8b97812008-10-29 14:49:59 -0400439 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400440 &BTRFS_I(inode)->io_tree,
441 start, end, NULL,
442 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400443 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400444 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc21677542009-11-12 09:34:21 +0000445
446 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400447 goto free_pages_out;
448 }
Yan, Zhengc21677542009-11-12 09:34:21 +0000449 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400450 }
451
452 if (will_compress) {
453 /*
454 * we aren't doing an inline extent round the compressed size
455 * up to a block size boundary so the allocator does sane
456 * things
457 */
458 total_compressed = (total_compressed + blocksize - 1) &
459 ~(blocksize - 1);
460
461 /*
462 * one last check to make sure the compression is really a
463 * win, compare the page count read with the blocks on disk
464 */
465 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
466 ~(PAGE_CACHE_SIZE - 1);
467 if (total_compressed >= total_in) {
468 will_compress = 0;
469 } else {
470 disk_num_bytes = total_compressed;
471 num_bytes = total_in;
472 }
473 }
474 if (!will_compress && pages) {
475 /*
476 * the compression code ran but failed to make things smaller,
477 * free any pages it allocated and our page pointer array
478 */
479 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400480 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400481 page_cache_release(pages[i]);
482 }
483 kfree(pages);
484 pages = NULL;
485 total_compressed = 0;
486 nr_pages_ret = 0;
487
488 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500489 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
490 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500491 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500492 }
Chris Masonc8b97812008-10-29 14:49:59 -0400493 }
Chris Mason771ed682008-11-06 22:02:51 -0500494 if (will_compress) {
495 *num_added += 1;
496
497 /* the async work queues will take care of doing actual
498 * allocation on disk for these compressed pages,
499 * and will submit them to the elevator.
500 */
501 add_async_extent(async_cow, start, num_bytes,
502 total_compressed, pages, nr_pages_ret);
503
Chris Mason42dc7ba2008-12-15 11:44:56 -0500504 if (start + num_bytes < end && start + num_bytes < actual_end) {
Chris Mason771ed682008-11-06 22:02:51 -0500505 start += num_bytes;
506 pages = NULL;
507 cond_resched();
508 goto again;
509 }
510 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500511cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500512 /*
513 * No compression, but we still need to write the pages in
514 * the file we've been given so far. redirty the locked
515 * page if it corresponds to our extent and set things up
516 * for the async work queue to run cow_file_range to do
517 * the normal delalloc dance
518 */
519 if (page_offset(locked_page) >= start &&
520 page_offset(locked_page) <= end) {
521 __set_page_dirty_nobuffers(locked_page);
522 /* unlocked later on in the async handlers */
523 }
524 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
525 *num_added += 1;
526 }
527
528out:
529 return 0;
530
531free_pages_out:
532 for (i = 0; i < nr_pages_ret; i++) {
533 WARN_ON(pages[i]->mapping);
534 page_cache_release(pages[i]);
535 }
Chris Masond3977122009-01-05 21:25:51 -0500536 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500537
538 goto out;
539}
540
541/*
542 * phase two of compressed writeback. This is the ordered portion
543 * of the code, which only gets called in the order the work was
544 * queued. We walk all the async extents created by compress_file_range
545 * and send them down to the disk.
546 */
547static noinline int submit_compressed_extents(struct inode *inode,
548 struct async_cow *async_cow)
549{
550 struct async_extent *async_extent;
551 u64 alloc_hint = 0;
552 struct btrfs_trans_handle *trans;
553 struct btrfs_key ins;
554 struct extent_map *em;
555 struct btrfs_root *root = BTRFS_I(inode)->root;
556 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
557 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500558 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500559
560 if (list_empty(&async_cow->extents))
561 return 0;
562
Chris Mason771ed682008-11-06 22:02:51 -0500563
Chris Masond3977122009-01-05 21:25:51 -0500564 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500565 async_extent = list_entry(async_cow->extents.next,
566 struct async_extent, list);
567 list_del(&async_extent->list);
568
569 io_tree = &BTRFS_I(inode)->io_tree;
570
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500571retry:
Chris Mason771ed682008-11-06 22:02:51 -0500572 /* did the compression code fall back to uncompressed IO? */
573 if (!async_extent->pages) {
574 int page_started = 0;
575 unsigned long nr_written = 0;
576
577 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000578 async_extent->start +
579 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500580
581 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500582 ret = cow_file_range(inode, async_cow->locked_page,
583 async_extent->start,
584 async_extent->start +
585 async_extent->ram_size - 1,
586 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500587
588 /*
589 * if page_started, cow_file_range inserted an
590 * inline extent and took care of all the unlocking
591 * and IO for us. Otherwise, we need to submit
592 * all those pages down to the drive.
593 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500594 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500595 extent_write_locked_range(io_tree,
596 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500597 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500598 async_extent->ram_size - 1,
599 btrfs_get_extent,
600 WB_SYNC_ALL);
601 kfree(async_extent);
602 cond_resched();
603 continue;
604 }
605
606 lock_extent(io_tree, async_extent->start,
607 async_extent->start + async_extent->ram_size - 1,
608 GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500609
Yan, Zhengc21677542009-11-12 09:34:21 +0000610 trans = btrfs_join_transaction(root, 1);
Chris Mason771ed682008-11-06 22:02:51 -0500611 ret = btrfs_reserve_extent(trans, root,
612 async_extent->compressed_size,
613 async_extent->compressed_size,
614 0, alloc_hint,
615 (u64)-1, &ins, 1);
Yan, Zhengc21677542009-11-12 09:34:21 +0000616 btrfs_end_transaction(trans, root);
617
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500618 if (ret) {
619 int i;
620 for (i = 0; i < async_extent->nr_pages; i++) {
621 WARN_ON(async_extent->pages[i]->mapping);
622 page_cache_release(async_extent->pages[i]);
623 }
624 kfree(async_extent->pages);
625 async_extent->nr_pages = 0;
626 async_extent->pages = NULL;
627 unlock_extent(io_tree, async_extent->start,
628 async_extent->start +
629 async_extent->ram_size - 1, GFP_NOFS);
630 goto retry;
631 }
632
Yan, Zhengc21677542009-11-12 09:34:21 +0000633 /*
634 * here we're doing allocation and writeback of the
635 * compressed pages
636 */
637 btrfs_drop_extent_cache(inode, async_extent->start,
638 async_extent->start +
639 async_extent->ram_size - 1, 0);
640
Chris Mason771ed682008-11-06 22:02:51 -0500641 em = alloc_extent_map(GFP_NOFS);
642 em->start = async_extent->start;
643 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500644 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500645
646 em->block_start = ins.objectid;
647 em->block_len = ins.offset;
648 em->bdev = root->fs_info->fs_devices->latest_bdev;
649 set_bit(EXTENT_FLAG_PINNED, &em->flags);
650 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
651
Chris Masond3977122009-01-05 21:25:51 -0500652 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400653 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500654 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400655 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500656 if (ret != -EEXIST) {
657 free_extent_map(em);
658 break;
659 }
660 btrfs_drop_extent_cache(inode, async_extent->start,
661 async_extent->start +
662 async_extent->ram_size - 1, 0);
663 }
664
665 ret = btrfs_add_ordered_extent(inode, async_extent->start,
666 ins.objectid,
667 async_extent->ram_size,
668 ins.offset,
669 BTRFS_ORDERED_COMPRESSED);
670 BUG_ON(ret);
671
Chris Mason771ed682008-11-06 22:02:51 -0500672 /*
673 * clear dirty, set writeback and unlock the pages.
674 */
675 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400676 &BTRFS_I(inode)->io_tree,
677 async_extent->start,
678 async_extent->start +
679 async_extent->ram_size - 1,
680 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
681 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400682 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400683 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500684
685 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500686 async_extent->start,
687 async_extent->ram_size,
688 ins.objectid,
689 ins.offset, async_extent->pages,
690 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500691
692 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -0500693 alloc_hint = ins.objectid + ins.offset;
694 kfree(async_extent);
695 cond_resched();
696 }
697
Chris Mason771ed682008-11-06 22:02:51 -0500698 return 0;
699}
700
701/*
702 * when extent_io.c finds a delayed allocation range in the file,
703 * the call backs end up in this code. The basic idea is to
704 * allocate extents on disk for the range, and create ordered data structs
705 * in ram to track those extents.
706 *
707 * locked_page is the page that writepage had locked already. We use
708 * it to make sure we don't do extra locks or unlocks.
709 *
710 * *page_started is set to one if we unlock locked_page and do everything
711 * required to start IO on it. It may be clean and already done with
712 * IO when we return.
713 */
714static noinline int cow_file_range(struct inode *inode,
715 struct page *locked_page,
716 u64 start, u64 end, int *page_started,
717 unsigned long *nr_written,
718 int unlock)
719{
720 struct btrfs_root *root = BTRFS_I(inode)->root;
721 struct btrfs_trans_handle *trans;
722 u64 alloc_hint = 0;
723 u64 num_bytes;
724 unsigned long ram_size;
725 u64 disk_num_bytes;
726 u64 cur_alloc_size;
727 u64 blocksize = root->sectorsize;
728 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500729 u64 isize = i_size_read(inode);
Chris Mason771ed682008-11-06 22:02:51 -0500730 struct btrfs_key ins;
731 struct extent_map *em;
732 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
733 int ret = 0;
734
735 trans = btrfs_join_transaction(root, 1);
736 BUG_ON(!trans);
737 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400738 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500739
Chris Mason42dc7ba2008-12-15 11:44:56 -0500740 actual_end = min_t(u64, isize, end + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500741
742 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
743 num_bytes = max(blocksize, num_bytes);
744 disk_num_bytes = num_bytes;
745 ret = 0;
746
747 if (start == 0) {
748 /* lets try to make an inline extent */
749 ret = cow_file_range_inline(trans, root, inode,
750 start, end, 0, NULL);
751 if (ret == 0) {
752 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400753 &BTRFS_I(inode)->io_tree,
754 start, end, NULL,
755 EXTENT_CLEAR_UNLOCK_PAGE |
756 EXTENT_CLEAR_UNLOCK |
757 EXTENT_CLEAR_DELALLOC |
758 EXTENT_CLEAR_DIRTY |
759 EXTENT_SET_WRITEBACK |
760 EXTENT_END_WRITEBACK);
Yan, Zhengc21677542009-11-12 09:34:21 +0000761
Chris Mason771ed682008-11-06 22:02:51 -0500762 *nr_written = *nr_written +
763 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
764 *page_started = 1;
765 ret = 0;
766 goto out;
767 }
768 }
Chris Masonc8b97812008-10-29 14:49:59 -0400769
770 BUG_ON(disk_num_bytes >
771 btrfs_super_total_bytes(&root->fs_info->super_copy));
772
Chris Masonb917b7c2009-09-18 16:07:03 -0400773
774 read_lock(&BTRFS_I(inode)->extent_tree.lock);
775 em = search_extent_mapping(&BTRFS_I(inode)->extent_tree,
776 start, num_bytes);
777 if (em) {
Josef Bacik6346c932009-11-10 21:23:47 -0500778 /*
779 * if block start isn't an actual block number then find the
780 * first block in this inode and use that as a hint. If that
781 * block is also bogus then just don't worry about it.
782 */
783 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
784 free_extent_map(em);
785 em = search_extent_mapping(em_tree, 0, 0);
786 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
787 alloc_hint = em->block_start;
788 if (em)
789 free_extent_map(em);
790 } else {
791 alloc_hint = em->block_start;
792 free_extent_map(em);
793 }
Chris Masonb917b7c2009-09-18 16:07:03 -0400794 }
795 read_unlock(&BTRFS_I(inode)->extent_tree.lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400796 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400797
Chris Masond3977122009-01-05 21:25:51 -0500798 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400799 unsigned long op;
800
Josef Bacik287a0ab2010-03-19 18:07:23 +0000801 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400802 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500803 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400804 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500805 BUG_ON(ret);
806
Chris Masone6dcd2d2008-07-17 12:53:50 -0400807 em = alloc_extent_map(GFP_NOFS);
808 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500809 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500810 ram_size = ins.offset;
811 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400812
Chris Masone6dcd2d2008-07-17 12:53:50 -0400813 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400814 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400815 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400816 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400817
Chris Masond3977122009-01-05 21:25:51 -0500818 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400819 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400820 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400821 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400822 if (ret != -EEXIST) {
823 free_extent_map(em);
824 break;
825 }
826 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400827 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400828 }
829
Chris Mason98d20f62008-04-14 09:46:10 -0400830 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400831 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500832 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400833 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400834
Yan Zheng17d217f2008-12-12 10:03:38 -0500835 if (root->root_key.objectid ==
836 BTRFS_DATA_RELOC_TREE_OBJECTID) {
837 ret = btrfs_reloc_clone_csums(inode, start,
838 cur_alloc_size);
839 BUG_ON(ret);
840 }
841
Chris Masond3977122009-01-05 21:25:51 -0500842 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400843 break;
Chris Masond3977122009-01-05 21:25:51 -0500844
Chris Masonc8b97812008-10-29 14:49:59 -0400845 /* we're not doing compressed IO, don't unlock the first
846 * page (which the caller expects to stay locked), don't
847 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400848 *
849 * Do set the Private2 bit so we know this page was properly
850 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400851 */
Chris Masona791e352009-10-08 11:27:10 -0400852 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
853 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
854 EXTENT_SET_PRIVATE2;
855
Chris Masonc8b97812008-10-29 14:49:59 -0400856 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
857 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400858 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400859 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500860 num_bytes -= cur_alloc_size;
861 alloc_hint = ins.objectid + ins.offset;
862 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400863 }
Chris Masonb888db22007-08-27 16:49:44 -0400864out:
Chris Mason771ed682008-11-06 22:02:51 -0500865 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400866 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400867
Chris Masonbe20aa92007-12-17 20:14:01 -0500868 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500869}
Chris Masonc8b97812008-10-29 14:49:59 -0400870
Chris Mason771ed682008-11-06 22:02:51 -0500871/*
872 * work queue call back to started compression on a file and pages
873 */
874static noinline void async_cow_start(struct btrfs_work *work)
875{
876 struct async_cow *async_cow;
877 int num_added = 0;
878 async_cow = container_of(work, struct async_cow, work);
879
880 compress_file_range(async_cow->inode, async_cow->locked_page,
881 async_cow->start, async_cow->end, async_cow,
882 &num_added);
883 if (num_added == 0)
884 async_cow->inode = NULL;
885}
886
887/*
888 * work queue call back to submit previously compressed pages
889 */
890static noinline void async_cow_submit(struct btrfs_work *work)
891{
892 struct async_cow *async_cow;
893 struct btrfs_root *root;
894 unsigned long nr_pages;
895
896 async_cow = container_of(work, struct async_cow, work);
897
898 root = async_cow->root;
899 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
900 PAGE_CACHE_SHIFT;
901
902 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
903
904 if (atomic_read(&root->fs_info->async_delalloc_pages) <
905 5 * 1042 * 1024 &&
906 waitqueue_active(&root->fs_info->async_submit_wait))
907 wake_up(&root->fs_info->async_submit_wait);
908
Chris Masond3977122009-01-05 21:25:51 -0500909 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500910 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500911}
Chris Masonc8b97812008-10-29 14:49:59 -0400912
Chris Mason771ed682008-11-06 22:02:51 -0500913static noinline void async_cow_free(struct btrfs_work *work)
914{
915 struct async_cow *async_cow;
916 async_cow = container_of(work, struct async_cow, work);
917 kfree(async_cow);
918}
919
920static int cow_file_range_async(struct inode *inode, struct page *locked_page,
921 u64 start, u64 end, int *page_started,
922 unsigned long *nr_written)
923{
924 struct async_cow *async_cow;
925 struct btrfs_root *root = BTRFS_I(inode)->root;
926 unsigned long nr_pages;
927 u64 cur_end;
928 int limit = 10 * 1024 * 1042;
929
Chris Masona3429ab2009-10-08 12:30:20 -0400930 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
931 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500932 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500933 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
934 async_cow->inode = inode;
935 async_cow->root = root;
936 async_cow->locked_page = locked_page;
937 async_cow->start = start;
938
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200939 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -0500940 cur_end = end;
941 else
942 cur_end = min(end, start + 512 * 1024 - 1);
943
944 async_cow->end = cur_end;
945 INIT_LIST_HEAD(&async_cow->extents);
946
947 async_cow->work.func = async_cow_start;
948 async_cow->work.ordered_func = async_cow_submit;
949 async_cow->work.ordered_free = async_cow_free;
950 async_cow->work.flags = 0;
951
Chris Mason771ed682008-11-06 22:02:51 -0500952 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
953 PAGE_CACHE_SHIFT;
954 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
955
956 btrfs_queue_worker(&root->fs_info->delalloc_workers,
957 &async_cow->work);
958
959 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
960 wait_event(root->fs_info->async_submit_wait,
961 (atomic_read(&root->fs_info->async_delalloc_pages) <
962 limit));
963 }
964
Chris Masond3977122009-01-05 21:25:51 -0500965 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500966 atomic_read(&root->fs_info->async_delalloc_pages)) {
967 wait_event(root->fs_info->async_submit_wait,
968 (atomic_read(&root->fs_info->async_delalloc_pages) ==
969 0));
970 }
971
972 *nr_written += nr_pages;
973 start = cur_end + 1;
974 }
975 *page_started = 1;
976 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500977}
978
Chris Masond3977122009-01-05 21:25:51 -0500979static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -0500980 u64 bytenr, u64 num_bytes)
981{
982 int ret;
983 struct btrfs_ordered_sum *sums;
984 LIST_HEAD(list);
985
Yan Zheng07d400a2009-01-06 11:42:00 -0500986 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
987 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -0500988 if (ret == 0 && list_empty(&list))
989 return 0;
990
991 while (!list_empty(&list)) {
992 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
993 list_del(&sums->list);
994 kfree(sums);
995 }
996 return 1;
997}
998
Chris Masond352ac62008-09-29 15:18:18 -0400999/*
1000 * when nowcow writeback call back. This checks for snapshots or COW copies
1001 * of the extents that exist in the file, and COWs the file as required.
1002 *
1003 * If no cow copies or snapshots exist, we write directly to the existing
1004 * blocks on disk
1005 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001006static noinline int run_delalloc_nocow(struct inode *inode,
1007 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001008 u64 start, u64 end, int *page_started, int force,
1009 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001010{
Chris Masonbe20aa92007-12-17 20:14:01 -05001011 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001012 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001013 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001014 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001015 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001016 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001017 u64 cow_start;
1018 u64 cur_offset;
1019 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001020 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001021 u64 disk_bytenr;
1022 u64 num_bytes;
1023 int extent_type;
1024 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001025 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001026 int nocow;
1027 int check_prev = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -05001028
1029 path = btrfs_alloc_path();
1030 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001031 trans = btrfs_join_transaction(root, 1);
1032 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -05001033
Yan Zheng80ff3852008-10-30 14:20:02 -04001034 cow_start = (u64)-1;
1035 cur_offset = start;
1036 while (1) {
1037 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1038 cur_offset, 0);
1039 BUG_ON(ret < 0);
1040 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1041 leaf = path->nodes[0];
1042 btrfs_item_key_to_cpu(leaf, &found_key,
1043 path->slots[0] - 1);
1044 if (found_key.objectid == inode->i_ino &&
1045 found_key.type == BTRFS_EXTENT_DATA_KEY)
1046 path->slots[0]--;
1047 }
1048 check_prev = 0;
1049next_slot:
1050 leaf = path->nodes[0];
1051 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1052 ret = btrfs_next_leaf(root, path);
1053 if (ret < 0)
1054 BUG_ON(1);
1055 if (ret > 0)
1056 break;
1057 leaf = path->nodes[0];
1058 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001059
Yan Zheng80ff3852008-10-30 14:20:02 -04001060 nocow = 0;
1061 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001062 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001063 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001064
Yan Zheng80ff3852008-10-30 14:20:02 -04001065 if (found_key.objectid > inode->i_ino ||
1066 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1067 found_key.offset > end)
1068 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001069
Yan Zheng80ff3852008-10-30 14:20:02 -04001070 if (found_key.offset > cur_offset) {
1071 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001072 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001073 goto out_check;
1074 }
Chris Masonc31f8832008-01-08 15:46:31 -05001075
Yan Zheng80ff3852008-10-30 14:20:02 -04001076 fi = btrfs_item_ptr(leaf, path->slots[0],
1077 struct btrfs_file_extent_item);
1078 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001079
Yan Zhengd899e052008-10-30 14:25:28 -04001080 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1081 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001082 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001083 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001084 extent_end = found_key.offset +
1085 btrfs_file_extent_num_bytes(leaf, fi);
1086 if (extent_end <= start) {
1087 path->slots[0]++;
1088 goto next_slot;
1089 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001090 if (disk_bytenr == 0)
1091 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001092 if (btrfs_file_extent_compression(leaf, fi) ||
1093 btrfs_file_extent_encryption(leaf, fi) ||
1094 btrfs_file_extent_other_encoding(leaf, fi))
1095 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001096 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1097 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001098 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001099 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001100 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001101 found_key.offset -
1102 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001103 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001104 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001105 disk_bytenr += cur_offset - found_key.offset;
1106 num_bytes = min(end + 1, extent_end) - cur_offset;
1107 /*
1108 * force cow if csum exists in the range.
1109 * this ensure that csum for a given extent are
1110 * either valid or do not exist.
1111 */
1112 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1113 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001114 nocow = 1;
1115 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1116 extent_end = found_key.offset +
1117 btrfs_file_extent_inline_len(leaf, fi);
1118 extent_end = ALIGN(extent_end, root->sectorsize);
1119 } else {
1120 BUG_ON(1);
1121 }
1122out_check:
1123 if (extent_end <= start) {
1124 path->slots[0]++;
1125 goto next_slot;
1126 }
1127 if (!nocow) {
1128 if (cow_start == (u64)-1)
1129 cow_start = cur_offset;
1130 cur_offset = extent_end;
1131 if (cur_offset > end)
1132 break;
1133 path->slots[0]++;
1134 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001135 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001136
Yan Zheng7ea394f2008-08-05 13:05:02 -04001137 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001138 if (cow_start != (u64)-1) {
1139 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001140 found_key.offset - 1, page_started,
1141 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001142 BUG_ON(ret);
1143 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001144 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001145
Yan Zhengd899e052008-10-30 14:25:28 -04001146 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1147 struct extent_map *em;
1148 struct extent_map_tree *em_tree;
1149 em_tree = &BTRFS_I(inode)->extent_tree;
1150 em = alloc_extent_map(GFP_NOFS);
1151 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001152 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001153 em->len = num_bytes;
1154 em->block_len = num_bytes;
1155 em->block_start = disk_bytenr;
1156 em->bdev = root->fs_info->fs_devices->latest_bdev;
1157 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1158 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001159 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001160 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001161 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001162 if (ret != -EEXIST) {
1163 free_extent_map(em);
1164 break;
1165 }
1166 btrfs_drop_extent_cache(inode, em->start,
1167 em->start + em->len - 1, 0);
1168 }
1169 type = BTRFS_ORDERED_PREALLOC;
1170 } else {
1171 type = BTRFS_ORDERED_NOCOW;
1172 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001173
1174 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001175 num_bytes, num_bytes, type);
1176 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001177
Yan, Zhengefa56462010-05-16 10:49:59 -04001178 if (root->root_key.objectid ==
1179 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1180 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1181 num_bytes);
1182 BUG_ON(ret);
1183 }
1184
Yan Zhengd899e052008-10-30 14:25:28 -04001185 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001186 cur_offset, cur_offset + num_bytes - 1,
1187 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1188 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1189 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001190 cur_offset = extent_end;
1191 if (cur_offset > end)
1192 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001193 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001194 btrfs_release_path(root, path);
1195
1196 if (cur_offset <= end && cow_start == (u64)-1)
1197 cow_start = cur_offset;
1198 if (cow_start != (u64)-1) {
1199 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001200 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001201 BUG_ON(ret);
1202 }
1203
1204 ret = btrfs_end_transaction(trans, root);
1205 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001206 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001207 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001208}
1209
Chris Masond352ac62008-09-29 15:18:18 -04001210/*
1211 * extent_io.c call back to do delayed allocation processing
1212 */
Chris Masonc8b97812008-10-29 14:49:59 -04001213static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001214 u64 start, u64 end, int *page_started,
1215 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001216{
Chris Masonbe20aa92007-12-17 20:14:01 -05001217 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001218 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001219
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001220 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001221 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001222 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001223 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001224 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001225 page_started, 0, nr_written);
Chris Mason1e701a32010-03-11 09:42:04 -05001226 else if (!btrfs_test_opt(root, COMPRESS) &&
1227 !(BTRFS_I(inode)->force_compress))
Chris Mason7f366cf2009-03-12 20:12:45 -04001228 ret = cow_file_range(inode, locked_page, start, end,
1229 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001230 else
Chris Mason771ed682008-11-06 22:02:51 -05001231 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001232 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001233 return ret;
1234}
1235
Josef Bacik9ed74f22009-09-11 16:12:44 -04001236static int btrfs_split_extent_hook(struct inode *inode,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001237 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001238{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001239 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001240 if (!(orig->state & EXTENT_DELALLOC))
1241 return 0;
1242
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001243 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001244 return 0;
1245}
1246
1247/*
1248 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1249 * extents so we can keep track of new extents that are just merged onto old
1250 * extents, such as when we are doing sequential writes, so we can properly
1251 * account for the metadata space we'll need.
1252 */
1253static int btrfs_merge_extent_hook(struct inode *inode,
1254 struct extent_state *new,
1255 struct extent_state *other)
1256{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001257 /* not delalloc, ignore it */
1258 if (!(other->state & EXTENT_DELALLOC))
1259 return 0;
1260
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001261 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001262 return 0;
1263}
1264
Chris Masond352ac62008-09-29 15:18:18 -04001265/*
1266 * extent_io.c set_bit_hook, used to track delayed allocation
1267 * bytes in this file, and to maintain the list of inodes that
1268 * have pending delalloc work to be done.
1269 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001270static int btrfs_set_bit_hook(struct inode *inode,
1271 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001272{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001273
Chris Mason75eff682008-12-15 15:54:40 -05001274 /*
1275 * set_bit and clear bit hooks normally require _irqsave/restore
1276 * but in this case, we are only testeing for the DELALLOC
1277 * bit, which is only set or cleared with irqs on
1278 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001279 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001280 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001281 u64 len = state->end + 1 - state->start;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001282
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001283 if (*bits & EXTENT_FIRST_DELALLOC)
1284 *bits &= ~EXTENT_FIRST_DELALLOC;
1285 else
1286 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001287
Chris Mason75eff682008-12-15 15:54:40 -05001288 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001289 BTRFS_I(inode)->delalloc_bytes += len;
1290 root->fs_info->delalloc_bytes += len;
Chris Masonea8c2812008-08-04 23:17:27 -04001291 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1292 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1293 &root->fs_info->delalloc_inodes);
1294 }
Chris Mason75eff682008-12-15 15:54:40 -05001295 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001296 }
1297 return 0;
1298}
1299
Chris Masond352ac62008-09-29 15:18:18 -04001300/*
1301 * extent_io.c clear_bit_hook, see set_bit_hook for why
1302 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001303static int btrfs_clear_bit_hook(struct inode *inode,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001304 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001305{
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;
Chris Masonbcbfce82008-04-22 13:26:47 -04001314
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001315 if (*bits & EXTENT_FIRST_DELALLOC)
1316 *bits &= ~EXTENT_FIRST_DELALLOC;
1317 else if (!(*bits & EXTENT_DO_ACCOUNTING))
1318 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
1319
1320 if (*bits & EXTENT_DO_ACCOUNTING)
1321 btrfs_delalloc_release_metadata(inode, len);
1322
1323 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID)
1324 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001325
Chris Mason75eff682008-12-15 15:54:40 -05001326 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001327 root->fs_info->delalloc_bytes -= len;
1328 BTRFS_I(inode)->delalloc_bytes -= len;
1329
Chris Masonea8c2812008-08-04 23:17:27 -04001330 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1331 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1332 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1333 }
Chris Mason75eff682008-12-15 15:54:40 -05001334 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001335 }
1336 return 0;
1337}
1338
Chris Masond352ac62008-09-29 15:18:18 -04001339/*
1340 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1341 * we don't create bios that span stripes or chunks
1342 */
Chris Mason239b14b2008-03-24 15:02:07 -04001343int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001344 size_t size, struct bio *bio,
1345 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001346{
1347 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1348 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001349 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001350 u64 length = 0;
1351 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001352 int ret;
1353
Chris Mason771ed682008-11-06 22:02:51 -05001354 if (bio_flags & EXTENT_BIO_COMPRESSED)
1355 return 0;
1356
Chris Masonf2d8d742008-04-21 10:03:05 -04001357 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001358 map_tree = &root->fs_info->mapping_tree;
1359 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001360 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001361 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001362
Chris Masond3977122009-01-05 21:25:51 -05001363 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001364 return 1;
Chris Mason239b14b2008-03-24 15:02:07 -04001365 return 0;
1366}
1367
Chris Masond352ac62008-09-29 15:18:18 -04001368/*
1369 * in order to insert checksums into the metadata in large chunks,
1370 * we wait until bio submission time. All the pages in the bio are
1371 * checksummed and sums are attached onto the ordered extent record.
1372 *
1373 * At IO completion time the cums attached on the ordered extent record
1374 * are inserted into the btree
1375 */
Chris Masond3977122009-01-05 21:25:51 -05001376static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1377 struct bio *bio, int mirror_num,
1378 unsigned long bio_flags)
Chris Mason065631f2008-02-20 12:07:25 -05001379{
Chris Mason065631f2008-02-20 12:07:25 -05001380 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001381 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001382
Chris Masond20f7042008-12-08 16:58:54 -05001383 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001384 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001385 return 0;
1386}
Chris Masone0156402008-04-16 11:15:20 -04001387
Chris Mason4a69a412008-11-06 22:03:00 -05001388/*
1389 * in order to insert checksums into the metadata in large chunks,
1390 * we wait until bio submission time. All the pages in the bio are
1391 * checksummed and sums are attached onto the ordered extent record.
1392 *
1393 * At IO completion time the cums attached on the ordered extent record
1394 * are inserted into the btree
1395 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001396static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Mason4a69a412008-11-06 22:03:00 -05001397 int mirror_num, unsigned long bio_flags)
1398{
1399 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001400 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001401}
1402
Chris Masond352ac62008-09-29 15:18:18 -04001403/*
Chris Masoncad321a2008-12-17 14:51:42 -05001404 * extent_io.c submission hook. This does the right thing for csum calculation
1405 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001406 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001407static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001408 int mirror_num, unsigned long bio_flags)
Chris Mason44b8bd72008-04-16 11:14:51 -04001409{
1410 struct btrfs_root *root = BTRFS_I(inode)->root;
1411 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001412 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001413
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001414 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001415
Chris Masone6dcd2d2008-07-17 12:53:50 -04001416 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1417 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001418
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001419 if (!(rw & (1 << BIO_RW))) {
Chris Masond20f7042008-12-08 16:58:54 -05001420 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001421 return btrfs_submit_compressed_read(inode, bio,
1422 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001423 } else if (!skip_sum)
1424 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001425 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001426 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001427 /* csum items have already been cloned */
1428 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1429 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001430 /* we're doing a write, do the async checksumming */
1431 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001432 inode, rw, bio, mirror_num,
Chris Mason4a69a412008-11-06 22:03:00 -05001433 bio_flags, __btrfs_submit_bio_start,
1434 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001435 }
1436
Chris Mason0b86a832008-03-24 15:01:56 -04001437mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001438 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001439}
Chris Mason6885f302008-02-20 16:11:05 -05001440
Chris Masond352ac62008-09-29 15:18:18 -04001441/*
1442 * given a list of ordered sums record them in the inode. This happens
1443 * at IO completion time based on sums calculated at bio submission time.
1444 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001445static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001446 struct inode *inode, u64 file_offset,
1447 struct list_head *list)
1448{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001449 struct btrfs_ordered_sum *sum;
1450
1451 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001452
1453 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001454 btrfs_csum_file_blocks(trans,
1455 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001456 }
1457 return 0;
1458}
1459
Josef Bacik2ac55d42010-02-03 19:33:23 +00001460int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1461 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001462{
Chris Masond3977122009-01-05 21:25:51 -05001463 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001464 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001465 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001466 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001467}
1468
Chris Masond352ac62008-09-29 15:18:18 -04001469/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001470struct btrfs_writepage_fixup {
1471 struct page *page;
1472 struct btrfs_work work;
1473};
1474
Christoph Hellwigb2950862008-12-02 09:54:17 -05001475static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001476{
1477 struct btrfs_writepage_fixup *fixup;
1478 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001479 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001480 struct page *page;
1481 struct inode *inode;
1482 u64 page_start;
1483 u64 page_end;
1484
1485 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1486 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001487again:
Chris Mason247e7432008-07-17 12:53:51 -04001488 lock_page(page);
1489 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1490 ClearPageChecked(page);
1491 goto out_page;
1492 }
1493
1494 inode = page->mapping->host;
1495 page_start = page_offset(page);
1496 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1497
Josef Bacik2ac55d42010-02-03 19:33:23 +00001498 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1499 &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001500
1501 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001502 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001503 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001504
1505 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1506 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001507 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1508 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001509 unlock_page(page);
1510 btrfs_start_ordered_extent(inode, ordered, 1);
1511 goto again;
1512 }
Chris Mason247e7432008-07-17 12:53:51 -04001513
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001514 BUG();
Josef Bacik2ac55d42010-02-03 19:33:23 +00001515 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001516 ClearPageChecked(page);
1517out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001518 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1519 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001520out_page:
1521 unlock_page(page);
1522 page_cache_release(page);
1523}
1524
1525/*
1526 * There are a few paths in the higher layers of the kernel that directly
1527 * set the page dirty bit without asking the filesystem if it is a
1528 * good idea. This causes problems because we want to make sure COW
1529 * properly happens and the data=ordered rules are followed.
1530 *
Chris Masonc8b97812008-10-29 14:49:59 -04001531 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001532 * hasn't been properly setup for IO. We kick off an async process
1533 * to fix it up. The async helper will wait for ordered extents, set
1534 * the delalloc bit and make it safe to write the page.
1535 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001536static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001537{
1538 struct inode *inode = page->mapping->host;
1539 struct btrfs_writepage_fixup *fixup;
1540 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001541
Chris Mason8b62b722009-09-02 16:53:46 -04001542 /* this page is properly in the ordered list */
1543 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001544 return 0;
1545
1546 if (PageChecked(page))
1547 return -EAGAIN;
1548
1549 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1550 if (!fixup)
1551 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001552
Chris Mason247e7432008-07-17 12:53:51 -04001553 SetPageChecked(page);
1554 page_cache_get(page);
1555 fixup->work.func = btrfs_writepage_fixup_worker;
1556 fixup->page = page;
1557 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1558 return -EAGAIN;
1559}
1560
Yan Zhengd899e052008-10-30 14:25:28 -04001561static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1562 struct inode *inode, u64 file_pos,
1563 u64 disk_bytenr, u64 disk_num_bytes,
1564 u64 num_bytes, u64 ram_bytes,
1565 u8 compression, u8 encryption,
1566 u16 other_encoding, int extent_type)
1567{
1568 struct btrfs_root *root = BTRFS_I(inode)->root;
1569 struct btrfs_file_extent_item *fi;
1570 struct btrfs_path *path;
1571 struct extent_buffer *leaf;
1572 struct btrfs_key ins;
1573 u64 hint;
1574 int ret;
1575
1576 path = btrfs_alloc_path();
1577 BUG_ON(!path);
1578
Chris Masonb9473432009-03-13 11:00:37 -04001579 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001580
1581 /*
1582 * we may be replacing one extent in the tree with another.
1583 * The new extent is pinned in the extent map, and we don't want
1584 * to drop it from the cache until it is completely in the btree.
1585 *
1586 * So, tell btrfs_drop_extents to leave this extent in the cache.
1587 * the caller is expected to unpin it and allow it to be merged
1588 * with the others.
1589 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001590 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1591 &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001592 BUG_ON(ret);
1593
1594 ins.objectid = inode->i_ino;
1595 ins.offset = file_pos;
1596 ins.type = BTRFS_EXTENT_DATA_KEY;
1597 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1598 BUG_ON(ret);
1599 leaf = path->nodes[0];
1600 fi = btrfs_item_ptr(leaf, path->slots[0],
1601 struct btrfs_file_extent_item);
1602 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1603 btrfs_set_file_extent_type(leaf, fi, extent_type);
1604 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1605 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1606 btrfs_set_file_extent_offset(leaf, fi, 0);
1607 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1608 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1609 btrfs_set_file_extent_compression(leaf, fi, compression);
1610 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1611 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001612
1613 btrfs_unlock_up_safe(path, 1);
1614 btrfs_set_lock_blocking(leaf);
1615
Yan Zhengd899e052008-10-30 14:25:28 -04001616 btrfs_mark_buffer_dirty(leaf);
1617
1618 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001619
1620 ins.objectid = disk_bytenr;
1621 ins.offset = disk_num_bytes;
1622 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001623 ret = btrfs_alloc_reserved_file_extent(trans, root,
1624 root->root_key.objectid,
1625 inode->i_ino, file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001626 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001627 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001628
Yan Zhengd899e052008-10-30 14:25:28 -04001629 return 0;
1630}
1631
Chris Mason5d13a982009-03-13 11:41:46 -04001632/*
1633 * helper function for btrfs_finish_ordered_io, this
1634 * just reads in some of the csum leaves to prime them into ram
1635 * before we start the transaction. It limits the amount of btree
1636 * reads required while inside the transaction.
1637 */
Chris Masond352ac62008-09-29 15:18:18 -04001638/* as ordered data IO finishes, this gets called so we can finish
1639 * an ordered extent if the range of bytes in the file it covers are
1640 * fully written.
1641 */
Chris Mason211f90e2008-07-18 11:56:15 -04001642static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001643{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001644 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001645 struct btrfs_trans_handle *trans = NULL;
Chris Mason5d13a982009-03-13 11:41:46 -04001646 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001647 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001648 struct extent_state *cached_state = NULL;
Yan Zhengd899e052008-10-30 14:25:28 -04001649 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001650 int ret;
1651
Josef Bacik5a1a3df2010-02-02 20:51:14 +00001652 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1653 end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001654 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001655 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001656 BUG_ON(!ordered_extent);
Josef Bacikefd049f2010-02-02 20:50:10 +00001657
Yan, Zhengc21677542009-11-12 09:34:21 +00001658 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1659 BUG_ON(!list_empty(&ordered_extent->list));
1660 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1661 if (!ret) {
1662 trans = btrfs_join_transaction(root, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001663 btrfs_set_trans_block_group(trans, inode);
1664 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc21677542009-11-12 09:34:21 +00001665 ret = btrfs_update_inode(trans, root, inode);
1666 BUG_ON(ret);
Yan, Zhengc21677542009-11-12 09:34:21 +00001667 }
1668 goto out;
1669 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001670
Josef Bacik2ac55d42010-02-03 19:33:23 +00001671 lock_extent_bits(io_tree, ordered_extent->file_offset,
1672 ordered_extent->file_offset + ordered_extent->len - 1,
1673 0, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001674
Yan, Zhengc21677542009-11-12 09:34:21 +00001675 trans = btrfs_join_transaction(root, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001676 btrfs_set_trans_block_group(trans, inode);
1677 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc21677542009-11-12 09:34:21 +00001678
Chris Masonc8b97812008-10-29 14:49:59 -04001679 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001680 compressed = 1;
1681 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1682 BUG_ON(compressed);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001683 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001684 ordered_extent->file_offset,
1685 ordered_extent->file_offset +
1686 ordered_extent->len);
1687 BUG_ON(ret);
1688 } else {
1689 ret = insert_reserved_file_extent(trans, inode,
1690 ordered_extent->file_offset,
1691 ordered_extent->start,
1692 ordered_extent->disk_len,
1693 ordered_extent->len,
1694 ordered_extent->len,
1695 compressed, 0, 0,
1696 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001697 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1698 ordered_extent->file_offset,
1699 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001700 BUG_ON(ret);
1701 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001702 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1703 ordered_extent->file_offset +
1704 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1705
Chris Masone6dcd2d2008-07-17 12:53:50 -04001706 add_pending_csums(trans, inode, ordered_extent->file_offset,
1707 &ordered_extent->list);
1708
Yan, Zhengc21677542009-11-12 09:34:21 +00001709 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1710 ret = btrfs_update_inode(trans, root, inode);
1711 BUG_ON(ret);
Yan, Zhengc21677542009-11-12 09:34:21 +00001712out:
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001713 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
1714 if (trans)
1715 btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001716 /* once for us */
1717 btrfs_put_ordered_extent(ordered_extent);
1718 /* once for the tree */
1719 btrfs_put_ordered_extent(ordered_extent);
1720
Chris Masone6dcd2d2008-07-17 12:53:50 -04001721 return 0;
1722}
1723
Christoph Hellwigb2950862008-12-02 09:54:17 -05001724static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001725 struct extent_state *state, int uptodate)
1726{
Chris Mason8b62b722009-09-02 16:53:46 -04001727 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001728 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1729}
1730
Chris Masond352ac62008-09-29 15:18:18 -04001731/*
1732 * When IO fails, either with EIO or csum verification fails, we
1733 * try other mirrors that might have a good copy of the data. This
1734 * io_failure_record is used to record state as we go through all the
1735 * mirrors. If another mirror has good data, the page is set up to date
1736 * and things continue. If a good mirror can't be found, the original
1737 * bio end_io callback is called to indicate things have failed.
1738 */
Chris Mason7e383262008-04-09 16:28:12 -04001739struct io_failure_record {
1740 struct page *page;
1741 u64 start;
1742 u64 len;
1743 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001744 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001745 int last_mirror;
1746};
1747
Christoph Hellwigb2950862008-12-02 09:54:17 -05001748static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001749 struct page *page, u64 start, u64 end,
1750 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001751{
1752 struct io_failure_record *failrec = NULL;
1753 u64 private;
1754 struct extent_map *em;
1755 struct inode *inode = page->mapping->host;
1756 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001757 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001758 struct bio *bio;
1759 int num_copies;
1760 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001761 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001762 u64 logical;
1763
1764 ret = get_state_private(failure_tree, start, &private);
1765 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001766 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1767 if (!failrec)
1768 return -ENOMEM;
1769 failrec->start = start;
1770 failrec->len = end - start + 1;
1771 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001772 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001773
Chris Mason890871b2009-09-02 16:24:52 -04001774 read_lock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -04001775 em = lookup_extent_mapping(em_tree, start, failrec->len);
1776 if (em->start > start || em->start + em->len < start) {
1777 free_extent_map(em);
1778 em = NULL;
1779 }
Chris Mason890871b2009-09-02 16:24:52 -04001780 read_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001781
1782 if (!em || IS_ERR(em)) {
1783 kfree(failrec);
1784 return -EIO;
1785 }
1786 logical = start - em->start;
1787 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001788 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1789 logical = em->block_start;
1790 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1791 }
Chris Mason7e383262008-04-09 16:28:12 -04001792 failrec->logical = logical;
1793 free_extent_map(em);
1794 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1795 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001796 set_state_private(failure_tree, start,
1797 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001798 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001799 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001800 }
1801 num_copies = btrfs_num_copies(
1802 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1803 failrec->logical, failrec->len);
1804 failrec->last_mirror++;
1805 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001806 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001807 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1808 failrec->start,
1809 EXTENT_LOCKED);
1810 if (state && state->start != failrec->start)
1811 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001812 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001813 }
1814 if (!state || failrec->last_mirror > num_copies) {
1815 set_state_private(failure_tree, failrec->start, 0);
1816 clear_extent_bits(failure_tree, failrec->start,
1817 failrec->start + failrec->len - 1,
1818 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1819 kfree(failrec);
1820 return -EIO;
1821 }
1822 bio = bio_alloc(GFP_NOFS, 1);
1823 bio->bi_private = state;
1824 bio->bi_end_io = failed_bio->bi_end_io;
1825 bio->bi_sector = failrec->logical >> 9;
1826 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001827 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001828
Chris Mason7e383262008-04-09 16:28:12 -04001829 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001830 if (failed_bio->bi_rw & (1 << BIO_RW))
1831 rw = WRITE;
1832 else
1833 rw = READ;
1834
1835 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001836 failrec->last_mirror,
Chris Masond20f7042008-12-08 16:58:54 -05001837 failrec->bio_flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001838 return 0;
1839}
1840
Chris Masond352ac62008-09-29 15:18:18 -04001841/*
1842 * each time an IO finishes, we do a fast check in the IO failure tree
1843 * to see if we need to process or clean up an io_failure_record
1844 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001845static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001846{
1847 u64 private;
1848 u64 private_failure;
1849 struct io_failure_record *failure;
1850 int ret;
1851
1852 private = 0;
1853 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1854 (u64)-1, 1, EXTENT_DIRTY)) {
1855 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1856 start, &private_failure);
1857 if (ret == 0) {
1858 failure = (struct io_failure_record *)(unsigned long)
1859 private_failure;
1860 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1861 failure->start, 0);
1862 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1863 failure->start,
1864 failure->start + failure->len - 1,
1865 EXTENT_DIRTY | EXTENT_LOCKED,
1866 GFP_NOFS);
1867 kfree(failure);
1868 }
1869 }
Chris Mason7e383262008-04-09 16:28:12 -04001870 return 0;
1871}
1872
Chris Masond352ac62008-09-29 15:18:18 -04001873/*
1874 * when reads are done, we need to check csums to verify the data is correct
1875 * if there's a match, we allow the bio to finish. If not, we go through
1876 * the io_failure_record routines to find good copies
1877 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001878static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001879 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001880{
Chris Mason35ebb932007-10-30 16:56:53 -04001881 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001882 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001883 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001884 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001885 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001886 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001887 struct btrfs_root *root = BTRFS_I(inode)->root;
1888 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001889
Chris Masond20f7042008-12-08 16:58:54 -05001890 if (PageChecked(page)) {
1891 ClearPageChecked(page);
1892 goto good;
1893 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001894
1895 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Chris Masonb6cda9b2007-12-14 15:30:32 -05001896 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001897
Yan Zheng17d217f2008-12-12 10:03:38 -05001898 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001899 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001900 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1901 GFP_NOFS);
1902 return 0;
1903 }
1904
Yanc2e639f2008-02-04 08:57:25 -05001905 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001906 private = state->private;
1907 ret = 0;
1908 } else {
1909 ret = get_state_private(io_tree, start, &private);
1910 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001911 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001912 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001913 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001914
Chris Masonff79f812007-10-15 16:22:25 -04001915 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1916 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001917 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001918 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001919
Chris Mason9ab86c82009-01-07 09:48:51 -05001920 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001921good:
Chris Mason7e383262008-04-09 16:28:12 -04001922 /* if the io failure tree for this inode is non-empty,
1923 * check to see if we've recovered from a failed IO
1924 */
Chris Mason1259ab72008-05-12 13:39:03 -04001925 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001926 return 0;
1927
1928zeroit:
Chris Mason193f2842009-04-27 07:29:05 -04001929 if (printk_ratelimit()) {
1930 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1931 "private %llu\n", page->mapping->host->i_ino,
1932 (unsigned long long)start, csum,
1933 (unsigned long long)private);
1934 }
Chris Masondb945352007-10-15 16:15:53 -04001935 memset(kaddr + offset, 1, end - start + 1);
1936 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05001937 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04001938 if (private == 0)
1939 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001940 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001941}
Chris Masonb888db22007-08-27 16:49:44 -04001942
Yan, Zheng24bbcf02009-11-12 09:36:34 +00001943struct delayed_iput {
1944 struct list_head list;
1945 struct inode *inode;
1946};
1947
1948void btrfs_add_delayed_iput(struct inode *inode)
1949{
1950 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1951 struct delayed_iput *delayed;
1952
1953 if (atomic_add_unless(&inode->i_count, -1, 1))
1954 return;
1955
1956 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
1957 delayed->inode = inode;
1958
1959 spin_lock(&fs_info->delayed_iput_lock);
1960 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
1961 spin_unlock(&fs_info->delayed_iput_lock);
1962}
1963
1964void btrfs_run_delayed_iputs(struct btrfs_root *root)
1965{
1966 LIST_HEAD(list);
1967 struct btrfs_fs_info *fs_info = root->fs_info;
1968 struct delayed_iput *delayed;
1969 int empty;
1970
1971 spin_lock(&fs_info->delayed_iput_lock);
1972 empty = list_empty(&fs_info->delayed_iputs);
1973 spin_unlock(&fs_info->delayed_iput_lock);
1974 if (empty)
1975 return;
1976
1977 down_read(&root->fs_info->cleanup_work_sem);
1978 spin_lock(&fs_info->delayed_iput_lock);
1979 list_splice_init(&fs_info->delayed_iputs, &list);
1980 spin_unlock(&fs_info->delayed_iput_lock);
1981
1982 while (!list_empty(&list)) {
1983 delayed = list_entry(list.next, struct delayed_iput, list);
1984 list_del(&delayed->list);
1985 iput(delayed->inode);
1986 kfree(delayed);
1987 }
1988 up_read(&root->fs_info->cleanup_work_sem);
1989}
1990
Josef Bacik7b128762008-07-24 12:17:14 -04001991/*
Yan, Zhengd68fc572010-05-16 10:49:58 -04001992 * calculate extra metadata reservation when snapshotting a subvolume
1993 * contains orphan files.
1994 */
1995void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans,
1996 struct btrfs_pending_snapshot *pending,
1997 u64 *bytes_to_reserve)
1998{
1999 struct btrfs_root *root;
2000 struct btrfs_block_rsv *block_rsv;
2001 u64 num_bytes;
2002 int index;
2003
2004 root = pending->root;
2005 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2006 return;
2007
2008 block_rsv = root->orphan_block_rsv;
2009
2010 /* orphan block reservation for the snapshot */
2011 num_bytes = block_rsv->size;
2012
2013 /*
2014 * after the snapshot is created, COWing tree blocks may use more
2015 * space than it frees. So we should make sure there is enough
2016 * reserved space.
2017 */
2018 index = trans->transid & 0x1;
2019 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2020 num_bytes += block_rsv->size -
2021 (block_rsv->reserved + block_rsv->freed[index]);
2022 }
2023
2024 *bytes_to_reserve += num_bytes;
2025}
2026
2027void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans,
2028 struct btrfs_pending_snapshot *pending)
2029{
2030 struct btrfs_root *root = pending->root;
2031 struct btrfs_root *snap = pending->snap;
2032 struct btrfs_block_rsv *block_rsv;
2033 u64 num_bytes;
2034 int index;
2035 int ret;
2036
2037 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2038 return;
2039
2040 /* refill source subvolume's orphan block reservation */
2041 block_rsv = root->orphan_block_rsv;
2042 index = trans->transid & 0x1;
2043 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2044 num_bytes = block_rsv->size -
2045 (block_rsv->reserved + block_rsv->freed[index]);
2046 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2047 root->orphan_block_rsv,
2048 num_bytes);
2049 BUG_ON(ret);
2050 }
2051
2052 /* setup orphan block reservation for the snapshot */
2053 block_rsv = btrfs_alloc_block_rsv(snap);
2054 BUG_ON(!block_rsv);
2055
2056 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2057 snap->orphan_block_rsv = block_rsv;
2058
2059 num_bytes = root->orphan_block_rsv->size;
2060 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2061 block_rsv, num_bytes);
2062 BUG_ON(ret);
2063
2064#if 0
2065 /* insert orphan item for the snapshot */
2066 WARN_ON(!root->orphan_item_inserted);
2067 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2068 snap->root_key.objectid);
2069 BUG_ON(ret);
2070 snap->orphan_item_inserted = 1;
2071#endif
2072}
2073
2074enum btrfs_orphan_cleanup_state {
2075 ORPHAN_CLEANUP_STARTED = 1,
2076 ORPHAN_CLEANUP_DONE = 2,
2077};
2078
2079/*
2080 * This is called in transaction commmit time. If there are no orphan
2081 * files in the subvolume, it removes orphan item and frees block_rsv
2082 * structure.
2083 */
2084void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2085 struct btrfs_root *root)
2086{
2087 int ret;
2088
2089 if (!list_empty(&root->orphan_list) ||
2090 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2091 return;
2092
2093 if (root->orphan_item_inserted &&
2094 btrfs_root_refs(&root->root_item) > 0) {
2095 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2096 root->root_key.objectid);
2097 BUG_ON(ret);
2098 root->orphan_item_inserted = 0;
2099 }
2100
2101 if (root->orphan_block_rsv) {
2102 WARN_ON(root->orphan_block_rsv->size > 0);
2103 btrfs_free_block_rsv(root, root->orphan_block_rsv);
2104 root->orphan_block_rsv = NULL;
2105 }
2106}
2107
2108/*
Josef Bacik7b128762008-07-24 12:17:14 -04002109 * This creates an orphan entry for the given inode in case something goes
2110 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002111 *
2112 * NOTE: caller of this function should reserve 5 units of metadata for
2113 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002114 */
2115int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2116{
2117 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002118 struct btrfs_block_rsv *block_rsv = NULL;
2119 int reserve = 0;
2120 int insert = 0;
2121 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002122
Yan, Zhengd68fc572010-05-16 10:49:58 -04002123 if (!root->orphan_block_rsv) {
2124 block_rsv = btrfs_alloc_block_rsv(root);
2125 BUG_ON(!block_rsv);
Josef Bacik7b128762008-07-24 12:17:14 -04002126 }
2127
Yan, Zhengd68fc572010-05-16 10:49:58 -04002128 spin_lock(&root->orphan_lock);
2129 if (!root->orphan_block_rsv) {
2130 root->orphan_block_rsv = block_rsv;
2131 } else if (block_rsv) {
2132 btrfs_free_block_rsv(root, block_rsv);
2133 block_rsv = NULL;
2134 }
Josef Bacik7b128762008-07-24 12:17:14 -04002135
Yan, Zhengd68fc572010-05-16 10:49:58 -04002136 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2137 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2138#if 0
2139 /*
2140 * For proper ENOSPC handling, we should do orphan
2141 * cleanup when mounting. But this introduces backward
2142 * compatibility issue.
2143 */
2144 if (!xchg(&root->orphan_item_inserted, 1))
2145 insert = 2;
2146 else
2147 insert = 1;
2148#endif
2149 insert = 1;
2150 } else {
2151 WARN_ON(!BTRFS_I(inode)->orphan_meta_reserved);
2152 }
Josef Bacik7b128762008-07-24 12:17:14 -04002153
Yan, Zhengd68fc572010-05-16 10:49:58 -04002154 if (!BTRFS_I(inode)->orphan_meta_reserved) {
2155 BTRFS_I(inode)->orphan_meta_reserved = 1;
2156 reserve = 1;
2157 }
2158 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002159
Yan, Zhengd68fc572010-05-16 10:49:58 -04002160 if (block_rsv)
2161 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2162
2163 /* grab metadata reservation from transaction handle */
2164 if (reserve) {
2165 ret = btrfs_orphan_reserve_metadata(trans, inode);
2166 BUG_ON(ret);
2167 }
2168
2169 /* insert an orphan item to track this unlinked/truncated file */
2170 if (insert >= 1) {
2171 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
2172 BUG_ON(ret);
2173 }
2174
2175 /* insert an orphan item to track subvolume contains orphan files */
2176 if (insert >= 2) {
2177 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2178 root->root_key.objectid);
2179 BUG_ON(ret);
2180 }
2181 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002182}
2183
2184/*
2185 * We have done the truncate/delete so we can go ahead and remove the orphan
2186 * item for this particular inode.
2187 */
2188int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2189{
2190 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002191 int delete_item = 0;
2192 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002193 int ret = 0;
2194
Yan, Zhengd68fc572010-05-16 10:49:58 -04002195 spin_lock(&root->orphan_lock);
2196 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2197 list_del_init(&BTRFS_I(inode)->i_orphan);
2198 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002199 }
2200
Yan, Zhengd68fc572010-05-16 10:49:58 -04002201 if (BTRFS_I(inode)->orphan_meta_reserved) {
2202 BTRFS_I(inode)->orphan_meta_reserved = 0;
2203 release_rsv = 1;
2204 }
2205 spin_unlock(&root->orphan_lock);
2206
2207 if (trans && delete_item) {
2208 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2209 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002210 }
2211
Yan, Zhengd68fc572010-05-16 10:49:58 -04002212 if (release_rsv)
2213 btrfs_orphan_release_metadata(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002214
Yan, Zhengd68fc572010-05-16 10:49:58 -04002215 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002216}
2217
2218/*
2219 * this cleans up any orphans that may be left on the list from the last use
2220 * of this root.
2221 */
2222void btrfs_orphan_cleanup(struct btrfs_root *root)
2223{
2224 struct btrfs_path *path;
2225 struct extent_buffer *leaf;
2226 struct btrfs_item *item;
2227 struct btrfs_key key, found_key;
2228 struct btrfs_trans_handle *trans;
2229 struct inode *inode;
2230 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2231
Yan, Zhengd68fc572010-05-16 10:49:58 -04002232 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik7b128762008-07-24 12:17:14 -04002233 return;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002234
2235 path = btrfs_alloc_path();
2236 BUG_ON(!path);
Josef Bacik7b128762008-07-24 12:17:14 -04002237 path->reada = -1;
2238
2239 key.objectid = BTRFS_ORPHAN_OBJECTID;
2240 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2241 key.offset = (u64)-1;
2242
Josef Bacik7b128762008-07-24 12:17:14 -04002243 while (1) {
2244 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2245 if (ret < 0) {
2246 printk(KERN_ERR "Error searching slot for orphan: %d"
2247 "\n", ret);
2248 break;
2249 }
2250
2251 /*
2252 * if ret == 0 means we found what we were searching for, which
2253 * is weird, but possible, so only screw with path if we didnt
2254 * find the key and see if we have stuff that matches
2255 */
2256 if (ret > 0) {
2257 if (path->slots[0] == 0)
2258 break;
2259 path->slots[0]--;
2260 }
2261
2262 /* pull out the item */
2263 leaf = path->nodes[0];
2264 item = btrfs_item_nr(leaf, path->slots[0]);
2265 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2266
2267 /* make sure the item matches what we want */
2268 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2269 break;
2270 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2271 break;
2272
2273 /* release the path since we're done with it */
2274 btrfs_release_path(root, path);
2275
2276 /*
2277 * this is where we are basically btrfs_lookup, without the
2278 * crossing root thing. we store the inode number in the
2279 * offset of the orphan item.
2280 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002281 found_key.objectid = found_key.offset;
2282 found_key.type = BTRFS_INODE_ITEM_KEY;
2283 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002284 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002285 BUG_ON(IS_ERR(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04002286
Josef Bacik7b128762008-07-24 12:17:14 -04002287 /*
2288 * add this inode to the orphan list so btrfs_orphan_del does
2289 * the proper thing when we hit it
2290 */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002291 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002292 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002293 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002294
2295 /*
2296 * if this is a bad inode, means we actually succeeded in
2297 * removing the inode, but not the orphan record, which means
2298 * we need to manually delete the orphan since iput will just
2299 * do a destroy_inode
2300 */
2301 if (is_bad_inode(inode)) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04002302 trans = btrfs_start_transaction(root, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04002303 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002304 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04002305 iput(inode);
2306 continue;
2307 }
2308
2309 /* if we have links, this was a truncate, lets do that */
2310 if (inode->i_nlink) {
2311 nr_truncate++;
2312 btrfs_truncate(inode);
2313 } else {
2314 nr_unlink++;
2315 }
2316
2317 /* this will do delete_inode and everything for us */
2318 iput(inode);
2319 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002320 btrfs_free_path(path);
2321
2322 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2323
2324 if (root->orphan_block_rsv)
2325 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2326 (u64)-1);
2327
2328 if (root->orphan_block_rsv || root->orphan_item_inserted) {
2329 trans = btrfs_join_transaction(root, 1);
2330 btrfs_end_transaction(trans, root);
2331 }
Josef Bacik7b128762008-07-24 12:17:14 -04002332
2333 if (nr_unlink)
2334 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2335 if (nr_truncate)
2336 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik7b128762008-07-24 12:17:14 -04002337}
2338
Chris Masond352ac62008-09-29 15:18:18 -04002339/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002340 * very simple check to peek ahead in the leaf looking for xattrs. If we
2341 * don't find any xattrs, we know there can't be any acls.
2342 *
2343 * slot is the slot the inode is in, objectid is the objectid of the inode
2344 */
2345static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2346 int slot, u64 objectid)
2347{
2348 u32 nritems = btrfs_header_nritems(leaf);
2349 struct btrfs_key found_key;
2350 int scanned = 0;
2351
2352 slot++;
2353 while (slot < nritems) {
2354 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2355
2356 /* we found a different objectid, there must not be acls */
2357 if (found_key.objectid != objectid)
2358 return 0;
2359
2360 /* we found an xattr, assume we've got an acl */
2361 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2362 return 1;
2363
2364 /*
2365 * we found a key greater than an xattr key, there can't
2366 * be any acls later on
2367 */
2368 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2369 return 0;
2370
2371 slot++;
2372 scanned++;
2373
2374 /*
2375 * it goes inode, inode backrefs, xattrs, extents,
2376 * so if there are a ton of hard links to an inode there can
2377 * be a lot of backrefs. Don't waste time searching too hard,
2378 * this is just an optimization
2379 */
2380 if (scanned >= 8)
2381 break;
2382 }
2383 /* we hit the end of the leaf before we found an xattr or
2384 * something larger than an xattr. We have to assume the inode
2385 * has acls
2386 */
2387 return 1;
2388}
2389
2390/*
Chris Masond352ac62008-09-29 15:18:18 -04002391 * read an inode from the btree into the in-memory inode
2392 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002393static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002394{
2395 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002396 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002397 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002398 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002399 struct btrfs_root *root = BTRFS_I(inode)->root;
2400 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002401 int maybe_acls;
Chris Mason39279cc2007-06-12 06:35:45 -04002402 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04002403 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002404 int ret;
2405
2406 path = btrfs_alloc_path();
2407 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002408 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002409
Chris Mason39279cc2007-06-12 06:35:45 -04002410 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002411 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002412 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002413
Chris Mason5f39d392007-10-15 16:14:19 -04002414 leaf = path->nodes[0];
2415 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2416 struct btrfs_inode_item);
2417
2418 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2419 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2420 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2421 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002422 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002423
2424 tspec = btrfs_inode_atime(inode_item);
2425 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2426 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2427
2428 tspec = btrfs_inode_mtime(inode_item);
2429 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2430 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2431
2432 tspec = btrfs_inode_ctime(inode_item);
2433 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2434 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2435
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002436 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002437 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002438 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002439 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002440 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002441 rdev = btrfs_inode_rdev(leaf, inode_item);
2442
Josef Bacikaec74772008-07-24 12:12:38 -04002443 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002444 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002445
Chris Mason5f39d392007-10-15 16:14:19 -04002446 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002447
Chris Mason46a53cc2009-04-27 11:47:50 -04002448 /*
2449 * try to precache a NULL acl entry for files that don't have
2450 * any xattrs or acls
2451 */
2452 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
Al Viro72c04902009-06-24 16:58:48 -04002453 if (!maybe_acls)
2454 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002455
Yan Zhengd2fb3432008-12-11 16:30:39 -05002456 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2457 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002458 btrfs_free_path(path);
2459 inode_item = NULL;
2460
Chris Mason39279cc2007-06-12 06:35:45 -04002461 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002462 case S_IFREG:
2463 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002464 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002465 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002466 inode->i_fop = &btrfs_file_operations;
2467 inode->i_op = &btrfs_file_inode_operations;
2468 break;
2469 case S_IFDIR:
2470 inode->i_fop = &btrfs_dir_file_operations;
2471 if (root == root->fs_info->tree_root)
2472 inode->i_op = &btrfs_dir_ro_inode_operations;
2473 else
2474 inode->i_op = &btrfs_dir_inode_operations;
2475 break;
2476 case S_IFLNK:
2477 inode->i_op = &btrfs_symlink_inode_operations;
2478 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002479 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002480 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002481 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002482 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002483 init_special_inode(inode, inode->i_mode, rdev);
2484 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002485 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002486
2487 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002488 return;
2489
2490make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002491 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002492 make_bad_inode(inode);
2493}
2494
Chris Masond352ac62008-09-29 15:18:18 -04002495/*
2496 * given a leaf and an inode, copy the inode fields into the leaf
2497 */
Chris Masone02119d2008-09-05 16:13:11 -04002498static void fill_inode_item(struct btrfs_trans_handle *trans,
2499 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002500 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002501 struct inode *inode)
2502{
Chris Mason5f39d392007-10-15 16:14:19 -04002503 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2504 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002505 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002506 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2507 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2508
2509 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2510 inode->i_atime.tv_sec);
2511 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2512 inode->i_atime.tv_nsec);
2513
2514 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2515 inode->i_mtime.tv_sec);
2516 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2517 inode->i_mtime.tv_nsec);
2518
2519 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2520 inode->i_ctime.tv_sec);
2521 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2522 inode->i_ctime.tv_nsec);
2523
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002524 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002525 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002526 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002527 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002528 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002529 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002530 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002531}
2532
Chris Masond352ac62008-09-29 15:18:18 -04002533/*
2534 * copy everything in the in-memory inode into the btree.
2535 */
Chris Masond3977122009-01-05 21:25:51 -05002536noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2537 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002538{
2539 struct btrfs_inode_item *inode_item;
2540 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002541 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002542 int ret;
2543
2544 path = btrfs_alloc_path();
2545 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002546 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002547 ret = btrfs_lookup_inode(trans, root, path,
2548 &BTRFS_I(inode)->location, 1);
2549 if (ret) {
2550 if (ret > 0)
2551 ret = -ENOENT;
2552 goto failed;
2553 }
2554
Chris Masonb4ce94d2009-02-04 09:25:08 -05002555 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002556 leaf = path->nodes[0];
2557 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002558 struct btrfs_inode_item);
2559
Chris Masone02119d2008-09-05 16:13:11 -04002560 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002561 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002562 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002563 ret = 0;
2564failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002565 btrfs_free_path(path);
2566 return ret;
2567}
2568
2569
Chris Masond352ac62008-09-29 15:18:18 -04002570/*
2571 * unlink helper that gets used here in inode.c and in the tree logging
2572 * recovery code. It remove a link in a directory with a given name, and
2573 * also drops the back refs in the inode to the directory
2574 */
Chris Masone02119d2008-09-05 16:13:11 -04002575int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2576 struct btrfs_root *root,
2577 struct inode *dir, struct inode *inode,
2578 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002579{
2580 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002581 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002582 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002583 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002584 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002585 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002586
2587 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002588 if (!path) {
2589 ret = -ENOMEM;
2590 goto err;
2591 }
2592
Chris Masonb9473432009-03-13 11:00:37 -04002593 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002594 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2595 name, name_len, -1);
2596 if (IS_ERR(di)) {
2597 ret = PTR_ERR(di);
2598 goto err;
2599 }
2600 if (!di) {
2601 ret = -ENOENT;
2602 goto err;
2603 }
Chris Mason5f39d392007-10-15 16:14:19 -04002604 leaf = path->nodes[0];
2605 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002606 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002607 if (ret)
2608 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002609 btrfs_release_path(root, path);
2610
Josef Bacikaec74772008-07-24 12:12:38 -04002611 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002612 inode->i_ino,
2613 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002614 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002615 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002616 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002617 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002618 goto err;
2619 }
2620
Chris Mason39279cc2007-06-12 06:35:45 -04002621 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002622 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002623 if (IS_ERR(di)) {
2624 ret = PTR_ERR(di);
2625 goto err;
2626 }
2627 if (!di) {
2628 ret = -ENOENT;
2629 goto err;
2630 }
2631 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002632 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002633
Chris Masone02119d2008-09-05 16:13:11 -04002634 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2635 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002636 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002637
2638 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2639 dir, index);
2640 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002641err:
2642 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002643 if (ret)
2644 goto out;
2645
2646 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2647 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2648 btrfs_update_inode(trans, root, dir);
2649 btrfs_drop_nlink(inode);
2650 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002651out:
Chris Mason39279cc2007-06-12 06:35:45 -04002652 return ret;
2653}
2654
Yan, Zhenga22285a2010-05-16 10:48:46 -04002655/* helper to check if there is any shared block in the path */
2656static int check_path_shared(struct btrfs_root *root,
2657 struct btrfs_path *path)
2658{
2659 struct extent_buffer *eb;
2660 int level;
2661 int ret;
2662 u64 refs;
2663
2664 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2665 if (!path->nodes[level])
2666 break;
2667 eb = path->nodes[level];
2668 if (!btrfs_block_can_be_shared(root, eb))
2669 continue;
2670 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2671 &refs, NULL);
2672 if (refs > 1)
2673 return 1;
2674 }
2675 return 0;
2676}
2677
2678/*
2679 * helper to start transaction for unlink and rmdir.
2680 *
2681 * unlink and rmdir are special in btrfs, they do not always free space.
2682 * so in enospc case, we should make sure they will free space before
2683 * allowing them to use the global metadata reservation.
2684 */
2685static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2686 struct dentry *dentry)
2687{
2688 struct btrfs_trans_handle *trans;
2689 struct btrfs_root *root = BTRFS_I(dir)->root;
2690 struct btrfs_path *path;
2691 struct btrfs_inode_ref *ref;
2692 struct btrfs_dir_item *di;
2693 struct inode *inode = dentry->d_inode;
2694 u64 index;
2695 int check_link = 1;
2696 int err = -ENOSPC;
2697 int ret;
2698
2699 trans = btrfs_start_transaction(root, 10);
2700 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2701 return trans;
2702
2703 if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2704 return ERR_PTR(-ENOSPC);
2705
2706 /* check if there is someone else holds reference */
2707 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2708 return ERR_PTR(-ENOSPC);
2709
2710 if (atomic_read(&inode->i_count) > 2)
2711 return ERR_PTR(-ENOSPC);
2712
2713 if (xchg(&root->fs_info->enospc_unlink, 1))
2714 return ERR_PTR(-ENOSPC);
2715
2716 path = btrfs_alloc_path();
2717 if (!path) {
2718 root->fs_info->enospc_unlink = 0;
2719 return ERR_PTR(-ENOMEM);
2720 }
2721
2722 trans = btrfs_start_transaction(root, 0);
2723 if (IS_ERR(trans)) {
2724 btrfs_free_path(path);
2725 root->fs_info->enospc_unlink = 0;
2726 return trans;
2727 }
2728
2729 path->skip_locking = 1;
2730 path->search_commit_root = 1;
2731
2732 ret = btrfs_lookup_inode(trans, root, path,
2733 &BTRFS_I(dir)->location, 0);
2734 if (ret < 0) {
2735 err = ret;
2736 goto out;
2737 }
2738 if (ret == 0) {
2739 if (check_path_shared(root, path))
2740 goto out;
2741 } else {
2742 check_link = 0;
2743 }
2744 btrfs_release_path(root, path);
2745
2746 ret = btrfs_lookup_inode(trans, root, path,
2747 &BTRFS_I(inode)->location, 0);
2748 if (ret < 0) {
2749 err = ret;
2750 goto out;
2751 }
2752 if (ret == 0) {
2753 if (check_path_shared(root, path))
2754 goto out;
2755 } else {
2756 check_link = 0;
2757 }
2758 btrfs_release_path(root, path);
2759
2760 if (ret == 0 && S_ISREG(inode->i_mode)) {
2761 ret = btrfs_lookup_file_extent(trans, root, path,
2762 inode->i_ino, (u64)-1, 0);
2763 if (ret < 0) {
2764 err = ret;
2765 goto out;
2766 }
2767 BUG_ON(ret == 0);
2768 if (check_path_shared(root, path))
2769 goto out;
2770 btrfs_release_path(root, path);
2771 }
2772
2773 if (!check_link) {
2774 err = 0;
2775 goto out;
2776 }
2777
2778 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2779 dentry->d_name.name, dentry->d_name.len, 0);
2780 if (IS_ERR(di)) {
2781 err = PTR_ERR(di);
2782 goto out;
2783 }
2784 if (di) {
2785 if (check_path_shared(root, path))
2786 goto out;
2787 } else {
2788 err = 0;
2789 goto out;
2790 }
2791 btrfs_release_path(root, path);
2792
2793 ref = btrfs_lookup_inode_ref(trans, root, path,
2794 dentry->d_name.name, dentry->d_name.len,
2795 inode->i_ino, dir->i_ino, 0);
2796 if (IS_ERR(ref)) {
2797 err = PTR_ERR(ref);
2798 goto out;
2799 }
2800 BUG_ON(!ref);
2801 if (check_path_shared(root, path))
2802 goto out;
2803 index = btrfs_inode_ref_index(path->nodes[0], ref);
2804 btrfs_release_path(root, path);
2805
2806 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index,
2807 dentry->d_name.name, dentry->d_name.len, 0);
2808 if (IS_ERR(di)) {
2809 err = PTR_ERR(di);
2810 goto out;
2811 }
2812 BUG_ON(ret == -ENOENT);
2813 if (check_path_shared(root, path))
2814 goto out;
2815
2816 err = 0;
2817out:
2818 btrfs_free_path(path);
2819 if (err) {
2820 btrfs_end_transaction(trans, root);
2821 root->fs_info->enospc_unlink = 0;
2822 return ERR_PTR(err);
2823 }
2824
2825 trans->block_rsv = &root->fs_info->global_block_rsv;
2826 return trans;
2827}
2828
2829static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2830 struct btrfs_root *root)
2831{
2832 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2833 BUG_ON(!root->fs_info->enospc_unlink);
2834 root->fs_info->enospc_unlink = 0;
2835 }
2836 btrfs_end_transaction_throttle(trans, root);
2837}
2838
Chris Mason39279cc2007-06-12 06:35:45 -04002839static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2840{
Yan, Zhenga22285a2010-05-16 10:48:46 -04002841 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002842 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002843 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002844 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002845 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002846
Yan, Zhenga22285a2010-05-16 10:48:46 -04002847 trans = __unlink_start_trans(dir, dentry);
2848 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002849 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04002850
Chris Mason39279cc2007-06-12 06:35:45 -04002851 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04002852
2853 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2854
Chris Masone02119d2008-09-05 16:13:11 -04002855 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2856 dentry->d_name.name, dentry->d_name.len);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002857 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002858
Yan, Zhenga22285a2010-05-16 10:48:46 -04002859 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04002860 ret = btrfs_orphan_add(trans, inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002861 BUG_ON(ret);
2862 }
Josef Bacik7b128762008-07-24 12:17:14 -04002863
Chris Masond3c2fdc2007-09-17 10:58:06 -04002864 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002865 __unlink_end_trans(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002866 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002867 return ret;
2868}
2869
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002870int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2871 struct btrfs_root *root,
2872 struct inode *dir, u64 objectid,
2873 const char *name, int name_len)
2874{
2875 struct btrfs_path *path;
2876 struct extent_buffer *leaf;
2877 struct btrfs_dir_item *di;
2878 struct btrfs_key key;
2879 u64 index;
2880 int ret;
2881
2882 path = btrfs_alloc_path();
2883 if (!path)
2884 return -ENOMEM;
2885
2886 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2887 name, name_len, -1);
2888 BUG_ON(!di || IS_ERR(di));
2889
2890 leaf = path->nodes[0];
2891 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2892 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2893 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2894 BUG_ON(ret);
2895 btrfs_release_path(root, path);
2896
2897 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2898 objectid, root->root_key.objectid,
2899 dir->i_ino, &index, name, name_len);
2900 if (ret < 0) {
2901 BUG_ON(ret != -ENOENT);
2902 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2903 name, name_len);
2904 BUG_ON(!di || IS_ERR(di));
2905
2906 leaf = path->nodes[0];
2907 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2908 btrfs_release_path(root, path);
2909 index = key.offset;
2910 }
2911
2912 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2913 index, name, name_len, -1);
2914 BUG_ON(!di || IS_ERR(di));
2915
2916 leaf = path->nodes[0];
2917 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2918 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2919 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2920 BUG_ON(ret);
2921 btrfs_release_path(root, path);
2922
2923 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2924 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2925 ret = btrfs_update_inode(trans, root, dir);
2926 BUG_ON(ret);
2927 dir->i_sb->s_dirt = 1;
2928
2929 btrfs_free_path(path);
2930 return 0;
2931}
2932
Chris Mason39279cc2007-06-12 06:35:45 -04002933static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2934{
2935 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002936 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002937 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002938 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002939 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002940
Chris Mason3394e162008-11-17 20:42:26 -05002941 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002942 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04002943 return -ENOTEMPTY;
2944
Yan, Zhenga22285a2010-05-16 10:48:46 -04002945 trans = __unlink_start_trans(dir, dentry);
2946 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002947 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002948
Chris Mason39279cc2007-06-12 06:35:45 -04002949 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002950
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002951 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
2952 err = btrfs_unlink_subvol(trans, root, dir,
2953 BTRFS_I(inode)->location.objectid,
2954 dentry->d_name.name,
2955 dentry->d_name.len);
2956 goto out;
2957 }
2958
Josef Bacik7b128762008-07-24 12:17:14 -04002959 err = btrfs_orphan_add(trans, inode);
2960 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002961 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002962
Chris Mason39279cc2007-06-12 06:35:45 -04002963 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04002964 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2965 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05002966 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04002967 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002968out:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002969 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002970 __unlink_end_trans(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002971 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05002972
Chris Mason39279cc2007-06-12 06:35:45 -04002973 return err;
2974}
2975
Chris Masond20f7042008-12-08 16:58:54 -05002976#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04002977/*
Chris Mason323ac952008-10-01 19:05:46 -04002978 * when truncating bytes in a file, it is possible to avoid reading
2979 * the leaves that contain only checksum items. This can be the
2980 * majority of the IO required to delete a large file, but it must
2981 * be done carefully.
2982 *
2983 * The keys in the level just above the leaves are checked to make sure
2984 * the lowest key in a given leaf is a csum key, and starts at an offset
2985 * after the new size.
2986 *
2987 * Then the key for the next leaf is checked to make sure it also has
2988 * a checksum item for the same file. If it does, we know our target leaf
2989 * contains only checksum items, and it can be safely freed without reading
2990 * it.
2991 *
2992 * This is just an optimization targeted at large files. It may do
2993 * nothing. It will return 0 unless things went badly.
2994 */
2995static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2996 struct btrfs_root *root,
2997 struct btrfs_path *path,
2998 struct inode *inode, u64 new_size)
2999{
3000 struct btrfs_key key;
3001 int ret;
3002 int nritems;
3003 struct btrfs_key found_key;
3004 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003005 struct btrfs_leaf_ref *ref;
3006 u64 leaf_gen;
3007 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04003008
3009 path->lowest_level = 1;
3010 key.objectid = inode->i_ino;
3011 key.type = BTRFS_CSUM_ITEM_KEY;
3012 key.offset = new_size;
3013again:
3014 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3015 if (ret < 0)
3016 goto out;
3017
3018 if (path->nodes[1] == NULL) {
3019 ret = 0;
3020 goto out;
3021 }
3022 ret = 0;
3023 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
3024 nritems = btrfs_header_nritems(path->nodes[1]);
3025
3026 if (!nritems)
3027 goto out;
3028
3029 if (path->slots[1] >= nritems)
3030 goto next_node;
3031
3032 /* did we find a key greater than anything we want to delete? */
3033 if (found_key.objectid > inode->i_ino ||
3034 (found_key.objectid == inode->i_ino && found_key.type > key.type))
3035 goto out;
3036
3037 /* we check the next key in the node to make sure the leave contains
3038 * only checksum items. This comparison doesn't work if our
3039 * leaf is the last one in the node
3040 */
3041 if (path->slots[1] + 1 >= nritems) {
3042next_node:
3043 /* search forward from the last key in the node, this
3044 * will bring us into the next node in the tree
3045 */
3046 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
3047
3048 /* unlikely, but we inc below, so check to be safe */
3049 if (found_key.offset == (u64)-1)
3050 goto out;
3051
3052 /* search_forward needs a path with locks held, do the
3053 * search again for the original key. It is possible
3054 * this will race with a balance and return a path that
3055 * we could modify, but this drop is just an optimization
3056 * and is allowed to miss some leaves.
3057 */
3058 btrfs_release_path(root, path);
3059 found_key.offset++;
3060
3061 /* setup a max key for search_forward */
3062 other_key.offset = (u64)-1;
3063 other_key.type = key.type;
3064 other_key.objectid = key.objectid;
3065
3066 path->keep_locks = 1;
3067 ret = btrfs_search_forward(root, &found_key, &other_key,
3068 path, 0, 0);
3069 path->keep_locks = 0;
3070 if (ret || found_key.objectid != key.objectid ||
3071 found_key.type != key.type) {
3072 ret = 0;
3073 goto out;
3074 }
3075
3076 key.offset = found_key.offset;
3077 btrfs_release_path(root, path);
3078 cond_resched();
3079 goto again;
3080 }
3081
3082 /* we know there's one more slot after us in the tree,
3083 * read that key so we can verify it is also a checksum item
3084 */
3085 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
3086
3087 if (found_key.objectid < inode->i_ino)
3088 goto next_key;
3089
3090 if (found_key.type != key.type || found_key.offset < new_size)
3091 goto next_key;
3092
3093 /*
3094 * if the key for the next leaf isn't a csum key from this objectid,
3095 * we can't be sure there aren't good items inside this leaf.
3096 * Bail out
3097 */
3098 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
3099 goto out;
3100
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003101 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
3102 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003103 /*
3104 * it is safe to delete this leaf, it contains only
3105 * csum items from this inode at an offset >= new_size
3106 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003107 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04003108 BUG_ON(ret);
3109
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003110 if (root->ref_cows && leaf_gen < trans->transid) {
3111 ref = btrfs_alloc_leaf_ref(root, 0);
3112 if (ref) {
3113 ref->root_gen = root->root_key.offset;
3114 ref->bytenr = leaf_start;
3115 ref->owner = 0;
3116 ref->generation = leaf_gen;
3117 ref->nritems = 0;
3118
Chris Masonbd56b302009-02-04 09:27:02 -05003119 btrfs_sort_leaf_ref(ref);
3120
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003121 ret = btrfs_add_leaf_ref(root, ref, 0);
3122 WARN_ON(ret);
3123 btrfs_free_leaf_ref(root, ref);
3124 } else {
3125 WARN_ON(1);
3126 }
3127 }
Chris Mason323ac952008-10-01 19:05:46 -04003128next_key:
3129 btrfs_release_path(root, path);
3130
3131 if (other_key.objectid == inode->i_ino &&
3132 other_key.type == key.type && other_key.offset > key.offset) {
3133 key.offset = other_key.offset;
3134 cond_resched();
3135 goto again;
3136 }
3137 ret = 0;
3138out:
3139 /* fixup any changes we've made to the path */
3140 path->lowest_level = 0;
3141 path->keep_locks = 0;
3142 btrfs_release_path(root, path);
3143 return ret;
3144}
3145
Chris Masond20f7042008-12-08 16:58:54 -05003146#endif
3147
Chris Mason323ac952008-10-01 19:05:46 -04003148/*
Chris Mason39279cc2007-06-12 06:35:45 -04003149 * this can truncate away extent items, csum items and directory items.
3150 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003151 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003152 *
3153 * csum items that cross the new i_size are truncated to the new size
3154 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003155 *
3156 * min_type is the minimum key type to truncate down to. If set to 0, this
3157 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003158 */
Yan, Zheng80825102009-11-12 09:35:36 +00003159int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3160 struct btrfs_root *root,
3161 struct inode *inode,
3162 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003163{
Chris Mason39279cc2007-06-12 06:35:45 -04003164 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003165 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003166 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003167 struct btrfs_key key;
3168 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003169 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003170 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003171 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003172 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003173 u64 mask = root->sectorsize - 1;
3174 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003175 int found_extent;
3176 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003177 int pending_del_nr = 0;
3178 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003179 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05003180 int encoding;
Yan, Zheng80825102009-11-12 09:35:36 +00003181 int ret;
3182 int err = 0;
3183
3184 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003185
Chris Masone02119d2008-09-05 16:13:11 -04003186 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003187 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003188
Chris Mason39279cc2007-06-12 06:35:45 -04003189 path = btrfs_alloc_path();
3190 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04003191 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04003192
Chris Mason39279cc2007-06-12 06:35:45 -04003193 key.objectid = inode->i_ino;
3194 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003195 key.type = (u8)-1;
3196
Chris Mason85e21ba2008-01-29 15:11:36 -05003197search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003198 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003199 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003200 if (ret < 0) {
3201 err = ret;
3202 goto out;
3203 }
Chris Masond3977122009-01-05 21:25:51 -05003204
Chris Mason85e21ba2008-01-29 15:11:36 -05003205 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003206 /* there are no items in the tree for us to truncate, we're
3207 * done
3208 */
Yan, Zheng80825102009-11-12 09:35:36 +00003209 if (path->slots[0] == 0)
3210 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003211 path->slots[0]--;
3212 }
3213
Chris Masond3977122009-01-05 21:25:51 -05003214 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003215 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003216 leaf = path->nodes[0];
3217 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3218 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05003219 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003220
Chris Mason5f39d392007-10-15 16:14:19 -04003221 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003222 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003223
Chris Mason85e21ba2008-01-29 15:11:36 -05003224 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003225 break;
3226
Chris Mason5f39d392007-10-15 16:14:19 -04003227 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003228 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003229 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003230 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003231 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003232 encoding = btrfs_file_extent_compression(leaf, fi);
3233 encoding |= btrfs_file_extent_encryption(leaf, fi);
3234 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
3235
Chris Mason179e29e2007-11-01 11:28:41 -04003236 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003237 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003238 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003239 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003240 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003241 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003242 }
Yan008630c2007-11-07 13:31:09 -05003243 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003244 }
Yan, Zheng80825102009-11-12 09:35:36 +00003245 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003246 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003247 } else {
3248 if (item_end < new_size)
3249 break;
3250 if (found_key.offset >= new_size)
3251 del_item = 1;
3252 else
3253 del_item = 0;
3254 }
Chris Mason39279cc2007-06-12 06:35:45 -04003255 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003256 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003257 if (found_type != BTRFS_EXTENT_DATA_KEY)
3258 goto delete;
3259
3260 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003261 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003262 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003263 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04003264 u64 orig_num_bytes =
3265 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003266 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003267 found_key.offset + root->sectorsize - 1;
Yanb1632b102008-01-30 11:54:04 -05003268 extent_num_bytes = extent_num_bytes &
3269 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003270 btrfs_set_file_extent_num_bytes(leaf, fi,
3271 extent_num_bytes);
3272 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003273 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003274 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003275 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003276 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003277 } else {
Chris Masondb945352007-10-15 16:15:53 -04003278 extent_num_bytes =
3279 btrfs_file_extent_disk_num_bytes(leaf,
3280 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003281 extent_offset = found_key.offset -
3282 btrfs_file_extent_offset(leaf, fi);
3283
Chris Mason39279cc2007-06-12 06:35:45 -04003284 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003285 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003286 if (extent_start != 0) {
3287 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003288 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003289 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003290 }
3291 }
Chris Mason90692182008-02-08 13:49:28 -05003292 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003293 /*
3294 * we can't truncate inline items that have had
3295 * special encodings
3296 */
3297 if (!del_item &&
3298 btrfs_file_extent_compression(leaf, fi) == 0 &&
3299 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3300 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003301 u32 size = new_size - found_key.offset;
3302
3303 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003304 inode_sub_bytes(inode, item_end + 1 -
3305 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003306 }
3307 size =
3308 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05003309 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04003310 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05003311 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04003312 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003313 inode_sub_bytes(inode, item_end + 1 -
3314 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003315 }
Chris Mason39279cc2007-06-12 06:35:45 -04003316 }
Chris Mason179e29e2007-11-01 11:28:41 -04003317delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003318 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003319 if (!pending_del_nr) {
3320 /* no pending yet, add ourselves */
3321 pending_del_slot = path->slots[0];
3322 pending_del_nr = 1;
3323 } else if (pending_del_nr &&
3324 path->slots[0] + 1 == pending_del_slot) {
3325 /* hop on the pending chunk */
3326 pending_del_nr++;
3327 pending_del_slot = path->slots[0];
3328 } else {
Chris Masond3977122009-01-05 21:25:51 -05003329 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003330 }
Chris Mason39279cc2007-06-12 06:35:45 -04003331 } else {
3332 break;
3333 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003334 if (found_extent && root->ref_cows) {
Chris Masonb9473432009-03-13 11:00:37 -04003335 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003336 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003337 extent_num_bytes, 0,
3338 btrfs_header_owner(leaf),
3339 inode->i_ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04003340 BUG_ON(ret);
3341 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003342
Yan, Zheng80825102009-11-12 09:35:36 +00003343 if (found_type == BTRFS_INODE_ITEM_KEY)
3344 break;
3345
3346 if (path->slots[0] == 0 ||
3347 path->slots[0] != pending_del_slot) {
3348 if (root->ref_cows) {
3349 err = -EAGAIN;
3350 goto out;
3351 }
3352 if (pending_del_nr) {
3353 ret = btrfs_del_items(trans, root, path,
3354 pending_del_slot,
3355 pending_del_nr);
3356 BUG_ON(ret);
3357 pending_del_nr = 0;
3358 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003359 btrfs_release_path(root, path);
3360 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003361 } else {
3362 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003363 }
Chris Mason39279cc2007-06-12 06:35:45 -04003364 }
Yan, Zheng80825102009-11-12 09:35:36 +00003365out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003366 if (pending_del_nr) {
3367 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3368 pending_del_nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003369 BUG_ON(ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003370 }
Chris Mason39279cc2007-06-12 06:35:45 -04003371 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003372 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003373}
3374
Chris Masona52d9a82007-08-27 16:49:44 -04003375/*
3376 * taken from block_truncate_page, but does cow as it zeros out
3377 * any bytes left in the last page in the file.
3378 */
3379static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3380{
3381 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003382 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003383 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3384 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003385 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003386 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003387 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003388 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3389 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3390 struct page *page;
3391 int ret = 0;
3392 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003393 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003394
3395 if ((offset & (blocksize - 1)) == 0)
3396 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003397 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003398 if (ret)
3399 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003400
3401 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003402again:
Chris Masona52d9a82007-08-27 16:49:44 -04003403 page = grab_cache_page(mapping, index);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003404 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003405 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003406 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003407 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003408
3409 page_start = page_offset(page);
3410 page_end = page_start + PAGE_CACHE_SIZE - 1;
3411
Chris Masona52d9a82007-08-27 16:49:44 -04003412 if (!PageUptodate(page)) {
3413 ret = btrfs_readpage(NULL, page);
3414 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003415 if (page->mapping != mapping) {
3416 unlock_page(page);
3417 page_cache_release(page);
3418 goto again;
3419 }
Chris Masona52d9a82007-08-27 16:49:44 -04003420 if (!PageUptodate(page)) {
3421 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003422 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003423 }
3424 }
Chris Mason211c17f2008-05-15 09:13:45 -04003425 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003426
Josef Bacik2ac55d42010-02-03 19:33:23 +00003427 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
3428 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003429 set_page_extent_mapped(page);
3430
3431 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3432 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003433 unlock_extent_cached(io_tree, page_start, page_end,
3434 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003435 unlock_page(page);
3436 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003437 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003438 btrfs_put_ordered_extent(ordered);
3439 goto again;
3440 }
3441
Josef Bacik2ac55d42010-02-03 19:33:23 +00003442 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003443 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003444 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003445
Josef Bacik2ac55d42010-02-03 19:33:23 +00003446 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3447 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003448 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003449 unlock_extent_cached(io_tree, page_start, page_end,
3450 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003451 goto out_unlock;
3452 }
3453
Chris Masone6dcd2d2008-07-17 12:53:50 -04003454 ret = 0;
3455 if (offset != PAGE_CACHE_SIZE) {
3456 kaddr = kmap(page);
3457 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3458 flush_dcache_page(page);
3459 kunmap(page);
3460 }
Chris Mason247e7432008-07-17 12:53:51 -04003461 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003462 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003463 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3464 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003465
Chris Mason89642222008-07-24 09:41:53 -04003466out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003467 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003468 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003469 unlock_page(page);
3470 page_cache_release(page);
3471out:
3472 return ret;
3473}
3474
Yan Zheng9036c102008-10-30 14:19:41 -04003475int btrfs_cont_expand(struct inode *inode, loff_t size)
3476{
3477 struct btrfs_trans_handle *trans;
3478 struct btrfs_root *root = BTRFS_I(inode)->root;
3479 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003480 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003481 struct extent_state *cached_state = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003482 u64 mask = root->sectorsize - 1;
3483 u64 hole_start = (inode->i_size + mask) & ~mask;
3484 u64 block_end = (size + mask) & ~mask;
3485 u64 last_byte;
3486 u64 cur_offset;
3487 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003488 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003489
3490 if (size <= hole_start)
3491 return 0;
3492
Yan Zheng9036c102008-10-30 14:19:41 -04003493 while (1) {
3494 struct btrfs_ordered_extent *ordered;
3495 btrfs_wait_ordered_range(inode, hole_start,
3496 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003497 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3498 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003499 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3500 if (!ordered)
3501 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003502 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3503 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003504 btrfs_put_ordered_extent(ordered);
3505 }
3506
Yan Zheng9036c102008-10-30 14:19:41 -04003507 cur_offset = hole_start;
3508 while (1) {
3509 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3510 block_end - cur_offset, 0);
3511 BUG_ON(IS_ERR(em) || !em);
3512 last_byte = min(extent_map_end(em), block_end);
3513 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003514 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003515 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003516 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003517
Yan, Zhenga22285a2010-05-16 10:48:46 -04003518 trans = btrfs_start_transaction(root, 2);
3519 if (IS_ERR(trans)) {
3520 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003521 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003522 }
Yan, Zheng80825102009-11-12 09:35:36 +00003523 btrfs_set_trans_block_group(trans, inode);
3524
3525 err = btrfs_drop_extents(trans, inode, cur_offset,
3526 cur_offset + hole_size,
3527 &hint_byte, 1);
3528 BUG_ON(err);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003529
Yan Zheng9036c102008-10-30 14:19:41 -04003530 err = btrfs_insert_file_extent(trans, root,
3531 inode->i_ino, cur_offset, 0,
3532 0, hole_size, 0, hole_size,
3533 0, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003534 BUG_ON(err);
3535
Yan Zheng9036c102008-10-30 14:19:41 -04003536 btrfs_drop_extent_cache(inode, hole_start,
3537 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003538
3539 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003540 }
3541 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003542 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003543 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003544 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003545 break;
3546 }
3547
Yan, Zhenga22285a2010-05-16 10:48:46 -04003548 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003549 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3550 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003551 return err;
3552}
3553
Yan, Zheng80825102009-11-12 09:35:36 +00003554static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
3555{
3556 struct btrfs_root *root = BTRFS_I(inode)->root;
3557 struct btrfs_trans_handle *trans;
3558 unsigned long nr;
3559 int ret;
3560
3561 if (attr->ia_size == inode->i_size)
3562 return 0;
3563
3564 if (attr->ia_size > inode->i_size) {
3565 unsigned long limit;
3566 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
3567 if (attr->ia_size > inode->i_sb->s_maxbytes)
3568 return -EFBIG;
3569 if (limit != RLIM_INFINITY && attr->ia_size > limit) {
3570 send_sig(SIGXFSZ, current, 0);
3571 return -EFBIG;
3572 }
3573 }
3574
Yan, Zhengd68fc572010-05-16 10:49:58 -04003575 trans = btrfs_start_transaction(root, 5);
3576 if (IS_ERR(trans))
3577 return PTR_ERR(trans);
3578
Yan, Zheng80825102009-11-12 09:35:36 +00003579 btrfs_set_trans_block_group(trans, inode);
3580
3581 ret = btrfs_orphan_add(trans, inode);
3582 BUG_ON(ret);
3583
3584 nr = trans->blocks_used;
3585 btrfs_end_transaction(trans, root);
Yan, Zheng80825102009-11-12 09:35:36 +00003586 btrfs_btree_balance_dirty(root, nr);
3587
3588 if (attr->ia_size > inode->i_size) {
3589 ret = btrfs_cont_expand(inode, attr->ia_size);
3590 if (ret) {
3591 btrfs_truncate(inode);
3592 return ret;
3593 }
3594
3595 i_size_write(inode, attr->ia_size);
3596 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
3597
Yan, Zhengd68fc572010-05-16 10:49:58 -04003598 trans = btrfs_start_transaction(root, 0);
3599 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00003600 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003601 trans->block_rsv = root->orphan_block_rsv;
3602 BUG_ON(!trans->block_rsv);
Yan, Zheng80825102009-11-12 09:35:36 +00003603
3604 ret = btrfs_update_inode(trans, root, inode);
3605 BUG_ON(ret);
3606 if (inode->i_nlink > 0) {
3607 ret = btrfs_orphan_del(trans, inode);
3608 BUG_ON(ret);
3609 }
3610 nr = trans->blocks_used;
3611 btrfs_end_transaction(trans, root);
3612 btrfs_btree_balance_dirty(root, nr);
3613 return 0;
3614 }
3615
3616 /*
3617 * We're truncating a file that used to have good data down to
3618 * zero. Make sure it gets into the ordered flush list so that
3619 * any new writes get down to disk quickly.
3620 */
3621 if (attr->ia_size == 0)
3622 BTRFS_I(inode)->ordered_data_close = 1;
3623
3624 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3625 ret = vmtruncate(inode, attr->ia_size);
3626 BUG_ON(ret);
3627
3628 return 0;
3629}
3630
Chris Mason39279cc2007-06-12 06:35:45 -04003631static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3632{
3633 struct inode *inode = dentry->d_inode;
3634 int err;
3635
3636 err = inode_change_ok(inode, attr);
3637 if (err)
3638 return err;
3639
Chris Mason5a3f23d2009-03-31 13:27:11 -04003640 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Yan, Zheng80825102009-11-12 09:35:36 +00003641 err = btrfs_setattr_size(inode, attr);
3642 if (err)
3643 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003644 }
Yan, Zheng80825102009-11-12 09:35:36 +00003645 attr->ia_valid &= ~ATTR_SIZE;
Yan Zheng9036c102008-10-30 14:19:41 -04003646
Yan, Zheng80825102009-11-12 09:35:36 +00003647 if (attr->ia_valid)
3648 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04003649
3650 if (!err && ((attr->ia_valid & ATTR_MODE)))
3651 err = btrfs_acl_chmod(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003652 return err;
3653}
Chris Mason61295eb2008-01-14 16:24:38 -05003654
Chris Mason39279cc2007-06-12 06:35:45 -04003655void btrfs_delete_inode(struct inode *inode)
3656{
3657 struct btrfs_trans_handle *trans;
3658 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04003659 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003660 int ret;
3661
3662 truncate_inode_pages(&inode->i_data, 0);
3663 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003664 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003665 goto no_delete;
3666 }
Chris Mason4a096752008-07-21 10:29:44 -04003667 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003668
Yan, Zhengc71bf092009-11-12 09:34:40 +00003669 if (root->fs_info->log_root_recovering) {
3670 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3671 goto no_delete;
3672 }
3673
Yan, Zheng76dda932009-09-21 16:00:26 -04003674 if (inode->i_nlink > 0) {
3675 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3676 goto no_delete;
3677 }
3678
Chris Masondbe674a2008-07-17 12:54:05 -04003679 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003680
Yan, Zheng80825102009-11-12 09:35:36 +00003681 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003682 trans = btrfs_start_transaction(root, 0);
3683 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00003684 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003685 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003686
Yan, Zhengd68fc572010-05-16 10:49:58 -04003687 ret = btrfs_block_rsv_check(trans, root,
3688 root->orphan_block_rsv, 0, 5);
3689 if (ret) {
3690 BUG_ON(ret != -EAGAIN);
3691 ret = btrfs_commit_transaction(trans, root);
3692 BUG_ON(ret);
3693 continue;
3694 }
3695
3696 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003697 if (ret != -EAGAIN)
3698 break;
3699
3700 nr = trans->blocks_used;
3701 btrfs_end_transaction(trans, root);
3702 trans = NULL;
3703 btrfs_btree_balance_dirty(root, nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003704
Josef Bacik7b128762008-07-24 12:17:14 -04003705 }
3706
Yan, Zheng80825102009-11-12 09:35:36 +00003707 if (ret == 0) {
3708 ret = btrfs_orphan_del(trans, inode);
3709 BUG_ON(ret);
3710 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003711
Chris Masond3c2fdc2007-09-17 10:58:06 -04003712 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003713 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003714 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003715no_delete:
3716 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003717 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003718}
3719
3720/*
3721 * this returns the key found in the dir entry in the location pointer.
3722 * If no dir entries were found, location->objectid is 0.
3723 */
3724static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3725 struct btrfs_key *location)
3726{
3727 const char *name = dentry->d_name.name;
3728 int namelen = dentry->d_name.len;
3729 struct btrfs_dir_item *di;
3730 struct btrfs_path *path;
3731 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003732 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003733
3734 path = btrfs_alloc_path();
3735 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003736
Chris Mason39279cc2007-06-12 06:35:45 -04003737 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3738 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003739 if (IS_ERR(di))
3740 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003741
3742 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003743 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003744
Chris Mason5f39d392007-10-15 16:14:19 -04003745 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003746out:
Chris Mason39279cc2007-06-12 06:35:45 -04003747 btrfs_free_path(path);
3748 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003749out_err:
3750 location->objectid = 0;
3751 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003752}
3753
3754/*
3755 * when we hit a tree root in a directory, the btrfs part of the inode
3756 * needs to be changed to reflect the root directory of the tree root. This
3757 * is kind of like crossing a mount point.
3758 */
3759static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003760 struct inode *dir,
3761 struct dentry *dentry,
3762 struct btrfs_key *location,
3763 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003764{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003765 struct btrfs_path *path;
3766 struct btrfs_root *new_root;
3767 struct btrfs_root_ref *ref;
3768 struct extent_buffer *leaf;
3769 int ret;
3770 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003771
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003772 path = btrfs_alloc_path();
3773 if (!path) {
3774 err = -ENOMEM;
3775 goto out;
3776 }
Chris Mason39279cc2007-06-12 06:35:45 -04003777
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003778 err = -ENOENT;
3779 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3780 BTRFS_I(dir)->root->root_key.objectid,
3781 location->objectid);
3782 if (ret) {
3783 if (ret < 0)
3784 err = ret;
3785 goto out;
3786 }
Chris Mason39279cc2007-06-12 06:35:45 -04003787
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003788 leaf = path->nodes[0];
3789 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3790 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3791 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3792 goto out;
3793
3794 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3795 (unsigned long)(ref + 1),
3796 dentry->d_name.len);
3797 if (ret)
3798 goto out;
3799
3800 btrfs_release_path(root->fs_info->tree_root, path);
3801
3802 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3803 if (IS_ERR(new_root)) {
3804 err = PTR_ERR(new_root);
3805 goto out;
3806 }
3807
3808 if (btrfs_root_refs(&new_root->root_item) == 0) {
3809 err = -ENOENT;
3810 goto out;
3811 }
3812
3813 *sub_root = new_root;
3814 location->objectid = btrfs_root_dirid(&new_root->root_item);
3815 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003816 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003817 err = 0;
3818out:
3819 btrfs_free_path(path);
3820 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003821}
3822
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003823static void inode_tree_add(struct inode *inode)
3824{
3825 struct btrfs_root *root = BTRFS_I(inode)->root;
3826 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003827 struct rb_node **p;
3828 struct rb_node *parent;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003829again:
3830 p = &root->inode_tree.rb_node;
3831 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003832
Yan, Zheng76dda932009-09-21 16:00:26 -04003833 if (hlist_unhashed(&inode->i_hash))
3834 return;
3835
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003836 spin_lock(&root->inode_lock);
3837 while (*p) {
3838 parent = *p;
3839 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3840
3841 if (inode->i_ino < entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003842 p = &parent->rb_left;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003843 else if (inode->i_ino > entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003844 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003845 else {
3846 WARN_ON(!(entry->vfs_inode.i_state &
3847 (I_WILL_FREE | I_FREEING | I_CLEAR)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003848 rb_erase(parent, &root->inode_tree);
3849 RB_CLEAR_NODE(parent);
3850 spin_unlock(&root->inode_lock);
3851 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003852 }
3853 }
3854 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3855 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3856 spin_unlock(&root->inode_lock);
3857}
3858
3859static void inode_tree_del(struct inode *inode)
3860{
3861 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003862 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003863
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003864 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003865 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003866 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003867 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003868 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003869 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003870 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003871
3872 if (empty && btrfs_root_refs(&root->root_item) == 0) {
3873 synchronize_srcu(&root->fs_info->subvol_srcu);
3874 spin_lock(&root->inode_lock);
3875 empty = RB_EMPTY_ROOT(&root->inode_tree);
3876 spin_unlock(&root->inode_lock);
3877 if (empty)
3878 btrfs_add_dead_root(root);
3879 }
3880}
3881
3882int btrfs_invalidate_inodes(struct btrfs_root *root)
3883{
3884 struct rb_node *node;
3885 struct rb_node *prev;
3886 struct btrfs_inode *entry;
3887 struct inode *inode;
3888 u64 objectid = 0;
3889
3890 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3891
3892 spin_lock(&root->inode_lock);
3893again:
3894 node = root->inode_tree.rb_node;
3895 prev = NULL;
3896 while (node) {
3897 prev = node;
3898 entry = rb_entry(node, struct btrfs_inode, rb_node);
3899
3900 if (objectid < entry->vfs_inode.i_ino)
3901 node = node->rb_left;
3902 else if (objectid > entry->vfs_inode.i_ino)
3903 node = node->rb_right;
3904 else
3905 break;
3906 }
3907 if (!node) {
3908 while (prev) {
3909 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3910 if (objectid <= entry->vfs_inode.i_ino) {
3911 node = prev;
3912 break;
3913 }
3914 prev = rb_next(prev);
3915 }
3916 }
3917 while (node) {
3918 entry = rb_entry(node, struct btrfs_inode, rb_node);
3919 objectid = entry->vfs_inode.i_ino + 1;
3920 inode = igrab(&entry->vfs_inode);
3921 if (inode) {
3922 spin_unlock(&root->inode_lock);
3923 if (atomic_read(&inode->i_count) > 1)
3924 d_prune_aliases(inode);
3925 /*
3926 * btrfs_drop_inode will remove it from
3927 * the inode cache when its usage count
3928 * hits zero.
3929 */
3930 iput(inode);
3931 cond_resched();
3932 spin_lock(&root->inode_lock);
3933 goto again;
3934 }
3935
3936 if (cond_resched_lock(&root->inode_lock))
3937 goto again;
3938
3939 node = rb_next(node);
3940 }
3941 spin_unlock(&root->inode_lock);
3942 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003943}
3944
Chris Masone02119d2008-09-05 16:13:11 -04003945static int btrfs_init_locked_inode(struct inode *inode, void *p)
3946{
3947 struct btrfs_iget_args *args = p;
3948 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04003949 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003950 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003951 return 0;
3952}
3953
3954static int btrfs_find_actor(struct inode *inode, void *opaque)
3955{
3956 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05003957 return args->ino == inode->i_ino &&
3958 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003959}
3960
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003961static struct inode *btrfs_iget_locked(struct super_block *s,
3962 u64 objectid,
3963 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04003964{
3965 struct inode *inode;
3966 struct btrfs_iget_args args;
3967 args.ino = objectid;
3968 args.root = root;
3969
3970 inode = iget5_locked(s, objectid, btrfs_find_actor,
3971 btrfs_init_locked_inode,
3972 (void *)&args);
3973 return inode;
3974}
3975
Balaji Rao1a54ef82008-07-21 02:01:04 +05303976/* Get an inode object given its location and corresponding root.
3977 * Returns in *is_new if the inode was read from disk
3978 */
3979struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00003980 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05303981{
3982 struct inode *inode;
3983
3984 inode = btrfs_iget_locked(s, location->objectid, root);
3985 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003986 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303987
3988 if (inode->i_state & I_NEW) {
3989 BTRFS_I(inode)->root = root;
3990 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3991 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003992
3993 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303994 unlock_new_inode(inode);
Josef Bacik73f73412009-12-04 17:38:27 +00003995 if (new)
3996 *new = 1;
Balaji Rao1a54ef82008-07-21 02:01:04 +05303997 }
3998
3999 return inode;
4000}
4001
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004002static struct inode *new_simple_dir(struct super_block *s,
4003 struct btrfs_key *key,
4004 struct btrfs_root *root)
4005{
4006 struct inode *inode = new_inode(s);
4007
4008 if (!inode)
4009 return ERR_PTR(-ENOMEM);
4010
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004011 BTRFS_I(inode)->root = root;
4012 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
4013 BTRFS_I(inode)->dummy_inode = 1;
4014
4015 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
4016 inode->i_op = &simple_dir_inode_operations;
4017 inode->i_fop = &simple_dir_operations;
4018 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4019 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4020
4021 return inode;
4022}
4023
Chris Mason3de45862008-11-17 21:02:50 -05004024struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004025{
Chris Masond3977122009-01-05 21:25:51 -05004026 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004027 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004028 struct btrfs_root *sub_root = root;
4029 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004030 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004031 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004032
Yan, Zheng76dda932009-09-21 16:00:26 -04004033 dentry->d_op = &btrfs_dentry_operations;
4034
Chris Mason39279cc2007-06-12 06:35:45 -04004035 if (dentry->d_name.len > BTRFS_NAME_LEN)
4036 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004037
Chris Mason39279cc2007-06-12 06:35:45 -04004038 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04004039
Chris Mason39279cc2007-06-12 06:35:45 -04004040 if (ret < 0)
4041 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004042
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004043 if (location.objectid == 0)
4044 return NULL;
4045
4046 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004047 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004048 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004049 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004050
4051 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4052
Yan, Zheng76dda932009-09-21 16:00:26 -04004053 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004054 ret = fixup_tree_root_location(root, dir, dentry,
4055 &location, &sub_root);
4056 if (ret < 0) {
4057 if (ret != -ENOENT)
4058 inode = ERR_PTR(ret);
4059 else
4060 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4061 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004062 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004063 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004064 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4065
Yan, Zhengc71bf092009-11-12 09:34:40 +00004066 if (root != sub_root) {
4067 down_read(&root->fs_info->cleanup_work_sem);
4068 if (!(inode->i_sb->s_flags & MS_RDONLY))
4069 btrfs_orphan_cleanup(sub_root);
4070 up_read(&root->fs_info->cleanup_work_sem);
4071 }
4072
Chris Mason3de45862008-11-17 21:02:50 -05004073 return inode;
4074}
4075
Yan, Zheng76dda932009-09-21 16:00:26 -04004076static int btrfs_dentry_delete(struct dentry *dentry)
4077{
4078 struct btrfs_root *root;
4079
Yan, Zhengefefb142009-10-09 09:25:16 -04004080 if (!dentry->d_inode && !IS_ROOT(dentry))
4081 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04004082
Yan, Zhengefefb142009-10-09 09:25:16 -04004083 if (dentry->d_inode) {
4084 root = BTRFS_I(dentry->d_inode)->root;
4085 if (btrfs_root_refs(&root->root_item) == 0)
4086 return 1;
4087 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004088 return 0;
4089}
4090
Chris Mason3de45862008-11-17 21:02:50 -05004091static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4092 struct nameidata *nd)
4093{
4094 struct inode *inode;
4095
Chris Mason3de45862008-11-17 21:02:50 -05004096 inode = btrfs_lookup_dentry(dir, dentry);
4097 if (IS_ERR(inode))
4098 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04004099
Chris Mason39279cc2007-06-12 06:35:45 -04004100 return d_splice_alias(inode, dentry);
4101}
4102
Chris Mason39279cc2007-06-12 06:35:45 -04004103static unsigned char btrfs_filetype_table[] = {
4104 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4105};
4106
David Woodhousecbdf5a22008-08-06 19:42:33 +01004107static int btrfs_real_readdir(struct file *filp, void *dirent,
4108 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004109{
Chris Mason6da6aba2007-12-18 16:15:09 -05004110 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004111 struct btrfs_root *root = BTRFS_I(inode)->root;
4112 struct btrfs_item *item;
4113 struct btrfs_dir_item *di;
4114 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004115 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004116 struct btrfs_path *path;
4117 int ret;
4118 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04004119 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004120 int slot;
4121 int advance;
4122 unsigned char d_type;
4123 int over = 0;
4124 u32 di_cur;
4125 u32 di_total;
4126 u32 di_len;
4127 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004128 char tmp_name[32];
4129 char *name_ptr;
4130 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04004131
4132 /* FIXME, use a real flag for deciding about the key type */
4133 if (root->fs_info->tree_root == root)
4134 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004135
Chris Mason39544012007-12-12 14:38:19 -05004136 /* special case for "." */
4137 if (filp->f_pos == 0) {
4138 over = filldir(dirent, ".", 1,
4139 1, inode->i_ino,
4140 DT_DIR);
4141 if (over)
4142 return 0;
4143 filp->f_pos = 1;
4144 }
Chris Mason39544012007-12-12 14:38:19 -05004145 /* special case for .., just use the back ref */
4146 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004147 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004148 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004149 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004150 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004151 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004152 filp->f_pos = 2;
4153 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004154 path = btrfs_alloc_path();
4155 path->reada = 2;
4156
Chris Mason39279cc2007-06-12 06:35:45 -04004157 btrfs_set_key_type(&key, key_type);
4158 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01004159 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04004160
Chris Mason39279cc2007-06-12 06:35:45 -04004161 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4162 if (ret < 0)
4163 goto err;
4164 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01004165
4166 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004167 leaf = path->nodes[0];
4168 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004169 slot = path->slots[0];
4170 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01004171 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004172 ret = btrfs_next_leaf(root, path);
4173 if (ret)
4174 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004175 leaf = path->nodes[0];
4176 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004177 slot = path->slots[0];
4178 } else {
4179 slot++;
4180 path->slots[0]++;
4181 }
4182 }
Chris Mason3de45862008-11-17 21:02:50 -05004183
Chris Mason39279cc2007-06-12 06:35:45 -04004184 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04004185 item = btrfs_item_nr(leaf, slot);
4186 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4187
4188 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004189 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004190 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004191 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004192 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04004193 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04004194
4195 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01004196
Chris Mason39279cc2007-06-12 06:35:45 -04004197 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4198 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004199 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004200
4201 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004202 struct btrfs_key location;
4203
4204 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004205 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004206 name_ptr = tmp_name;
4207 } else {
4208 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004209 if (!name_ptr) {
4210 ret = -ENOMEM;
4211 goto err;
4212 }
Chris Mason5f39d392007-10-15 16:14:19 -04004213 }
4214 read_extent_buffer(leaf, name_ptr,
4215 (unsigned long)(di + 1), name_len);
4216
4217 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4218 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004219
4220 /* is this a reference to our own snapshot? If so
4221 * skip it
4222 */
4223 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4224 location.objectid == root->root_key.objectid) {
4225 over = 0;
4226 goto skip;
4227 }
Chris Mason5f39d392007-10-15 16:14:19 -04004228 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004229 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004230 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004231
Chris Mason3de45862008-11-17 21:02:50 -05004232skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004233 if (name_ptr != tmp_name)
4234 kfree(name_ptr);
4235
Chris Mason39279cc2007-06-12 06:35:45 -04004236 if (over)
4237 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004238 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004239 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004240 di_cur += di_len;
4241 di = (struct btrfs_dir_item *)((char *)di + di_len);
4242 }
4243 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004244
4245 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004246 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004247 /*
4248 * 32-bit glibc will use getdents64, but then strtol -
4249 * so the last number we can serve is this.
4250 */
4251 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004252 else
4253 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004254nopos:
4255 ret = 0;
4256err:
Chris Mason39279cc2007-06-12 06:35:45 -04004257 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004258 return ret;
4259}
4260
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004261int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004262{
4263 struct btrfs_root *root = BTRFS_I(inode)->root;
4264 struct btrfs_trans_handle *trans;
4265 int ret = 0;
4266
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004267 if (BTRFS_I(inode)->dummy_inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04004268 return 0;
4269
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004270 if (wbc->sync_mode == WB_SYNC_ALL) {
Chris Masonf9295742008-07-17 12:54:14 -04004271 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004272 btrfs_set_trans_block_group(trans, inode);
4273 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004274 }
4275 return ret;
4276}
4277
4278/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004279 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004280 * inode changes. But, it is most likely to find the inode in cache.
4281 * FIXME, needs more benchmarking...there are no reasons other than performance
4282 * to keep or drop this code.
4283 */
4284void btrfs_dirty_inode(struct inode *inode)
4285{
4286 struct btrfs_root *root = BTRFS_I(inode)->root;
4287 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004288 int ret;
4289
4290 if (BTRFS_I(inode)->dummy_inode)
4291 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004292
Chris Masonf9295742008-07-17 12:54:14 -04004293 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004294 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004295
4296 ret = btrfs_update_inode(trans, root, inode);
4297 if (ret)
4298 printk(KERN_ERR"btrfs: fail to dirty inode %lu error %d\n",
4299 inode->i_ino, ret);
4300
Chris Mason39279cc2007-06-12 06:35:45 -04004301 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004302}
4303
Chris Masond352ac62008-09-29 15:18:18 -04004304/*
4305 * find the highest existing sequence number in a directory
4306 * and then set the in-memory index_cnt variable to reflect
4307 * free sequence numbers
4308 */
Josef Bacikaec74772008-07-24 12:12:38 -04004309static int btrfs_set_inode_index_count(struct inode *inode)
4310{
4311 struct btrfs_root *root = BTRFS_I(inode)->root;
4312 struct btrfs_key key, found_key;
4313 struct btrfs_path *path;
4314 struct extent_buffer *leaf;
4315 int ret;
4316
4317 key.objectid = inode->i_ino;
4318 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4319 key.offset = (u64)-1;
4320
4321 path = btrfs_alloc_path();
4322 if (!path)
4323 return -ENOMEM;
4324
4325 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4326 if (ret < 0)
4327 goto out;
4328 /* FIXME: we should be able to handle this */
4329 if (ret == 0)
4330 goto out;
4331 ret = 0;
4332
4333 /*
4334 * MAGIC NUMBER EXPLANATION:
4335 * since we search a directory based on f_pos we have to start at 2
4336 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4337 * else has to start at 2
4338 */
4339 if (path->slots[0] == 0) {
4340 BTRFS_I(inode)->index_cnt = 2;
4341 goto out;
4342 }
4343
4344 path->slots[0]--;
4345
4346 leaf = path->nodes[0];
4347 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4348
4349 if (found_key.objectid != inode->i_ino ||
4350 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4351 BTRFS_I(inode)->index_cnt = 2;
4352 goto out;
4353 }
4354
4355 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4356out:
4357 btrfs_free_path(path);
4358 return ret;
4359}
4360
Chris Masond352ac62008-09-29 15:18:18 -04004361/*
4362 * helper to find a free sequence number in a given directory. This current
4363 * code is very simple, later versions will do smarter things in the btree
4364 */
Chris Mason3de45862008-11-17 21:02:50 -05004365int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004366{
4367 int ret = 0;
4368
4369 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4370 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05004371 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04004372 return ret;
4373 }
4374
Chris Mason00e4e6b2008-08-05 11:18:09 -04004375 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004376 BTRFS_I(dir)->index_cnt++;
4377
4378 return ret;
4379}
4380
Chris Mason39279cc2007-06-12 06:35:45 -04004381static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4382 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004383 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004384 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004385 u64 ref_objectid, u64 objectid,
4386 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004387{
4388 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004389 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004390 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004391 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004392 struct btrfs_inode_ref *ref;
4393 struct btrfs_key key[2];
4394 u32 sizes[2];
4395 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004396 int ret;
4397 int owner;
4398
Chris Mason5f39d392007-10-15 16:14:19 -04004399 path = btrfs_alloc_path();
4400 BUG_ON(!path);
4401
Chris Mason39279cc2007-06-12 06:35:45 -04004402 inode = new_inode(root->fs_info->sb);
4403 if (!inode)
4404 return ERR_PTR(-ENOMEM);
4405
Josef Bacikaec74772008-07-24 12:12:38 -04004406 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05004407 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004408 if (ret) {
4409 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004410 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004411 }
Josef Bacikaec74772008-07-24 12:12:38 -04004412 }
4413 /*
4414 * index_cnt is ignored for everything but a dir,
4415 * btrfs_get_inode_index_count has an explanation for the magic
4416 * number
4417 */
4418 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004419 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004420 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik6a632092009-02-20 11:00:09 -05004421 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004422
Chris Mason39279cc2007-06-12 06:35:45 -04004423 if (mode & S_IFDIR)
4424 owner = 0;
4425 else
4426 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004427 BTRFS_I(inode)->block_group =
4428 btrfs_find_block_group(root, 0, alloc_hint, owner);
Chris Mason9c583092008-01-29 15:15:18 -05004429
4430 key[0].objectid = objectid;
4431 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4432 key[0].offset = 0;
4433
4434 key[1].objectid = objectid;
4435 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4436 key[1].offset = ref_objectid;
4437
4438 sizes[0] = sizeof(struct btrfs_inode_item);
4439 sizes[1] = name_len + sizeof(*ref);
4440
Chris Masonb9473432009-03-13 11:00:37 -04004441 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004442 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4443 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004444 goto fail;
4445
Chris Mason79683f22008-11-19 22:00:53 -05004446 inode->i_uid = current_fsuid();
Chris Ball8c087b52009-02-04 09:29:54 -05004447
Chris Mason42f15d72009-02-06 11:35:57 -05004448 if (dir && (dir->i_mode & S_ISGID)) {
Chris Ball8c087b52009-02-04 09:29:54 -05004449 inode->i_gid = dir->i_gid;
4450 if (S_ISDIR(mode))
4451 mode |= S_ISGID;
4452 } else
4453 inode->i_gid = current_fsgid();
4454
Chris Mason39279cc2007-06-12 06:35:45 -04004455 inode->i_mode = mode;
4456 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004457 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004458 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004459 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4460 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004461 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004462
4463 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4464 struct btrfs_inode_ref);
4465 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004466 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004467 ptr = (unsigned long)(ref + 1);
4468 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4469
Chris Mason5f39d392007-10-15 16:14:19 -04004470 btrfs_mark_buffer_dirty(path->nodes[0]);
4471 btrfs_free_path(path);
4472
Chris Mason39279cc2007-06-12 06:35:45 -04004473 location = &BTRFS_I(inode)->location;
4474 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004475 location->offset = 0;
4476 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4477
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004478 btrfs_inherit_iflags(inode, dir);
4479
Chris Mason94272162009-07-02 12:26:06 -04004480 if ((mode & S_IFREG)) {
4481 if (btrfs_test_opt(root, NODATASUM))
4482 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4483 if (btrfs_test_opt(root, NODATACOW))
4484 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4485 }
4486
Chris Mason39279cc2007-06-12 06:35:45 -04004487 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004488 inode_tree_add(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004489 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004490fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004491 if (dir)
4492 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004493 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004494 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004495 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004496}
4497
4498static inline u8 btrfs_inode_type(struct inode *inode)
4499{
4500 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4501}
4502
Chris Masond352ac62008-09-29 15:18:18 -04004503/*
4504 * utility function to add 'inode' into 'parent_inode' with
4505 * a give name and a given sequence number.
4506 * if 'add_backref' is true, also insert a backref from the
4507 * inode to the parent directory.
4508 */
Chris Masone02119d2008-09-05 16:13:11 -04004509int btrfs_add_link(struct btrfs_trans_handle *trans,
4510 struct inode *parent_inode, struct inode *inode,
4511 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004512{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004513 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004514 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004515 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04004516
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004517 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4518 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4519 } else {
4520 key.objectid = inode->i_ino;
4521 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4522 key.offset = 0;
4523 }
Chris Mason39279cc2007-06-12 06:35:45 -04004524
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004525 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4526 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4527 key.objectid, root->root_key.objectid,
4528 parent_inode->i_ino,
4529 index, name, name_len);
4530 } else if (add_backref) {
4531 ret = btrfs_insert_inode_ref(trans, root,
4532 name, name_len, inode->i_ino,
4533 parent_inode->i_ino, index);
4534 }
4535
Chris Mason39279cc2007-06-12 06:35:45 -04004536 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004537 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4538 parent_inode->i_ino, &key,
4539 btrfs_inode_type(inode), index);
4540 BUG_ON(ret);
4541
Chris Masondbe674a2008-07-17 12:54:05 -04004542 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004543 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004544 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004545 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004546 }
4547 return ret;
4548}
4549
4550static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05004551 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004552 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004553{
Chris Masone02119d2008-09-05 16:13:11 -04004554 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4555 inode, dentry->d_name.name,
4556 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004557 if (!err) {
4558 d_instantiate(dentry, inode);
4559 return 0;
4560 }
4561 if (err > 0)
4562 err = -EEXIST;
4563 return err;
4564}
4565
Josef Bacik618e21d2007-07-11 10:18:17 -04004566static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4567 int mode, dev_t rdev)
4568{
4569 struct btrfs_trans_handle *trans;
4570 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004571 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004572 int err;
4573 int drop_inode = 0;
4574 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004575 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004576 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004577
4578 if (!new_valid_dev(rdev))
4579 return -EINVAL;
4580
Yan, Zhenga22285a2010-05-16 10:48:46 -04004581 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4582 if (err)
4583 return err;
4584
Josef Bacik9ed74f22009-09-11 16:12:44 -04004585 /*
4586 * 2 for inode item and ref
4587 * 2 for dir items
4588 * 1 for xattr if selinux is on
4589 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004590 trans = btrfs_start_transaction(root, 5);
4591 if (IS_ERR(trans))
4592 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004593
Josef Bacik618e21d2007-07-11 10:18:17 -04004594 btrfs_set_trans_block_group(trans, dir);
4595
Josef Bacikaec74772008-07-24 12:12:38 -04004596 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004597 dentry->d_name.len,
4598 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004599 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004600 err = PTR_ERR(inode);
4601 if (IS_ERR(inode))
4602 goto out_unlock;
4603
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004604 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004605 if (err) {
4606 drop_inode = 1;
4607 goto out_unlock;
4608 }
4609
Josef Bacik618e21d2007-07-11 10:18:17 -04004610 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004611 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004612 if (err)
4613 drop_inode = 1;
4614 else {
4615 inode->i_op = &btrfs_special_inode_operations;
4616 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004617 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004618 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004619 btrfs_update_inode_block_group(trans, inode);
4620 btrfs_update_inode_block_group(trans, dir);
4621out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004622 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004623 btrfs_end_transaction_throttle(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004624 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004625 if (drop_inode) {
4626 inode_dec_link_count(inode);
4627 iput(inode);
4628 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004629 return err;
4630}
4631
Chris Mason39279cc2007-06-12 06:35:45 -04004632static int btrfs_create(struct inode *dir, struct dentry *dentry,
4633 int mode, struct nameidata *nd)
4634{
4635 struct btrfs_trans_handle *trans;
4636 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004637 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004638 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004639 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004640 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004641 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004642 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004643
Yan, Zhenga22285a2010-05-16 10:48:46 -04004644 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4645 if (err)
4646 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004647 /*
4648 * 2 for inode item and ref
4649 * 2 for dir items
4650 * 1 for xattr if selinux is on
4651 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004652 trans = btrfs_start_transaction(root, 5);
4653 if (IS_ERR(trans))
4654 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004655
Chris Mason39279cc2007-06-12 06:35:45 -04004656 btrfs_set_trans_block_group(trans, dir);
4657
Josef Bacikaec74772008-07-24 12:12:38 -04004658 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004659 dentry->d_name.len,
4660 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004661 objectid, BTRFS_I(dir)->block_group, mode,
4662 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004663 err = PTR_ERR(inode);
4664 if (IS_ERR(inode))
4665 goto out_unlock;
4666
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004667 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004668 if (err) {
4669 drop_inode = 1;
4670 goto out_unlock;
4671 }
4672
Chris Mason39279cc2007-06-12 06:35:45 -04004673 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004674 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004675 if (err)
4676 drop_inode = 1;
4677 else {
4678 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004679 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004680 inode->i_fop = &btrfs_file_operations;
4681 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004682 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004683 }
Chris Mason39279cc2007-06-12 06:35:45 -04004684 btrfs_update_inode_block_group(trans, inode);
4685 btrfs_update_inode_block_group(trans, dir);
4686out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004687 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004688 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004689 if (drop_inode) {
4690 inode_dec_link_count(inode);
4691 iput(inode);
4692 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04004693 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004694 return err;
4695}
4696
4697static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4698 struct dentry *dentry)
4699{
4700 struct btrfs_trans_handle *trans;
4701 struct btrfs_root *root = BTRFS_I(dir)->root;
4702 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004703 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004704 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004705 int err;
4706 int drop_inode = 0;
4707
4708 if (inode->i_nlink == 0)
4709 return -ENOENT;
4710
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004711 /* do not allow sys_link's with other subvols of the same device */
4712 if (root->objectid != BTRFS_I(inode)->root->objectid)
4713 return -EPERM;
4714
Josef Bacik9ed74f22009-09-11 16:12:44 -04004715 btrfs_inc_nlink(inode);
4716
Chris Mason3de45862008-11-17 21:02:50 -05004717 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004718 if (err)
4719 goto fail;
4720
Yan, Zhenga22285a2010-05-16 10:48:46 -04004721 /*
4722 * 1 item for inode ref
4723 * 2 items for dir items
4724 */
4725 trans = btrfs_start_transaction(root, 3);
4726 if (IS_ERR(trans)) {
4727 err = PTR_ERR(trans);
4728 goto fail;
4729 }
Chris Mason5f39d392007-10-15 16:14:19 -04004730
Chris Mason39279cc2007-06-12 06:35:45 -04004731 btrfs_set_trans_block_group(trans, dir);
4732 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04004733
Chris Mason00e4e6b2008-08-05 11:18:09 -04004734 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004735
Yan, Zhenga5719522009-09-24 09:17:31 -04004736 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004737 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004738 } else {
4739 btrfs_update_inode_block_group(trans, dir);
4740 err = btrfs_update_inode(trans, root, inode);
4741 BUG_ON(err);
4742 btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
4743 }
Chris Mason39279cc2007-06-12 06:35:45 -04004744
Chris Masond3c2fdc2007-09-17 10:58:06 -04004745 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004746 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004747fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004748 if (drop_inode) {
4749 inode_dec_link_count(inode);
4750 iput(inode);
4751 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04004752 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004753 return err;
4754}
4755
Chris Mason39279cc2007-06-12 06:35:45 -04004756static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4757{
Chris Masonb9d86662008-05-02 16:13:49 -04004758 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004759 struct btrfs_trans_handle *trans;
4760 struct btrfs_root *root = BTRFS_I(dir)->root;
4761 int err = 0;
4762 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004763 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004764 u64 index = 0;
Chris Masond3c2fdc2007-09-17 10:58:06 -04004765 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004766
Yan, Zhenga22285a2010-05-16 10:48:46 -04004767 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4768 if (err)
4769 return err;
4770
Josef Bacik9ed74f22009-09-11 16:12:44 -04004771 /*
4772 * 2 items for inode and ref
4773 * 2 items for dir items
4774 * 1 for xattr if selinux is on
4775 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004776 trans = btrfs_start_transaction(root, 5);
4777 if (IS_ERR(trans))
4778 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004779 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004780
Josef Bacikaec74772008-07-24 12:12:38 -04004781 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004782 dentry->d_name.len,
4783 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004784 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4785 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004786 if (IS_ERR(inode)) {
4787 err = PTR_ERR(inode);
4788 goto out_fail;
4789 }
Chris Mason5f39d392007-10-15 16:14:19 -04004790
Chris Mason39279cc2007-06-12 06:35:45 -04004791 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004792
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004793 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004794 if (err)
4795 goto out_fail;
4796
Chris Mason39279cc2007-06-12 06:35:45 -04004797 inode->i_op = &btrfs_dir_inode_operations;
4798 inode->i_fop = &btrfs_dir_file_operations;
4799 btrfs_set_trans_block_group(trans, inode);
4800
Chris Masondbe674a2008-07-17 12:54:05 -04004801 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004802 err = btrfs_update_inode(trans, root, inode);
4803 if (err)
4804 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004805
Chris Masone02119d2008-09-05 16:13:11 -04004806 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4807 inode, dentry->d_name.name,
4808 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004809 if (err)
4810 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004811
Chris Mason39279cc2007-06-12 06:35:45 -04004812 d_instantiate(dentry, inode);
4813 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004814 btrfs_update_inode_block_group(trans, inode);
4815 btrfs_update_inode_block_group(trans, dir);
4816
4817out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004818 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004819 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004820 if (drop_on_err)
4821 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04004822 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004823 return err;
4824}
4825
Chris Masond352ac62008-09-29 15:18:18 -04004826/* helper for btfs_get_extent. Given an existing extent in the tree,
4827 * and an extent that you want to insert, deal with overlap and insert
4828 * the new extent into the tree.
4829 */
Chris Mason3b951512008-04-17 11:29:12 -04004830static int merge_extent_mapping(struct extent_map_tree *em_tree,
4831 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004832 struct extent_map *em,
4833 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04004834{
4835 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04004836
Chris Masone6dcd2d2008-07-17 12:53:50 -04004837 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4838 start_diff = map_start - em->start;
4839 em->start = map_start;
4840 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04004841 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4842 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004843 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04004844 em->block_len -= start_diff;
4845 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004846 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004847}
4848
Chris Masonc8b97812008-10-29 14:49:59 -04004849static noinline int uncompress_inline(struct btrfs_path *path,
4850 struct inode *inode, struct page *page,
4851 size_t pg_offset, u64 extent_offset,
4852 struct btrfs_file_extent_item *item)
4853{
4854 int ret;
4855 struct extent_buffer *leaf = path->nodes[0];
4856 char *tmp;
4857 size_t max_size;
4858 unsigned long inline_size;
4859 unsigned long ptr;
4860
4861 WARN_ON(pg_offset != 0);
4862 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4863 inline_size = btrfs_file_extent_inline_item_len(leaf,
4864 btrfs_item_nr(leaf, path->slots[0]));
4865 tmp = kmalloc(inline_size, GFP_NOFS);
4866 ptr = btrfs_file_extent_inline_start(item);
4867
4868 read_extent_buffer(leaf, tmp, ptr, inline_size);
4869
Chris Mason5b050f02008-11-11 09:34:41 -05004870 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004871 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
4872 inline_size, max_size);
4873 if (ret) {
4874 char *kaddr = kmap_atomic(page, KM_USER0);
4875 unsigned long copy_size = min_t(u64,
4876 PAGE_CACHE_SIZE - pg_offset,
4877 max_size - extent_offset);
4878 memset(kaddr + pg_offset, 0, copy_size);
4879 kunmap_atomic(kaddr, KM_USER0);
4880 }
4881 kfree(tmp);
4882 return 0;
4883}
4884
Chris Masond352ac62008-09-29 15:18:18 -04004885/*
4886 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05004887 * the ugly parts come from merging extents from the disk with the in-ram
4888 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04004889 * where the in-ram extents might be locked pending data=ordered completion.
4890 *
4891 * This also copies inline extents directly into the page.
4892 */
Chris Masond3977122009-01-05 21:25:51 -05004893
Chris Masona52d9a82007-08-27 16:49:44 -04004894struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05004895 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04004896 int create)
4897{
4898 int ret;
4899 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04004900 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04004901 u64 extent_start = 0;
4902 u64 extent_end = 0;
4903 u64 objectid = inode->i_ino;
4904 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04004905 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04004906 struct btrfs_root *root = BTRFS_I(inode)->root;
4907 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04004908 struct extent_buffer *leaf;
4909 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04004910 struct extent_map *em = NULL;
4911 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05004912 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004913 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004914 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04004915
Chris Masona52d9a82007-08-27 16:49:44 -04004916again:
Chris Mason890871b2009-09-02 16:24:52 -04004917 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004918 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04004919 if (em)
4920 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04004921 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004922
Chris Masona52d9a82007-08-27 16:49:44 -04004923 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04004924 if (em->start > start || em->start + em->len <= start)
4925 free_extent_map(em);
4926 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05004927 free_extent_map(em);
4928 else
4929 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004930 }
Chris Masond1310b22008-01-24 16:13:08 -05004931 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004932 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05004933 err = -ENOMEM;
4934 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004935 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004936 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05004937 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05004938 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05004939 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04004940 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04004941
4942 if (!path) {
4943 path = btrfs_alloc_path();
4944 BUG_ON(!path);
4945 }
4946
Chris Mason179e29e2007-11-01 11:28:41 -04004947 ret = btrfs_lookup_file_extent(trans, root, path,
4948 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04004949 if (ret < 0) {
4950 err = ret;
4951 goto out;
4952 }
4953
4954 if (ret != 0) {
4955 if (path->slots[0] == 0)
4956 goto not_found;
4957 path->slots[0]--;
4958 }
4959
Chris Mason5f39d392007-10-15 16:14:19 -04004960 leaf = path->nodes[0];
4961 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04004962 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04004963 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04004964 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4965 found_type = btrfs_key_type(&found_key);
4966 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04004967 found_type != BTRFS_EXTENT_DATA_KEY) {
4968 goto not_found;
4969 }
4970
Chris Mason5f39d392007-10-15 16:14:19 -04004971 found_type = btrfs_file_extent_type(leaf, item);
4972 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004973 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04004974 if (found_type == BTRFS_FILE_EXTENT_REG ||
4975 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04004976 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04004977 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04004978 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
4979 size_t size;
4980 size = btrfs_file_extent_inline_len(leaf, item);
4981 extent_end = (extent_start + size + root->sectorsize - 1) &
4982 ~((u64)root->sectorsize - 1);
4983 }
4984
4985 if (start >= extent_end) {
4986 path->slots[0]++;
4987 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
4988 ret = btrfs_next_leaf(root, path);
4989 if (ret < 0) {
4990 err = ret;
4991 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004992 }
Yan Zheng9036c102008-10-30 14:19:41 -04004993 if (ret > 0)
4994 goto not_found;
4995 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04004996 }
Yan Zheng9036c102008-10-30 14:19:41 -04004997 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4998 if (found_key.objectid != objectid ||
4999 found_key.type != BTRFS_EXTENT_DATA_KEY)
5000 goto not_found;
5001 if (start + len <= found_key.offset)
5002 goto not_found;
5003 em->start = start;
5004 em->len = found_key.offset - start;
5005 goto not_found_em;
5006 }
5007
Yan Zhengd899e052008-10-30 14:25:28 -04005008 if (found_type == BTRFS_FILE_EXTENT_REG ||
5009 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005010 em->start = extent_start;
5011 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005012 em->orig_start = extent_start -
5013 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005014 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5015 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005016 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005017 goto insert;
5018 }
Chris Masonc8b97812008-10-29 14:49:59 -04005019 if (compressed) {
5020 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
5021 em->block_start = bytenr;
5022 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5023 item);
5024 } else {
5025 bytenr += btrfs_file_extent_offset(leaf, item);
5026 em->block_start = bytenr;
5027 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005028 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5029 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005030 }
Chris Masona52d9a82007-08-27 16:49:44 -04005031 goto insert;
5032 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005033 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005034 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005035 size_t size;
5036 size_t extent_offset;
5037 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005038
Chris Masona52d9a82007-08-27 16:49:44 -04005039 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005040 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005041 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005042 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005043 goto out;
5044 }
5045
Yan Zheng9036c102008-10-30 14:19:41 -04005046 size = btrfs_file_extent_inline_len(leaf, item);
5047 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005048 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005049 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005050 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005051 em->len = (copy_size + root->sectorsize - 1) &
5052 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005053 em->orig_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005054 if (compressed)
5055 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04005056 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005057 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04005058 if (btrfs_file_extent_compression(leaf, item) ==
5059 BTRFS_COMPRESS_ZLIB) {
5060 ret = uncompress_inline(path, inode, page,
5061 pg_offset,
5062 extent_offset, item);
5063 BUG_ON(ret);
5064 } else {
5065 map = kmap(page);
5066 read_extent_buffer(leaf, map + pg_offset, ptr,
5067 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005068 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5069 memset(map + pg_offset + copy_size, 0,
5070 PAGE_CACHE_SIZE - pg_offset -
5071 copy_size);
5072 }
Chris Masonc8b97812008-10-29 14:49:59 -04005073 kunmap(page);
5074 }
Chris Mason179e29e2007-11-01 11:28:41 -04005075 flush_dcache_page(page);
5076 } else if (create && PageUptodate(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005077 WARN_ON(1);
Chris Mason179e29e2007-11-01 11:28:41 -04005078 if (!trans) {
5079 kunmap(page);
5080 free_extent_map(em);
5081 em = NULL;
5082 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04005083 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04005084 goto again;
5085 }
Chris Masonc8b97812008-10-29 14:49:59 -04005086 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005087 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005088 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005089 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005090 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005091 }
Chris Masond1310b22008-01-24 16:13:08 -05005092 set_extent_uptodate(io_tree, em->start,
5093 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005094 goto insert;
5095 } else {
Chris Masond3977122009-01-05 21:25:51 -05005096 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005097 WARN_ON(1);
5098 }
5099not_found:
5100 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005101 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005102not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005103 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005104 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005105insert:
5106 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05005107 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005108 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5109 "[%llu %llu]\n", (unsigned long long)em->start,
5110 (unsigned long long)em->len,
5111 (unsigned long long)start,
5112 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005113 err = -EIO;
5114 goto out;
5115 }
Chris Masond1310b22008-01-24 16:13:08 -05005116
5117 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005118 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005119 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005120 /* it is possible that someone inserted the extent into the tree
5121 * while we had the lock dropped. It is also possible that
5122 * an overlapping map exists in the tree
5123 */
Chris Masona52d9a82007-08-27 16:49:44 -04005124 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005125 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005126
5127 ret = 0;
5128
Chris Mason3b951512008-04-17 11:29:12 -04005129 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005130 if (existing && (existing->start > start ||
5131 existing->start + existing->len <= start)) {
5132 free_extent_map(existing);
5133 existing = NULL;
5134 }
Chris Mason3b951512008-04-17 11:29:12 -04005135 if (!existing) {
5136 existing = lookup_extent_mapping(em_tree, em->start,
5137 em->len);
5138 if (existing) {
5139 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005140 em, start,
5141 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005142 free_extent_map(existing);
5143 if (err) {
5144 free_extent_map(em);
5145 em = NULL;
5146 }
5147 } else {
5148 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005149 free_extent_map(em);
5150 em = NULL;
5151 }
5152 } else {
5153 free_extent_map(em);
5154 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005155 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005156 }
Chris Masona52d9a82007-08-27 16:49:44 -04005157 }
Chris Mason890871b2009-09-02 16:24:52 -04005158 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005159out:
Chris Masonf4219502008-07-22 11:18:09 -04005160 if (path)
5161 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005162 if (trans) {
5163 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005164 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005165 err = ret;
5166 }
Chris Masona52d9a82007-08-27 16:49:44 -04005167 if (err) {
5168 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005169 return ERR_PTR(err);
5170 }
5171 return em;
5172}
5173
Chris Mason16432982008-04-10 10:23:21 -04005174static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
5175 const struct iovec *iov, loff_t offset,
5176 unsigned long nr_segs)
5177{
Chris Masone1c4b742008-04-22 13:26:46 -04005178 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04005179}
5180
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05005181static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
5182 __u64 start, __u64 len)
5183{
5184 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
5185}
5186
Chris Mason9ebefb182007-06-15 13:50:00 -04005187int btrfs_readpage(struct file *file, struct page *page)
5188{
Chris Masond1310b22008-01-24 16:13:08 -05005189 struct extent_io_tree *tree;
5190 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005191 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04005192}
Chris Mason1832a6d2007-12-21 16:27:21 -05005193
Chris Mason39279cc2007-06-12 06:35:45 -04005194static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
5195{
Chris Masond1310b22008-01-24 16:13:08 -05005196 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04005197
5198
5199 if (current->flags & PF_MEMALLOC) {
5200 redirty_page_for_writepage(wbc, page);
5201 unlock_page(page);
5202 return 0;
5203 }
Chris Masond1310b22008-01-24 16:13:08 -05005204 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005205 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
5206}
Chris Mason39279cc2007-06-12 06:35:45 -04005207
Chris Masonf4219502008-07-22 11:18:09 -04005208int btrfs_writepages(struct address_space *mapping,
5209 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04005210{
Chris Masond1310b22008-01-24 16:13:08 -05005211 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05005212
Chris Masond1310b22008-01-24 16:13:08 -05005213 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04005214 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
5215}
5216
Chris Mason3ab2fb52007-11-08 10:59:22 -05005217static int
5218btrfs_readpages(struct file *file, struct address_space *mapping,
5219 struct list_head *pages, unsigned nr_pages)
5220{
Chris Masond1310b22008-01-24 16:13:08 -05005221 struct extent_io_tree *tree;
5222 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05005223 return extent_readpages(tree, mapping, pages, nr_pages,
5224 btrfs_get_extent);
5225}
Chris Masone6dcd2d2008-07-17 12:53:50 -04005226static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04005227{
Chris Masond1310b22008-01-24 16:13:08 -05005228 struct extent_io_tree *tree;
5229 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04005230 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005231
Chris Masond1310b22008-01-24 16:13:08 -05005232 tree = &BTRFS_I(page->mapping->host)->io_tree;
5233 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05005234 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005235 if (ret == 1) {
5236 ClearPagePrivate(page);
5237 set_page_private(page, 0);
5238 page_cache_release(page);
5239 }
5240 return ret;
5241}
Chris Mason39279cc2007-06-12 06:35:45 -04005242
Chris Masone6dcd2d2008-07-17 12:53:50 -04005243static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
5244{
Chris Mason98509cf2008-09-11 15:51:43 -04005245 if (PageWriteback(page) || PageDirty(page))
5246 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05005247 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005248}
5249
Chris Masona52d9a82007-08-27 16:49:44 -04005250static void btrfs_invalidatepage(struct page *page, unsigned long offset)
5251{
Chris Masond1310b22008-01-24 16:13:08 -05005252 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005253 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005254 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005255 u64 page_start = page_offset(page);
5256 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04005257
Chris Mason8b62b722009-09-02 16:53:46 -04005258
5259 /*
5260 * we have the page locked, so new writeback can't start,
5261 * and the dirty bit won't be cleared while we are here.
5262 *
5263 * Wait for IO on this page so that we can safely clear
5264 * the PagePrivate2 bit and do ordered accounting
5265 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005266 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04005267
Chris Masond1310b22008-01-24 16:13:08 -05005268 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005269 if (offset) {
5270 btrfs_releasepage(page, GFP_NOFS);
5271 return;
5272 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00005273 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
5274 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005275 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
5276 page_offset(page));
5277 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04005278 /*
5279 * IO on this page will never be started, so we need
5280 * to account for any ordered extents now
5281 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005282 clear_extent_bit(tree, page_start, page_end,
5283 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04005284 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00005285 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04005286 /*
5287 * whoever cleared the private bit is responsible
5288 * for the finish_ordered_io
5289 */
5290 if (TestClearPagePrivate2(page)) {
5291 btrfs_finish_ordered_io(page->mapping->host,
5292 page_start, page_end);
5293 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005294 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00005295 cached_state = NULL;
5296 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
5297 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005298 }
5299 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04005300 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00005301 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005302 __btrfs_releasepage(page, GFP_NOFS);
5303
Chris Mason4a096752008-07-21 10:29:44 -04005304 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04005305 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04005306 ClearPagePrivate(page);
5307 set_page_private(page, 0);
5308 page_cache_release(page);
5309 }
Chris Mason39279cc2007-06-12 06:35:45 -04005310}
5311
Chris Mason9ebefb182007-06-15 13:50:00 -04005312/*
5313 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
5314 * called from a page fault handler when a page is first dirtied. Hence we must
5315 * be careful to check for EOF conditions here. We set the page up correctly
5316 * for a written page which means we get ENOSPC checking when writing into
5317 * holes and correct delalloc and unwritten extent mapping on filesystems that
5318 * support these features.
5319 *
5320 * We are not allowed to take the i_mutex here so we have to play games to
5321 * protect against truncate races as the page could now be beyond EOF. Because
5322 * vmtruncate() writes the inode size before removing pages, once we have the
5323 * page lock we can determine safely if the page is beyond EOF. If it is not
5324 * beyond EOF, then the page is guaranteed safe against truncation until we
5325 * unlock the page.
5326 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07005327int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04005328{
Nick Pigginc2ec1752009-03-31 15:23:21 -07005329 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05005330 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05005331 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005332 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5333 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005334 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005335 char *kaddr;
5336 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04005337 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05005338 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04005339 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005340 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04005341
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005342 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07005343 if (ret) {
5344 if (ret == -ENOMEM)
5345 ret = VM_FAULT_OOM;
5346 else /* -ENOSPC, -EIO, etc */
5347 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05005348 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07005349 }
Chris Mason1832a6d2007-12-21 16:27:21 -05005350
Nick Piggin56a76f82009-03-31 15:23:23 -07005351 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005352again:
Chris Mason9ebefb182007-06-15 13:50:00 -04005353 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04005354 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005355 page_start = page_offset(page);
5356 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04005357
Chris Mason9ebefb182007-06-15 13:50:00 -04005358 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04005359 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04005360 /* page got truncated out from underneath us */
5361 goto out_unlock;
5362 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005363 wait_on_page_writeback(page);
5364
Josef Bacik2ac55d42010-02-03 19:33:23 +00005365 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
5366 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005367 set_page_extent_mapped(page);
5368
Chris Masoneb84ae02008-07-17 13:53:27 -04005369 /*
5370 * we can't set the delalloc bits if there are pending ordered
5371 * extents. Drop our locks and wait for them to finish
5372 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005373 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5374 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00005375 unlock_extent_cached(io_tree, page_start, page_end,
5376 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005377 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04005378 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005379 btrfs_put_ordered_extent(ordered);
5380 goto again;
5381 }
5382
Josef Bacikfbf19082009-10-01 17:10:23 -04005383 /*
5384 * XXX - page_mkwrite gets called every time the page is dirtied, even
5385 * if it was already dirty, so for space accounting reasons we need to
5386 * clear any delalloc bits for the range we are fixing to save. There
5387 * is probably a better way to do this, but for now keep consistent with
5388 * prepare_pages in the normal write path.
5389 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00005390 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04005391 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00005392 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04005393
Josef Bacik2ac55d42010-02-03 19:33:23 +00005394 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
5395 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005396 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00005397 unlock_extent_cached(io_tree, page_start, page_end,
5398 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005399 ret = VM_FAULT_SIGBUS;
5400 goto out_unlock;
5401 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005402 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04005403
5404 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04005405 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005406 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04005407 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04005408 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04005409
Chris Masone6dcd2d2008-07-17 12:53:50 -04005410 if (zero_start != PAGE_CACHE_SIZE) {
5411 kaddr = kmap(page);
5412 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
5413 flush_dcache_page(page);
5414 kunmap(page);
5415 }
Chris Mason247e7432008-07-17 12:53:51 -04005416 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005417 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04005418 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005419
Chris Mason257c62e2009-10-13 13:21:08 -04005420 BTRFS_I(inode)->last_trans = root->fs_info->generation;
5421 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
5422
Josef Bacik2ac55d42010-02-03 19:33:23 +00005423 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04005424
5425out_unlock:
Chris Mason50a9b212009-09-11 12:33:12 -04005426 if (!ret)
5427 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04005428 unlock_page(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005429 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason1832a6d2007-12-21 16:27:21 -05005430out:
Chris Mason9ebefb182007-06-15 13:50:00 -04005431 return ret;
5432}
5433
Chris Mason39279cc2007-06-12 06:35:45 -04005434static void btrfs_truncate(struct inode *inode)
5435{
5436 struct btrfs_root *root = BTRFS_I(inode)->root;
5437 int ret;
5438 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04005439 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04005440 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005441
Yan, Zheng80825102009-11-12 09:35:36 +00005442 if (!S_ISREG(inode->i_mode)) {
5443 WARN_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -04005444 return;
Yan, Zheng80825102009-11-12 09:35:36 +00005445 }
Chris Mason39279cc2007-06-12 06:35:45 -04005446
Josef Bacik5d5e1032009-10-13 16:46:49 -04005447 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
5448 if (ret)
5449 return;
Yan, Zheng80825102009-11-12 09:35:36 +00005450
Chris Mason4a096752008-07-21 10:29:44 -04005451 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00005452 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005453
Yan, Zhengd68fc572010-05-16 10:49:58 -04005454 trans = btrfs_start_transaction(root, 0);
5455 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00005456 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04005457 trans->block_rsv = root->orphan_block_rsv;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005458
5459 /*
5460 * setattr is responsible for setting the ordered_data_close flag,
5461 * but that is only tested during the last file release. That
5462 * could happen well after the next commit, leaving a great big
5463 * window where new writes may get lost if someone chooses to write
5464 * to this file after truncating to zero
5465 *
5466 * The inode doesn't have any dirty data here, and so if we commit
5467 * this is a noop. If someone immediately starts writing to the inode
5468 * it is very likely we'll catch some of their writes in this
5469 * transaction, and the commit will find this file on the ordered
5470 * data list with good things to send down.
5471 *
5472 * This is a best effort solution, there is still a window where
5473 * using truncate to replace the contents of the file will
5474 * end up with a zero length file after a crash.
5475 */
5476 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
5477 btrfs_add_ordered_operation(trans, root, inode);
5478
Yan, Zheng80825102009-11-12 09:35:36 +00005479 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04005480 if (!trans) {
5481 trans = btrfs_start_transaction(root, 0);
5482 BUG_ON(IS_ERR(trans));
5483 btrfs_set_trans_block_group(trans, inode);
5484 trans->block_rsv = root->orphan_block_rsv;
5485 }
5486
5487 ret = btrfs_block_rsv_check(trans, root,
5488 root->orphan_block_rsv, 0, 5);
5489 if (ret) {
5490 BUG_ON(ret != -EAGAIN);
5491 ret = btrfs_commit_transaction(trans, root);
5492 BUG_ON(ret);
5493 trans = NULL;
5494 continue;
5495 }
5496
Yan, Zheng80825102009-11-12 09:35:36 +00005497 ret = btrfs_truncate_inode_items(trans, root, inode,
5498 inode->i_size,
5499 BTRFS_EXTENT_DATA_KEY);
5500 if (ret != -EAGAIN)
5501 break;
Chris Mason39279cc2007-06-12 06:35:45 -04005502
Yan, Zheng80825102009-11-12 09:35:36 +00005503 ret = btrfs_update_inode(trans, root, inode);
5504 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005505
Yan, Zheng80825102009-11-12 09:35:36 +00005506 nr = trans->blocks_used;
5507 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04005508 trans = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00005509 btrfs_btree_balance_dirty(root, nr);
Yan, Zheng80825102009-11-12 09:35:36 +00005510 }
5511
5512 if (ret == 0 && inode->i_nlink > 0) {
5513 ret = btrfs_orphan_del(trans, inode);
5514 BUG_ON(ret);
5515 }
5516
5517 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04005518 BUG_ON(ret);
5519
Josef Bacik7b128762008-07-24 12:17:14 -04005520 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04005521 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005522 BUG_ON(ret);
Chris Masond3c2fdc2007-09-17 10:58:06 -04005523 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005524}
5525
Sven Wegener3b963622008-06-09 21:57:42 -04005526/*
Chris Masond352ac62008-09-29 15:18:18 -04005527 * create a new subvolume directory/inode (helper for the ioctl).
5528 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05005529int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Yan, Zheng76dda932009-09-21 16:00:26 -04005530 struct btrfs_root *new_root,
Yan Zhengd2fb3432008-12-11 16:30:39 -05005531 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04005532{
Chris Mason39279cc2007-06-12 06:35:45 -04005533 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005534 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005535 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005536
Josef Bacikaec74772008-07-24 12:12:38 -04005537 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05005538 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04005539 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005540 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005541 inode->i_op = &btrfs_dir_inode_operations;
5542 inode->i_fop = &btrfs_dir_file_operations;
5543
Chris Mason39279cc2007-06-12 06:35:45 -04005544 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04005545 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04005546
Yan, Zheng76dda932009-09-21 16:00:26 -04005547 err = btrfs_update_inode(trans, new_root, inode);
5548 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04005549
Yan, Zheng76dda932009-09-21 16:00:26 -04005550 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04005551 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005552}
5553
Chris Masond352ac62008-09-29 15:18:18 -04005554/* helper function for file defrag and space balancing. This
5555 * forces readahead on a given range of bytes in an inode
5556 */
Chris Masonedbd8d42007-12-21 16:27:24 -05005557unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04005558 struct file_ra_state *ra, struct file *file,
5559 pgoff_t offset, pgoff_t last_index)
5560{
Chris Mason8e7bf942008-04-28 09:02:36 -04005561 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04005562
Chris Mason86479a02007-09-10 19:58:16 -04005563 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
5564 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04005565}
5566
Chris Mason39279cc2007-06-12 06:35:45 -04005567struct inode *btrfs_alloc_inode(struct super_block *sb)
5568{
5569 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04005570 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005571
5572 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
5573 if (!ei)
5574 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04005575
5576 ei->root = NULL;
5577 ei->space_info = NULL;
5578 ei->generation = 0;
5579 ei->sequence = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04005580 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04005581 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04005582 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04005583 ei->delalloc_bytes = 0;
5584 ei->reserved_bytes = 0;
5585 ei->disk_i_size = 0;
5586 ei->flags = 0;
5587 ei->index_cnt = (u64)-1;
5588 ei->last_unlink_trans = 0;
5589
5590 spin_lock_init(&ei->accounting_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005591 atomic_set(&ei->outstanding_extents, 0);
Josef Bacik32c00af2009-10-08 13:34:05 -04005592 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04005593
5594 ei->ordered_data_close = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005595 ei->orphan_meta_reserved = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04005596 ei->dummy_inode = 0;
5597 ei->force_compress = 0;
5598
5599 inode = &ei->vfs_inode;
5600 extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
5601 extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
5602 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
5603 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005604 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04005605 INIT_LIST_HEAD(&ei->i_orphan);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04005606 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005607 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04005608 RB_CLEAR_NODE(&ei->rb_node);
5609
5610 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005611}
5612
5613void btrfs_destroy_inode(struct inode *inode)
5614{
Chris Masone6dcd2d2008-07-17 12:53:50 -04005615 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005616 struct btrfs_root *root = BTRFS_I(inode)->root;
5617
Chris Mason39279cc2007-06-12 06:35:45 -04005618 WARN_ON(!list_empty(&inode->i_dentry));
5619 WARN_ON(inode->i_data.nrpages);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005620 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
5621 WARN_ON(BTRFS_I(inode)->reserved_extents);
Chris Mason39279cc2007-06-12 06:35:45 -04005622
Chris Mason5a3f23d2009-03-31 13:27:11 -04005623 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05005624 * This can happen where we create an inode, but somebody else also
5625 * created the same inode and we need to destroy the one we already
5626 * created.
5627 */
5628 if (!root)
5629 goto free;
5630
5631 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04005632 * Make sure we're properly removed from the ordered operation
5633 * lists.
5634 */
5635 smp_mb();
5636 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
5637 spin_lock(&root->fs_info->ordered_extent_lock);
5638 list_del_init(&BTRFS_I(inode)->ordered_operations);
5639 spin_unlock(&root->fs_info->ordered_extent_lock);
5640 }
5641
Yan, Zhengd68fc572010-05-16 10:49:58 -04005642 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04005643 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yan, Zheng80825102009-11-12 09:35:36 +00005644 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
5645 inode->i_ino);
5646 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04005647 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04005648 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04005649
Chris Masond3977122009-01-05 21:25:51 -05005650 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005651 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
5652 if (!ordered)
5653 break;
5654 else {
Chris Masond3977122009-01-05 21:25:51 -05005655 printk(KERN_ERR "btrfs found ordered "
5656 "extent %llu %llu on inode cleanup\n",
5657 (unsigned long long)ordered->file_offset,
5658 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005659 btrfs_remove_ordered_extent(inode, ordered);
5660 btrfs_put_ordered_extent(ordered);
5661 btrfs_put_ordered_extent(ordered);
5662 }
5663 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005664 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005665 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05005666free:
Chris Mason39279cc2007-06-12 06:35:45 -04005667 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5668}
5669
Yan, Zheng76dda932009-09-21 16:00:26 -04005670void btrfs_drop_inode(struct inode *inode)
5671{
5672 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005673 if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
5674 generic_delete_inode(inode);
5675 else
5676 generic_drop_inode(inode);
5677}
5678
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005679static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04005680{
5681 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
5682
5683 inode_init_once(&ei->vfs_inode);
5684}
5685
5686void btrfs_destroy_cachep(void)
5687{
5688 if (btrfs_inode_cachep)
5689 kmem_cache_destroy(btrfs_inode_cachep);
5690 if (btrfs_trans_handle_cachep)
5691 kmem_cache_destroy(btrfs_trans_handle_cachep);
5692 if (btrfs_transaction_cachep)
5693 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04005694 if (btrfs_path_cachep)
5695 kmem_cache_destroy(btrfs_path_cachep);
5696}
5697
5698int btrfs_init_cachep(void)
5699{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005700 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
5701 sizeof(struct btrfs_inode), 0,
5702 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04005703 if (!btrfs_inode_cachep)
5704 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005705
5706 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
5707 sizeof(struct btrfs_trans_handle), 0,
5708 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005709 if (!btrfs_trans_handle_cachep)
5710 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005711
5712 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
5713 sizeof(struct btrfs_transaction), 0,
5714 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005715 if (!btrfs_transaction_cachep)
5716 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005717
5718 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
5719 sizeof(struct btrfs_path), 0,
5720 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005721 if (!btrfs_path_cachep)
5722 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005723
Chris Mason39279cc2007-06-12 06:35:45 -04005724 return 0;
5725fail:
5726 btrfs_destroy_cachep();
5727 return -ENOMEM;
5728}
5729
5730static int btrfs_getattr(struct vfsmount *mnt,
5731 struct dentry *dentry, struct kstat *stat)
5732{
5733 struct inode *inode = dentry->d_inode;
5734 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05005735 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05005736 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005737 stat->blocks = (inode_get_bytes(inode) +
5738 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04005739 return 0;
5740}
5741
Chris Masond3977122009-01-05 21:25:51 -05005742static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
5743 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005744{
5745 struct btrfs_trans_handle *trans;
5746 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005747 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005748 struct inode *new_inode = new_dentry->d_inode;
5749 struct inode *old_inode = old_dentry->d_inode;
5750 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005751 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005752 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005753 int ret;
5754
Yan, Zhengf679a842009-09-24 09:17:31 -04005755 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5756 return -EPERM;
5757
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005758 /* we only allow rename subvolume link between subvolumes */
5759 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05005760 return -EXDEV;
5761
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005762 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
5763 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
5764 return -ENOTEMPTY;
5765
Chris Mason39279cc2007-06-12 06:35:45 -04005766 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005767 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04005768 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005769 /*
5770 * we're using rename to replace one file with another.
5771 * and the replacement file is large. Start IO on it now so
5772 * we don't add too much work to the end of the transaction
5773 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04005774 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04005775 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
5776 filemap_flush(old_inode->i_mapping);
5777
Yan, Zheng76dda932009-09-21 16:00:26 -04005778 /* close the racy window with snapshot create/destroy ioctl */
5779 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5780 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005781 /*
5782 * We want to reserve the absolute worst case amount of items. So if
5783 * both inodes are subvols and we need to unlink them then that would
5784 * require 4 item modifications, but if they are both normal inodes it
5785 * would require 5 item modifications, so we'll assume their normal
5786 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
5787 * should cover the worst case number of items we'll modify.
5788 */
5789 trans = btrfs_start_transaction(root, 20);
5790 if (IS_ERR(trans))
5791 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04005792
Yan, Zhenga5719522009-09-24 09:17:31 -04005793 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04005794
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005795 if (dest != root)
5796 btrfs_record_root_in_trans(trans, dest);
5797
Yan, Zhenga5719522009-09-24 09:17:31 -04005798 ret = btrfs_set_inode_index(new_dir, &index);
5799 if (ret)
5800 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005801
Yan, Zhenga5719522009-09-24 09:17:31 -04005802 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005803 /* force full log commit if subvolume involved. */
5804 root->fs_info->last_trans_log_full_commit = trans->transid;
5805 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04005806 ret = btrfs_insert_inode_ref(trans, dest,
5807 new_dentry->d_name.name,
5808 new_dentry->d_name.len,
5809 old_inode->i_ino,
5810 new_dir->i_ino, index);
5811 if (ret)
5812 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005813 /*
5814 * this is an ugly little race, but the rename is required
5815 * to make sure that if we crash, the inode is either at the
5816 * old name or the new one. pinning the log transaction lets
5817 * us make sure we don't allow a log commit to come in after
5818 * we unlink the name but before we add the new name back in.
5819 */
5820 btrfs_pin_log_trans(root);
5821 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005822 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04005823 * make sure the inode gets flushed if it is replacing
5824 * something.
5825 */
5826 if (new_inode && new_inode->i_size &&
5827 old_inode && S_ISREG(old_inode->i_mode)) {
5828 btrfs_add_ordered_operation(trans, root, old_inode);
5829 }
5830
Chris Mason39279cc2007-06-12 06:35:45 -04005831 old_dir->i_ctime = old_dir->i_mtime = ctime;
5832 new_dir->i_ctime = new_dir->i_mtime = ctime;
5833 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04005834
Chris Mason12fcfd22009-03-24 10:24:20 -04005835 if (old_dentry->d_parent != new_dentry->d_parent)
5836 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
5837
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005838 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
5839 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
5840 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
5841 old_dentry->d_name.name,
5842 old_dentry->d_name.len);
5843 } else {
5844 btrfs_inc_nlink(old_dentry->d_inode);
5845 ret = btrfs_unlink_inode(trans, root, old_dir,
5846 old_dentry->d_inode,
5847 old_dentry->d_name.name,
5848 old_dentry->d_name.len);
5849 }
5850 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005851
5852 if (new_inode) {
5853 new_inode->i_ctime = CURRENT_TIME;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005854 if (unlikely(new_inode->i_ino ==
5855 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
5856 root_objectid = BTRFS_I(new_inode)->location.objectid;
5857 ret = btrfs_unlink_subvol(trans, dest, new_dir,
5858 root_objectid,
5859 new_dentry->d_name.name,
5860 new_dentry->d_name.len);
5861 BUG_ON(new_inode->i_nlink == 0);
5862 } else {
5863 ret = btrfs_unlink_inode(trans, dest, new_dir,
5864 new_dentry->d_inode,
5865 new_dentry->d_name.name,
5866 new_dentry->d_name.len);
5867 }
5868 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005869 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04005870 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005871 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005872 }
Chris Mason39279cc2007-06-12 06:35:45 -04005873 }
Josef Bacikaec74772008-07-24 12:12:38 -04005874
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005875 ret = btrfs_add_link(trans, new_dir, old_inode,
5876 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04005877 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005878 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005879
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005880 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
5881 btrfs_log_new_name(trans, old_inode, old_dir,
5882 new_dentry->d_parent);
5883 btrfs_end_log_trans(root);
5884 }
Chris Mason39279cc2007-06-12 06:35:45 -04005885out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04005886 btrfs_end_transaction_throttle(trans, root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005887
Yan, Zheng76dda932009-09-21 16:00:26 -04005888 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5889 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005890
Chris Mason39279cc2007-06-12 06:35:45 -04005891 return ret;
5892}
5893
Chris Masond352ac62008-09-29 15:18:18 -04005894/*
5895 * some fairly slow code that needs optimization. This walks the list
5896 * of all the inodes with pending delalloc and forces them to disk.
5897 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00005898int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04005899{
5900 struct list_head *head = &root->fs_info->delalloc_inodes;
5901 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005902 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04005903
Yan Zhengc146afa2008-11-12 14:34:12 -05005904 if (root->fs_info->sb->s_flags & MS_RDONLY)
5905 return -EROFS;
5906
Chris Mason75eff682008-12-15 15:54:40 -05005907 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05005908 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04005909 binode = list_entry(head->next, struct btrfs_inode,
5910 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005911 inode = igrab(&binode->vfs_inode);
5912 if (!inode)
5913 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05005914 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005915 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005916 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00005917 if (delay_iput)
5918 btrfs_add_delayed_iput(inode);
5919 else
5920 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005921 }
5922 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05005923 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04005924 }
Chris Mason75eff682008-12-15 15:54:40 -05005925 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04005926
5927 /* the filemap_flush will queue IO into the worker threads, but
5928 * we have to make sure the IO is actually started and that
5929 * ordered extents get created before we return
5930 */
5931 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05005932 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05005933 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005934 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05005935 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
5936 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04005937 }
5938 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04005939 return 0;
5940}
5941
Yan, Zheng5da9d012010-05-16 10:46:25 -04005942int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput)
5943{
5944 struct btrfs_inode *binode;
5945 struct inode *inode = NULL;
5946
5947 spin_lock(&root->fs_info->delalloc_lock);
5948 while (!list_empty(&root->fs_info->delalloc_inodes)) {
5949 binode = list_entry(root->fs_info->delalloc_inodes.next,
5950 struct btrfs_inode, delalloc_inodes);
5951 inode = igrab(&binode->vfs_inode);
5952 if (inode) {
5953 list_move_tail(&binode->delalloc_inodes,
5954 &root->fs_info->delalloc_inodes);
5955 break;
5956 }
5957
5958 list_del_init(&binode->delalloc_inodes);
5959 cond_resched_lock(&root->fs_info->delalloc_lock);
5960 }
5961 spin_unlock(&root->fs_info->delalloc_lock);
5962
5963 if (inode) {
5964 write_inode_now(inode, 0);
5965 if (delay_iput)
5966 btrfs_add_delayed_iput(inode);
5967 else
5968 iput(inode);
5969 return 1;
5970 }
5971 return 0;
5972}
5973
Chris Mason39279cc2007-06-12 06:35:45 -04005974static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
5975 const char *symname)
5976{
5977 struct btrfs_trans_handle *trans;
5978 struct btrfs_root *root = BTRFS_I(dir)->root;
5979 struct btrfs_path *path;
5980 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05005981 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005982 int err;
5983 int drop_inode = 0;
5984 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005985 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04005986 int name_len;
5987 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04005988 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005989 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04005990 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05005991 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005992
5993 name_len = strlen(symname) + 1;
5994 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
5995 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05005996
Yan, Zhenga22285a2010-05-16 10:48:46 -04005997 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
5998 if (err)
5999 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006000 /*
6001 * 2 items for inode item and ref
6002 * 2 items for dir items
6003 * 1 item for xattr if selinux is on
6004 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006005 trans = btrfs_start_transaction(root, 5);
6006 if (IS_ERR(trans))
6007 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006008
Chris Mason39279cc2007-06-12 06:35:45 -04006009 btrfs_set_trans_block_group(trans, dir);
6010
Josef Bacikaec74772008-07-24 12:12:38 -04006011 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05006012 dentry->d_name.len,
6013 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04006014 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
6015 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006016 err = PTR_ERR(inode);
6017 if (IS_ERR(inode))
6018 goto out_unlock;
6019
Yan, Zhengf34f57a2009-11-12 09:35:27 +00006020 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04006021 if (err) {
6022 drop_inode = 1;
6023 goto out_unlock;
6024 }
6025
Chris Mason39279cc2007-06-12 06:35:45 -04006026 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006027 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006028 if (err)
6029 drop_inode = 1;
6030 else {
6031 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04006032 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04006033 inode->i_fop = &btrfs_file_operations;
6034 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05006035 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04006036 }
Chris Mason39279cc2007-06-12 06:35:45 -04006037 btrfs_update_inode_block_group(trans, inode);
6038 btrfs_update_inode_block_group(trans, dir);
6039 if (drop_inode)
6040 goto out_unlock;
6041
6042 path = btrfs_alloc_path();
6043 BUG_ON(!path);
6044 key.objectid = inode->i_ino;
6045 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006046 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
6047 datasize = btrfs_file_extent_calc_inline_size(name_len);
6048 err = btrfs_insert_empty_item(trans, root, path, &key,
6049 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04006050 if (err) {
6051 drop_inode = 1;
6052 goto out_unlock;
6053 }
Chris Mason5f39d392007-10-15 16:14:19 -04006054 leaf = path->nodes[0];
6055 ei = btrfs_item_ptr(leaf, path->slots[0],
6056 struct btrfs_file_extent_item);
6057 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
6058 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04006059 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04006060 btrfs_set_file_extent_encryption(leaf, ei, 0);
6061 btrfs_set_file_extent_compression(leaf, ei, 0);
6062 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
6063 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
6064
Chris Mason39279cc2007-06-12 06:35:45 -04006065 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04006066 write_extent_buffer(leaf, symname, ptr, name_len);
6067 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04006068 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04006069
Chris Mason39279cc2007-06-12 06:35:45 -04006070 inode->i_op = &btrfs_symlink_inode_operations;
6071 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04006072 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04006073 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04006074 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04006075 err = btrfs_update_inode(trans, root, inode);
6076 if (err)
6077 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04006078
6079out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04006080 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04006081 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006082 if (drop_inode) {
6083 inode_dec_link_count(inode);
6084 iput(inode);
6085 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04006086 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04006087 return err;
6088}
Chris Mason16432982008-04-10 10:23:21 -04006089
Yan, Zhengefa56462010-05-16 10:49:59 -04006090int btrfs_prealloc_file_range(struct inode *inode, int mode,
6091 u64 start, u64 num_bytes, u64 min_size,
6092 loff_t actual_len, u64 *alloc_hint)
Yan Zhengd899e052008-10-30 14:25:28 -04006093{
Yan, Zheng5a303d52009-11-12 09:34:52 +00006094 struct btrfs_trans_handle *trans;
Yan Zhengd899e052008-10-30 14:25:28 -04006095 struct btrfs_root *root = BTRFS_I(inode)->root;
6096 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04006097 u64 cur_offset = start;
Yan Zhengd899e052008-10-30 14:25:28 -04006098 int ret = 0;
6099
Yan Zhengd899e052008-10-30 14:25:28 -04006100 while (num_bytes > 0) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04006101 trans = btrfs_start_transaction(root, 3);
6102 if (IS_ERR(trans)) {
6103 ret = PTR_ERR(trans);
6104 break;
6105 }
Chris Mason3a1abec2009-12-17 15:47:17 -05006106
Yan, Zhengefa56462010-05-16 10:49:59 -04006107 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
6108 0, *alloc_hint, (u64)-1, &ins, 1);
Yan Zhengd899e052008-10-30 14:25:28 -04006109 if (ret) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04006110 btrfs_end_transaction(trans, root);
6111 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00006112 }
6113
Yan Zhengd899e052008-10-30 14:25:28 -04006114 ret = insert_reserved_file_extent(trans, inode,
6115 cur_offset, ins.objectid,
6116 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00006117 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04006118 BTRFS_FILE_EXTENT_PREALLOC);
6119 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04006120 btrfs_drop_extent_cache(inode, cur_offset,
6121 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00006122
Yan Zhengd899e052008-10-30 14:25:28 -04006123 num_bytes -= ins.offset;
6124 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04006125 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00006126
Yan Zhengd899e052008-10-30 14:25:28 -04006127 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006128 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04006129 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04006130 (actual_len > inode->i_size) &&
6131 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00006132 if (cur_offset > actual_len)
Yan, Zhengefa56462010-05-16 10:49:59 -04006133 i_size_write(inode, actual_len);
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00006134 else
Yan, Zhengefa56462010-05-16 10:49:59 -04006135 i_size_write(inode, cur_offset);
6136 i_size_write(inode, cur_offset);
6137 btrfs_ordered_update_i_size(inode, cur_offset, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00006138 }
6139
Yan Zhengd899e052008-10-30 14:25:28 -04006140 ret = btrfs_update_inode(trans, root, inode);
6141 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04006142
Yan, Zheng5a303d52009-11-12 09:34:52 +00006143 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00006144 }
Yan Zhengd899e052008-10-30 14:25:28 -04006145 return ret;
6146}
6147
6148static long btrfs_fallocate(struct inode *inode, int mode,
6149 loff_t offset, loff_t len)
6150{
Josef Bacik2ac55d42010-02-03 19:33:23 +00006151 struct extent_state *cached_state = NULL;
Yan Zhengd899e052008-10-30 14:25:28 -04006152 u64 cur_offset;
6153 u64 last_byte;
6154 u64 alloc_start;
6155 u64 alloc_end;
6156 u64 alloc_hint = 0;
Chris Masone980b502009-04-24 14:39:24 -04006157 u64 locked_end;
Yan Zhengd899e052008-10-30 14:25:28 -04006158 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
6159 struct extent_map *em;
6160 int ret;
6161
6162 alloc_start = offset & ~mask;
6163 alloc_end = (offset + len + mask) & ~mask;
6164
Chris Mason546888d2009-04-21 11:53:38 -04006165 /*
6166 * wait for ordered IO before we have any locks. We'll loop again
6167 * below with the locks held.
6168 */
6169 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
6170
Yan Zhengd899e052008-10-30 14:25:28 -04006171 mutex_lock(&inode->i_mutex);
6172 if (alloc_start > inode->i_size) {
6173 ret = btrfs_cont_expand(inode, alloc_start);
6174 if (ret)
6175 goto out;
6176 }
6177
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006178 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
Josef Bacika970b0a2009-06-27 21:07:34 -04006179 if (ret)
6180 goto out;
6181
Chris Masone980b502009-04-24 14:39:24 -04006182 locked_end = alloc_end - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04006183 while (1) {
6184 struct btrfs_ordered_extent *ordered;
Chris Mason546888d2009-04-21 11:53:38 -04006185
Chris Mason546888d2009-04-21 11:53:38 -04006186 /* the extent lock is ordered inside the running
6187 * transaction
6188 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006189 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
6190 locked_end, 0, &cached_state, GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04006191 ordered = btrfs_lookup_first_ordered_extent(inode,
6192 alloc_end - 1);
6193 if (ordered &&
6194 ordered->file_offset + ordered->len > alloc_start &&
6195 ordered->file_offset < alloc_end) {
6196 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006197 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
6198 alloc_start, locked_end,
6199 &cached_state, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04006200 /*
6201 * we can't wait on the range with the transaction
6202 * running or with the extent lock held
6203 */
Yan Zhengd899e052008-10-30 14:25:28 -04006204 btrfs_wait_ordered_range(inode, alloc_start,
6205 alloc_end - alloc_start);
6206 } else {
6207 if (ordered)
6208 btrfs_put_ordered_extent(ordered);
6209 break;
6210 }
6211 }
6212
6213 cur_offset = alloc_start;
6214 while (1) {
6215 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
6216 alloc_end - cur_offset, 0);
6217 BUG_ON(IS_ERR(em) || !em);
6218 last_byte = min(extent_map_end(em), alloc_end);
6219 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng5a303d52009-11-12 09:34:52 +00006220 if (em->block_start == EXTENT_MAP_HOLE ||
6221 (cur_offset >= inode->i_size &&
6222 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
Yan, Zhengefa56462010-05-16 10:49:59 -04006223 ret = btrfs_prealloc_file_range(inode, 0, cur_offset,
6224 last_byte - cur_offset,
6225 1 << inode->i_blkbits,
6226 offset + len,
6227 &alloc_hint);
Yan Zhengd899e052008-10-30 14:25:28 -04006228 if (ret < 0) {
6229 free_extent_map(em);
6230 break;
6231 }
6232 }
Yan Zhengd899e052008-10-30 14:25:28 -04006233 free_extent_map(em);
6234
6235 cur_offset = last_byte;
6236 if (cur_offset >= alloc_end) {
6237 ret = 0;
6238 break;
6239 }
6240 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00006241 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
6242 &cached_state, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04006243
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006244 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
Yan Zhengd899e052008-10-30 14:25:28 -04006245out:
6246 mutex_unlock(&inode->i_mutex);
6247 return ret;
6248}
6249
Chris Masone6dcd2d2008-07-17 12:53:50 -04006250static int btrfs_set_page_dirty(struct page *page)
6251{
Chris Masone6dcd2d2008-07-17 12:53:50 -04006252 return __set_page_dirty_nobuffers(page);
6253}
6254
Sven Wegener0ee0fda2008-07-30 16:54:26 -04006255static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05006256{
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006257 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05006258 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04006259 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05006260}
Chris Mason39279cc2007-06-12 06:35:45 -04006261
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006262static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05006263 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04006264 .lookup = btrfs_lookup,
6265 .create = btrfs_create,
6266 .unlink = btrfs_unlink,
6267 .link = btrfs_link,
6268 .mkdir = btrfs_mkdir,
6269 .rmdir = btrfs_rmdir,
6270 .rename = btrfs_rename,
6271 .symlink = btrfs_symlink,
6272 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04006273 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006274 .setxattr = btrfs_setxattr,
6275 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05006276 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006277 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05006278 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04006279};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006280static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006281 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05006282 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04006283};
Yan, Zheng76dda932009-09-21 16:00:26 -04006284
Alexey Dobriyan828c0952009-10-01 15:43:56 -07006285static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006286 .llseek = generic_file_llseek,
6287 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01006288 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04006289 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04006290#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04006291 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04006292#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04006293 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04006294 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04006295};
6296
Chris Masond1310b22008-01-24 16:13:08 -05006297static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04006298 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05006299 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04006300 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04006301 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006302 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04006303 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04006304 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05006305 .set_bit_hook = btrfs_set_bit_hook,
6306 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04006307 .merge_extent_hook = btrfs_merge_extent_hook,
6308 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04006309};
6310
Chris Mason35054392009-01-21 13:11:13 -05006311/*
6312 * btrfs doesn't support the bmap operation because swapfiles
6313 * use bmap to make a mapping of extents in the file. They assume
6314 * these extents won't change over the life of the file and they
6315 * use the bmap result to do IO directly to the drive.
6316 *
6317 * the btrfs bmap call would return logical addresses that aren't
6318 * suitable for IO and they also will change frequently as COW
6319 * operations happen. So, swapfile + btrfs == corruption.
6320 *
6321 * For now we're avoiding this by dropping bmap.
6322 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07006323static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04006324 .readpage = btrfs_readpage,
6325 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04006326 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05006327 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04006328 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04006329 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04006330 .invalidatepage = btrfs_invalidatepage,
6331 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006332 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02006333 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04006334};
6335
Alexey Dobriyan7f094102009-09-21 17:01:10 -07006336static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04006337 .readpage = btrfs_readpage,
6338 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04006339 .invalidatepage = btrfs_invalidatepage,
6340 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04006341};
6342
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006343static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006344 .truncate = btrfs_truncate,
6345 .getattr = btrfs_getattr,
6346 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006347 .setxattr = btrfs_setxattr,
6348 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05006349 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006350 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05006351 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04006352 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006353 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04006354};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006355static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04006356 .getattr = btrfs_getattr,
6357 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05006358 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006359 .setxattr = btrfs_setxattr,
6360 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04006361 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006362 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04006363};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006364static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006365 .readlink = generic_readlink,
6366 .follow_link = page_follow_link_light,
6367 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05006368 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05006369 .setxattr = btrfs_setxattr,
6370 .getxattr = btrfs_getxattr,
6371 .listxattr = btrfs_listxattr,
6372 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04006373};
Yan, Zheng76dda932009-09-21 16:00:26 -04006374
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04006375const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04006376 .d_delete = btrfs_dentry_delete,
6377};