blob: 3cc5677f544029fab14bc3c8f26f913c297f63a1 [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>
Chris Mason4b4e25f2008-11-20 10:22:27 -050039#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040040#include "ctree.h"
41#include "disk-io.h"
42#include "transaction.h"
43#include "btrfs_inode.h"
44#include "ioctl.h"
45#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040046#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040047#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040048#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040049#include "tree-log.h"
Chris Masonc8b97812008-10-29 14:49:59 -040050#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050051#include "locking.h"
Chris Mason39279cc2007-06-12 06:35:45 -040052
53struct btrfs_iget_args {
54 u64 ino;
55 struct btrfs_root *root;
56};
57
58static struct inode_operations btrfs_dir_inode_operations;
59static struct inode_operations btrfs_symlink_inode_operations;
60static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040061static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040062static struct inode_operations btrfs_file_inode_operations;
63static struct address_space_operations btrfs_aops;
64static struct address_space_operations btrfs_symlink_aops;
65static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050066static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040067
68static struct kmem_cache *btrfs_inode_cachep;
69struct kmem_cache *btrfs_trans_handle_cachep;
70struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040071struct kmem_cache *btrfs_path_cachep;
72
73#define S_SHIFT 12
74static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
75 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
76 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
77 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
78 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
79 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
80 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
81 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
82};
83
Josef Bacik7b128762008-07-24 12:17:14 -040084static void btrfs_truncate(struct inode *inode);
Chris Masonc8b97812008-10-29 14:49:59 -040085static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
Chris Mason771ed682008-11-06 22:02:51 -050086static noinline int cow_file_range(struct inode *inode,
87 struct page *locked_page,
88 u64 start, u64 end, int *page_started,
89 unsigned long *nr_written, int unlock);
Josef Bacik7b128762008-07-24 12:17:14 -040090
Jim Owens0279b4c2009-02-04 09:29:13 -050091static int btrfs_init_inode_security(struct inode *inode, struct inode *dir)
92{
93 int err;
94
95 err = btrfs_init_acl(inode, dir);
96 if (!err)
97 err = btrfs_xattr_security_init(inode, dir);
98 return err;
99}
100
Chris Masond352ac62008-09-29 15:18:18 -0400101/*
Chris Masonc8b97812008-10-29 14:49:59 -0400102 * this does all the hard work for inserting an inline extent into
103 * the btree. The caller should have done a btrfs_drop_extents so that
104 * no overlapping inline items exist in the btree
105 */
Chris Masond3977122009-01-05 21:25:51 -0500106static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400107 struct btrfs_root *root, struct inode *inode,
108 u64 start, size_t size, size_t compressed_size,
109 struct page **compressed_pages)
110{
111 struct btrfs_key key;
112 struct btrfs_path *path;
113 struct extent_buffer *leaf;
114 struct page *page = NULL;
115 char *kaddr;
116 unsigned long ptr;
117 struct btrfs_file_extent_item *ei;
118 int err = 0;
119 int ret;
120 size_t cur_size = size;
121 size_t datasize;
122 unsigned long offset;
123 int use_compress = 0;
124
125 if (compressed_size && compressed_pages) {
126 use_compress = 1;
127 cur_size = compressed_size;
128 }
129
Chris Masond3977122009-01-05 21:25:51 -0500130 path = btrfs_alloc_path();
131 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400132 return -ENOMEM;
133
Chris Masonb9473432009-03-13 11:00:37 -0400134 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400135 btrfs_set_trans_block_group(trans, inode);
136
137 key.objectid = inode->i_ino;
138 key.offset = start;
139 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400140 datasize = btrfs_file_extent_calc_inline_size(cur_size);
141
142 inode_add_bytes(inode, size);
143 ret = btrfs_insert_empty_item(trans, root, path, &key,
144 datasize);
145 BUG_ON(ret);
146 if (ret) {
147 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400148 goto fail;
149 }
150 leaf = path->nodes[0];
151 ei = btrfs_item_ptr(leaf, path->slots[0],
152 struct btrfs_file_extent_item);
153 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
154 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
155 btrfs_set_file_extent_encryption(leaf, ei, 0);
156 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
157 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
158 ptr = btrfs_file_extent_inline_start(ei);
159
160 if (use_compress) {
161 struct page *cpage;
162 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500163 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400164 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500165 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400166 PAGE_CACHE_SIZE);
167
Chris Masonb9473432009-03-13 11:00:37 -0400168 kaddr = kmap_atomic(cpage, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400169 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Chris Masonb9473432009-03-13 11:00:37 -0400170 kunmap_atomic(kaddr, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400171
172 i++;
173 ptr += cur_size;
174 compressed_size -= cur_size;
175 }
176 btrfs_set_file_extent_compression(leaf, ei,
177 BTRFS_COMPRESS_ZLIB);
178 } else {
179 page = find_get_page(inode->i_mapping,
180 start >> PAGE_CACHE_SHIFT);
181 btrfs_set_file_extent_compression(leaf, ei, 0);
182 kaddr = kmap_atomic(page, KM_USER0);
183 offset = start & (PAGE_CACHE_SIZE - 1);
184 write_extent_buffer(leaf, kaddr + offset, ptr, size);
185 kunmap_atomic(kaddr, KM_USER0);
186 page_cache_release(page);
187 }
188 btrfs_mark_buffer_dirty(leaf);
189 btrfs_free_path(path);
190
191 BTRFS_I(inode)->disk_i_size = inode->i_size;
192 btrfs_update_inode(trans, root, inode);
193 return 0;
194fail:
195 btrfs_free_path(path);
196 return err;
197}
198
199
200/*
201 * conditionally insert an inline extent into the file. This
202 * does the checks required to make sure the data is small enough
203 * to fit as an inline extent.
204 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400205static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400206 struct btrfs_root *root,
207 struct inode *inode, u64 start, u64 end,
208 size_t compressed_size,
209 struct page **compressed_pages)
210{
211 u64 isize = i_size_read(inode);
212 u64 actual_end = min(end + 1, isize);
213 u64 inline_len = actual_end - start;
214 u64 aligned_end = (end + root->sectorsize - 1) &
215 ~((u64)root->sectorsize - 1);
216 u64 hint_byte;
217 u64 data_len = inline_len;
218 int ret;
219
220 if (compressed_size)
221 data_len = compressed_size;
222
223 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400224 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400225 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
226 (!compressed_size &&
227 (actual_end & (root->sectorsize - 1)) == 0) ||
228 end + 1 < isize ||
229 data_len > root->fs_info->max_inline) {
230 return 1;
231 }
232
Chris Masonc8b97812008-10-29 14:49:59 -0400233 ret = btrfs_drop_extents(trans, root, inode, start,
Chris Masona1ed8352009-09-11 12:27:37 -0400234 aligned_end, aligned_end, start,
235 &hint_byte, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400236 BUG_ON(ret);
237
238 if (isize > actual_end)
239 inline_len = min_t(u64, isize, actual_end);
240 ret = insert_inline_extent(trans, root, inode, start,
241 inline_len, compressed_size,
242 compressed_pages);
243 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -0400244 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400245 return 0;
246}
247
Chris Mason771ed682008-11-06 22:02:51 -0500248struct async_extent {
249 u64 start;
250 u64 ram_size;
251 u64 compressed_size;
252 struct page **pages;
253 unsigned long nr_pages;
254 struct list_head list;
255};
256
257struct async_cow {
258 struct inode *inode;
259 struct btrfs_root *root;
260 struct page *locked_page;
261 u64 start;
262 u64 end;
263 struct list_head extents;
264 struct btrfs_work work;
265};
266
267static noinline int add_async_extent(struct async_cow *cow,
268 u64 start, u64 ram_size,
269 u64 compressed_size,
270 struct page **pages,
271 unsigned long nr_pages)
272{
273 struct async_extent *async_extent;
274
275 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
276 async_extent->start = start;
277 async_extent->ram_size = ram_size;
278 async_extent->compressed_size = compressed_size;
279 async_extent->pages = pages;
280 async_extent->nr_pages = nr_pages;
281 list_add_tail(&async_extent->list, &cow->extents);
282 return 0;
283}
284
Chris Masonc8b97812008-10-29 14:49:59 -0400285/*
Chris Mason771ed682008-11-06 22:02:51 -0500286 * we create compressed extents in two phases. The first
287 * phase compresses a range of pages that have already been
288 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400289 *
Chris Mason771ed682008-11-06 22:02:51 -0500290 * This is done inside an ordered work queue, and the compression
291 * is spread across many cpus. The actual IO submission is step
292 * two, and the ordered work queue takes care of making sure that
293 * happens in the same order things were put onto the queue by
294 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400295 *
Chris Mason771ed682008-11-06 22:02:51 -0500296 * If this code finds it can't get good compression, it puts an
297 * entry onto the work queue to write the uncompressed bytes. This
298 * makes sure that both compressed inodes and uncompressed inodes
299 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400300 */
Chris Mason771ed682008-11-06 22:02:51 -0500301static noinline int compress_file_range(struct inode *inode,
302 struct page *locked_page,
303 u64 start, u64 end,
304 struct async_cow *async_cow,
305 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400306{
307 struct btrfs_root *root = BTRFS_I(inode)->root;
308 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400309 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -0400310 u64 orig_start;
311 u64 disk_num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400312 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400313 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500314 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400315 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400316 struct page **pages = NULL;
317 unsigned long nr_pages;
318 unsigned long nr_pages_ret = 0;
319 unsigned long total_compressed = 0;
320 unsigned long total_in = 0;
321 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500322 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400323 int i;
324 int will_compress;
Chris Masonb888db2b2007-08-27 16:49:44 -0400325
Chris Masonc8b97812008-10-29 14:49:59 -0400326 orig_start = start;
Chris Masonbe20aa92007-12-17 20:14:01 -0500327
Chris Mason42dc7ba2008-12-15 11:44:56 -0500328 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400329again:
330 will_compress = 0;
331 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
332 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
333
Chris Masonf03d9301f2009-02-04 09:31:06 -0500334 /*
335 * we don't want to send crud past the end of i_size through
336 * compression, that's just a waste of CPU time. So, if the
337 * end of the file is before the start of our current
338 * requested range of bytes, we bail out to the uncompressed
339 * cleanup code that can deal with all of this.
340 *
341 * It isn't really the fastest way to fix things, but this is a
342 * very uncommon corner.
343 */
344 if (actual_end <= start)
345 goto cleanup_and_bail_uncompressed;
346
Chris Masonc8b97812008-10-29 14:49:59 -0400347 total_compressed = actual_end - start;
348
349 /* we want to make sure that amount of ram required to uncompress
350 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500351 * of a compressed extent to 128k. This is a crucial number
352 * because it also controls how easily we can spread reads across
353 * cpus for decompression.
354 *
355 * We also want to make sure the amount of IO required to do
356 * a random read is reasonably small, so we limit the size of
357 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400358 */
359 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400360 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500361 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400362 disk_num_bytes = num_bytes;
363 total_in = 0;
364 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400365
Chris Mason771ed682008-11-06 22:02:51 -0500366 /*
367 * we do compression for mount -o compress and when the
368 * inode has not been flagged as nocompress. This flag can
369 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400370 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200371 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Masonc8b97812008-10-29 14:49:59 -0400372 btrfs_test_opt(root, COMPRESS)) {
373 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400374 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400375
Chris Masonc8b97812008-10-29 14:49:59 -0400376 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
377 total_compressed, pages,
378 nr_pages, &nr_pages_ret,
379 &total_in,
380 &total_compressed,
381 max_compressed);
382
383 if (!ret) {
384 unsigned long offset = total_compressed &
385 (PAGE_CACHE_SIZE - 1);
386 struct page *page = pages[nr_pages_ret - 1];
387 char *kaddr;
388
389 /* zero the tail end of the last page, we might be
390 * sending it down to disk
391 */
392 if (offset) {
393 kaddr = kmap_atomic(page, KM_USER0);
394 memset(kaddr + offset, 0,
395 PAGE_CACHE_SIZE - offset);
396 kunmap_atomic(kaddr, KM_USER0);
397 }
398 will_compress = 1;
399 }
400 }
401 if (start == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500402 trans = btrfs_join_transaction(root, 1);
403 BUG_ON(!trans);
404 btrfs_set_trans_block_group(trans, inode);
405
Chris Masonc8b97812008-10-29 14:49:59 -0400406 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500407 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400408 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500409 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400410 */
411 ret = cow_file_range_inline(trans, root, inode,
412 start, end, 0, NULL);
413 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500414 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400415 ret = cow_file_range_inline(trans, root, inode,
416 start, end,
417 total_compressed, pages);
418 }
Chris Mason771ed682008-11-06 22:02:51 -0500419 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400420 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500421 /*
422 * inline extent creation worked, we don't need
423 * to create any more async work items. Unlock
424 * and free up our temp pages.
425 */
Chris Masonc8b97812008-10-29 14:49:59 -0400426 extent_clear_unlock_delalloc(inode,
427 &BTRFS_I(inode)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -0500428 start, end, NULL, 1, 0,
Chris Mason8b62b722009-09-02 16:53:46 -0400429 0, 1, 1, 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400430 ret = 0;
431 goto free_pages_out;
432 }
433 }
434
435 if (will_compress) {
436 /*
437 * we aren't doing an inline extent round the compressed size
438 * up to a block size boundary so the allocator does sane
439 * things
440 */
441 total_compressed = (total_compressed + blocksize - 1) &
442 ~(blocksize - 1);
443
444 /*
445 * one last check to make sure the compression is really a
446 * win, compare the page count read with the blocks on disk
447 */
448 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
449 ~(PAGE_CACHE_SIZE - 1);
450 if (total_compressed >= total_in) {
451 will_compress = 0;
452 } else {
453 disk_num_bytes = total_compressed;
454 num_bytes = total_in;
455 }
456 }
457 if (!will_compress && pages) {
458 /*
459 * the compression code ran but failed to make things smaller,
460 * free any pages it allocated and our page pointer array
461 */
462 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400463 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400464 page_cache_release(pages[i]);
465 }
466 kfree(pages);
467 pages = NULL;
468 total_compressed = 0;
469 nr_pages_ret = 0;
470
471 /* flag the file so we don't compress in the future */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200472 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Masonc8b97812008-10-29 14:49:59 -0400473 }
Chris Mason771ed682008-11-06 22:02:51 -0500474 if (will_compress) {
475 *num_added += 1;
476
477 /* the async work queues will take care of doing actual
478 * allocation on disk for these compressed pages,
479 * and will submit them to the elevator.
480 */
481 add_async_extent(async_cow, start, num_bytes,
482 total_compressed, pages, nr_pages_ret);
483
Chris Mason42dc7ba2008-12-15 11:44:56 -0500484 if (start + num_bytes < end && start + num_bytes < actual_end) {
Chris Mason771ed682008-11-06 22:02:51 -0500485 start += num_bytes;
486 pages = NULL;
487 cond_resched();
488 goto again;
489 }
490 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500491cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500492 /*
493 * No compression, but we still need to write the pages in
494 * the file we've been given so far. redirty the locked
495 * page if it corresponds to our extent and set things up
496 * for the async work queue to run cow_file_range to do
497 * the normal delalloc dance
498 */
499 if (page_offset(locked_page) >= start &&
500 page_offset(locked_page) <= end) {
501 __set_page_dirty_nobuffers(locked_page);
502 /* unlocked later on in the async handlers */
503 }
504 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
505 *num_added += 1;
506 }
507
508out:
509 return 0;
510
511free_pages_out:
512 for (i = 0; i < nr_pages_ret; i++) {
513 WARN_ON(pages[i]->mapping);
514 page_cache_release(pages[i]);
515 }
Chris Masond3977122009-01-05 21:25:51 -0500516 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500517
518 goto out;
519}
520
521/*
522 * phase two of compressed writeback. This is the ordered portion
523 * of the code, which only gets called in the order the work was
524 * queued. We walk all the async extents created by compress_file_range
525 * and send them down to the disk.
526 */
527static noinline int submit_compressed_extents(struct inode *inode,
528 struct async_cow *async_cow)
529{
530 struct async_extent *async_extent;
531 u64 alloc_hint = 0;
532 struct btrfs_trans_handle *trans;
533 struct btrfs_key ins;
534 struct extent_map *em;
535 struct btrfs_root *root = BTRFS_I(inode)->root;
536 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
537 struct extent_io_tree *io_tree;
538 int ret;
539
540 if (list_empty(&async_cow->extents))
541 return 0;
542
543 trans = btrfs_join_transaction(root, 1);
544
Chris Masond3977122009-01-05 21:25:51 -0500545 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500546 async_extent = list_entry(async_cow->extents.next,
547 struct async_extent, list);
548 list_del(&async_extent->list);
549
550 io_tree = &BTRFS_I(inode)->io_tree;
551
552 /* did the compression code fall back to uncompressed IO? */
553 if (!async_extent->pages) {
554 int page_started = 0;
555 unsigned long nr_written = 0;
556
557 lock_extent(io_tree, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500558 async_extent->start +
559 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500560
561 /* allocate blocks */
562 cow_file_range(inode, async_cow->locked_page,
563 async_extent->start,
564 async_extent->start +
565 async_extent->ram_size - 1,
566 &page_started, &nr_written, 0);
567
568 /*
569 * if page_started, cow_file_range inserted an
570 * inline extent and took care of all the unlocking
571 * and IO for us. Otherwise, we need to submit
572 * all those pages down to the drive.
573 */
574 if (!page_started)
575 extent_write_locked_range(io_tree,
576 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500577 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500578 async_extent->ram_size - 1,
579 btrfs_get_extent,
580 WB_SYNC_ALL);
581 kfree(async_extent);
582 cond_resched();
583 continue;
584 }
585
586 lock_extent(io_tree, async_extent->start,
587 async_extent->start + async_extent->ram_size - 1,
588 GFP_NOFS);
589 /*
590 * here we're doing allocation and writeback of the
591 * compressed pages
592 */
593 btrfs_drop_extent_cache(inode, async_extent->start,
594 async_extent->start +
595 async_extent->ram_size - 1, 0);
596
597 ret = btrfs_reserve_extent(trans, root,
598 async_extent->compressed_size,
599 async_extent->compressed_size,
600 0, alloc_hint,
601 (u64)-1, &ins, 1);
602 BUG_ON(ret);
603 em = alloc_extent_map(GFP_NOFS);
604 em->start = async_extent->start;
605 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500606 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500607
608 em->block_start = ins.objectid;
609 em->block_len = ins.offset;
610 em->bdev = root->fs_info->fs_devices->latest_bdev;
611 set_bit(EXTENT_FLAG_PINNED, &em->flags);
612 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
613
Chris Masond3977122009-01-05 21:25:51 -0500614 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400615 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500616 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400617 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500618 if (ret != -EEXIST) {
619 free_extent_map(em);
620 break;
621 }
622 btrfs_drop_extent_cache(inode, async_extent->start,
623 async_extent->start +
624 async_extent->ram_size - 1, 0);
625 }
626
627 ret = btrfs_add_ordered_extent(inode, async_extent->start,
628 ins.objectid,
629 async_extent->ram_size,
630 ins.offset,
631 BTRFS_ORDERED_COMPRESSED);
632 BUG_ON(ret);
633
634 btrfs_end_transaction(trans, root);
635
636 /*
637 * clear dirty, set writeback and unlock the pages.
638 */
639 extent_clear_unlock_delalloc(inode,
640 &BTRFS_I(inode)->io_tree,
641 async_extent->start,
642 async_extent->start +
643 async_extent->ram_size - 1,
Chris Mason8b62b722009-09-02 16:53:46 -0400644 NULL, 1, 1, 0, 1, 1, 0, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500645
646 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500647 async_extent->start,
648 async_extent->ram_size,
649 ins.objectid,
650 ins.offset, async_extent->pages,
651 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500652
653 BUG_ON(ret);
654 trans = btrfs_join_transaction(root, 1);
655 alloc_hint = ins.objectid + ins.offset;
656 kfree(async_extent);
657 cond_resched();
658 }
659
660 btrfs_end_transaction(trans, root);
661 return 0;
662}
663
664/*
665 * when extent_io.c finds a delayed allocation range in the file,
666 * the call backs end up in this code. The basic idea is to
667 * allocate extents on disk for the range, and create ordered data structs
668 * in ram to track those extents.
669 *
670 * locked_page is the page that writepage had locked already. We use
671 * it to make sure we don't do extra locks or unlocks.
672 *
673 * *page_started is set to one if we unlock locked_page and do everything
674 * required to start IO on it. It may be clean and already done with
675 * IO when we return.
676 */
677static noinline int cow_file_range(struct inode *inode,
678 struct page *locked_page,
679 u64 start, u64 end, int *page_started,
680 unsigned long *nr_written,
681 int unlock)
682{
683 struct btrfs_root *root = BTRFS_I(inode)->root;
684 struct btrfs_trans_handle *trans;
685 u64 alloc_hint = 0;
686 u64 num_bytes;
687 unsigned long ram_size;
688 u64 disk_num_bytes;
689 u64 cur_alloc_size;
690 u64 blocksize = root->sectorsize;
691 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500692 u64 isize = i_size_read(inode);
Chris Mason771ed682008-11-06 22:02:51 -0500693 struct btrfs_key ins;
694 struct extent_map *em;
695 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
696 int ret = 0;
697
698 trans = btrfs_join_transaction(root, 1);
699 BUG_ON(!trans);
700 btrfs_set_trans_block_group(trans, inode);
701
Chris Mason42dc7ba2008-12-15 11:44:56 -0500702 actual_end = min_t(u64, isize, end + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500703
704 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
705 num_bytes = max(blocksize, num_bytes);
706 disk_num_bytes = num_bytes;
707 ret = 0;
708
709 if (start == 0) {
710 /* lets try to make an inline extent */
711 ret = cow_file_range_inline(trans, root, inode,
712 start, end, 0, NULL);
713 if (ret == 0) {
714 extent_clear_unlock_delalloc(inode,
715 &BTRFS_I(inode)->io_tree,
716 start, end, NULL, 1, 1,
Chris Mason8b62b722009-09-02 16:53:46 -0400717 1, 1, 1, 1, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500718 *nr_written = *nr_written +
719 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
720 *page_started = 1;
721 ret = 0;
722 goto out;
723 }
724 }
Chris Masonc8b97812008-10-29 14:49:59 -0400725
726 BUG_ON(disk_num_bytes >
727 btrfs_super_total_bytes(&root->fs_info->super_copy));
728
Chris Masonb917b7c2009-09-18 16:07:03 -0400729
730 read_lock(&BTRFS_I(inode)->extent_tree.lock);
731 em = search_extent_mapping(&BTRFS_I(inode)->extent_tree,
732 start, num_bytes);
733 if (em) {
734 alloc_hint = em->block_start;
735 free_extent_map(em);
736 }
737 read_unlock(&BTRFS_I(inode)->extent_tree.lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400738 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400739
Chris Masond3977122009-01-05 21:25:51 -0500740 while (disk_num_bytes > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400741 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400742 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500743 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400744 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500745 BUG_ON(ret);
746
Chris Masone6dcd2d2008-07-17 12:53:50 -0400747 em = alloc_extent_map(GFP_NOFS);
748 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500749 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500750 ram_size = ins.offset;
751 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400752
Chris Masone6dcd2d2008-07-17 12:53:50 -0400753 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400754 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400755 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400756 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400757
Chris Masond3977122009-01-05 21:25:51 -0500758 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400759 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400760 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400761 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400762 if (ret != -EEXIST) {
763 free_extent_map(em);
764 break;
765 }
766 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400767 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400768 }
769
Chris Mason98d20f62008-04-14 09:46:10 -0400770 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400771 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500772 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400773 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400774
Yan Zheng17d217f2008-12-12 10:03:38 -0500775 if (root->root_key.objectid ==
776 BTRFS_DATA_RELOC_TREE_OBJECTID) {
777 ret = btrfs_reloc_clone_csums(inode, start,
778 cur_alloc_size);
779 BUG_ON(ret);
780 }
781
Chris Masond3977122009-01-05 21:25:51 -0500782 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400783 break;
Chris Masond3977122009-01-05 21:25:51 -0500784
Chris Masonc8b97812008-10-29 14:49:59 -0400785 /* we're not doing compressed IO, don't unlock the first
786 * page (which the caller expects to stay locked), don't
787 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400788 *
789 * Do set the Private2 bit so we know this page was properly
790 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400791 */
792 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
793 start, start + ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500794 locked_page, unlock, 1,
Chris Mason8b62b722009-09-02 16:53:46 -0400795 1, 0, 0, 0, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400796 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500797 num_bytes -= cur_alloc_size;
798 alloc_hint = ins.objectid + ins.offset;
799 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -0400800 }
Chris Masonb888db2b2007-08-27 16:49:44 -0400801out:
Chris Mason771ed682008-11-06 22:02:51 -0500802 ret = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400803 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400804
Chris Masonbe20aa92007-12-17 20:14:01 -0500805 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500806}
Chris Masonc8b97812008-10-29 14:49:59 -0400807
Chris Mason771ed682008-11-06 22:02:51 -0500808/*
809 * work queue call back to started compression on a file and pages
810 */
811static noinline void async_cow_start(struct btrfs_work *work)
812{
813 struct async_cow *async_cow;
814 int num_added = 0;
815 async_cow = container_of(work, struct async_cow, work);
816
817 compress_file_range(async_cow->inode, async_cow->locked_page,
818 async_cow->start, async_cow->end, async_cow,
819 &num_added);
820 if (num_added == 0)
821 async_cow->inode = NULL;
822}
823
824/*
825 * work queue call back to submit previously compressed pages
826 */
827static noinline void async_cow_submit(struct btrfs_work *work)
828{
829 struct async_cow *async_cow;
830 struct btrfs_root *root;
831 unsigned long nr_pages;
832
833 async_cow = container_of(work, struct async_cow, work);
834
835 root = async_cow->root;
836 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
837 PAGE_CACHE_SHIFT;
838
839 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
840
841 if (atomic_read(&root->fs_info->async_delalloc_pages) <
842 5 * 1042 * 1024 &&
843 waitqueue_active(&root->fs_info->async_submit_wait))
844 wake_up(&root->fs_info->async_submit_wait);
845
Chris Masond3977122009-01-05 21:25:51 -0500846 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500847 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500848}
Chris Masonc8b97812008-10-29 14:49:59 -0400849
Chris Mason771ed682008-11-06 22:02:51 -0500850static noinline void async_cow_free(struct btrfs_work *work)
851{
852 struct async_cow *async_cow;
853 async_cow = container_of(work, struct async_cow, work);
854 kfree(async_cow);
855}
856
857static int cow_file_range_async(struct inode *inode, struct page *locked_page,
858 u64 start, u64 end, int *page_started,
859 unsigned long *nr_written)
860{
861 struct async_cow *async_cow;
862 struct btrfs_root *root = BTRFS_I(inode)->root;
863 unsigned long nr_pages;
864 u64 cur_end;
865 int limit = 10 * 1024 * 1042;
866
Chris Mason771ed682008-11-06 22:02:51 -0500867 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED |
Chris Mason2c64c532009-09-02 15:04:12 -0400868 EXTENT_DELALLOC, 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500869 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500870 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
871 async_cow->inode = inode;
872 async_cow->root = root;
873 async_cow->locked_page = locked_page;
874 async_cow->start = start;
875
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200876 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -0500877 cur_end = end;
878 else
879 cur_end = min(end, start + 512 * 1024 - 1);
880
881 async_cow->end = cur_end;
882 INIT_LIST_HEAD(&async_cow->extents);
883
884 async_cow->work.func = async_cow_start;
885 async_cow->work.ordered_func = async_cow_submit;
886 async_cow->work.ordered_free = async_cow_free;
887 async_cow->work.flags = 0;
888
Chris Mason771ed682008-11-06 22:02:51 -0500889 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
890 PAGE_CACHE_SHIFT;
891 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
892
893 btrfs_queue_worker(&root->fs_info->delalloc_workers,
894 &async_cow->work);
895
896 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
897 wait_event(root->fs_info->async_submit_wait,
898 (atomic_read(&root->fs_info->async_delalloc_pages) <
899 limit));
900 }
901
Chris Masond3977122009-01-05 21:25:51 -0500902 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500903 atomic_read(&root->fs_info->async_delalloc_pages)) {
904 wait_event(root->fs_info->async_submit_wait,
905 (atomic_read(&root->fs_info->async_delalloc_pages) ==
906 0));
907 }
908
909 *nr_written += nr_pages;
910 start = cur_end + 1;
911 }
912 *page_started = 1;
913 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500914}
915
Chris Masond3977122009-01-05 21:25:51 -0500916static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -0500917 u64 bytenr, u64 num_bytes)
918{
919 int ret;
920 struct btrfs_ordered_sum *sums;
921 LIST_HEAD(list);
922
Yan Zheng07d400a2009-01-06 11:42:00 -0500923 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
924 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -0500925 if (ret == 0 && list_empty(&list))
926 return 0;
927
928 while (!list_empty(&list)) {
929 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
930 list_del(&sums->list);
931 kfree(sums);
932 }
933 return 1;
934}
935
Chris Masond352ac62008-09-29 15:18:18 -0400936/*
937 * when nowcow writeback call back. This checks for snapshots or COW copies
938 * of the extents that exist in the file, and COWs the file as required.
939 *
940 * If no cow copies or snapshots exist, we write directly to the existing
941 * blocks on disk
942 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400943static noinline int run_delalloc_nocow(struct inode *inode,
944 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -0500945 u64 start, u64 end, int *page_started, int force,
946 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -0500947{
Chris Masonbe20aa92007-12-17 20:14:01 -0500948 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400949 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -0500950 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -0500951 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -0400952 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -0500953 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -0400954 u64 cow_start;
955 u64 cur_offset;
956 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400957 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -0400958 u64 disk_bytenr;
959 u64 num_bytes;
960 int extent_type;
961 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -0400962 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -0400963 int nocow;
964 int check_prev = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500965
966 path = btrfs_alloc_path();
967 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400968 trans = btrfs_join_transaction(root, 1);
969 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500970
Yan Zheng80ff3852008-10-30 14:20:02 -0400971 cow_start = (u64)-1;
972 cur_offset = start;
973 while (1) {
974 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
975 cur_offset, 0);
976 BUG_ON(ret < 0);
977 if (ret > 0 && path->slots[0] > 0 && check_prev) {
978 leaf = path->nodes[0];
979 btrfs_item_key_to_cpu(leaf, &found_key,
980 path->slots[0] - 1);
981 if (found_key.objectid == inode->i_ino &&
982 found_key.type == BTRFS_EXTENT_DATA_KEY)
983 path->slots[0]--;
984 }
985 check_prev = 0;
986next_slot:
987 leaf = path->nodes[0];
988 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
989 ret = btrfs_next_leaf(root, path);
990 if (ret < 0)
991 BUG_ON(1);
992 if (ret > 0)
993 break;
994 leaf = path->nodes[0];
995 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500996
Yan Zheng80ff3852008-10-30 14:20:02 -0400997 nocow = 0;
998 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -0500999 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001000 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001001
Yan Zheng80ff3852008-10-30 14:20:02 -04001002 if (found_key.objectid > inode->i_ino ||
1003 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1004 found_key.offset > end)
1005 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001006
Yan Zheng80ff3852008-10-30 14:20:02 -04001007 if (found_key.offset > cur_offset) {
1008 extent_end = found_key.offset;
1009 goto out_check;
1010 }
Chris Masonc31f8832008-01-08 15:46:31 -05001011
Yan Zheng80ff3852008-10-30 14:20:02 -04001012 fi = btrfs_item_ptr(leaf, path->slots[0],
1013 struct btrfs_file_extent_item);
1014 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001015
Yan Zhengd899e052008-10-30 14:25:28 -04001016 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1017 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001018 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001019 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001020 extent_end = found_key.offset +
1021 btrfs_file_extent_num_bytes(leaf, fi);
1022 if (extent_end <= start) {
1023 path->slots[0]++;
1024 goto next_slot;
1025 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001026 if (disk_bytenr == 0)
1027 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001028 if (btrfs_file_extent_compression(leaf, fi) ||
1029 btrfs_file_extent_encryption(leaf, fi) ||
1030 btrfs_file_extent_other_encoding(leaf, fi))
1031 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001032 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1033 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001034 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001035 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001036 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001037 found_key.offset -
1038 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001039 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001040 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001041 disk_bytenr += cur_offset - found_key.offset;
1042 num_bytes = min(end + 1, extent_end) - cur_offset;
1043 /*
1044 * force cow if csum exists in the range.
1045 * this ensure that csum for a given extent are
1046 * either valid or do not exist.
1047 */
1048 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1049 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001050 nocow = 1;
1051 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1052 extent_end = found_key.offset +
1053 btrfs_file_extent_inline_len(leaf, fi);
1054 extent_end = ALIGN(extent_end, root->sectorsize);
1055 } else {
1056 BUG_ON(1);
1057 }
1058out_check:
1059 if (extent_end <= start) {
1060 path->slots[0]++;
1061 goto next_slot;
1062 }
1063 if (!nocow) {
1064 if (cow_start == (u64)-1)
1065 cow_start = cur_offset;
1066 cur_offset = extent_end;
1067 if (cur_offset > end)
1068 break;
1069 path->slots[0]++;
1070 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001071 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001072
Yan Zheng7ea394f2008-08-05 13:05:02 -04001073 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001074 if (cow_start != (u64)-1) {
1075 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001076 found_key.offset - 1, page_started,
1077 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001078 BUG_ON(ret);
1079 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001080 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001081
Yan Zhengd899e052008-10-30 14:25:28 -04001082 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1083 struct extent_map *em;
1084 struct extent_map_tree *em_tree;
1085 em_tree = &BTRFS_I(inode)->extent_tree;
1086 em = alloc_extent_map(GFP_NOFS);
1087 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001088 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001089 em->len = num_bytes;
1090 em->block_len = num_bytes;
1091 em->block_start = disk_bytenr;
1092 em->bdev = root->fs_info->fs_devices->latest_bdev;
1093 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1094 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001095 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001096 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001097 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001098 if (ret != -EEXIST) {
1099 free_extent_map(em);
1100 break;
1101 }
1102 btrfs_drop_extent_cache(inode, em->start,
1103 em->start + em->len - 1, 0);
1104 }
1105 type = BTRFS_ORDERED_PREALLOC;
1106 } else {
1107 type = BTRFS_ORDERED_NOCOW;
1108 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001109
1110 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001111 num_bytes, num_bytes, type);
1112 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001113
Yan Zhengd899e052008-10-30 14:25:28 -04001114 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1115 cur_offset, cur_offset + num_bytes - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04001116 locked_page, 1, 1, 1, 0, 0, 0, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001117 cur_offset = extent_end;
1118 if (cur_offset > end)
1119 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001120 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001121 btrfs_release_path(root, path);
1122
1123 if (cur_offset <= end && cow_start == (u64)-1)
1124 cow_start = cur_offset;
1125 if (cow_start != (u64)-1) {
1126 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001127 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001128 BUG_ON(ret);
1129 }
1130
1131 ret = btrfs_end_transaction(trans, root);
1132 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001133 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001134 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001135}
1136
Chris Masond352ac62008-09-29 15:18:18 -04001137/*
1138 * extent_io.c call back to do delayed allocation processing
1139 */
Chris Masonc8b97812008-10-29 14:49:59 -04001140static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001141 u64 start, u64 end, int *page_started,
1142 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001143{
Chris Masonbe20aa92007-12-17 20:14:01 -05001144 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001145 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001146
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001147 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001148 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001149 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001150 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001151 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001152 page_started, 0, nr_written);
Chris Mason7f366cf2009-03-12 20:12:45 -04001153 else if (!btrfs_test_opt(root, COMPRESS))
1154 ret = cow_file_range(inode, locked_page, start, end,
1155 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001156 else
Chris Mason771ed682008-11-06 22:02:51 -05001157 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001158 page_started, nr_written);
Chris Masonb888db2b2007-08-27 16:49:44 -04001159 return ret;
1160}
1161
Josef Bacik9ed74f22009-09-11 16:12:44 -04001162static int btrfs_split_extent_hook(struct inode *inode,
1163 struct extent_state *orig, u64 split)
1164{
1165 struct btrfs_root *root = BTRFS_I(inode)->root;
1166 u64 size;
1167
1168 if (!(orig->state & EXTENT_DELALLOC))
1169 return 0;
1170
1171 size = orig->end - orig->start + 1;
1172 if (size > root->fs_info->max_extent) {
1173 u64 num_extents;
1174 u64 new_size;
1175
1176 new_size = orig->end - split + 1;
1177 num_extents = div64_u64(size + root->fs_info->max_extent - 1,
1178 root->fs_info->max_extent);
1179
1180 /*
1181 * if we break a large extent up then leave delalloc_extents be,
1182 * since we've already accounted for the large extent.
1183 */
1184 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1185 root->fs_info->max_extent) < num_extents)
1186 return 0;
1187 }
1188
1189 BTRFS_I(inode)->delalloc_extents++;
1190
1191 return 0;
1192}
1193
1194/*
1195 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1196 * extents so we can keep track of new extents that are just merged onto old
1197 * extents, such as when we are doing sequential writes, so we can properly
1198 * account for the metadata space we'll need.
1199 */
1200static int btrfs_merge_extent_hook(struct inode *inode,
1201 struct extent_state *new,
1202 struct extent_state *other)
1203{
1204 struct btrfs_root *root = BTRFS_I(inode)->root;
1205 u64 new_size, old_size;
1206 u64 num_extents;
1207
1208 /* not delalloc, ignore it */
1209 if (!(other->state & EXTENT_DELALLOC))
1210 return 0;
1211
1212 old_size = other->end - other->start + 1;
1213 if (new->start < other->start)
1214 new_size = other->end - new->start + 1;
1215 else
1216 new_size = new->end - other->start + 1;
1217
1218 /* we're not bigger than the max, unreserve the space and go */
1219 if (new_size <= root->fs_info->max_extent) {
1220 BTRFS_I(inode)->delalloc_extents--;
1221 return 0;
1222 }
1223
1224 /*
1225 * If we grew by another max_extent, just return, we want to keep that
1226 * reserved amount.
1227 */
1228 num_extents = div64_u64(old_size + root->fs_info->max_extent - 1,
1229 root->fs_info->max_extent);
1230 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1231 root->fs_info->max_extent) > num_extents)
1232 return 0;
1233
1234 BTRFS_I(inode)->delalloc_extents--;
1235
1236 return 0;
1237}
1238
Chris Masond352ac62008-09-29 15:18:18 -04001239/*
1240 * extent_io.c set_bit_hook, used to track delayed allocation
1241 * bytes in this file, and to maintain the list of inodes that
1242 * have pending delalloc work to be done.
1243 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001244static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -05001245 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001246{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001247
Chris Mason75eff682008-12-15 15:54:40 -05001248 /*
1249 * set_bit and clear bit hooks normally require _irqsave/restore
1250 * but in this case, we are only testeing for the DELALLOC
1251 * bit, which is only set or cleared with irqs on
1252 */
Chris Masonb0c68f82008-01-31 11:05:37 -05001253 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001254 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001255
1256 BTRFS_I(inode)->delalloc_extents++;
Josef Bacik6a632092009-02-20 11:00:09 -05001257 btrfs_delalloc_reserve_space(root, inode, end - start + 1);
Chris Mason75eff682008-12-15 15:54:40 -05001258 spin_lock(&root->fs_info->delalloc_lock);
Chris Mason90692182008-02-08 13:49:28 -05001259 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -05001260 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -04001261 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1262 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1263 &root->fs_info->delalloc_inodes);
1264 }
Chris Mason75eff682008-12-15 15:54:40 -05001265 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001266 }
1267 return 0;
1268}
1269
Chris Masond352ac62008-09-29 15:18:18 -04001270/*
1271 * extent_io.c clear_bit_hook, see set_bit_hook for why
1272 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001273static int btrfs_clear_bit_hook(struct inode *inode,
1274 struct extent_state *state, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001275{
Chris Mason75eff682008-12-15 15:54:40 -05001276 /*
1277 * set_bit and clear bit hooks normally require _irqsave/restore
1278 * but in this case, we are only testeing for the DELALLOC
1279 * bit, which is only set or cleared with irqs on
1280 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001281 if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001282 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -04001283
Josef Bacik9ed74f22009-09-11 16:12:44 -04001284 BTRFS_I(inode)->delalloc_extents--;
1285 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
1286
Chris Mason75eff682008-12-15 15:54:40 -05001287 spin_lock(&root->fs_info->delalloc_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001288 if (state->end - state->start + 1 >
1289 root->fs_info->delalloc_bytes) {
Chris Masond3977122009-01-05 21:25:51 -05001290 printk(KERN_INFO "btrfs warning: delalloc account "
1291 "%llu %llu\n",
Josef Bacik9ed74f22009-09-11 16:12:44 -04001292 (unsigned long long)
1293 state->end - state->start + 1,
Chris Masond3977122009-01-05 21:25:51 -05001294 (unsigned long long)
1295 root->fs_info->delalloc_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05001296 btrfs_delalloc_free_space(root, inode, (u64)-1);
Chris Masonb0c68f82008-01-31 11:05:37 -05001297 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -05001298 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -05001299 } else {
Josef Bacik6a632092009-02-20 11:00:09 -05001300 btrfs_delalloc_free_space(root, inode,
Josef Bacik9ed74f22009-09-11 16:12:44 -04001301 state->end -
1302 state->start + 1);
1303 root->fs_info->delalloc_bytes -= state->end -
1304 state->start + 1;
1305 BTRFS_I(inode)->delalloc_bytes -= state->end -
1306 state->start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -05001307 }
Chris Masonea8c2812008-08-04 23:17:27 -04001308 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1309 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1310 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1311 }
Chris Mason75eff682008-12-15 15:54:40 -05001312 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001313 }
1314 return 0;
1315}
1316
Chris Masond352ac62008-09-29 15:18:18 -04001317/*
1318 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1319 * we don't create bios that span stripes or chunks
1320 */
Chris Mason239b14b2008-03-24 15:02:07 -04001321int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001322 size_t size, struct bio *bio,
1323 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001324{
1325 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1326 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001327 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001328 u64 length = 0;
1329 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001330 int ret;
1331
Chris Mason771ed682008-11-06 22:02:51 -05001332 if (bio_flags & EXTENT_BIO_COMPRESSED)
1333 return 0;
1334
Chris Masonf2d8d742008-04-21 10:03:05 -04001335 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001336 map_tree = &root->fs_info->mapping_tree;
1337 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001338 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001339 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001340
Chris Masond3977122009-01-05 21:25:51 -05001341 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001342 return 1;
Chris Mason239b14b2008-03-24 15:02:07 -04001343 return 0;
1344}
1345
Chris Masond352ac62008-09-29 15:18:18 -04001346/*
1347 * in order to insert checksums into the metadata in large chunks,
1348 * we wait until bio submission time. All the pages in the bio are
1349 * checksummed and sums are attached onto the ordered extent record.
1350 *
1351 * At IO completion time the cums attached on the ordered extent record
1352 * are inserted into the btree
1353 */
Chris Masond3977122009-01-05 21:25:51 -05001354static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1355 struct bio *bio, int mirror_num,
1356 unsigned long bio_flags)
Chris Mason065631f2008-02-20 12:07:25 -05001357{
Chris Mason065631f2008-02-20 12:07:25 -05001358 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001359 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001360
Chris Masond20f7042008-12-08 16:58:54 -05001361 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001362 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001363 return 0;
1364}
Chris Masone0156402008-04-16 11:15:20 -04001365
Chris Mason4a69a412008-11-06 22:03:00 -05001366/*
1367 * in order to insert checksums into the metadata in large chunks,
1368 * we wait until bio submission time. All the pages in the bio are
1369 * checksummed and sums are attached onto the ordered extent record.
1370 *
1371 * At IO completion time the cums attached on the ordered extent record
1372 * are inserted into the btree
1373 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001374static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Mason4a69a412008-11-06 22:03:00 -05001375 int mirror_num, unsigned long bio_flags)
1376{
1377 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001378 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001379}
1380
Chris Masond352ac62008-09-29 15:18:18 -04001381/*
Chris Masoncad321a2008-12-17 14:51:42 -05001382 * extent_io.c submission hook. This does the right thing for csum calculation
1383 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001384 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001385static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001386 int mirror_num, unsigned long bio_flags)
Chris Mason44b8bd72008-04-16 11:14:51 -04001387{
1388 struct btrfs_root *root = BTRFS_I(inode)->root;
1389 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001390 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001391
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001392 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001393
Chris Masone6dcd2d2008-07-17 12:53:50 -04001394 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1395 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001396
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001397 if (!(rw & (1 << BIO_RW))) {
Chris Masond20f7042008-12-08 16:58:54 -05001398 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001399 return btrfs_submit_compressed_read(inode, bio,
1400 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001401 } else if (!skip_sum)
1402 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001403 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001404 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001405 /* csum items have already been cloned */
1406 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1407 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001408 /* we're doing a write, do the async checksumming */
1409 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001410 inode, rw, bio, mirror_num,
Chris Mason4a69a412008-11-06 22:03:00 -05001411 bio_flags, __btrfs_submit_bio_start,
1412 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001413 }
1414
Chris Mason0b86a832008-03-24 15:01:56 -04001415mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001416 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001417}
Chris Mason6885f302008-02-20 16:11:05 -05001418
Chris Masond352ac62008-09-29 15:18:18 -04001419/*
1420 * given a list of ordered sums record them in the inode. This happens
1421 * at IO completion time based on sums calculated at bio submission time.
1422 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001423static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001424 struct inode *inode, u64 file_offset,
1425 struct list_head *list)
1426{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001427 struct btrfs_ordered_sum *sum;
1428
1429 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001430
1431 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001432 btrfs_csum_file_blocks(trans,
1433 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001434 }
1435 return 0;
1436}
1437
Chris Masonea8c2812008-08-04 23:17:27 -04001438int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
1439{
Chris Masond3977122009-01-05 21:25:51 -05001440 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001441 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001442 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1443 GFP_NOFS);
1444}
1445
Chris Masond352ac62008-09-29 15:18:18 -04001446/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001447struct btrfs_writepage_fixup {
1448 struct page *page;
1449 struct btrfs_work work;
1450};
1451
Christoph Hellwigb2950862008-12-02 09:54:17 -05001452static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001453{
1454 struct btrfs_writepage_fixup *fixup;
1455 struct btrfs_ordered_extent *ordered;
1456 struct page *page;
1457 struct inode *inode;
1458 u64 page_start;
1459 u64 page_end;
1460
1461 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1462 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001463again:
Chris Mason247e7432008-07-17 12:53:51 -04001464 lock_page(page);
1465 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1466 ClearPageChecked(page);
1467 goto out_page;
1468 }
1469
1470 inode = page->mapping->host;
1471 page_start = page_offset(page);
1472 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1473
1474 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001475
1476 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001477 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001478 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001479
1480 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1481 if (ordered) {
1482 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
1483 page_end, GFP_NOFS);
1484 unlock_page(page);
1485 btrfs_start_ordered_extent(inode, ordered, 1);
1486 goto again;
1487 }
Chris Mason247e7432008-07-17 12:53:51 -04001488
Chris Masonea8c2812008-08-04 23:17:27 -04001489 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -04001490 ClearPageChecked(page);
1491out:
1492 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1493out_page:
1494 unlock_page(page);
1495 page_cache_release(page);
1496}
1497
1498/*
1499 * There are a few paths in the higher layers of the kernel that directly
1500 * set the page dirty bit without asking the filesystem if it is a
1501 * good idea. This causes problems because we want to make sure COW
1502 * properly happens and the data=ordered rules are followed.
1503 *
Chris Masonc8b97812008-10-29 14:49:59 -04001504 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001505 * hasn't been properly setup for IO. We kick off an async process
1506 * to fix it up. The async helper will wait for ordered extents, set
1507 * the delalloc bit and make it safe to write the page.
1508 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001509static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001510{
1511 struct inode *inode = page->mapping->host;
1512 struct btrfs_writepage_fixup *fixup;
1513 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001514
Chris Mason8b62b722009-09-02 16:53:46 -04001515 /* this page is properly in the ordered list */
1516 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001517 return 0;
1518
1519 if (PageChecked(page))
1520 return -EAGAIN;
1521
1522 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1523 if (!fixup)
1524 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001525
Chris Mason247e7432008-07-17 12:53:51 -04001526 SetPageChecked(page);
1527 page_cache_get(page);
1528 fixup->work.func = btrfs_writepage_fixup_worker;
1529 fixup->page = page;
1530 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1531 return -EAGAIN;
1532}
1533
Yan Zhengd899e052008-10-30 14:25:28 -04001534static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1535 struct inode *inode, u64 file_pos,
1536 u64 disk_bytenr, u64 disk_num_bytes,
1537 u64 num_bytes, u64 ram_bytes,
Chris Masone980b502009-04-24 14:39:24 -04001538 u64 locked_end,
Yan Zhengd899e052008-10-30 14:25:28 -04001539 u8 compression, u8 encryption,
1540 u16 other_encoding, int extent_type)
1541{
1542 struct btrfs_root *root = BTRFS_I(inode)->root;
1543 struct btrfs_file_extent_item *fi;
1544 struct btrfs_path *path;
1545 struct extent_buffer *leaf;
1546 struct btrfs_key ins;
1547 u64 hint;
1548 int ret;
1549
1550 path = btrfs_alloc_path();
1551 BUG_ON(!path);
1552
Chris Masonb9473432009-03-13 11:00:37 -04001553 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001554
1555 /*
1556 * we may be replacing one extent in the tree with another.
1557 * The new extent is pinned in the extent map, and we don't want
1558 * to drop it from the cache until it is completely in the btree.
1559 *
1560 * So, tell btrfs_drop_extents to leave this extent in the cache.
1561 * the caller is expected to unpin it and allow it to be merged
1562 * with the others.
1563 */
Yan Zhengd899e052008-10-30 14:25:28 -04001564 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Chris Masone980b502009-04-24 14:39:24 -04001565 file_pos + num_bytes, locked_end,
Chris Masona1ed8352009-09-11 12:27:37 -04001566 file_pos, &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001567 BUG_ON(ret);
1568
1569 ins.objectid = inode->i_ino;
1570 ins.offset = file_pos;
1571 ins.type = BTRFS_EXTENT_DATA_KEY;
1572 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1573 BUG_ON(ret);
1574 leaf = path->nodes[0];
1575 fi = btrfs_item_ptr(leaf, path->slots[0],
1576 struct btrfs_file_extent_item);
1577 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1578 btrfs_set_file_extent_type(leaf, fi, extent_type);
1579 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1580 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1581 btrfs_set_file_extent_offset(leaf, fi, 0);
1582 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1583 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1584 btrfs_set_file_extent_compression(leaf, fi, compression);
1585 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1586 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001587
1588 btrfs_unlock_up_safe(path, 1);
1589 btrfs_set_lock_blocking(leaf);
1590
Yan Zhengd899e052008-10-30 14:25:28 -04001591 btrfs_mark_buffer_dirty(leaf);
1592
1593 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001594
1595 ins.objectid = disk_bytenr;
1596 ins.offset = disk_num_bytes;
1597 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001598 ret = btrfs_alloc_reserved_file_extent(trans, root,
1599 root->root_key.objectid,
1600 inode->i_ino, file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001601 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001602 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001603
Yan Zhengd899e052008-10-30 14:25:28 -04001604 return 0;
1605}
1606
Chris Mason5d13a982009-03-13 11:41:46 -04001607/*
1608 * helper function for btrfs_finish_ordered_io, this
1609 * just reads in some of the csum leaves to prime them into ram
1610 * before we start the transaction. It limits the amount of btree
1611 * reads required while inside the transaction.
1612 */
1613static noinline void reada_csum(struct btrfs_root *root,
1614 struct btrfs_path *path,
1615 struct btrfs_ordered_extent *ordered_extent)
1616{
1617 struct btrfs_ordered_sum *sum;
1618 u64 bytenr;
1619
1620 sum = list_entry(ordered_extent->list.next, struct btrfs_ordered_sum,
1621 list);
1622 bytenr = sum->sums[0].bytenr;
1623
1624 /*
1625 * we don't care about the results, the point of this search is
1626 * just to get the btree leaves into ram
1627 */
1628 btrfs_lookup_csum(NULL, root->fs_info->csum_root, path, bytenr, 0);
1629}
1630
Chris Masond352ac62008-09-29 15:18:18 -04001631/* as ordered data IO finishes, this gets called so we can finish
1632 * an ordered extent if the range of bytes in the file it covers are
1633 * fully written.
1634 */
Chris Mason211f90e2008-07-18 11:56:15 -04001635static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001636{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001637 struct btrfs_root *root = BTRFS_I(inode)->root;
1638 struct btrfs_trans_handle *trans;
Chris Mason5d13a982009-03-13 11:41:46 -04001639 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001640 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonb7ec40d2009-03-12 20:12:45 -04001641 struct btrfs_path *path;
Yan Zhengd899e052008-10-30 14:25:28 -04001642 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001643 int ret;
1644
1645 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001646 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001647 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001648
Chris Masonb7ec40d2009-03-12 20:12:45 -04001649 /*
1650 * before we join the transaction, try to do some of our IO.
1651 * This will limit the amount of IO that we have to do with
1652 * the transaction running. We're unlikely to need to do any
1653 * IO if the file extents are new, the disk_i_size checks
1654 * covers the most common case.
1655 */
1656 if (start < BTRFS_I(inode)->disk_i_size) {
1657 path = btrfs_alloc_path();
1658 if (path) {
1659 ret = btrfs_lookup_file_extent(NULL, root, path,
1660 inode->i_ino,
1661 start, 0);
Chris Mason5d13a982009-03-13 11:41:46 -04001662 ordered_extent = btrfs_lookup_ordered_extent(inode,
1663 start);
1664 if (!list_empty(&ordered_extent->list)) {
1665 btrfs_release_path(root, path);
1666 reada_csum(root, path, ordered_extent);
1667 }
Chris Masonb7ec40d2009-03-12 20:12:45 -04001668 btrfs_free_path(path);
1669 }
1670 }
1671
Chris Masonf9295742008-07-17 12:54:14 -04001672 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001673
Chris Mason5d13a982009-03-13 11:41:46 -04001674 if (!ordered_extent)
1675 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001676 BUG_ON(!ordered_extent);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001677 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
1678 goto nocow;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001679
1680 lock_extent(io_tree, ordered_extent->file_offset,
1681 ordered_extent->file_offset + ordered_extent->len - 1,
1682 GFP_NOFS);
1683
Chris Masonc8b97812008-10-29 14:49:59 -04001684 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001685 compressed = 1;
1686 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1687 BUG_ON(compressed);
1688 ret = btrfs_mark_extent_written(trans, root, inode,
1689 ordered_extent->file_offset,
1690 ordered_extent->file_offset +
1691 ordered_extent->len);
1692 BUG_ON(ret);
1693 } else {
1694 ret = insert_reserved_file_extent(trans, inode,
1695 ordered_extent->file_offset,
1696 ordered_extent->start,
1697 ordered_extent->disk_len,
1698 ordered_extent->len,
1699 ordered_extent->len,
Chris Masone980b502009-04-24 14:39:24 -04001700 ordered_extent->file_offset +
1701 ordered_extent->len,
Yan Zhengd899e052008-10-30 14:25:28 -04001702 compressed, 0, 0,
1703 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001704 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1705 ordered_extent->file_offset,
1706 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001707 BUG_ON(ret);
1708 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001709 unlock_extent(io_tree, ordered_extent->file_offset,
1710 ordered_extent->file_offset + ordered_extent->len - 1,
1711 GFP_NOFS);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001712nocow:
Chris Masone6dcd2d2008-07-17 12:53:50 -04001713 add_pending_csums(trans, inode, ordered_extent->file_offset,
1714 &ordered_extent->list);
1715
Chris Mason34353022008-09-23 20:19:49 -04001716 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masondbe674a2008-07-17 12:54:05 -04001717 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone02119d2008-09-05 16:13:11 -04001718 btrfs_update_inode(trans, root, inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001719 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason34353022008-09-23 20:19:49 -04001720 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -04001721
Chris Masone6dcd2d2008-07-17 12:53:50 -04001722 /* once for us */
1723 btrfs_put_ordered_extent(ordered_extent);
1724 /* once for the tree */
1725 btrfs_put_ordered_extent(ordered_extent);
1726
Chris Masone6dcd2d2008-07-17 12:53:50 -04001727 btrfs_end_transaction(trans, root);
1728 return 0;
1729}
1730
Christoph Hellwigb2950862008-12-02 09:54:17 -05001731static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001732 struct extent_state *state, int uptodate)
1733{
Chris Mason8b62b722009-09-02 16:53:46 -04001734 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001735 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1736}
1737
Chris Masond352ac62008-09-29 15:18:18 -04001738/*
1739 * When IO fails, either with EIO or csum verification fails, we
1740 * try other mirrors that might have a good copy of the data. This
1741 * io_failure_record is used to record state as we go through all the
1742 * mirrors. If another mirror has good data, the page is set up to date
1743 * and things continue. If a good mirror can't be found, the original
1744 * bio end_io callback is called to indicate things have failed.
1745 */
Chris Mason7e383262008-04-09 16:28:12 -04001746struct io_failure_record {
1747 struct page *page;
1748 u64 start;
1749 u64 len;
1750 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001751 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001752 int last_mirror;
1753};
1754
Christoph Hellwigb2950862008-12-02 09:54:17 -05001755static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001756 struct page *page, u64 start, u64 end,
1757 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001758{
1759 struct io_failure_record *failrec = NULL;
1760 u64 private;
1761 struct extent_map *em;
1762 struct inode *inode = page->mapping->host;
1763 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001764 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001765 struct bio *bio;
1766 int num_copies;
1767 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001768 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001769 u64 logical;
1770
1771 ret = get_state_private(failure_tree, start, &private);
1772 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001773 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1774 if (!failrec)
1775 return -ENOMEM;
1776 failrec->start = start;
1777 failrec->len = end - start + 1;
1778 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001779 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001780
Chris Mason890871b2009-09-02 16:24:52 -04001781 read_lock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -04001782 em = lookup_extent_mapping(em_tree, start, failrec->len);
1783 if (em->start > start || em->start + em->len < start) {
1784 free_extent_map(em);
1785 em = NULL;
1786 }
Chris Mason890871b2009-09-02 16:24:52 -04001787 read_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001788
1789 if (!em || IS_ERR(em)) {
1790 kfree(failrec);
1791 return -EIO;
1792 }
1793 logical = start - em->start;
1794 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001795 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1796 logical = em->block_start;
1797 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1798 }
Chris Mason7e383262008-04-09 16:28:12 -04001799 failrec->logical = logical;
1800 free_extent_map(em);
1801 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1802 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001803 set_state_private(failure_tree, start,
1804 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001805 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001806 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001807 }
1808 num_copies = btrfs_num_copies(
1809 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1810 failrec->logical, failrec->len);
1811 failrec->last_mirror++;
1812 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001813 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001814 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1815 failrec->start,
1816 EXTENT_LOCKED);
1817 if (state && state->start != failrec->start)
1818 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001819 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001820 }
1821 if (!state || failrec->last_mirror > num_copies) {
1822 set_state_private(failure_tree, failrec->start, 0);
1823 clear_extent_bits(failure_tree, failrec->start,
1824 failrec->start + failrec->len - 1,
1825 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1826 kfree(failrec);
1827 return -EIO;
1828 }
1829 bio = bio_alloc(GFP_NOFS, 1);
1830 bio->bi_private = state;
1831 bio->bi_end_io = failed_bio->bi_end_io;
1832 bio->bi_sector = failrec->logical >> 9;
1833 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001834 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001835
Chris Mason7e383262008-04-09 16:28:12 -04001836 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001837 if (failed_bio->bi_rw & (1 << BIO_RW))
1838 rw = WRITE;
1839 else
1840 rw = READ;
1841
1842 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001843 failrec->last_mirror,
Chris Masond20f7042008-12-08 16:58:54 -05001844 failrec->bio_flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001845 return 0;
1846}
1847
Chris Masond352ac62008-09-29 15:18:18 -04001848/*
1849 * each time an IO finishes, we do a fast check in the IO failure tree
1850 * to see if we need to process or clean up an io_failure_record
1851 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001852static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001853{
1854 u64 private;
1855 u64 private_failure;
1856 struct io_failure_record *failure;
1857 int ret;
1858
1859 private = 0;
1860 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1861 (u64)-1, 1, EXTENT_DIRTY)) {
1862 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1863 start, &private_failure);
1864 if (ret == 0) {
1865 failure = (struct io_failure_record *)(unsigned long)
1866 private_failure;
1867 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1868 failure->start, 0);
1869 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1870 failure->start,
1871 failure->start + failure->len - 1,
1872 EXTENT_DIRTY | EXTENT_LOCKED,
1873 GFP_NOFS);
1874 kfree(failure);
1875 }
1876 }
Chris Mason7e383262008-04-09 16:28:12 -04001877 return 0;
1878}
1879
Chris Masond352ac62008-09-29 15:18:18 -04001880/*
1881 * when reads are done, we need to check csums to verify the data is correct
1882 * if there's a match, we allow the bio to finish. If not, we go through
1883 * the io_failure_record routines to find good copies
1884 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001885static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001886 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001887{
Chris Mason35ebb932007-10-30 16:56:53 -04001888 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001889 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001890 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001891 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001892 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001893 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001894 struct btrfs_root *root = BTRFS_I(inode)->root;
1895 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001896
Chris Masond20f7042008-12-08 16:58:54 -05001897 if (PageChecked(page)) {
1898 ClearPageChecked(page);
1899 goto good;
1900 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001901
1902 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Chris Masonb6cda9b2007-12-14 15:30:32 -05001903 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001904
Yan Zheng17d217f2008-12-12 10:03:38 -05001905 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001906 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001907 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1908 GFP_NOFS);
1909 return 0;
1910 }
1911
Yanc2e639f2008-02-04 08:57:25 -05001912 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001913 private = state->private;
1914 ret = 0;
1915 } else {
1916 ret = get_state_private(io_tree, start, &private);
1917 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001918 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001919 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001920 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001921
Chris Masonff79f812007-10-15 16:22:25 -04001922 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1923 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001924 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001925 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001926
Chris Mason9ab86c82009-01-07 09:48:51 -05001927 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001928good:
Chris Mason7e383262008-04-09 16:28:12 -04001929 /* if the io failure tree for this inode is non-empty,
1930 * check to see if we've recovered from a failed IO
1931 */
Chris Mason1259ab72008-05-12 13:39:03 -04001932 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001933 return 0;
1934
1935zeroit:
Chris Mason193f2842009-04-27 07:29:05 -04001936 if (printk_ratelimit()) {
1937 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1938 "private %llu\n", page->mapping->host->i_ino,
1939 (unsigned long long)start, csum,
1940 (unsigned long long)private);
1941 }
Chris Masondb945352007-10-15 16:15:53 -04001942 memset(kaddr + offset, 1, end - start + 1);
1943 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05001944 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04001945 if (private == 0)
1946 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001947 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001948}
Chris Masonb888db2b2007-08-27 16:49:44 -04001949
Josef Bacik7b128762008-07-24 12:17:14 -04001950/*
1951 * This creates an orphan entry for the given inode in case something goes
1952 * wrong in the middle of an unlink/truncate.
1953 */
1954int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
1955{
1956 struct btrfs_root *root = BTRFS_I(inode)->root;
1957 int ret = 0;
1958
Yanbcc63ab2008-07-30 16:29:20 -04001959 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001960
1961 /* already on the orphan list, we're good */
1962 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001963 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001964 return 0;
1965 }
1966
1967 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
1968
Yanbcc63ab2008-07-30 16:29:20 -04001969 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001970
1971 /*
1972 * insert an orphan item to track this unlinked/truncated file
1973 */
1974 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
1975
1976 return ret;
1977}
1978
1979/*
1980 * We have done the truncate/delete so we can go ahead and remove the orphan
1981 * item for this particular inode.
1982 */
1983int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
1984{
1985 struct btrfs_root *root = BTRFS_I(inode)->root;
1986 int ret = 0;
1987
Yanbcc63ab2008-07-30 16:29:20 -04001988 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001989
1990 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001991 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001992 return 0;
1993 }
1994
1995 list_del_init(&BTRFS_I(inode)->i_orphan);
1996 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -04001997 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001998 return 0;
1999 }
2000
Yanbcc63ab2008-07-30 16:29:20 -04002001 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002002
2003 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2004
2005 return ret;
2006}
2007
2008/*
2009 * this cleans up any orphans that may be left on the list from the last use
2010 * of this root.
2011 */
2012void btrfs_orphan_cleanup(struct btrfs_root *root)
2013{
2014 struct btrfs_path *path;
2015 struct extent_buffer *leaf;
2016 struct btrfs_item *item;
2017 struct btrfs_key key, found_key;
2018 struct btrfs_trans_handle *trans;
2019 struct inode *inode;
2020 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2021
Josef Bacik7b128762008-07-24 12:17:14 -04002022 path = btrfs_alloc_path();
2023 if (!path)
2024 return;
2025 path->reada = -1;
2026
2027 key.objectid = BTRFS_ORPHAN_OBJECTID;
2028 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2029 key.offset = (u64)-1;
2030
Josef Bacik7b128762008-07-24 12:17:14 -04002031
2032 while (1) {
2033 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2034 if (ret < 0) {
2035 printk(KERN_ERR "Error searching slot for orphan: %d"
2036 "\n", ret);
2037 break;
2038 }
2039
2040 /*
2041 * if ret == 0 means we found what we were searching for, which
2042 * is weird, but possible, so only screw with path if we didnt
2043 * find the key and see if we have stuff that matches
2044 */
2045 if (ret > 0) {
2046 if (path->slots[0] == 0)
2047 break;
2048 path->slots[0]--;
2049 }
2050
2051 /* pull out the item */
2052 leaf = path->nodes[0];
2053 item = btrfs_item_nr(leaf, path->slots[0]);
2054 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2055
2056 /* make sure the item matches what we want */
2057 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2058 break;
2059 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2060 break;
2061
2062 /* release the path since we're done with it */
2063 btrfs_release_path(root, path);
2064
2065 /*
2066 * this is where we are basically btrfs_lookup, without the
2067 * crossing root thing. we store the inode number in the
2068 * offset of the orphan item.
2069 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002070 found_key.objectid = found_key.offset;
2071 found_key.type = BTRFS_INODE_ITEM_KEY;
2072 found_key.offset = 0;
2073 inode = btrfs_iget(root->fs_info->sb, &found_key, root);
2074 if (IS_ERR(inode))
Josef Bacik7b128762008-07-24 12:17:14 -04002075 break;
2076
Josef Bacik7b128762008-07-24 12:17:14 -04002077 /*
2078 * add this inode to the orphan list so btrfs_orphan_del does
2079 * the proper thing when we hit it
2080 */
Yanbcc63ab2008-07-30 16:29:20 -04002081 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002082 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04002083 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002084
2085 /*
2086 * if this is a bad inode, means we actually succeeded in
2087 * removing the inode, but not the orphan record, which means
2088 * we need to manually delete the orphan since iput will just
2089 * do a destroy_inode
2090 */
2091 if (is_bad_inode(inode)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002092 trans = btrfs_start_transaction(root, 1);
Josef Bacik7b128762008-07-24 12:17:14 -04002093 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002094 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04002095 iput(inode);
2096 continue;
2097 }
2098
2099 /* if we have links, this was a truncate, lets do that */
2100 if (inode->i_nlink) {
2101 nr_truncate++;
2102 btrfs_truncate(inode);
2103 } else {
2104 nr_unlink++;
2105 }
2106
2107 /* this will do delete_inode and everything for us */
2108 iput(inode);
2109 }
2110
2111 if (nr_unlink)
2112 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2113 if (nr_truncate)
2114 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2115
2116 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002117}
2118
Chris Masond352ac62008-09-29 15:18:18 -04002119/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002120 * very simple check to peek ahead in the leaf looking for xattrs. If we
2121 * don't find any xattrs, we know there can't be any acls.
2122 *
2123 * slot is the slot the inode is in, objectid is the objectid of the inode
2124 */
2125static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2126 int slot, u64 objectid)
2127{
2128 u32 nritems = btrfs_header_nritems(leaf);
2129 struct btrfs_key found_key;
2130 int scanned = 0;
2131
2132 slot++;
2133 while (slot < nritems) {
2134 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2135
2136 /* we found a different objectid, there must not be acls */
2137 if (found_key.objectid != objectid)
2138 return 0;
2139
2140 /* we found an xattr, assume we've got an acl */
2141 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2142 return 1;
2143
2144 /*
2145 * we found a key greater than an xattr key, there can't
2146 * be any acls later on
2147 */
2148 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2149 return 0;
2150
2151 slot++;
2152 scanned++;
2153
2154 /*
2155 * it goes inode, inode backrefs, xattrs, extents,
2156 * so if there are a ton of hard links to an inode there can
2157 * be a lot of backrefs. Don't waste time searching too hard,
2158 * this is just an optimization
2159 */
2160 if (scanned >= 8)
2161 break;
2162 }
2163 /* we hit the end of the leaf before we found an xattr or
2164 * something larger than an xattr. We have to assume the inode
2165 * has acls
2166 */
2167 return 1;
2168}
2169
2170/*
Chris Masond352ac62008-09-29 15:18:18 -04002171 * read an inode from the btree into the in-memory inode
2172 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002173static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002174{
2175 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002176 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002177 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002178 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002179 struct btrfs_root *root = BTRFS_I(inode)->root;
2180 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002181 int maybe_acls;
Chris Mason39279cc2007-06-12 06:35:45 -04002182 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04002183 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002184 int ret;
2185
2186 path = btrfs_alloc_path();
2187 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002188 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002189
Chris Mason39279cc2007-06-12 06:35:45 -04002190 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002191 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002192 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002193
Chris Mason5f39d392007-10-15 16:14:19 -04002194 leaf = path->nodes[0];
2195 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2196 struct btrfs_inode_item);
2197
2198 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2199 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2200 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2201 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002202 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002203
2204 tspec = btrfs_inode_atime(inode_item);
2205 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2206 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2207
2208 tspec = btrfs_inode_mtime(inode_item);
2209 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2210 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2211
2212 tspec = btrfs_inode_ctime(inode_item);
2213 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2214 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2215
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002216 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002217 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002218 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002219 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002220 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002221 rdev = btrfs_inode_rdev(leaf, inode_item);
2222
Josef Bacikaec74772008-07-24 12:12:38 -04002223 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002224 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002225
Chris Mason5f39d392007-10-15 16:14:19 -04002226 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002227
Chris Mason46a53cc2009-04-27 11:47:50 -04002228 /*
2229 * try to precache a NULL acl entry for files that don't have
2230 * any xattrs or acls
2231 */
2232 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
Al Viro72c04902009-06-24 16:58:48 -04002233 if (!maybe_acls)
2234 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002235
Yan Zhengd2fb3432008-12-11 16:30:39 -05002236 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2237 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002238 btrfs_free_path(path);
2239 inode_item = NULL;
2240
Chris Mason39279cc2007-06-12 06:35:45 -04002241 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002242 case S_IFREG:
2243 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002244 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002245 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002246 inode->i_fop = &btrfs_file_operations;
2247 inode->i_op = &btrfs_file_inode_operations;
2248 break;
2249 case S_IFDIR:
2250 inode->i_fop = &btrfs_dir_file_operations;
2251 if (root == root->fs_info->tree_root)
2252 inode->i_op = &btrfs_dir_ro_inode_operations;
2253 else
2254 inode->i_op = &btrfs_dir_inode_operations;
2255 break;
2256 case S_IFLNK:
2257 inode->i_op = &btrfs_symlink_inode_operations;
2258 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002259 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002260 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002261 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002262 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002263 init_special_inode(inode, inode->i_mode, rdev);
2264 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002265 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002266
2267 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002268 return;
2269
2270make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002271 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002272 make_bad_inode(inode);
2273}
2274
Chris Masond352ac62008-09-29 15:18:18 -04002275/*
2276 * given a leaf and an inode, copy the inode fields into the leaf
2277 */
Chris Masone02119d2008-09-05 16:13:11 -04002278static void fill_inode_item(struct btrfs_trans_handle *trans,
2279 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002280 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002281 struct inode *inode)
2282{
Chris Mason5f39d392007-10-15 16:14:19 -04002283 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2284 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002285 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002286 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2287 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2288
2289 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2290 inode->i_atime.tv_sec);
2291 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2292 inode->i_atime.tv_nsec);
2293
2294 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2295 inode->i_mtime.tv_sec);
2296 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2297 inode->i_mtime.tv_nsec);
2298
2299 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2300 inode->i_ctime.tv_sec);
2301 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2302 inode->i_ctime.tv_nsec);
2303
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002304 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002305 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002306 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002307 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002308 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002309 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002310 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002311}
2312
Chris Masond352ac62008-09-29 15:18:18 -04002313/*
2314 * copy everything in the in-memory inode into the btree.
2315 */
Chris Masond3977122009-01-05 21:25:51 -05002316noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2317 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002318{
2319 struct btrfs_inode_item *inode_item;
2320 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002321 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002322 int ret;
2323
2324 path = btrfs_alloc_path();
2325 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002326 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002327 ret = btrfs_lookup_inode(trans, root, path,
2328 &BTRFS_I(inode)->location, 1);
2329 if (ret) {
2330 if (ret > 0)
2331 ret = -ENOENT;
2332 goto failed;
2333 }
2334
Chris Masonb4ce94d2009-02-04 09:25:08 -05002335 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002336 leaf = path->nodes[0];
2337 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002338 struct btrfs_inode_item);
2339
Chris Masone02119d2008-09-05 16:13:11 -04002340 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002341 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002342 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002343 ret = 0;
2344failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002345 btrfs_free_path(path);
2346 return ret;
2347}
2348
2349
Chris Masond352ac62008-09-29 15:18:18 -04002350/*
2351 * unlink helper that gets used here in inode.c and in the tree logging
2352 * recovery code. It remove a link in a directory with a given name, and
2353 * also drops the back refs in the inode to the directory
2354 */
Chris Masone02119d2008-09-05 16:13:11 -04002355int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2356 struct btrfs_root *root,
2357 struct inode *dir, struct inode *inode,
2358 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002359{
2360 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002361 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002362 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002363 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002364 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002365 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002366
2367 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002368 if (!path) {
2369 ret = -ENOMEM;
2370 goto err;
2371 }
2372
Chris Masonb9473432009-03-13 11:00:37 -04002373 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002374 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2375 name, name_len, -1);
2376 if (IS_ERR(di)) {
2377 ret = PTR_ERR(di);
2378 goto err;
2379 }
2380 if (!di) {
2381 ret = -ENOENT;
2382 goto err;
2383 }
Chris Mason5f39d392007-10-15 16:14:19 -04002384 leaf = path->nodes[0];
2385 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002386 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002387 if (ret)
2388 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002389 btrfs_release_path(root, path);
2390
Josef Bacikaec74772008-07-24 12:12:38 -04002391 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002392 inode->i_ino,
2393 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002394 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002395 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002396 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002397 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002398 goto err;
2399 }
2400
Chris Mason39279cc2007-06-12 06:35:45 -04002401 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002402 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002403 if (IS_ERR(di)) {
2404 ret = PTR_ERR(di);
2405 goto err;
2406 }
2407 if (!di) {
2408 ret = -ENOENT;
2409 goto err;
2410 }
2411 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002412 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002413
Chris Masone02119d2008-09-05 16:13:11 -04002414 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2415 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002416 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002417
2418 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2419 dir, index);
2420 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002421err:
2422 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002423 if (ret)
2424 goto out;
2425
2426 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2427 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2428 btrfs_update_inode(trans, root, dir);
2429 btrfs_drop_nlink(inode);
2430 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002431out:
Chris Mason39279cc2007-06-12 06:35:45 -04002432 return ret;
2433}
2434
2435static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2436{
2437 struct btrfs_root *root;
2438 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002439 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002440 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002441 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002442
2443 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002444
Chris Mason39279cc2007-06-12 06:35:45 -04002445 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002446
Chris Mason39279cc2007-06-12 06:35:45 -04002447 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04002448
2449 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2450
Chris Masone02119d2008-09-05 16:13:11 -04002451 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2452 dentry->d_name.name, dentry->d_name.len);
Josef Bacik7b128762008-07-24 12:17:14 -04002453
2454 if (inode->i_nlink == 0)
2455 ret = btrfs_orphan_add(trans, inode);
2456
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002457 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002458
Chris Mason89ce8a62008-06-25 16:01:31 -04002459 btrfs_end_transaction_throttle(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002460 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002461 return ret;
2462}
2463
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002464int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2465 struct btrfs_root *root,
2466 struct inode *dir, u64 objectid,
2467 const char *name, int name_len)
2468{
2469 struct btrfs_path *path;
2470 struct extent_buffer *leaf;
2471 struct btrfs_dir_item *di;
2472 struct btrfs_key key;
2473 u64 index;
2474 int ret;
2475
2476 path = btrfs_alloc_path();
2477 if (!path)
2478 return -ENOMEM;
2479
2480 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2481 name, name_len, -1);
2482 BUG_ON(!di || IS_ERR(di));
2483
2484 leaf = path->nodes[0];
2485 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2486 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2487 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2488 BUG_ON(ret);
2489 btrfs_release_path(root, path);
2490
2491 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2492 objectid, root->root_key.objectid,
2493 dir->i_ino, &index, name, name_len);
2494 if (ret < 0) {
2495 BUG_ON(ret != -ENOENT);
2496 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2497 name, name_len);
2498 BUG_ON(!di || IS_ERR(di));
2499
2500 leaf = path->nodes[0];
2501 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2502 btrfs_release_path(root, path);
2503 index = key.offset;
2504 }
2505
2506 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2507 index, name, name_len, -1);
2508 BUG_ON(!di || IS_ERR(di));
2509
2510 leaf = path->nodes[0];
2511 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2512 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2513 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2514 BUG_ON(ret);
2515 btrfs_release_path(root, path);
2516
2517 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2518 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2519 ret = btrfs_update_inode(trans, root, dir);
2520 BUG_ON(ret);
2521 dir->i_sb->s_dirt = 1;
2522
2523 btrfs_free_path(path);
2524 return 0;
2525}
2526
Chris Mason39279cc2007-06-12 06:35:45 -04002527static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2528{
2529 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002530 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002531 int ret;
2532 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002533 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002534 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002535
Chris Mason3394e162008-11-17 20:42:26 -05002536 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002537 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04002538 return -ENOTEMPTY;
2539
Chris Mason39279cc2007-06-12 06:35:45 -04002540 trans = btrfs_start_transaction(root, 1);
2541 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002542
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002543 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
2544 err = btrfs_unlink_subvol(trans, root, dir,
2545 BTRFS_I(inode)->location.objectid,
2546 dentry->d_name.name,
2547 dentry->d_name.len);
2548 goto out;
2549 }
2550
Josef Bacik7b128762008-07-24 12:17:14 -04002551 err = btrfs_orphan_add(trans, inode);
2552 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002553 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002554
Chris Mason39279cc2007-06-12 06:35:45 -04002555 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04002556 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2557 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05002558 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04002559 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002560out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002561 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002562 ret = btrfs_end_transaction_throttle(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002563 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05002564
Chris Mason39279cc2007-06-12 06:35:45 -04002565 if (ret && !err)
2566 err = ret;
2567 return err;
2568}
2569
Chris Masond20f7042008-12-08 16:58:54 -05002570#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04002571/*
Chris Mason323ac952008-10-01 19:05:46 -04002572 * when truncating bytes in a file, it is possible to avoid reading
2573 * the leaves that contain only checksum items. This can be the
2574 * majority of the IO required to delete a large file, but it must
2575 * be done carefully.
2576 *
2577 * The keys in the level just above the leaves are checked to make sure
2578 * the lowest key in a given leaf is a csum key, and starts at an offset
2579 * after the new size.
2580 *
2581 * Then the key for the next leaf is checked to make sure it also has
2582 * a checksum item for the same file. If it does, we know our target leaf
2583 * contains only checksum items, and it can be safely freed without reading
2584 * it.
2585 *
2586 * This is just an optimization targeted at large files. It may do
2587 * nothing. It will return 0 unless things went badly.
2588 */
2589static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2590 struct btrfs_root *root,
2591 struct btrfs_path *path,
2592 struct inode *inode, u64 new_size)
2593{
2594 struct btrfs_key key;
2595 int ret;
2596 int nritems;
2597 struct btrfs_key found_key;
2598 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002599 struct btrfs_leaf_ref *ref;
2600 u64 leaf_gen;
2601 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04002602
2603 path->lowest_level = 1;
2604 key.objectid = inode->i_ino;
2605 key.type = BTRFS_CSUM_ITEM_KEY;
2606 key.offset = new_size;
2607again:
2608 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2609 if (ret < 0)
2610 goto out;
2611
2612 if (path->nodes[1] == NULL) {
2613 ret = 0;
2614 goto out;
2615 }
2616 ret = 0;
2617 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2618 nritems = btrfs_header_nritems(path->nodes[1]);
2619
2620 if (!nritems)
2621 goto out;
2622
2623 if (path->slots[1] >= nritems)
2624 goto next_node;
2625
2626 /* did we find a key greater than anything we want to delete? */
2627 if (found_key.objectid > inode->i_ino ||
2628 (found_key.objectid == inode->i_ino && found_key.type > key.type))
2629 goto out;
2630
2631 /* we check the next key in the node to make sure the leave contains
2632 * only checksum items. This comparison doesn't work if our
2633 * leaf is the last one in the node
2634 */
2635 if (path->slots[1] + 1 >= nritems) {
2636next_node:
2637 /* search forward from the last key in the node, this
2638 * will bring us into the next node in the tree
2639 */
2640 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2641
2642 /* unlikely, but we inc below, so check to be safe */
2643 if (found_key.offset == (u64)-1)
2644 goto out;
2645
2646 /* search_forward needs a path with locks held, do the
2647 * search again for the original key. It is possible
2648 * this will race with a balance and return a path that
2649 * we could modify, but this drop is just an optimization
2650 * and is allowed to miss some leaves.
2651 */
2652 btrfs_release_path(root, path);
2653 found_key.offset++;
2654
2655 /* setup a max key for search_forward */
2656 other_key.offset = (u64)-1;
2657 other_key.type = key.type;
2658 other_key.objectid = key.objectid;
2659
2660 path->keep_locks = 1;
2661 ret = btrfs_search_forward(root, &found_key, &other_key,
2662 path, 0, 0);
2663 path->keep_locks = 0;
2664 if (ret || found_key.objectid != key.objectid ||
2665 found_key.type != key.type) {
2666 ret = 0;
2667 goto out;
2668 }
2669
2670 key.offset = found_key.offset;
2671 btrfs_release_path(root, path);
2672 cond_resched();
2673 goto again;
2674 }
2675
2676 /* we know there's one more slot after us in the tree,
2677 * read that key so we can verify it is also a checksum item
2678 */
2679 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2680
2681 if (found_key.objectid < inode->i_ino)
2682 goto next_key;
2683
2684 if (found_key.type != key.type || found_key.offset < new_size)
2685 goto next_key;
2686
2687 /*
2688 * if the key for the next leaf isn't a csum key from this objectid,
2689 * we can't be sure there aren't good items inside this leaf.
2690 * Bail out
2691 */
2692 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2693 goto out;
2694
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002695 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2696 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04002697 /*
2698 * it is safe to delete this leaf, it contains only
2699 * csum items from this inode at an offset >= new_size
2700 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002701 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04002702 BUG_ON(ret);
2703
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002704 if (root->ref_cows && leaf_gen < trans->transid) {
2705 ref = btrfs_alloc_leaf_ref(root, 0);
2706 if (ref) {
2707 ref->root_gen = root->root_key.offset;
2708 ref->bytenr = leaf_start;
2709 ref->owner = 0;
2710 ref->generation = leaf_gen;
2711 ref->nritems = 0;
2712
Chris Masonbd56b302009-02-04 09:27:02 -05002713 btrfs_sort_leaf_ref(ref);
2714
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002715 ret = btrfs_add_leaf_ref(root, ref, 0);
2716 WARN_ON(ret);
2717 btrfs_free_leaf_ref(root, ref);
2718 } else {
2719 WARN_ON(1);
2720 }
2721 }
Chris Mason323ac952008-10-01 19:05:46 -04002722next_key:
2723 btrfs_release_path(root, path);
2724
2725 if (other_key.objectid == inode->i_ino &&
2726 other_key.type == key.type && other_key.offset > key.offset) {
2727 key.offset = other_key.offset;
2728 cond_resched();
2729 goto again;
2730 }
2731 ret = 0;
2732out:
2733 /* fixup any changes we've made to the path */
2734 path->lowest_level = 0;
2735 path->keep_locks = 0;
2736 btrfs_release_path(root, path);
2737 return ret;
2738}
2739
Chris Masond20f7042008-12-08 16:58:54 -05002740#endif
2741
Chris Mason323ac952008-10-01 19:05:46 -04002742/*
Chris Mason39279cc2007-06-12 06:35:45 -04002743 * this can truncate away extent items, csum items and directory items.
2744 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04002745 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04002746 *
2747 * csum items that cross the new i_size are truncated to the new size
2748 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04002749 *
2750 * min_type is the minimum key type to truncate down to. If set to 0, this
2751 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04002752 */
Chris Masone02119d2008-09-05 16:13:11 -04002753noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2754 struct btrfs_root *root,
2755 struct inode *inode,
2756 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002757{
2758 int ret;
2759 struct btrfs_path *path;
2760 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002761 struct btrfs_key found_key;
Chris Mason06d9a8d2009-02-04 09:30:58 -05002762 u32 found_type = (u8)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002763 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002764 struct btrfs_file_extent_item *fi;
2765 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04002766 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002767 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002768 u64 item_end = 0;
2769 int found_extent;
2770 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002771 int pending_del_nr = 0;
2772 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04002773 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05002774 int encoding;
Chris Mason3b951512008-04-17 11:29:12 -04002775 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002776
Chris Masone02119d2008-09-05 16:13:11 -04002777 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002778 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002779 path = btrfs_alloc_path();
2780 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04002781 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04002782
Chris Mason39279cc2007-06-12 06:35:45 -04002783 /* FIXME, add redo link to tree so we don't leak on crash */
2784 key.objectid = inode->i_ino;
2785 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002786 key.type = (u8)-1;
2787
Chris Mason85e21ba2008-01-29 15:11:36 -05002788search_again:
Chris Masonb9473432009-03-13 11:00:37 -04002789 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05002790 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002791 if (ret < 0)
Chris Mason85e21ba2008-01-29 15:11:36 -05002792 goto error;
Chris Masond3977122009-01-05 21:25:51 -05002793
Chris Mason85e21ba2008-01-29 15:11:36 -05002794 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002795 /* there are no items in the tree for us to truncate, we're
2796 * done
2797 */
2798 if (path->slots[0] == 0) {
2799 ret = 0;
2800 goto error;
2801 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002802 path->slots[0]--;
2803 }
2804
Chris Masond3977122009-01-05 21:25:51 -05002805 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002806 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04002807 leaf = path->nodes[0];
2808 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2809 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05002810 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002811
Chris Mason5f39d392007-10-15 16:14:19 -04002812 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04002813 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002814
Chris Mason85e21ba2008-01-29 15:11:36 -05002815 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002816 break;
2817
Chris Mason5f39d392007-10-15 16:14:19 -04002818 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002819 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04002820 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002821 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04002822 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002823 encoding = btrfs_file_extent_compression(leaf, fi);
2824 encoding |= btrfs_file_extent_encryption(leaf, fi);
2825 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
2826
Chris Mason179e29e2007-11-01 11:28:41 -04002827 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04002828 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04002829 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04002830 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04002831 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04002832 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002833 }
Yan008630c2007-11-07 13:31:09 -05002834 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04002835 }
Chris Masone02119d2008-09-05 16:13:11 -04002836 if (item_end < new_size) {
Chris Masond3977122009-01-05 21:25:51 -05002837 if (found_type == BTRFS_DIR_ITEM_KEY)
Chris Masonb888db2b2007-08-27 16:49:44 -04002838 found_type = BTRFS_INODE_ITEM_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002839 else if (found_type == BTRFS_EXTENT_ITEM_KEY)
Chris Masond20f7042008-12-08 16:58:54 -05002840 found_type = BTRFS_EXTENT_DATA_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002841 else if (found_type == BTRFS_EXTENT_DATA_KEY)
Chris Mason85e21ba2008-01-29 15:11:36 -05002842 found_type = BTRFS_XATTR_ITEM_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002843 else if (found_type == BTRFS_XATTR_ITEM_KEY)
Chris Mason85e21ba2008-01-29 15:11:36 -05002844 found_type = BTRFS_INODE_REF_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002845 else if (found_type)
Chris Masonb888db2b2007-08-27 16:49:44 -04002846 found_type--;
Chris Masond3977122009-01-05 21:25:51 -05002847 else
Chris Masonb888db2b2007-08-27 16:49:44 -04002848 break;
Yana61721d2007-09-17 11:08:38 -04002849 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05002850 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04002851 }
Chris Masone02119d2008-09-05 16:13:11 -04002852 if (found_key.offset >= new_size)
Chris Mason39279cc2007-06-12 06:35:45 -04002853 del_item = 1;
2854 else
2855 del_item = 0;
2856 found_extent = 0;
2857
2858 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04002859 if (found_type != BTRFS_EXTENT_DATA_KEY)
2860 goto delete;
2861
2862 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04002863 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04002864 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002865 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04002866 u64 orig_num_bytes =
2867 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04002868 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04002869 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05002870 extent_num_bytes = extent_num_bytes &
2871 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04002872 btrfs_set_file_extent_num_bytes(leaf, fi,
2873 extent_num_bytes);
2874 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05002875 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04002876 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002877 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04002878 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002879 } else {
Chris Masondb945352007-10-15 16:15:53 -04002880 extent_num_bytes =
2881 btrfs_file_extent_disk_num_bytes(leaf,
2882 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002883 extent_offset = found_key.offset -
2884 btrfs_file_extent_offset(leaf, fi);
2885
Chris Mason39279cc2007-06-12 06:35:45 -04002886 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05002887 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002888 if (extent_start != 0) {
2889 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04002890 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002891 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04002892 }
2893 }
Chris Mason90692182008-02-08 13:49:28 -05002894 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002895 /*
2896 * we can't truncate inline items that have had
2897 * special encodings
2898 */
2899 if (!del_item &&
2900 btrfs_file_extent_compression(leaf, fi) == 0 &&
2901 btrfs_file_extent_encryption(leaf, fi) == 0 &&
2902 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002903 u32 size = new_size - found_key.offset;
2904
2905 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002906 inode_sub_bytes(inode, item_end + 1 -
2907 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04002908 }
2909 size =
2910 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05002911 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04002912 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05002913 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04002914 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002915 inode_sub_bytes(inode, item_end + 1 -
2916 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05002917 }
Chris Mason39279cc2007-06-12 06:35:45 -04002918 }
Chris Mason179e29e2007-11-01 11:28:41 -04002919delete:
Chris Mason39279cc2007-06-12 06:35:45 -04002920 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002921 if (!pending_del_nr) {
2922 /* no pending yet, add ourselves */
2923 pending_del_slot = path->slots[0];
2924 pending_del_nr = 1;
2925 } else if (pending_del_nr &&
2926 path->slots[0] + 1 == pending_del_slot) {
2927 /* hop on the pending chunk */
2928 pending_del_nr++;
2929 pending_del_slot = path->slots[0];
2930 } else {
Chris Masond3977122009-01-05 21:25:51 -05002931 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05002932 }
Chris Mason39279cc2007-06-12 06:35:45 -04002933 } else {
2934 break;
2935 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002936 if (found_extent && root->ref_cows) {
Chris Masonb9473432009-03-13 11:00:37 -04002937 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002938 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002939 extent_num_bytes, 0,
2940 btrfs_header_owner(leaf),
2941 inode->i_ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04002942 BUG_ON(ret);
2943 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002944next:
2945 if (path->slots[0] == 0) {
2946 if (pending_del_nr)
2947 goto del_pending;
2948 btrfs_release_path(root, path);
Chris Mason06d9a8d2009-02-04 09:30:58 -05002949 if (found_type == BTRFS_INODE_ITEM_KEY)
2950 break;
Chris Mason85e21ba2008-01-29 15:11:36 -05002951 goto search_again;
2952 }
2953
2954 path->slots[0]--;
2955 if (pending_del_nr &&
2956 path->slots[0] + 1 != pending_del_slot) {
2957 struct btrfs_key debug;
2958del_pending:
2959 btrfs_item_key_to_cpu(path->nodes[0], &debug,
2960 pending_del_slot);
2961 ret = btrfs_del_items(trans, root, path,
2962 pending_del_slot,
2963 pending_del_nr);
2964 BUG_ON(ret);
2965 pending_del_nr = 0;
2966 btrfs_release_path(root, path);
Chris Mason06d9a8d2009-02-04 09:30:58 -05002967 if (found_type == BTRFS_INODE_ITEM_KEY)
2968 break;
Chris Mason85e21ba2008-01-29 15:11:36 -05002969 goto search_again;
2970 }
Chris Mason39279cc2007-06-12 06:35:45 -04002971 }
2972 ret = 0;
2973error:
Chris Mason85e21ba2008-01-29 15:11:36 -05002974 if (pending_del_nr) {
2975 ret = btrfs_del_items(trans, root, path, pending_del_slot,
2976 pending_del_nr);
2977 }
Chris Mason39279cc2007-06-12 06:35:45 -04002978 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002979 return ret;
2980}
2981
Chris Masona52d9a82007-08-27 16:49:44 -04002982/*
2983 * taken from block_truncate_page, but does cow as it zeros out
2984 * any bytes left in the last page in the file.
2985 */
2986static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
2987{
2988 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04002989 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002990 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2991 struct btrfs_ordered_extent *ordered;
2992 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04002993 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04002994 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2995 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2996 struct page *page;
2997 int ret = 0;
2998 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002999 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003000
3001 if ((offset & (blocksize - 1)) == 0)
3002 goto out;
3003
3004 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003005again:
Chris Masona52d9a82007-08-27 16:49:44 -04003006 page = grab_cache_page(mapping, index);
3007 if (!page)
3008 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003009
3010 page_start = page_offset(page);
3011 page_end = page_start + PAGE_CACHE_SIZE - 1;
3012
Chris Masona52d9a82007-08-27 16:49:44 -04003013 if (!PageUptodate(page)) {
3014 ret = btrfs_readpage(NULL, page);
3015 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003016 if (page->mapping != mapping) {
3017 unlock_page(page);
3018 page_cache_release(page);
3019 goto again;
3020 }
Chris Masona52d9a82007-08-27 16:49:44 -04003021 if (!PageUptodate(page)) {
3022 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003023 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003024 }
3025 }
Chris Mason211c17f2008-05-15 09:13:45 -04003026 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003027
3028 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3029 set_page_extent_mapped(page);
3030
3031 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3032 if (ordered) {
3033 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3034 unlock_page(page);
3035 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003036 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003037 btrfs_put_ordered_extent(ordered);
3038 goto again;
3039 }
3040
Josef Bacik9ed74f22009-09-11 16:12:44 -04003041 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
3042 if (ret) {
3043 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3044 goto out_unlock;
3045 }
3046
Chris Masone6dcd2d2008-07-17 12:53:50 -04003047 ret = 0;
3048 if (offset != PAGE_CACHE_SIZE) {
3049 kaddr = kmap(page);
3050 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3051 flush_dcache_page(page);
3052 kunmap(page);
3053 }
Chris Mason247e7432008-07-17 12:53:51 -04003054 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003055 set_page_dirty(page);
3056 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003057
Chris Mason89642222008-07-24 09:41:53 -04003058out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003059 unlock_page(page);
3060 page_cache_release(page);
3061out:
3062 return ret;
3063}
3064
Yan Zheng9036c102008-10-30 14:19:41 -04003065int btrfs_cont_expand(struct inode *inode, loff_t size)
3066{
3067 struct btrfs_trans_handle *trans;
3068 struct btrfs_root *root = BTRFS_I(inode)->root;
3069 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3070 struct extent_map *em;
3071 u64 mask = root->sectorsize - 1;
3072 u64 hole_start = (inode->i_size + mask) & ~mask;
3073 u64 block_end = (size + mask) & ~mask;
3074 u64 last_byte;
3075 u64 cur_offset;
3076 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003077 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003078
3079 if (size <= hole_start)
3080 return 0;
3081
Yan Zheng9036c102008-10-30 14:19:41 -04003082 btrfs_truncate_page(inode->i_mapping, inode->i_size);
3083
3084 while (1) {
3085 struct btrfs_ordered_extent *ordered;
3086 btrfs_wait_ordered_range(inode, hole_start,
3087 block_end - hole_start);
3088 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3089 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3090 if (!ordered)
3091 break;
3092 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3093 btrfs_put_ordered_extent(ordered);
3094 }
3095
3096 trans = btrfs_start_transaction(root, 1);
3097 btrfs_set_trans_block_group(trans, inode);
3098
3099 cur_offset = hole_start;
3100 while (1) {
3101 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3102 block_end - cur_offset, 0);
3103 BUG_ON(IS_ERR(em) || !em);
3104 last_byte = min(extent_map_end(em), block_end);
3105 last_byte = (last_byte + mask) & ~mask;
3106 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003107 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003108 hole_size = last_byte - cur_offset;
Chris Mason771ed682008-11-06 22:02:51 -05003109 err = btrfs_drop_extents(trans, root, inode,
3110 cur_offset,
3111 cur_offset + hole_size,
Chris Masone980b502009-04-24 14:39:24 -04003112 block_end,
Chris Masona1ed8352009-09-11 12:27:37 -04003113 cur_offset, &hint_byte, 1);
Chris Mason771ed682008-11-06 22:02:51 -05003114 if (err)
3115 break;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003116
3117 err = btrfs_reserve_metadata_space(root, 1);
3118 if (err)
3119 break;
3120
Yan Zheng9036c102008-10-30 14:19:41 -04003121 err = btrfs_insert_file_extent(trans, root,
3122 inode->i_ino, cur_offset, 0,
3123 0, hole_size, 0, hole_size,
3124 0, 0, 0);
3125 btrfs_drop_extent_cache(inode, hole_start,
3126 last_byte - 1, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003127 btrfs_unreserve_metadata_space(root, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04003128 }
3129 free_extent_map(em);
3130 cur_offset = last_byte;
3131 if (err || cur_offset >= block_end)
3132 break;
3133 }
3134
3135 btrfs_end_transaction(trans, root);
3136 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3137 return err;
3138}
3139
Chris Mason39279cc2007-06-12 06:35:45 -04003140static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3141{
3142 struct inode *inode = dentry->d_inode;
3143 int err;
3144
3145 err = inode_change_ok(inode, attr);
3146 if (err)
3147 return err;
3148
Chris Mason5a3f23d2009-03-31 13:27:11 -04003149 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
3150 if (attr->ia_size > inode->i_size) {
3151 err = btrfs_cont_expand(inode, attr->ia_size);
3152 if (err)
3153 return err;
3154 } else if (inode->i_size > 0 &&
3155 attr->ia_size == 0) {
3156
3157 /* we're truncating a file that used to have good
3158 * data down to zero. Make sure it gets into
3159 * the ordered flush list so that any new writes
3160 * get down to disk quickly.
3161 */
3162 BTRFS_I(inode)->ordered_data_close = 1;
3163 }
Chris Mason39279cc2007-06-12 06:35:45 -04003164 }
Yan Zheng9036c102008-10-30 14:19:41 -04003165
Chris Mason39279cc2007-06-12 06:35:45 -04003166 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04003167
3168 if (!err && ((attr->ia_valid & ATTR_MODE)))
3169 err = btrfs_acl_chmod(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003170 return err;
3171}
Chris Mason61295eb2008-01-14 16:24:38 -05003172
Chris Mason39279cc2007-06-12 06:35:45 -04003173void btrfs_delete_inode(struct inode *inode)
3174{
3175 struct btrfs_trans_handle *trans;
3176 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003177 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003178 int ret;
3179
3180 truncate_inode_pages(&inode->i_data, 0);
3181 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003182 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003183 goto no_delete;
3184 }
Chris Mason4a096752008-07-21 10:29:44 -04003185 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003186
Yan, Zheng76dda932009-09-21 16:00:26 -04003187 if (inode->i_nlink > 0) {
3188 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3189 goto no_delete;
3190 }
3191
Chris Masondbe674a2008-07-17 12:54:05 -04003192 btrfs_i_size_write(inode, 0);
Yan Zheng180591b2009-01-06 09:58:06 -05003193 trans = btrfs_join_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003194
Chris Mason39279cc2007-06-12 06:35:45 -04003195 btrfs_set_trans_block_group(trans, inode);
Chris Masone02119d2008-09-05 16:13:11 -04003196 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04003197 if (ret) {
3198 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04003199 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04003200 }
3201
3202 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05003203
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003204 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05003205 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003206
Chris Mason39279cc2007-06-12 06:35:45 -04003207 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003208 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003209 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04003210
3211no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003212 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003213 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003214 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003215no_delete:
3216 clear_inode(inode);
3217}
3218
3219/*
3220 * this returns the key found in the dir entry in the location pointer.
3221 * If no dir entries were found, location->objectid is 0.
3222 */
3223static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3224 struct btrfs_key *location)
3225{
3226 const char *name = dentry->d_name.name;
3227 int namelen = dentry->d_name.len;
3228 struct btrfs_dir_item *di;
3229 struct btrfs_path *path;
3230 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003231 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003232
3233 path = btrfs_alloc_path();
3234 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003235
Chris Mason39279cc2007-06-12 06:35:45 -04003236 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3237 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003238 if (IS_ERR(di))
3239 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003240
3241 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003242 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003243
Chris Mason5f39d392007-10-15 16:14:19 -04003244 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003245out:
Chris Mason39279cc2007-06-12 06:35:45 -04003246 btrfs_free_path(path);
3247 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003248out_err:
3249 location->objectid = 0;
3250 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003251}
3252
3253/*
3254 * when we hit a tree root in a directory, the btrfs part of the inode
3255 * needs to be changed to reflect the root directory of the tree root. This
3256 * is kind of like crossing a mount point.
3257 */
3258static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003259 struct inode *dir,
3260 struct dentry *dentry,
3261 struct btrfs_key *location,
3262 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003263{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003264 struct btrfs_path *path;
3265 struct btrfs_root *new_root;
3266 struct btrfs_root_ref *ref;
3267 struct extent_buffer *leaf;
3268 int ret;
3269 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003270
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003271 path = btrfs_alloc_path();
3272 if (!path) {
3273 err = -ENOMEM;
3274 goto out;
3275 }
Chris Mason39279cc2007-06-12 06:35:45 -04003276
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003277 err = -ENOENT;
3278 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3279 BTRFS_I(dir)->root->root_key.objectid,
3280 location->objectid);
3281 if (ret) {
3282 if (ret < 0)
3283 err = ret;
3284 goto out;
3285 }
Chris Mason39279cc2007-06-12 06:35:45 -04003286
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003287 leaf = path->nodes[0];
3288 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3289 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3290 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3291 goto out;
3292
3293 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3294 (unsigned long)(ref + 1),
3295 dentry->d_name.len);
3296 if (ret)
3297 goto out;
3298
3299 btrfs_release_path(root->fs_info->tree_root, path);
3300
3301 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3302 if (IS_ERR(new_root)) {
3303 err = PTR_ERR(new_root);
3304 goto out;
3305 }
3306
3307 if (btrfs_root_refs(&new_root->root_item) == 0) {
3308 err = -ENOENT;
3309 goto out;
3310 }
3311
3312 *sub_root = new_root;
3313 location->objectid = btrfs_root_dirid(&new_root->root_item);
3314 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003315 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003316 err = 0;
3317out:
3318 btrfs_free_path(path);
3319 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003320}
3321
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003322static void inode_tree_add(struct inode *inode)
3323{
3324 struct btrfs_root *root = BTRFS_I(inode)->root;
3325 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003326 struct rb_node **p;
3327 struct rb_node *parent;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003328again:
3329 p = &root->inode_tree.rb_node;
3330 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003331
Yan, Zheng76dda932009-09-21 16:00:26 -04003332 if (hlist_unhashed(&inode->i_hash))
3333 return;
3334
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003335 spin_lock(&root->inode_lock);
3336 while (*p) {
3337 parent = *p;
3338 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3339
3340 if (inode->i_ino < entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003341 p = &parent->rb_left;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003342 else if (inode->i_ino > entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003343 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003344 else {
3345 WARN_ON(!(entry->vfs_inode.i_state &
3346 (I_WILL_FREE | I_FREEING | I_CLEAR)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003347 rb_erase(parent, &root->inode_tree);
3348 RB_CLEAR_NODE(parent);
3349 spin_unlock(&root->inode_lock);
3350 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003351 }
3352 }
3353 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3354 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3355 spin_unlock(&root->inode_lock);
3356}
3357
3358static void inode_tree_del(struct inode *inode)
3359{
3360 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003361 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003362
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003363 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003364 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003365 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003366 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003367 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003368 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003369 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003370
3371 if (empty && btrfs_root_refs(&root->root_item) == 0) {
3372 synchronize_srcu(&root->fs_info->subvol_srcu);
3373 spin_lock(&root->inode_lock);
3374 empty = RB_EMPTY_ROOT(&root->inode_tree);
3375 spin_unlock(&root->inode_lock);
3376 if (empty)
3377 btrfs_add_dead_root(root);
3378 }
3379}
3380
3381int btrfs_invalidate_inodes(struct btrfs_root *root)
3382{
3383 struct rb_node *node;
3384 struct rb_node *prev;
3385 struct btrfs_inode *entry;
3386 struct inode *inode;
3387 u64 objectid = 0;
3388
3389 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3390
3391 spin_lock(&root->inode_lock);
3392again:
3393 node = root->inode_tree.rb_node;
3394 prev = NULL;
3395 while (node) {
3396 prev = node;
3397 entry = rb_entry(node, struct btrfs_inode, rb_node);
3398
3399 if (objectid < entry->vfs_inode.i_ino)
3400 node = node->rb_left;
3401 else if (objectid > entry->vfs_inode.i_ino)
3402 node = node->rb_right;
3403 else
3404 break;
3405 }
3406 if (!node) {
3407 while (prev) {
3408 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3409 if (objectid <= entry->vfs_inode.i_ino) {
3410 node = prev;
3411 break;
3412 }
3413 prev = rb_next(prev);
3414 }
3415 }
3416 while (node) {
3417 entry = rb_entry(node, struct btrfs_inode, rb_node);
3418 objectid = entry->vfs_inode.i_ino + 1;
3419 inode = igrab(&entry->vfs_inode);
3420 if (inode) {
3421 spin_unlock(&root->inode_lock);
3422 if (atomic_read(&inode->i_count) > 1)
3423 d_prune_aliases(inode);
3424 /*
3425 * btrfs_drop_inode will remove it from
3426 * the inode cache when its usage count
3427 * hits zero.
3428 */
3429 iput(inode);
3430 cond_resched();
3431 spin_lock(&root->inode_lock);
3432 goto again;
3433 }
3434
3435 if (cond_resched_lock(&root->inode_lock))
3436 goto again;
3437
3438 node = rb_next(node);
3439 }
3440 spin_unlock(&root->inode_lock);
3441 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003442}
3443
Chris Masone02119d2008-09-05 16:13:11 -04003444static noinline void init_btrfs_i(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003445{
Chris Masone02119d2008-09-05 16:13:11 -04003446 struct btrfs_inode *bi = BTRFS_I(inode);
3447
Chris Masone02119d2008-09-05 16:13:11 -04003448 bi->generation = 0;
Chris Masonc3027eb2008-12-08 16:40:21 -05003449 bi->sequence = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003450 bi->last_trans = 0;
3451 bi->logged_trans = 0;
3452 bi->delalloc_bytes = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003453 bi->reserved_bytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003454 bi->disk_i_size = 0;
3455 bi->flags = 0;
3456 bi->index_cnt = (u64)-1;
Chris Mason12fcfd22009-03-24 10:24:20 -04003457 bi->last_unlink_trans = 0;
Chris Mason27574952009-05-14 13:10:02 -04003458 bi->ordered_data_close = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003459 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3460 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db2b2007-08-27 16:49:44 -04003461 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003462 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3463 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04003464 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04003465 INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003466 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Chris Masonba1da2f2008-07-17 12:54:15 -04003467 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Masonee6e6502008-07-17 12:54:40 -04003468 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003469 mutex_init(&BTRFS_I(inode)->log_mutex);
3470}
3471
3472static int btrfs_init_locked_inode(struct inode *inode, void *p)
3473{
3474 struct btrfs_iget_args *args = p;
3475 inode->i_ino = args->ino;
3476 init_btrfs_i(inode);
3477 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003478 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003479 return 0;
3480}
3481
3482static int btrfs_find_actor(struct inode *inode, void *opaque)
3483{
3484 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05003485 return args->ino == inode->i_ino &&
3486 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003487}
3488
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003489static struct inode *btrfs_iget_locked(struct super_block *s,
3490 u64 objectid,
3491 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04003492{
3493 struct inode *inode;
3494 struct btrfs_iget_args args;
3495 args.ino = objectid;
3496 args.root = root;
3497
3498 inode = iget5_locked(s, objectid, btrfs_find_actor,
3499 btrfs_init_locked_inode,
3500 (void *)&args);
3501 return inode;
3502}
3503
Balaji Rao1a54ef82008-07-21 02:01:04 +05303504/* Get an inode object given its location and corresponding root.
3505 * Returns in *is_new if the inode was read from disk
3506 */
3507struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003508 struct btrfs_root *root)
Balaji Rao1a54ef82008-07-21 02:01:04 +05303509{
3510 struct inode *inode;
3511
3512 inode = btrfs_iget_locked(s, location->objectid, root);
3513 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003514 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303515
3516 if (inode->i_state & I_NEW) {
3517 BTRFS_I(inode)->root = root;
3518 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3519 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003520
3521 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303522 unlock_new_inode(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303523 }
3524
3525 return inode;
3526}
3527
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003528static struct inode *new_simple_dir(struct super_block *s,
3529 struct btrfs_key *key,
3530 struct btrfs_root *root)
3531{
3532 struct inode *inode = new_inode(s);
3533
3534 if (!inode)
3535 return ERR_PTR(-ENOMEM);
3536
3537 init_btrfs_i(inode);
3538
3539 BTRFS_I(inode)->root = root;
3540 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
3541 BTRFS_I(inode)->dummy_inode = 1;
3542
3543 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
3544 inode->i_op = &simple_dir_inode_operations;
3545 inode->i_fop = &simple_dir_operations;
3546 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
3547 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3548
3549 return inode;
3550}
3551
Chris Mason3de45862008-11-17 21:02:50 -05003552struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04003553{
Chris Masond3977122009-01-05 21:25:51 -05003554 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003555 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003556 struct btrfs_root *sub_root = root;
3557 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04003558 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003559 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003560
Yan, Zheng76dda932009-09-21 16:00:26 -04003561 dentry->d_op = &btrfs_dentry_operations;
3562
Chris Mason39279cc2007-06-12 06:35:45 -04003563 if (dentry->d_name.len > BTRFS_NAME_LEN)
3564 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04003565
Chris Mason39279cc2007-06-12 06:35:45 -04003566 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04003567
Chris Mason39279cc2007-06-12 06:35:45 -04003568 if (ret < 0)
3569 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003570
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003571 if (location.objectid == 0)
3572 return NULL;
3573
3574 if (location.type == BTRFS_INODE_ITEM_KEY) {
3575 inode = btrfs_iget(dir->i_sb, &location, root);
3576 return inode;
3577 }
3578
3579 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
3580
Yan, Zheng76dda932009-09-21 16:00:26 -04003581 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003582 ret = fixup_tree_root_location(root, dir, dentry,
3583 &location, &sub_root);
3584 if (ret < 0) {
3585 if (ret != -ENOENT)
3586 inode = ERR_PTR(ret);
3587 else
3588 inode = new_simple_dir(dir->i_sb, &location, sub_root);
3589 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003590 inode = btrfs_iget(dir->i_sb, &location, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04003591 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003592 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
3593
Chris Mason3de45862008-11-17 21:02:50 -05003594 return inode;
3595}
3596
Yan, Zheng76dda932009-09-21 16:00:26 -04003597static int btrfs_dentry_delete(struct dentry *dentry)
3598{
3599 struct btrfs_root *root;
3600
3601 if (!dentry->d_inode)
3602 return 0;
3603
3604 root = BTRFS_I(dentry->d_inode)->root;
3605 if (btrfs_root_refs(&root->root_item) == 0)
3606 return 1;
3607 return 0;
3608}
3609
Chris Mason3de45862008-11-17 21:02:50 -05003610static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3611 struct nameidata *nd)
3612{
3613 struct inode *inode;
3614
Chris Mason3de45862008-11-17 21:02:50 -05003615 inode = btrfs_lookup_dentry(dir, dentry);
3616 if (IS_ERR(inode))
3617 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003618
Chris Mason39279cc2007-06-12 06:35:45 -04003619 return d_splice_alias(inode, dentry);
3620}
3621
Chris Mason39279cc2007-06-12 06:35:45 -04003622static unsigned char btrfs_filetype_table[] = {
3623 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3624};
3625
David Woodhousecbdf5a22008-08-06 19:42:33 +01003626static int btrfs_real_readdir(struct file *filp, void *dirent,
3627 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04003628{
Chris Mason6da6aba2007-12-18 16:15:09 -05003629 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003630 struct btrfs_root *root = BTRFS_I(inode)->root;
3631 struct btrfs_item *item;
3632 struct btrfs_dir_item *di;
3633 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003634 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003635 struct btrfs_path *path;
3636 int ret;
3637 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003638 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003639 int slot;
3640 int advance;
3641 unsigned char d_type;
3642 int over = 0;
3643 u32 di_cur;
3644 u32 di_total;
3645 u32 di_len;
3646 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003647 char tmp_name[32];
3648 char *name_ptr;
3649 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04003650
3651 /* FIXME, use a real flag for deciding about the key type */
3652 if (root->fs_info->tree_root == root)
3653 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003654
Chris Mason39544012007-12-12 14:38:19 -05003655 /* special case for "." */
3656 if (filp->f_pos == 0) {
3657 over = filldir(dirent, ".", 1,
3658 1, inode->i_ino,
3659 DT_DIR);
3660 if (over)
3661 return 0;
3662 filp->f_pos = 1;
3663 }
Chris Mason39544012007-12-12 14:38:19 -05003664 /* special case for .., just use the back ref */
3665 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003666 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05003667 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003668 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05003669 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01003670 return 0;
Chris Mason39544012007-12-12 14:38:19 -05003671 filp->f_pos = 2;
3672 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003673 path = btrfs_alloc_path();
3674 path->reada = 2;
3675
Chris Mason39279cc2007-06-12 06:35:45 -04003676 btrfs_set_key_type(&key, key_type);
3677 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01003678 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04003679
Chris Mason39279cc2007-06-12 06:35:45 -04003680 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3681 if (ret < 0)
3682 goto err;
3683 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01003684
3685 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003686 leaf = path->nodes[0];
3687 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003688 slot = path->slots[0];
3689 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01003690 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003691 ret = btrfs_next_leaf(root, path);
3692 if (ret)
3693 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003694 leaf = path->nodes[0];
3695 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003696 slot = path->slots[0];
3697 } else {
3698 slot++;
3699 path->slots[0]++;
3700 }
3701 }
Chris Mason3de45862008-11-17 21:02:50 -05003702
Chris Mason39279cc2007-06-12 06:35:45 -04003703 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003704 item = btrfs_item_nr(leaf, slot);
3705 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3706
3707 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04003708 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003709 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003710 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003711 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04003712 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003713
3714 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01003715
Chris Mason39279cc2007-06-12 06:35:45 -04003716 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3717 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003718 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01003719
3720 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04003721 struct btrfs_key location;
3722
3723 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01003724 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003725 name_ptr = tmp_name;
3726 } else {
3727 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01003728 if (!name_ptr) {
3729 ret = -ENOMEM;
3730 goto err;
3731 }
Chris Mason5f39d392007-10-15 16:14:19 -04003732 }
3733 read_extent_buffer(leaf, name_ptr,
3734 (unsigned long)(di + 1), name_len);
3735
3736 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
3737 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05003738
3739 /* is this a reference to our own snapshot? If so
3740 * skip it
3741 */
3742 if (location.type == BTRFS_ROOT_ITEM_KEY &&
3743 location.objectid == root->root_key.objectid) {
3744 over = 0;
3745 goto skip;
3746 }
Chris Mason5f39d392007-10-15 16:14:19 -04003747 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01003748 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003749 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04003750
Chris Mason3de45862008-11-17 21:02:50 -05003751skip:
Chris Mason5f39d392007-10-15 16:14:19 -04003752 if (name_ptr != tmp_name)
3753 kfree(name_ptr);
3754
Chris Mason39279cc2007-06-12 06:35:45 -04003755 if (over)
3756 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05003757 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01003758 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04003759 di_cur += di_len;
3760 di = (struct btrfs_dir_item *)((char *)di + di_len);
3761 }
3762 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003763
3764 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05003765 if (key_type == BTRFS_DIR_INDEX_KEY)
Chris Mason89f135d2009-01-28 15:34:27 -05003766 filp->f_pos = INT_LIMIT(off_t);
Yan Zheng5e591a02008-02-19 11:41:02 -05003767 else
3768 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04003769nopos:
3770 ret = 0;
3771err:
Chris Mason39279cc2007-06-12 06:35:45 -04003772 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003773 return ret;
3774}
3775
3776int btrfs_write_inode(struct inode *inode, int wait)
3777{
3778 struct btrfs_root *root = BTRFS_I(inode)->root;
3779 struct btrfs_trans_handle *trans;
3780 int ret = 0;
3781
Yan Zhengc146afa2008-11-12 14:34:12 -05003782 if (root->fs_info->btree_inode == inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04003783 return 0;
3784
Chris Mason39279cc2007-06-12 06:35:45 -04003785 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04003786 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003787 btrfs_set_trans_block_group(trans, inode);
3788 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003789 }
3790 return ret;
3791}
3792
3793/*
Chris Mason54aa1f42007-06-22 14:16:25 -04003794 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04003795 * inode changes. But, it is most likely to find the inode in cache.
3796 * FIXME, needs more benchmarking...there are no reasons other than performance
3797 * to keep or drop this code.
3798 */
3799void btrfs_dirty_inode(struct inode *inode)
3800{
3801 struct btrfs_root *root = BTRFS_I(inode)->root;
3802 struct btrfs_trans_handle *trans;
3803
Chris Masonf9295742008-07-17 12:54:14 -04003804 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003805 btrfs_set_trans_block_group(trans, inode);
3806 btrfs_update_inode(trans, root, inode);
3807 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003808}
3809
Chris Masond352ac62008-09-29 15:18:18 -04003810/*
3811 * find the highest existing sequence number in a directory
3812 * and then set the in-memory index_cnt variable to reflect
3813 * free sequence numbers
3814 */
Josef Bacikaec74772008-07-24 12:12:38 -04003815static int btrfs_set_inode_index_count(struct inode *inode)
3816{
3817 struct btrfs_root *root = BTRFS_I(inode)->root;
3818 struct btrfs_key key, found_key;
3819 struct btrfs_path *path;
3820 struct extent_buffer *leaf;
3821 int ret;
3822
3823 key.objectid = inode->i_ino;
3824 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
3825 key.offset = (u64)-1;
3826
3827 path = btrfs_alloc_path();
3828 if (!path)
3829 return -ENOMEM;
3830
3831 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3832 if (ret < 0)
3833 goto out;
3834 /* FIXME: we should be able to handle this */
3835 if (ret == 0)
3836 goto out;
3837 ret = 0;
3838
3839 /*
3840 * MAGIC NUMBER EXPLANATION:
3841 * since we search a directory based on f_pos we have to start at 2
3842 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
3843 * else has to start at 2
3844 */
3845 if (path->slots[0] == 0) {
3846 BTRFS_I(inode)->index_cnt = 2;
3847 goto out;
3848 }
3849
3850 path->slots[0]--;
3851
3852 leaf = path->nodes[0];
3853 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3854
3855 if (found_key.objectid != inode->i_ino ||
3856 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
3857 BTRFS_I(inode)->index_cnt = 2;
3858 goto out;
3859 }
3860
3861 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
3862out:
3863 btrfs_free_path(path);
3864 return ret;
3865}
3866
Chris Masond352ac62008-09-29 15:18:18 -04003867/*
3868 * helper to find a free sequence number in a given directory. This current
3869 * code is very simple, later versions will do smarter things in the btree
3870 */
Chris Mason3de45862008-11-17 21:02:50 -05003871int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04003872{
3873 int ret = 0;
3874
3875 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
3876 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05003877 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04003878 return ret;
3879 }
3880
Chris Mason00e4e6b2008-08-05 11:18:09 -04003881 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04003882 BTRFS_I(dir)->index_cnt++;
3883
3884 return ret;
3885}
3886
Chris Mason39279cc2007-06-12 06:35:45 -04003887static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
3888 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04003889 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05003890 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05003891 u64 ref_objectid, u64 objectid,
3892 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04003893{
3894 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003895 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04003896 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04003897 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05003898 struct btrfs_inode_ref *ref;
3899 struct btrfs_key key[2];
3900 u32 sizes[2];
3901 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003902 int ret;
3903 int owner;
3904
Chris Mason5f39d392007-10-15 16:14:19 -04003905 path = btrfs_alloc_path();
3906 BUG_ON(!path);
3907
Chris Mason39279cc2007-06-12 06:35:45 -04003908 inode = new_inode(root->fs_info->sb);
3909 if (!inode)
3910 return ERR_PTR(-ENOMEM);
3911
Josef Bacikaec74772008-07-24 12:12:38 -04003912 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05003913 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04003914 if (ret) {
3915 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04003916 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04003917 }
Josef Bacikaec74772008-07-24 12:12:38 -04003918 }
3919 /*
3920 * index_cnt is ignored for everything but a dir,
3921 * btrfs_get_inode_index_count has an explanation for the magic
3922 * number
3923 */
Chris Masone02119d2008-09-05 16:13:11 -04003924 init_btrfs_i(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04003925 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04003926 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04003927 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik6a632092009-02-20 11:00:09 -05003928 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db2b2007-08-27 16:49:44 -04003929
Chris Mason39279cc2007-06-12 06:35:45 -04003930 if (mode & S_IFDIR)
3931 owner = 0;
3932 else
3933 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003934 BTRFS_I(inode)->block_group =
3935 btrfs_find_block_group(root, 0, alloc_hint, owner);
Chris Mason9c583092008-01-29 15:15:18 -05003936
3937 key[0].objectid = objectid;
3938 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
3939 key[0].offset = 0;
3940
3941 key[1].objectid = objectid;
3942 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
3943 key[1].offset = ref_objectid;
3944
3945 sizes[0] = sizeof(struct btrfs_inode_item);
3946 sizes[1] = name_len + sizeof(*ref);
3947
Chris Masonb9473432009-03-13 11:00:37 -04003948 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05003949 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
3950 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04003951 goto fail;
3952
Chris Mason79683f22008-11-19 22:00:53 -05003953 inode->i_uid = current_fsuid();
Chris Ball8c087b52009-02-04 09:29:54 -05003954
Chris Mason42f15d72009-02-06 11:35:57 -05003955 if (dir && (dir->i_mode & S_ISGID)) {
Chris Ball8c087b52009-02-04 09:29:54 -05003956 inode->i_gid = dir->i_gid;
3957 if (S_ISDIR(mode))
3958 mode |= S_ISGID;
3959 } else
3960 inode->i_gid = current_fsgid();
3961
Chris Mason39279cc2007-06-12 06:35:45 -04003962 inode->i_mode = mode;
3963 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003964 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003965 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04003966 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3967 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003968 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003969
3970 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3971 struct btrfs_inode_ref);
3972 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003973 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05003974 ptr = (unsigned long)(ref + 1);
3975 write_extent_buffer(path->nodes[0], name, ptr, name_len);
3976
Chris Mason5f39d392007-10-15 16:14:19 -04003977 btrfs_mark_buffer_dirty(path->nodes[0]);
3978 btrfs_free_path(path);
3979
Chris Mason39279cc2007-06-12 06:35:45 -04003980 location = &BTRFS_I(inode)->location;
3981 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04003982 location->offset = 0;
3983 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
3984
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003985 btrfs_inherit_iflags(inode, dir);
3986
Chris Mason94272162009-07-02 12:26:06 -04003987 if ((mode & S_IFREG)) {
3988 if (btrfs_test_opt(root, NODATASUM))
3989 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
3990 if (btrfs_test_opt(root, NODATACOW))
3991 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
3992 }
3993
Chris Mason39279cc2007-06-12 06:35:45 -04003994 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003995 inode_tree_add(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003996 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003997fail:
Josef Bacikaec74772008-07-24 12:12:38 -04003998 if (dir)
3999 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004000 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004001 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004002 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004003}
4004
4005static inline u8 btrfs_inode_type(struct inode *inode)
4006{
4007 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4008}
4009
Chris Masond352ac62008-09-29 15:18:18 -04004010/*
4011 * utility function to add 'inode' into 'parent_inode' with
4012 * a give name and a given sequence number.
4013 * if 'add_backref' is true, also insert a backref from the
4014 * inode to the parent directory.
4015 */
Chris Masone02119d2008-09-05 16:13:11 -04004016int btrfs_add_link(struct btrfs_trans_handle *trans,
4017 struct inode *parent_inode, struct inode *inode,
4018 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004019{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004020 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004021 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004022 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04004023
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004024 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4025 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4026 } else {
4027 key.objectid = inode->i_ino;
4028 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4029 key.offset = 0;
4030 }
Chris Mason39279cc2007-06-12 06:35:45 -04004031
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004032 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4033 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4034 key.objectid, root->root_key.objectid,
4035 parent_inode->i_ino,
4036 index, name, name_len);
4037 } else if (add_backref) {
4038 ret = btrfs_insert_inode_ref(trans, root,
4039 name, name_len, inode->i_ino,
4040 parent_inode->i_ino, index);
4041 }
4042
Chris Mason39279cc2007-06-12 06:35:45 -04004043 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004044 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4045 parent_inode->i_ino, &key,
4046 btrfs_inode_type(inode), index);
4047 BUG_ON(ret);
4048
Chris Masondbe674a2008-07-17 12:54:05 -04004049 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004050 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004051 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004052 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004053 }
4054 return ret;
4055}
4056
4057static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05004058 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004059 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004060{
Chris Masone02119d2008-09-05 16:13:11 -04004061 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4062 inode, dentry->d_name.name,
4063 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004064 if (!err) {
4065 d_instantiate(dentry, inode);
4066 return 0;
4067 }
4068 if (err > 0)
4069 err = -EEXIST;
4070 return err;
4071}
4072
Josef Bacik618e21d2007-07-11 10:18:17 -04004073static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4074 int mode, dev_t rdev)
4075{
4076 struct btrfs_trans_handle *trans;
4077 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004078 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004079 int err;
4080 int drop_inode = 0;
4081 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004082 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004083 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004084
4085 if (!new_valid_dev(rdev))
4086 return -EINVAL;
4087
Josef Bacik9ed74f22009-09-11 16:12:44 -04004088 /*
4089 * 2 for inode item and ref
4090 * 2 for dir items
4091 * 1 for xattr if selinux is on
4092 */
4093 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004094 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004095 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004096
Josef Bacik618e21d2007-07-11 10:18:17 -04004097 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004098 if (!trans)
4099 goto fail;
Josef Bacik618e21d2007-07-11 10:18:17 -04004100 btrfs_set_trans_block_group(trans, dir);
4101
4102 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4103 if (err) {
4104 err = -ENOSPC;
4105 goto out_unlock;
4106 }
4107
Josef Bacikaec74772008-07-24 12:12:38 -04004108 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004109 dentry->d_name.len,
4110 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004111 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004112 err = PTR_ERR(inode);
4113 if (IS_ERR(inode))
4114 goto out_unlock;
4115
Jim Owens0279b4c2009-02-04 09:29:13 -05004116 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004117 if (err) {
4118 drop_inode = 1;
4119 goto out_unlock;
4120 }
4121
Josef Bacik618e21d2007-07-11 10:18:17 -04004122 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004123 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004124 if (err)
4125 drop_inode = 1;
4126 else {
4127 inode->i_op = &btrfs_special_inode_operations;
4128 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004129 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004130 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004131 btrfs_update_inode_block_group(trans, inode);
4132 btrfs_update_inode_block_group(trans, dir);
4133out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004134 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004135 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004136fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004137 btrfs_unreserve_metadata_space(root, 5);
Josef Bacik618e21d2007-07-11 10:18:17 -04004138 if (drop_inode) {
4139 inode_dec_link_count(inode);
4140 iput(inode);
4141 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004142 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004143 return err;
4144}
4145
Chris Mason39279cc2007-06-12 06:35:45 -04004146static int btrfs_create(struct inode *dir, struct dentry *dentry,
4147 int mode, struct nameidata *nd)
4148{
4149 struct btrfs_trans_handle *trans;
4150 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004151 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004152 int err;
4153 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05004154 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004155 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004156 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004157
Josef Bacik9ed74f22009-09-11 16:12:44 -04004158 /*
4159 * 2 for inode item and ref
4160 * 2 for dir items
4161 * 1 for xattr if selinux is on
4162 */
4163 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004164 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004165 return err;
4166
Chris Mason39279cc2007-06-12 06:35:45 -04004167 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004168 if (!trans)
4169 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04004170 btrfs_set_trans_block_group(trans, dir);
4171
4172 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4173 if (err) {
4174 err = -ENOSPC;
4175 goto out_unlock;
4176 }
4177
Josef Bacikaec74772008-07-24 12:12:38 -04004178 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004179 dentry->d_name.len,
4180 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004181 objectid, BTRFS_I(dir)->block_group, mode,
4182 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004183 err = PTR_ERR(inode);
4184 if (IS_ERR(inode))
4185 goto out_unlock;
4186
Jim Owens0279b4c2009-02-04 09:29:13 -05004187 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004188 if (err) {
4189 drop_inode = 1;
4190 goto out_unlock;
4191 }
4192
Chris Mason39279cc2007-06-12 06:35:45 -04004193 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004194 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004195 if (err)
4196 drop_inode = 1;
4197 else {
4198 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004199 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004200 inode->i_fop = &btrfs_file_operations;
4201 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004202 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004203 }
Chris Mason39279cc2007-06-12 06:35:45 -04004204 btrfs_update_inode_block_group(trans, inode);
4205 btrfs_update_inode_block_group(trans, dir);
4206out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004207 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004208 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004209fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004210 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04004211 if (drop_inode) {
4212 inode_dec_link_count(inode);
4213 iput(inode);
4214 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004215 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004216 return err;
4217}
4218
4219static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4220 struct dentry *dentry)
4221{
4222 struct btrfs_trans_handle *trans;
4223 struct btrfs_root *root = BTRFS_I(dir)->root;
4224 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004225 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004226 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004227 int err;
4228 int drop_inode = 0;
4229
4230 if (inode->i_nlink == 0)
4231 return -ENOENT;
4232
Josef Bacik9ed74f22009-09-11 16:12:44 -04004233 /*
4234 * 1 item for inode ref
4235 * 2 items for dir items
4236 */
4237 err = btrfs_reserve_metadata_space(root, 3);
Chris Mason1832a6d2007-12-21 16:27:21 -05004238 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004239 return err;
4240
4241 btrfs_inc_nlink(inode);
4242
Chris Mason3de45862008-11-17 21:02:50 -05004243 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004244 if (err)
4245 goto fail;
4246
Chris Mason39279cc2007-06-12 06:35:45 -04004247 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004248
Chris Mason39279cc2007-06-12 06:35:45 -04004249 btrfs_set_trans_block_group(trans, dir);
4250 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04004251
Chris Mason00e4e6b2008-08-05 11:18:09 -04004252 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004253
Yan, Zhenga5719522009-09-24 09:17:31 -04004254 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004255 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004256 } else {
4257 btrfs_update_inode_block_group(trans, dir);
4258 err = btrfs_update_inode(trans, root, inode);
4259 BUG_ON(err);
4260 btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
4261 }
Chris Mason39279cc2007-06-12 06:35:45 -04004262
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004263 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004264 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004265fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004266 btrfs_unreserve_metadata_space(root, 3);
Chris Mason39279cc2007-06-12 06:35:45 -04004267 if (drop_inode) {
4268 inode_dec_link_count(inode);
4269 iput(inode);
4270 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004271 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004272 return err;
4273}
4274
Chris Mason39279cc2007-06-12 06:35:45 -04004275static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4276{
Chris Masonb9d86662008-05-02 16:13:49 -04004277 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004278 struct btrfs_trans_handle *trans;
4279 struct btrfs_root *root = BTRFS_I(dir)->root;
4280 int err = 0;
4281 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004282 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004283 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004284 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004285
Josef Bacik9ed74f22009-09-11 16:12:44 -04004286 /*
4287 * 2 items for inode and ref
4288 * 2 items for dir items
4289 * 1 for xattr if selinux is on
4290 */
4291 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004292 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004293 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004294
Chris Mason39279cc2007-06-12 06:35:45 -04004295 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004296 if (!trans) {
4297 err = -ENOMEM;
Chris Mason39279cc2007-06-12 06:35:45 -04004298 goto out_unlock;
4299 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004300 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004301
4302 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4303 if (err) {
4304 err = -ENOSPC;
4305 goto out_unlock;
4306 }
4307
Josef Bacikaec74772008-07-24 12:12:38 -04004308 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004309 dentry->d_name.len,
4310 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004311 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4312 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004313 if (IS_ERR(inode)) {
4314 err = PTR_ERR(inode);
4315 goto out_fail;
4316 }
Chris Mason5f39d392007-10-15 16:14:19 -04004317
Chris Mason39279cc2007-06-12 06:35:45 -04004318 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004319
Jim Owens0279b4c2009-02-04 09:29:13 -05004320 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004321 if (err)
4322 goto out_fail;
4323
Chris Mason39279cc2007-06-12 06:35:45 -04004324 inode->i_op = &btrfs_dir_inode_operations;
4325 inode->i_fop = &btrfs_dir_file_operations;
4326 btrfs_set_trans_block_group(trans, inode);
4327
Chris Masondbe674a2008-07-17 12:54:05 -04004328 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004329 err = btrfs_update_inode(trans, root, inode);
4330 if (err)
4331 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004332
Chris Masone02119d2008-09-05 16:13:11 -04004333 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4334 inode, dentry->d_name.name,
4335 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004336 if (err)
4337 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004338
Chris Mason39279cc2007-06-12 06:35:45 -04004339 d_instantiate(dentry, inode);
4340 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004341 btrfs_update_inode_block_group(trans, inode);
4342 btrfs_update_inode_block_group(trans, dir);
4343
4344out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004345 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004346 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04004347
Chris Mason39279cc2007-06-12 06:35:45 -04004348out_unlock:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004349 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04004350 if (drop_on_err)
4351 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004352 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004353 return err;
4354}
4355
Chris Masond352ac62008-09-29 15:18:18 -04004356/* helper for btfs_get_extent. Given an existing extent in the tree,
4357 * and an extent that you want to insert, deal with overlap and insert
4358 * the new extent into the tree.
4359 */
Chris Mason3b951512008-04-17 11:29:12 -04004360static int merge_extent_mapping(struct extent_map_tree *em_tree,
4361 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004362 struct extent_map *em,
4363 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04004364{
4365 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04004366
Chris Masone6dcd2d2008-07-17 12:53:50 -04004367 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4368 start_diff = map_start - em->start;
4369 em->start = map_start;
4370 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04004371 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4372 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004373 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04004374 em->block_len -= start_diff;
4375 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004376 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004377}
4378
Chris Masonc8b97812008-10-29 14:49:59 -04004379static noinline int uncompress_inline(struct btrfs_path *path,
4380 struct inode *inode, struct page *page,
4381 size_t pg_offset, u64 extent_offset,
4382 struct btrfs_file_extent_item *item)
4383{
4384 int ret;
4385 struct extent_buffer *leaf = path->nodes[0];
4386 char *tmp;
4387 size_t max_size;
4388 unsigned long inline_size;
4389 unsigned long ptr;
4390
4391 WARN_ON(pg_offset != 0);
4392 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4393 inline_size = btrfs_file_extent_inline_item_len(leaf,
4394 btrfs_item_nr(leaf, path->slots[0]));
4395 tmp = kmalloc(inline_size, GFP_NOFS);
4396 ptr = btrfs_file_extent_inline_start(item);
4397
4398 read_extent_buffer(leaf, tmp, ptr, inline_size);
4399
Chris Mason5b050f02008-11-11 09:34:41 -05004400 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004401 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
4402 inline_size, max_size);
4403 if (ret) {
4404 char *kaddr = kmap_atomic(page, KM_USER0);
4405 unsigned long copy_size = min_t(u64,
4406 PAGE_CACHE_SIZE - pg_offset,
4407 max_size - extent_offset);
4408 memset(kaddr + pg_offset, 0, copy_size);
4409 kunmap_atomic(kaddr, KM_USER0);
4410 }
4411 kfree(tmp);
4412 return 0;
4413}
4414
Chris Masond352ac62008-09-29 15:18:18 -04004415/*
4416 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05004417 * the ugly parts come from merging extents from the disk with the in-ram
4418 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04004419 * where the in-ram extents might be locked pending data=ordered completion.
4420 *
4421 * This also copies inline extents directly into the page.
4422 */
Chris Masond3977122009-01-05 21:25:51 -05004423
Chris Masona52d9a82007-08-27 16:49:44 -04004424struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05004425 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04004426 int create)
4427{
4428 int ret;
4429 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04004430 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04004431 u64 extent_start = 0;
4432 u64 extent_end = 0;
4433 u64 objectid = inode->i_ino;
4434 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04004435 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04004436 struct btrfs_root *root = BTRFS_I(inode)->root;
4437 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04004438 struct extent_buffer *leaf;
4439 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04004440 struct extent_map *em = NULL;
4441 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05004442 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004443 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004444 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04004445
Chris Masona52d9a82007-08-27 16:49:44 -04004446again:
Chris Mason890871b2009-09-02 16:24:52 -04004447 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004448 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04004449 if (em)
4450 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04004451 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004452
Chris Masona52d9a82007-08-27 16:49:44 -04004453 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04004454 if (em->start > start || em->start + em->len <= start)
4455 free_extent_map(em);
4456 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05004457 free_extent_map(em);
4458 else
4459 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004460 }
Chris Masond1310b22008-01-24 16:13:08 -05004461 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004462 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05004463 err = -ENOMEM;
4464 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004465 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004466 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05004467 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05004468 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05004469 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04004470 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04004471
4472 if (!path) {
4473 path = btrfs_alloc_path();
4474 BUG_ON(!path);
4475 }
4476
Chris Mason179e29e2007-11-01 11:28:41 -04004477 ret = btrfs_lookup_file_extent(trans, root, path,
4478 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04004479 if (ret < 0) {
4480 err = ret;
4481 goto out;
4482 }
4483
4484 if (ret != 0) {
4485 if (path->slots[0] == 0)
4486 goto not_found;
4487 path->slots[0]--;
4488 }
4489
Chris Mason5f39d392007-10-15 16:14:19 -04004490 leaf = path->nodes[0];
4491 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04004492 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04004493 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04004494 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4495 found_type = btrfs_key_type(&found_key);
4496 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04004497 found_type != BTRFS_EXTENT_DATA_KEY) {
4498 goto not_found;
4499 }
4500
Chris Mason5f39d392007-10-15 16:14:19 -04004501 found_type = btrfs_file_extent_type(leaf, item);
4502 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004503 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04004504 if (found_type == BTRFS_FILE_EXTENT_REG ||
4505 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04004506 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04004507 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04004508 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
4509 size_t size;
4510 size = btrfs_file_extent_inline_len(leaf, item);
4511 extent_end = (extent_start + size + root->sectorsize - 1) &
4512 ~((u64)root->sectorsize - 1);
4513 }
4514
4515 if (start >= extent_end) {
4516 path->slots[0]++;
4517 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
4518 ret = btrfs_next_leaf(root, path);
4519 if (ret < 0) {
4520 err = ret;
4521 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004522 }
Yan Zheng9036c102008-10-30 14:19:41 -04004523 if (ret > 0)
4524 goto not_found;
4525 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04004526 }
Yan Zheng9036c102008-10-30 14:19:41 -04004527 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4528 if (found_key.objectid != objectid ||
4529 found_key.type != BTRFS_EXTENT_DATA_KEY)
4530 goto not_found;
4531 if (start + len <= found_key.offset)
4532 goto not_found;
4533 em->start = start;
4534 em->len = found_key.offset - start;
4535 goto not_found_em;
4536 }
4537
Yan Zhengd899e052008-10-30 14:25:28 -04004538 if (found_type == BTRFS_FILE_EXTENT_REG ||
4539 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04004540 em->start = extent_start;
4541 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004542 em->orig_start = extent_start -
4543 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04004544 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
4545 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004546 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04004547 goto insert;
4548 }
Chris Masonc8b97812008-10-29 14:49:59 -04004549 if (compressed) {
4550 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4551 em->block_start = bytenr;
4552 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
4553 item);
4554 } else {
4555 bytenr += btrfs_file_extent_offset(leaf, item);
4556 em->block_start = bytenr;
4557 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04004558 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
4559 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04004560 }
Chris Masona52d9a82007-08-27 16:49:44 -04004561 goto insert;
4562 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004563 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04004564 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04004565 size_t size;
4566 size_t extent_offset;
4567 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04004568
Chris Masona52d9a82007-08-27 16:49:44 -04004569 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004570 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04004571 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04004572 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04004573 goto out;
4574 }
4575
Yan Zheng9036c102008-10-30 14:19:41 -04004576 size = btrfs_file_extent_inline_len(leaf, item);
4577 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05004578 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04004579 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04004580 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05004581 em->len = (copy_size + root->sectorsize - 1) &
4582 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004583 em->orig_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004584 if (compressed)
4585 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04004586 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04004587 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04004588 if (btrfs_file_extent_compression(leaf, item) ==
4589 BTRFS_COMPRESS_ZLIB) {
4590 ret = uncompress_inline(path, inode, page,
4591 pg_offset,
4592 extent_offset, item);
4593 BUG_ON(ret);
4594 } else {
4595 map = kmap(page);
4596 read_extent_buffer(leaf, map + pg_offset, ptr,
4597 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04004598 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
4599 memset(map + pg_offset + copy_size, 0,
4600 PAGE_CACHE_SIZE - pg_offset -
4601 copy_size);
4602 }
Chris Masonc8b97812008-10-29 14:49:59 -04004603 kunmap(page);
4604 }
Chris Mason179e29e2007-11-01 11:28:41 -04004605 flush_dcache_page(page);
4606 } else if (create && PageUptodate(page)) {
4607 if (!trans) {
4608 kunmap(page);
4609 free_extent_map(em);
4610 em = NULL;
4611 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04004612 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004613 goto again;
4614 }
Chris Masonc8b97812008-10-29 14:49:59 -04004615 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05004616 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04004617 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004618 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04004619 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04004620 }
Chris Masond1310b22008-01-24 16:13:08 -05004621 set_extent_uptodate(io_tree, em->start,
4622 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004623 goto insert;
4624 } else {
Chris Masond3977122009-01-05 21:25:51 -05004625 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04004626 WARN_ON(1);
4627 }
4628not_found:
4629 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05004630 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04004631not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04004632 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04004633 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004634insert:
4635 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05004636 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05004637 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
4638 "[%llu %llu]\n", (unsigned long long)em->start,
4639 (unsigned long long)em->len,
4640 (unsigned long long)start,
4641 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04004642 err = -EIO;
4643 goto out;
4644 }
Chris Masond1310b22008-01-24 16:13:08 -05004645
4646 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04004647 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004648 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004649 /* it is possible that someone inserted the extent into the tree
4650 * while we had the lock dropped. It is also possible that
4651 * an overlapping map exists in the tree
4652 */
Chris Masona52d9a82007-08-27 16:49:44 -04004653 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04004654 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004655
4656 ret = 0;
4657
Chris Mason3b951512008-04-17 11:29:12 -04004658 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04004659 if (existing && (existing->start > start ||
4660 existing->start + existing->len <= start)) {
4661 free_extent_map(existing);
4662 existing = NULL;
4663 }
Chris Mason3b951512008-04-17 11:29:12 -04004664 if (!existing) {
4665 existing = lookup_extent_mapping(em_tree, em->start,
4666 em->len);
4667 if (existing) {
4668 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004669 em, start,
4670 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04004671 free_extent_map(existing);
4672 if (err) {
4673 free_extent_map(em);
4674 em = NULL;
4675 }
4676 } else {
4677 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04004678 free_extent_map(em);
4679 em = NULL;
4680 }
4681 } else {
4682 free_extent_map(em);
4683 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004684 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04004685 }
Chris Masona52d9a82007-08-27 16:49:44 -04004686 }
Chris Mason890871b2009-09-02 16:24:52 -04004687 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004688out:
Chris Masonf4219502008-07-22 11:18:09 -04004689 if (path)
4690 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04004691 if (trans) {
4692 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05004693 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04004694 err = ret;
4695 }
Chris Masona52d9a82007-08-27 16:49:44 -04004696 if (err) {
4697 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04004698 return ERR_PTR(err);
4699 }
4700 return em;
4701}
4702
Chris Mason16432982008-04-10 10:23:21 -04004703static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4704 const struct iovec *iov, loff_t offset,
4705 unsigned long nr_segs)
4706{
Chris Masone1c4b742008-04-22 13:26:46 -04004707 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04004708}
4709
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004710static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4711 __u64 start, __u64 len)
4712{
4713 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
4714}
4715
Chris Mason9ebefb182007-06-15 13:50:00 -04004716int btrfs_readpage(struct file *file, struct page *page)
4717{
Chris Masond1310b22008-01-24 16:13:08 -05004718 struct extent_io_tree *tree;
4719 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004720 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04004721}
Chris Mason1832a6d2007-12-21 16:27:21 -05004722
Chris Mason39279cc2007-06-12 06:35:45 -04004723static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
4724{
Chris Masond1310b22008-01-24 16:13:08 -05004725 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04004726
4727
4728 if (current->flags & PF_MEMALLOC) {
4729 redirty_page_for_writepage(wbc, page);
4730 unlock_page(page);
4731 return 0;
4732 }
Chris Masond1310b22008-01-24 16:13:08 -05004733 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004734 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
4735}
Chris Mason39279cc2007-06-12 06:35:45 -04004736
Chris Masonf4219502008-07-22 11:18:09 -04004737int btrfs_writepages(struct address_space *mapping,
4738 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04004739{
Chris Masond1310b22008-01-24 16:13:08 -05004740 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05004741
Chris Masond1310b22008-01-24 16:13:08 -05004742 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04004743 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
4744}
4745
Chris Mason3ab2fb52007-11-08 10:59:22 -05004746static int
4747btrfs_readpages(struct file *file, struct address_space *mapping,
4748 struct list_head *pages, unsigned nr_pages)
4749{
Chris Masond1310b22008-01-24 16:13:08 -05004750 struct extent_io_tree *tree;
4751 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05004752 return extent_readpages(tree, mapping, pages, nr_pages,
4753 btrfs_get_extent);
4754}
Chris Masone6dcd2d2008-07-17 12:53:50 -04004755static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04004756{
Chris Masond1310b22008-01-24 16:13:08 -05004757 struct extent_io_tree *tree;
4758 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04004759 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004760
Chris Masond1310b22008-01-24 16:13:08 -05004761 tree = &BTRFS_I(page->mapping->host)->io_tree;
4762 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05004763 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004764 if (ret == 1) {
4765 ClearPagePrivate(page);
4766 set_page_private(page, 0);
4767 page_cache_release(page);
4768 }
4769 return ret;
4770}
Chris Mason39279cc2007-06-12 06:35:45 -04004771
Chris Masone6dcd2d2008-07-17 12:53:50 -04004772static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4773{
Chris Mason98509cf2008-09-11 15:51:43 -04004774 if (PageWriteback(page) || PageDirty(page))
4775 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05004776 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004777}
4778
Chris Masona52d9a82007-08-27 16:49:44 -04004779static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4780{
Chris Masond1310b22008-01-24 16:13:08 -05004781 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004782 struct btrfs_ordered_extent *ordered;
4783 u64 page_start = page_offset(page);
4784 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004785
Chris Mason8b62b722009-09-02 16:53:46 -04004786
4787 /*
4788 * we have the page locked, so new writeback can't start,
4789 * and the dirty bit won't be cleared while we are here.
4790 *
4791 * Wait for IO on this page so that we can safely clear
4792 * the PagePrivate2 bit and do ordered accounting
4793 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004794 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04004795
Chris Masond1310b22008-01-24 16:13:08 -05004796 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004797 if (offset) {
4798 btrfs_releasepage(page, GFP_NOFS);
4799 return;
4800 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004801 lock_extent(tree, page_start, page_end, GFP_NOFS);
4802 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
4803 page_offset(page));
4804 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04004805 /*
4806 * IO on this page will never be started, so we need
4807 * to account for any ordered extents now
4808 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004809 clear_extent_bit(tree, page_start, page_end,
4810 EXTENT_DIRTY | EXTENT_DELALLOC |
Chris Mason2c64c532009-09-02 15:04:12 -04004811 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04004812 /*
4813 * whoever cleared the private bit is responsible
4814 * for the finish_ordered_io
4815 */
4816 if (TestClearPagePrivate2(page)) {
4817 btrfs_finish_ordered_io(page->mapping->host,
4818 page_start, page_end);
4819 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004820 btrfs_put_ordered_extent(ordered);
4821 lock_extent(tree, page_start, page_end, GFP_NOFS);
4822 }
4823 clear_extent_bit(tree, page_start, page_end,
Chris Mason8b62b722009-09-02 16:53:46 -04004824 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
Chris Mason2c64c532009-09-02 15:04:12 -04004825 1, 1, NULL, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004826 __btrfs_releasepage(page, GFP_NOFS);
4827
Chris Mason4a096752008-07-21 10:29:44 -04004828 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04004829 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04004830 ClearPagePrivate(page);
4831 set_page_private(page, 0);
4832 page_cache_release(page);
4833 }
Chris Mason39279cc2007-06-12 06:35:45 -04004834}
4835
Chris Mason9ebefb182007-06-15 13:50:00 -04004836/*
4837 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
4838 * called from a page fault handler when a page is first dirtied. Hence we must
4839 * be careful to check for EOF conditions here. We set the page up correctly
4840 * for a written page which means we get ENOSPC checking when writing into
4841 * holes and correct delalloc and unwritten extent mapping on filesystems that
4842 * support these features.
4843 *
4844 * We are not allowed to take the i_mutex here so we have to play games to
4845 * protect against truncate races as the page could now be beyond EOF. Because
4846 * vmtruncate() writes the inode size before removing pages, once we have the
4847 * page lock we can determine safely if the page is beyond EOF. If it is not
4848 * beyond EOF, then the page is guaranteed safe against truncation until we
4849 * unlock the page.
4850 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07004851int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04004852{
Nick Pigginc2ec1752009-03-31 15:23:21 -07004853 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05004854 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004855 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004856 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4857 struct btrfs_ordered_extent *ordered;
4858 char *kaddr;
4859 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04004860 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05004861 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04004862 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004863 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04004864
Josef Bacik6a632092009-02-20 11:00:09 -05004865 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07004866 if (ret) {
4867 if (ret == -ENOMEM)
4868 ret = VM_FAULT_OOM;
4869 else /* -ENOSPC, -EIO, etc */
4870 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05004871 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07004872 }
Chris Mason1832a6d2007-12-21 16:27:21 -05004873
Josef Bacik9ed74f22009-09-11 16:12:44 -04004874 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
4875 if (ret) {
4876 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
4877 ret = VM_FAULT_SIGBUS;
4878 goto out;
4879 }
4880
Nick Piggin56a76f82009-03-31 15:23:23 -07004881 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004882again:
Chris Mason9ebefb182007-06-15 13:50:00 -04004883 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04004884 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004885 page_start = page_offset(page);
4886 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004887
Chris Mason9ebefb182007-06-15 13:50:00 -04004888 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04004889 (page_start >= size)) {
Josef Bacik6a632092009-02-20 11:00:09 -05004890 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
Chris Mason9ebefb182007-06-15 13:50:00 -04004891 /* page got truncated out from underneath us */
4892 goto out_unlock;
4893 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004894 wait_on_page_writeback(page);
4895
4896 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
4897 set_page_extent_mapped(page);
4898
Chris Masoneb84ae02008-07-17 13:53:27 -04004899 /*
4900 * we can't set the delalloc bits if there are pending ordered
4901 * extents. Drop our locks and wait for them to finish
4902 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004903 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4904 if (ordered) {
4905 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4906 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004907 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004908 btrfs_put_ordered_extent(ordered);
4909 goto again;
4910 }
4911
Josef Bacik9ed74f22009-09-11 16:12:44 -04004912 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
4913 if (ret) {
4914 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4915 ret = VM_FAULT_SIGBUS;
4916 goto out_unlock;
4917 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004918 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04004919
4920 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04004921 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004922 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04004923 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04004924 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04004925
Chris Masone6dcd2d2008-07-17 12:53:50 -04004926 if (zero_start != PAGE_CACHE_SIZE) {
4927 kaddr = kmap(page);
4928 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
4929 flush_dcache_page(page);
4930 kunmap(page);
4931 }
Chris Mason247e7432008-07-17 12:53:51 -04004932 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004933 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04004934 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04004935
4936 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004937 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04004938
4939out_unlock:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004940 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Mason50a9b212009-09-11 12:33:12 -04004941 if (!ret)
4942 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04004943 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05004944out:
Chris Mason9ebefb182007-06-15 13:50:00 -04004945 return ret;
4946}
4947
Chris Mason39279cc2007-06-12 06:35:45 -04004948static void btrfs_truncate(struct inode *inode)
4949{
4950 struct btrfs_root *root = BTRFS_I(inode)->root;
4951 int ret;
4952 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004953 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04004954 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004955
4956 if (!S_ISREG(inode->i_mode))
4957 return;
4958 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4959 return;
4960
4961 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04004962 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04004963
Chris Mason39279cc2007-06-12 06:35:45 -04004964 trans = btrfs_start_transaction(root, 1);
Chris Mason5a3f23d2009-03-31 13:27:11 -04004965
4966 /*
4967 * setattr is responsible for setting the ordered_data_close flag,
4968 * but that is only tested during the last file release. That
4969 * could happen well after the next commit, leaving a great big
4970 * window where new writes may get lost if someone chooses to write
4971 * to this file after truncating to zero
4972 *
4973 * The inode doesn't have any dirty data here, and so if we commit
4974 * this is a noop. If someone immediately starts writing to the inode
4975 * it is very likely we'll catch some of their writes in this
4976 * transaction, and the commit will find this file on the ordered
4977 * data list with good things to send down.
4978 *
4979 * This is a best effort solution, there is still a window where
4980 * using truncate to replace the contents of the file will
4981 * end up with a zero length file after a crash.
4982 */
4983 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
4984 btrfs_add_ordered_operation(trans, root, inode);
4985
Chris Mason39279cc2007-06-12 06:35:45 -04004986 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04004987 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004988
Josef Bacik7b128762008-07-24 12:17:14 -04004989 ret = btrfs_orphan_add(trans, inode);
4990 if (ret)
4991 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004992 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Masone02119d2008-09-05 16:13:11 -04004993 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
Chris Mason85e21ba2008-01-29 15:11:36 -05004994 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004995 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004996
Josef Bacik7b128762008-07-24 12:17:14 -04004997 ret = btrfs_orphan_del(trans, inode);
4998 BUG_ON(ret);
4999
5000out:
5001 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04005002 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005003 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005004 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005005}
5006
Sven Wegener3b963622008-06-09 21:57:42 -04005007/*
Chris Masond352ac62008-09-29 15:18:18 -04005008 * create a new subvolume directory/inode (helper for the ioctl).
5009 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05005010int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Yan, Zheng76dda932009-09-21 16:00:26 -04005011 struct btrfs_root *new_root,
Yan Zhengd2fb3432008-12-11 16:30:39 -05005012 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04005013{
Chris Mason39279cc2007-06-12 06:35:45 -04005014 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005015 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005016 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005017
Josef Bacikaec74772008-07-24 12:12:38 -04005018 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05005019 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04005020 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005021 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005022 inode->i_op = &btrfs_dir_inode_operations;
5023 inode->i_fop = &btrfs_dir_file_operations;
5024
Chris Mason39279cc2007-06-12 06:35:45 -04005025 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04005026 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04005027
Yan, Zheng76dda932009-09-21 16:00:26 -04005028 err = btrfs_update_inode(trans, new_root, inode);
5029 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04005030
Yan, Zheng76dda932009-09-21 16:00:26 -04005031 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04005032 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005033}
5034
Chris Masond352ac62008-09-29 15:18:18 -04005035/* helper function for file defrag and space balancing. This
5036 * forces readahead on a given range of bytes in an inode
5037 */
Chris Masonedbd8d42007-12-21 16:27:24 -05005038unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04005039 struct file_ra_state *ra, struct file *file,
5040 pgoff_t offset, pgoff_t last_index)
5041{
Chris Mason8e7bf942008-04-28 09:02:36 -04005042 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04005043
Chris Mason86479a02007-09-10 19:58:16 -04005044 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
5045 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04005046}
5047
Chris Mason39279cc2007-06-12 06:35:45 -04005048struct inode *btrfs_alloc_inode(struct super_block *sb)
5049{
5050 struct btrfs_inode *ei;
5051
5052 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
5053 if (!ei)
5054 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04005055 ei->last_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04005056 ei->logged_trans = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005057 ei->delalloc_extents = 0;
5058 ei->delalloc_reserved_extents = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005059 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04005060 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005061 INIT_LIST_HEAD(&ei->ordered_operations);
Chris Mason39279cc2007-06-12 06:35:45 -04005062 return &ei->vfs_inode;
5063}
5064
5065void btrfs_destroy_inode(struct inode *inode)
5066{
Chris Masone6dcd2d2008-07-17 12:53:50 -04005067 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005068 struct btrfs_root *root = BTRFS_I(inode)->root;
5069
Chris Mason39279cc2007-06-12 06:35:45 -04005070 WARN_ON(!list_empty(&inode->i_dentry));
5071 WARN_ON(inode->i_data.nrpages);
5072
Chris Mason5a3f23d2009-03-31 13:27:11 -04005073 /*
5074 * Make sure we're properly removed from the ordered operation
5075 * lists.
5076 */
5077 smp_mb();
5078 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
5079 spin_lock(&root->fs_info->ordered_extent_lock);
5080 list_del_init(&BTRFS_I(inode)->ordered_operations);
5081 spin_unlock(&root->fs_info->ordered_extent_lock);
5082 }
5083
5084 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04005085 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
5086 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
5087 " list\n", inode->i_ino);
5088 dump_stack();
5089 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04005090 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04005091
Chris Masond3977122009-01-05 21:25:51 -05005092 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005093 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
5094 if (!ordered)
5095 break;
5096 else {
Chris Masond3977122009-01-05 21:25:51 -05005097 printk(KERN_ERR "btrfs found ordered "
5098 "extent %llu %llu on inode cleanup\n",
5099 (unsigned long long)ordered->file_offset,
5100 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005101 btrfs_remove_ordered_extent(inode, ordered);
5102 btrfs_put_ordered_extent(ordered);
5103 btrfs_put_ordered_extent(ordered);
5104 }
5105 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005106 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005107 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005108 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5109}
5110
Yan, Zheng76dda932009-09-21 16:00:26 -04005111void btrfs_drop_inode(struct inode *inode)
5112{
5113 struct btrfs_root *root = BTRFS_I(inode)->root;
5114
5115 if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
5116 generic_delete_inode(inode);
5117 else
5118 generic_drop_inode(inode);
5119}
5120
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005121static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04005122{
5123 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
5124
5125 inode_init_once(&ei->vfs_inode);
5126}
5127
5128void btrfs_destroy_cachep(void)
5129{
5130 if (btrfs_inode_cachep)
5131 kmem_cache_destroy(btrfs_inode_cachep);
5132 if (btrfs_trans_handle_cachep)
5133 kmem_cache_destroy(btrfs_trans_handle_cachep);
5134 if (btrfs_transaction_cachep)
5135 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04005136 if (btrfs_path_cachep)
5137 kmem_cache_destroy(btrfs_path_cachep);
5138}
5139
5140int btrfs_init_cachep(void)
5141{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005142 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
5143 sizeof(struct btrfs_inode), 0,
5144 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04005145 if (!btrfs_inode_cachep)
5146 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005147
5148 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
5149 sizeof(struct btrfs_trans_handle), 0,
5150 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005151 if (!btrfs_trans_handle_cachep)
5152 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005153
5154 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
5155 sizeof(struct btrfs_transaction), 0,
5156 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005157 if (!btrfs_transaction_cachep)
5158 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005159
5160 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
5161 sizeof(struct btrfs_path), 0,
5162 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005163 if (!btrfs_path_cachep)
5164 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005165
Chris Mason39279cc2007-06-12 06:35:45 -04005166 return 0;
5167fail:
5168 btrfs_destroy_cachep();
5169 return -ENOMEM;
5170}
5171
5172static int btrfs_getattr(struct vfsmount *mnt,
5173 struct dentry *dentry, struct kstat *stat)
5174{
5175 struct inode *inode = dentry->d_inode;
5176 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05005177 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05005178 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005179 stat->blocks = (inode_get_bytes(inode) +
5180 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04005181 return 0;
5182}
5183
Chris Masond3977122009-01-05 21:25:51 -05005184static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
5185 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005186{
5187 struct btrfs_trans_handle *trans;
5188 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005189 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005190 struct inode *new_inode = new_dentry->d_inode;
5191 struct inode *old_inode = old_dentry->d_inode;
5192 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005193 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005194 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005195 int ret;
5196
Yan, Zhengf679a842009-09-24 09:17:31 -04005197 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5198 return -EPERM;
5199
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005200 /* we only allow rename subvolume link between subvolumes */
5201 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05005202 return -EXDEV;
5203
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005204 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
5205 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
5206 return -ENOTEMPTY;
5207
Chris Mason39279cc2007-06-12 06:35:45 -04005208 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005209 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04005210 return -ENOTEMPTY;
Chris Mason0660b5a2008-11-17 20:37:39 -05005211
Josef Bacik9ed74f22009-09-11 16:12:44 -04005212 /*
5213 * 2 items for dir items
5214 * 1 item for orphan entry
5215 * 1 item for ref
5216 */
5217 ret = btrfs_reserve_metadata_space(root, 4);
Chris Mason1832a6d2007-12-21 16:27:21 -05005218 if (ret)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005219 return ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05005220
Chris Mason5a3f23d2009-03-31 13:27:11 -04005221 /*
5222 * we're using rename to replace one file with another.
5223 * and the replacement file is large. Start IO on it now so
5224 * we don't add too much work to the end of the transaction
5225 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04005226 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04005227 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
5228 filemap_flush(old_inode->i_mapping);
5229
Yan, Zheng76dda932009-09-21 16:00:26 -04005230 /* close the racy window with snapshot create/destroy ioctl */
5231 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5232 down_read(&root->fs_info->subvol_sem);
5233
Chris Mason39279cc2007-06-12 06:35:45 -04005234 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga5719522009-09-24 09:17:31 -04005235 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04005236
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005237 if (dest != root)
5238 btrfs_record_root_in_trans(trans, dest);
5239
Yan, Zhenga5719522009-09-24 09:17:31 -04005240 ret = btrfs_set_inode_index(new_dir, &index);
5241 if (ret)
5242 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005243
Yan, Zhenga5719522009-09-24 09:17:31 -04005244 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005245 /* force full log commit if subvolume involved. */
5246 root->fs_info->last_trans_log_full_commit = trans->transid;
5247 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04005248 ret = btrfs_insert_inode_ref(trans, dest,
5249 new_dentry->d_name.name,
5250 new_dentry->d_name.len,
5251 old_inode->i_ino,
5252 new_dir->i_ino, index);
5253 if (ret)
5254 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005255 /*
5256 * this is an ugly little race, but the rename is required
5257 * to make sure that if we crash, the inode is either at the
5258 * old name or the new one. pinning the log transaction lets
5259 * us make sure we don't allow a log commit to come in after
5260 * we unlink the name but before we add the new name back in.
5261 */
5262 btrfs_pin_log_trans(root);
5263 }
Yan, Zhenga5719522009-09-24 09:17:31 -04005264 /*
5265 * make sure the inode gets flushed if it is replacing
5266 * something.
5267 */
5268 if (new_inode && new_inode->i_size &&
5269 old_inode && S_ISREG(old_inode->i_mode)) {
5270 btrfs_add_ordered_operation(trans, root, old_inode);
5271 }
Chris Mason39279cc2007-06-12 06:35:45 -04005272
Chris Mason39279cc2007-06-12 06:35:45 -04005273 old_dir->i_ctime = old_dir->i_mtime = ctime;
5274 new_dir->i_ctime = new_dir->i_mtime = ctime;
5275 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04005276
Chris Mason12fcfd22009-03-24 10:24:20 -04005277 if (old_dentry->d_parent != new_dentry->d_parent)
5278 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
5279
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005280 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
5281 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
5282 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
5283 old_dentry->d_name.name,
5284 old_dentry->d_name.len);
5285 } else {
5286 btrfs_inc_nlink(old_dentry->d_inode);
5287 ret = btrfs_unlink_inode(trans, root, old_dir,
5288 old_dentry->d_inode,
5289 old_dentry->d_name.name,
5290 old_dentry->d_name.len);
5291 }
5292 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005293
5294 if (new_inode) {
5295 new_inode->i_ctime = CURRENT_TIME;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005296 if (unlikely(new_inode->i_ino ==
5297 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
5298 root_objectid = BTRFS_I(new_inode)->location.objectid;
5299 ret = btrfs_unlink_subvol(trans, dest, new_dir,
5300 root_objectid,
5301 new_dentry->d_name.name,
5302 new_dentry->d_name.len);
5303 BUG_ON(new_inode->i_nlink == 0);
5304 } else {
5305 ret = btrfs_unlink_inode(trans, dest, new_dir,
5306 new_dentry->d_inode,
5307 new_dentry->d_name.name,
5308 new_dentry->d_name.len);
5309 }
5310 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005311 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04005312 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005313 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005314 }
Chris Mason39279cc2007-06-12 06:35:45 -04005315 }
Josef Bacikaec74772008-07-24 12:12:38 -04005316
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005317 ret = btrfs_add_link(trans, new_dir, old_inode,
5318 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04005319 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005320 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005321
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005322 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
5323 btrfs_log_new_name(trans, old_inode, old_dir,
5324 new_dentry->d_parent);
5325 btrfs_end_log_trans(root);
5326 }
Yan, Zhenga5719522009-09-24 09:17:31 -04005327out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04005328 btrfs_end_transaction_throttle(trans, root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005329
Yan, Zheng76dda932009-09-21 16:00:26 -04005330 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5331 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005332
5333 btrfs_unreserve_metadata_space(root, 4);
Chris Mason39279cc2007-06-12 06:35:45 -04005334 return ret;
5335}
5336
Chris Masond352ac62008-09-29 15:18:18 -04005337/*
5338 * some fairly slow code that needs optimization. This walks the list
5339 * of all the inodes with pending delalloc and forces them to disk.
5340 */
Chris Masonea8c2812008-08-04 23:17:27 -04005341int btrfs_start_delalloc_inodes(struct btrfs_root *root)
5342{
5343 struct list_head *head = &root->fs_info->delalloc_inodes;
5344 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005345 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04005346
Yan Zhengc146afa2008-11-12 14:34:12 -05005347 if (root->fs_info->sb->s_flags & MS_RDONLY)
5348 return -EROFS;
5349
Chris Mason75eff682008-12-15 15:54:40 -05005350 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05005351 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04005352 binode = list_entry(head->next, struct btrfs_inode,
5353 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005354 inode = igrab(&binode->vfs_inode);
5355 if (!inode)
5356 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05005357 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005358 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005359 filemap_flush(inode->i_mapping);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005360 iput(inode);
5361 }
5362 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05005363 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04005364 }
Chris Mason75eff682008-12-15 15:54:40 -05005365 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04005366
5367 /* the filemap_flush will queue IO into the worker threads, but
5368 * we have to make sure the IO is actually started and that
5369 * ordered extents get created before we return
5370 */
5371 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05005372 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05005373 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005374 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05005375 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
5376 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04005377 }
5378 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04005379 return 0;
5380}
5381
Chris Mason39279cc2007-06-12 06:35:45 -04005382static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
5383 const char *symname)
5384{
5385 struct btrfs_trans_handle *trans;
5386 struct btrfs_root *root = BTRFS_I(dir)->root;
5387 struct btrfs_path *path;
5388 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05005389 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005390 int err;
5391 int drop_inode = 0;
5392 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005393 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04005394 int name_len;
5395 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04005396 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005397 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04005398 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05005399 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005400
5401 name_len = strlen(symname) + 1;
5402 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
5403 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05005404
Josef Bacik9ed74f22009-09-11 16:12:44 -04005405 /*
5406 * 2 items for inode item and ref
5407 * 2 items for dir items
5408 * 1 item for xattr if selinux is on
5409 */
5410 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05005411 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04005412 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05005413
Chris Mason39279cc2007-06-12 06:35:45 -04005414 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005415 if (!trans)
5416 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04005417 btrfs_set_trans_block_group(trans, dir);
5418
5419 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
5420 if (err) {
5421 err = -ENOSPC;
5422 goto out_unlock;
5423 }
5424
Josef Bacikaec74772008-07-24 12:12:38 -04005425 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05005426 dentry->d_name.len,
5427 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04005428 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
5429 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005430 err = PTR_ERR(inode);
5431 if (IS_ERR(inode))
5432 goto out_unlock;
5433
Jim Owens0279b4c2009-02-04 09:29:13 -05005434 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04005435 if (err) {
5436 drop_inode = 1;
5437 goto out_unlock;
5438 }
5439
Chris Mason39279cc2007-06-12 06:35:45 -04005440 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005441 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005442 if (err)
5443 drop_inode = 1;
5444 else {
5445 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04005446 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04005447 inode->i_fop = &btrfs_file_operations;
5448 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05005449 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04005450 }
Chris Mason39279cc2007-06-12 06:35:45 -04005451 btrfs_update_inode_block_group(trans, inode);
5452 btrfs_update_inode_block_group(trans, dir);
5453 if (drop_inode)
5454 goto out_unlock;
5455
5456 path = btrfs_alloc_path();
5457 BUG_ON(!path);
5458 key.objectid = inode->i_ino;
5459 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005460 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
5461 datasize = btrfs_file_extent_calc_inline_size(name_len);
5462 err = btrfs_insert_empty_item(trans, root, path, &key,
5463 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005464 if (err) {
5465 drop_inode = 1;
5466 goto out_unlock;
5467 }
Chris Mason5f39d392007-10-15 16:14:19 -04005468 leaf = path->nodes[0];
5469 ei = btrfs_item_ptr(leaf, path->slots[0],
5470 struct btrfs_file_extent_item);
5471 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
5472 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04005473 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04005474 btrfs_set_file_extent_encryption(leaf, ei, 0);
5475 btrfs_set_file_extent_compression(leaf, ei, 0);
5476 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
5477 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
5478
Chris Mason39279cc2007-06-12 06:35:45 -04005479 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04005480 write_extent_buffer(leaf, symname, ptr, name_len);
5481 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04005482 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04005483
Chris Mason39279cc2007-06-12 06:35:45 -04005484 inode->i_op = &btrfs_symlink_inode_operations;
5485 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04005486 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04005487 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04005488 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005489 err = btrfs_update_inode(trans, root, inode);
5490 if (err)
5491 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005492
5493out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005494 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04005495 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005496out_fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04005497 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04005498 if (drop_inode) {
5499 inode_dec_link_count(inode);
5500 iput(inode);
5501 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005502 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005503 return err;
5504}
Chris Mason16432982008-04-10 10:23:21 -04005505
Chris Mason546888d2009-04-21 11:53:38 -04005506static int prealloc_file_range(struct btrfs_trans_handle *trans,
5507 struct inode *inode, u64 start, u64 end,
Chris Masone980b502009-04-24 14:39:24 -04005508 u64 locked_end, u64 alloc_hint, int mode)
Yan Zhengd899e052008-10-30 14:25:28 -04005509{
Yan Zhengd899e052008-10-30 14:25:28 -04005510 struct btrfs_root *root = BTRFS_I(inode)->root;
5511 struct btrfs_key ins;
5512 u64 alloc_size;
5513 u64 cur_offset = start;
5514 u64 num_bytes = end - start;
5515 int ret = 0;
5516
Yan Zhengd899e052008-10-30 14:25:28 -04005517 while (num_bytes > 0) {
5518 alloc_size = min(num_bytes, root->fs_info->max_extent);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005519
5520 ret = btrfs_reserve_metadata_space(root, 1);
5521 if (ret)
5522 goto out;
5523
Yan Zhengd899e052008-10-30 14:25:28 -04005524 ret = btrfs_reserve_extent(trans, root, alloc_size,
5525 root->sectorsize, 0, alloc_hint,
5526 (u64)-1, &ins, 1);
5527 if (ret) {
5528 WARN_ON(1);
5529 goto out;
5530 }
5531 ret = insert_reserved_file_extent(trans, inode,
5532 cur_offset, ins.objectid,
5533 ins.offset, ins.offset,
Chris Masone980b502009-04-24 14:39:24 -04005534 ins.offset, locked_end,
5535 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04005536 BTRFS_FILE_EXTENT_PREALLOC);
5537 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04005538 btrfs_drop_extent_cache(inode, cur_offset,
5539 cur_offset + ins.offset -1, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04005540 num_bytes -= ins.offset;
5541 cur_offset += ins.offset;
5542 alloc_hint = ins.objectid + ins.offset;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005543 btrfs_unreserve_metadata_space(root, 1);
Yan Zhengd899e052008-10-30 14:25:28 -04005544 }
5545out:
5546 if (cur_offset > start) {
5547 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005548 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04005549 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
5550 cur_offset > i_size_read(inode))
5551 btrfs_i_size_write(inode, cur_offset);
5552 ret = btrfs_update_inode(trans, root, inode);
5553 BUG_ON(ret);
5554 }
5555
Yan Zhengd899e052008-10-30 14:25:28 -04005556 return ret;
5557}
5558
5559static long btrfs_fallocate(struct inode *inode, int mode,
5560 loff_t offset, loff_t len)
5561{
5562 u64 cur_offset;
5563 u64 last_byte;
5564 u64 alloc_start;
5565 u64 alloc_end;
5566 u64 alloc_hint = 0;
Chris Masone980b502009-04-24 14:39:24 -04005567 u64 locked_end;
Yan Zhengd899e052008-10-30 14:25:28 -04005568 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
5569 struct extent_map *em;
Chris Mason546888d2009-04-21 11:53:38 -04005570 struct btrfs_trans_handle *trans;
Josef Bacika970b0a2009-06-27 21:07:34 -04005571 struct btrfs_root *root;
Yan Zhengd899e052008-10-30 14:25:28 -04005572 int ret;
5573
5574 alloc_start = offset & ~mask;
5575 alloc_end = (offset + len + mask) & ~mask;
5576
Chris Mason546888d2009-04-21 11:53:38 -04005577 /*
5578 * wait for ordered IO before we have any locks. We'll loop again
5579 * below with the locks held.
5580 */
5581 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
5582
Yan Zhengd899e052008-10-30 14:25:28 -04005583 mutex_lock(&inode->i_mutex);
5584 if (alloc_start > inode->i_size) {
5585 ret = btrfs_cont_expand(inode, alloc_start);
5586 if (ret)
5587 goto out;
5588 }
5589
Josef Bacika970b0a2009-06-27 21:07:34 -04005590 root = BTRFS_I(inode)->root;
5591
5592 ret = btrfs_check_data_free_space(root, inode,
5593 alloc_end - alloc_start);
5594 if (ret)
5595 goto out;
5596
Chris Masone980b502009-04-24 14:39:24 -04005597 locked_end = alloc_end - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04005598 while (1) {
5599 struct btrfs_ordered_extent *ordered;
Chris Mason546888d2009-04-21 11:53:38 -04005600
5601 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
5602 if (!trans) {
5603 ret = -EIO;
Josef Bacika970b0a2009-06-27 21:07:34 -04005604 goto out_free;
Chris Mason546888d2009-04-21 11:53:38 -04005605 }
5606
5607 /* the extent lock is ordered inside the running
5608 * transaction
5609 */
Chris Masone980b502009-04-24 14:39:24 -04005610 lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
5611 GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04005612 ordered = btrfs_lookup_first_ordered_extent(inode,
5613 alloc_end - 1);
5614 if (ordered &&
5615 ordered->file_offset + ordered->len > alloc_start &&
5616 ordered->file_offset < alloc_end) {
5617 btrfs_put_ordered_extent(ordered);
5618 unlock_extent(&BTRFS_I(inode)->io_tree,
Chris Masone980b502009-04-24 14:39:24 -04005619 alloc_start, locked_end, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04005620 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
5621
5622 /*
5623 * we can't wait on the range with the transaction
5624 * running or with the extent lock held
5625 */
Yan Zhengd899e052008-10-30 14:25:28 -04005626 btrfs_wait_ordered_range(inode, alloc_start,
5627 alloc_end - alloc_start);
5628 } else {
5629 if (ordered)
5630 btrfs_put_ordered_extent(ordered);
5631 break;
5632 }
5633 }
5634
5635 cur_offset = alloc_start;
5636 while (1) {
5637 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
5638 alloc_end - cur_offset, 0);
5639 BUG_ON(IS_ERR(em) || !em);
5640 last_byte = min(extent_map_end(em), alloc_end);
5641 last_byte = (last_byte + mask) & ~mask;
5642 if (em->block_start == EXTENT_MAP_HOLE) {
Chris Mason546888d2009-04-21 11:53:38 -04005643 ret = prealloc_file_range(trans, inode, cur_offset,
Chris Masone980b502009-04-24 14:39:24 -04005644 last_byte, locked_end + 1,
5645 alloc_hint, mode);
Yan Zhengd899e052008-10-30 14:25:28 -04005646 if (ret < 0) {
5647 free_extent_map(em);
5648 break;
5649 }
5650 }
5651 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
5652 alloc_hint = em->block_start;
5653 free_extent_map(em);
5654
5655 cur_offset = last_byte;
5656 if (cur_offset >= alloc_end) {
5657 ret = 0;
5658 break;
5659 }
5660 }
Chris Masone980b502009-04-24 14:39:24 -04005661 unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
Yan Zhengd899e052008-10-30 14:25:28 -04005662 GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04005663
5664 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
Josef Bacika970b0a2009-06-27 21:07:34 -04005665out_free:
5666 btrfs_free_reserved_data_space(root, inode, alloc_end - alloc_start);
Yan Zhengd899e052008-10-30 14:25:28 -04005667out:
5668 mutex_unlock(&inode->i_mutex);
5669 return ret;
5670}
5671
Chris Masone6dcd2d2008-07-17 12:53:50 -04005672static int btrfs_set_page_dirty(struct page *page)
5673{
Chris Masone6dcd2d2008-07-17 12:53:50 -04005674 return __set_page_dirty_nobuffers(page);
5675}
5676
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005677static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05005678{
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005679 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05005680 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04005681 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05005682}
Chris Mason39279cc2007-06-12 06:35:45 -04005683
5684static struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05005685 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04005686 .lookup = btrfs_lookup,
5687 .create = btrfs_create,
5688 .unlink = btrfs_unlink,
5689 .link = btrfs_link,
5690 .mkdir = btrfs_mkdir,
5691 .rmdir = btrfs_rmdir,
5692 .rename = btrfs_rename,
5693 .symlink = btrfs_symlink,
5694 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04005695 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005696 .setxattr = btrfs_setxattr,
5697 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05005698 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005699 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005700 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005701};
Chris Mason39279cc2007-06-12 06:35:45 -04005702static struct inode_operations btrfs_dir_ro_inode_operations = {
5703 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05005704 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005705};
Yan, Zheng76dda932009-09-21 16:00:26 -04005706
Chris Mason39279cc2007-06-12 06:35:45 -04005707static struct file_operations btrfs_dir_file_operations = {
5708 .llseek = generic_file_llseek,
5709 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01005710 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04005711 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04005712#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04005713 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04005714#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04005715 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04005716 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04005717};
5718
Chris Masond1310b22008-01-24 16:13:08 -05005719static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04005720 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05005721 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04005722 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04005723 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005724 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04005725 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04005726 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05005727 .set_bit_hook = btrfs_set_bit_hook,
5728 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005729 .merge_extent_hook = btrfs_merge_extent_hook,
5730 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04005731};
5732
Chris Mason35054392009-01-21 13:11:13 -05005733/*
5734 * btrfs doesn't support the bmap operation because swapfiles
5735 * use bmap to make a mapping of extents in the file. They assume
5736 * these extents won't change over the life of the file and they
5737 * use the bmap result to do IO directly to the drive.
5738 *
5739 * the btrfs bmap call would return logical addresses that aren't
5740 * suitable for IO and they also will change frequently as COW
5741 * operations happen. So, swapfile + btrfs == corruption.
5742 *
5743 * For now we're avoiding this by dropping bmap.
5744 */
Chris Mason39279cc2007-06-12 06:35:45 -04005745static struct address_space_operations btrfs_aops = {
5746 .readpage = btrfs_readpage,
5747 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04005748 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05005749 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04005750 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04005751 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04005752 .invalidatepage = btrfs_invalidatepage,
5753 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005754 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04005755};
5756
5757static struct address_space_operations btrfs_symlink_aops = {
5758 .readpage = btrfs_readpage,
5759 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04005760 .invalidatepage = btrfs_invalidatepage,
5761 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04005762};
5763
5764static struct inode_operations btrfs_file_inode_operations = {
5765 .truncate = btrfs_truncate,
5766 .getattr = btrfs_getattr,
5767 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005768 .setxattr = btrfs_setxattr,
5769 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05005770 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005771 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005772 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04005773 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05005774 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04005775};
Josef Bacik618e21d2007-07-11 10:18:17 -04005776static struct inode_operations btrfs_special_inode_operations = {
5777 .getattr = btrfs_getattr,
5778 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05005779 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005780 .setxattr = btrfs_setxattr,
5781 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04005782 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005783 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04005784};
Chris Mason39279cc2007-06-12 06:35:45 -04005785static struct inode_operations btrfs_symlink_inode_operations = {
5786 .readlink = generic_readlink,
5787 .follow_link = page_follow_link_light,
5788 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05005789 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05005790 .setxattr = btrfs_setxattr,
5791 .getxattr = btrfs_getxattr,
5792 .listxattr = btrfs_listxattr,
5793 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04005794};
Yan, Zheng76dda932009-09-21 16:00:26 -04005795
5796struct dentry_operations btrfs_dentry_operations = {
5797 .d_delete = btrfs_dentry_delete,
5798};