Btrfs: hash_lock is no longer needed
[linux-2.6-block.git] / fs / btrfs / inode.c
CommitLineData
6cbd5570
CM
1/*
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
8f18cf13 19#include <linux/kernel.h>
065631f6 20#include <linux/bio.h>
39279cc3 21#include <linux/buffer_head.h>
f2eb0a24 22#include <linux/file.h>
39279cc3
CM
23#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>
29#include <linux/smp_lock.h>
30#include <linux/backing-dev.h>
31#include <linux/mpage.h>
32#include <linux/swap.h>
33#include <linux/writeback.h>
34#include <linux/statfs.h>
35#include <linux/compat.h>
9ebefb18 36#include <linux/bit_spinlock.h>
5103e947 37#include <linux/xattr.h>
33268eaf 38#include <linux/posix_acl.h>
d899e052 39#include <linux/falloc.h>
4b4e25f2 40#include "compat.h"
39279cc3
CM
41#include "ctree.h"
42#include "disk-io.h"
43#include "transaction.h"
44#include "btrfs_inode.h"
45#include "ioctl.h"
46#include "print-tree.h"
0b86a832 47#include "volumes.h"
e6dcd2dc 48#include "ordered-data.h"
95819c05 49#include "xattr.h"
e02119d5 50#include "tree-log.h"
5b84e8d6 51#include "ref-cache.h"
c8b97818 52#include "compression.h"
39279cc3
CM
53
54struct btrfs_iget_args {
55 u64 ino;
56 struct btrfs_root *root;
57};
58
59static struct inode_operations btrfs_dir_inode_operations;
60static struct inode_operations btrfs_symlink_inode_operations;
61static struct inode_operations btrfs_dir_ro_inode_operations;
618e21d5 62static struct inode_operations btrfs_special_inode_operations;
39279cc3
CM
63static struct inode_operations btrfs_file_inode_operations;
64static struct address_space_operations btrfs_aops;
65static struct address_space_operations btrfs_symlink_aops;
66static struct file_operations btrfs_dir_file_operations;
d1310b2e 67static struct extent_io_ops btrfs_extent_io_ops;
39279cc3
CM
68
69static struct kmem_cache *btrfs_inode_cachep;
70struct kmem_cache *btrfs_trans_handle_cachep;
71struct kmem_cache *btrfs_transaction_cachep;
72struct kmem_cache *btrfs_bit_radix_cachep;
73struct kmem_cache *btrfs_path_cachep;
74
75#define S_SHIFT 12
76static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
77 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
78 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
79 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
80 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
81 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
82 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
83 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
84};
85
7b128766 86static void btrfs_truncate(struct inode *inode);
c8b97818 87static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
771ed689
CM
88static noinline int cow_file_range(struct inode *inode,
89 struct page *locked_page,
90 u64 start, u64 end, int *page_started,
91 unsigned long *nr_written, int unlock);
7b128766 92
0279b4cd
JO
93static int btrfs_init_inode_security(struct inode *inode, struct inode *dir)
94{
95 int err;
96
97 err = btrfs_init_acl(inode, dir);
98 if (!err)
99 err = btrfs_xattr_security_init(inode, dir);
100 return err;
101}
102
d352ac68
CM
103/*
104 * a very lame attempt at stopping writes when the FS is 85% full. There
105 * are countless ways this is incorrect, but it is better than nothing.
106 */
1832a6d5
CM
107int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
108 int for_del)
109{
a2135011
CM
110 u64 total;
111 u64 used;
1832a6d5
CM
112 u64 thresh;
113 int ret = 0;
114
75eff68e 115 spin_lock(&root->fs_info->delalloc_lock);
a2135011
CM
116 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
117 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
1832a6d5 118 if (for_del)
f9ef6604 119 thresh = total * 90;
1832a6d5 120 else
f9ef6604
CM
121 thresh = total * 85;
122
123 do_div(thresh, 100);
1832a6d5 124
1832a6d5
CM
125 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
126 ret = -ENOSPC;
75eff68e 127 spin_unlock(&root->fs_info->delalloc_lock);
1832a6d5
CM
128 return ret;
129}
130
c8b97818
CM
131/*
132 * this does all the hard work for inserting an inline extent into
133 * the btree. The caller should have done a btrfs_drop_extents so that
134 * no overlapping inline items exist in the btree
135 */
d397712b 136static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
c8b97818
CM
137 struct btrfs_root *root, struct inode *inode,
138 u64 start, size_t size, size_t compressed_size,
139 struct page **compressed_pages)
140{
141 struct btrfs_key key;
142 struct btrfs_path *path;
143 struct extent_buffer *leaf;
144 struct page *page = NULL;
145 char *kaddr;
146 unsigned long ptr;
147 struct btrfs_file_extent_item *ei;
148 int err = 0;
149 int ret;
150 size_t cur_size = size;
151 size_t datasize;
152 unsigned long offset;
153 int use_compress = 0;
154
155 if (compressed_size && compressed_pages) {
156 use_compress = 1;
157 cur_size = compressed_size;
158 }
159
d397712b
CM
160 path = btrfs_alloc_path();
161 if (!path)
c8b97818
CM
162 return -ENOMEM;
163
164 btrfs_set_trans_block_group(trans, inode);
165
166 key.objectid = inode->i_ino;
167 key.offset = start;
168 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
c8b97818
CM
169 datasize = btrfs_file_extent_calc_inline_size(cur_size);
170
171 inode_add_bytes(inode, size);
172 ret = btrfs_insert_empty_item(trans, root, path, &key,
173 datasize);
174 BUG_ON(ret);
175 if (ret) {
176 err = ret;
c8b97818
CM
177 goto fail;
178 }
179 leaf = path->nodes[0];
180 ei = btrfs_item_ptr(leaf, path->slots[0],
181 struct btrfs_file_extent_item);
182 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
183 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
184 btrfs_set_file_extent_encryption(leaf, ei, 0);
185 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
186 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
187 ptr = btrfs_file_extent_inline_start(ei);
188
189 if (use_compress) {
190 struct page *cpage;
191 int i = 0;
d397712b 192 while (compressed_size > 0) {
c8b97818 193 cpage = compressed_pages[i];
5b050f04 194 cur_size = min_t(unsigned long, compressed_size,
c8b97818
CM
195 PAGE_CACHE_SIZE);
196
197 kaddr = kmap(cpage);
198 write_extent_buffer(leaf, kaddr, ptr, cur_size);
199 kunmap(cpage);
200
201 i++;
202 ptr += cur_size;
203 compressed_size -= cur_size;
204 }
205 btrfs_set_file_extent_compression(leaf, ei,
206 BTRFS_COMPRESS_ZLIB);
207 } else {
208 page = find_get_page(inode->i_mapping,
209 start >> PAGE_CACHE_SHIFT);
210 btrfs_set_file_extent_compression(leaf, ei, 0);
211 kaddr = kmap_atomic(page, KM_USER0);
212 offset = start & (PAGE_CACHE_SIZE - 1);
213 write_extent_buffer(leaf, kaddr + offset, ptr, size);
214 kunmap_atomic(kaddr, KM_USER0);
215 page_cache_release(page);
216 }
217 btrfs_mark_buffer_dirty(leaf);
218 btrfs_free_path(path);
219
220 BTRFS_I(inode)->disk_i_size = inode->i_size;
221 btrfs_update_inode(trans, root, inode);
222 return 0;
223fail:
224 btrfs_free_path(path);
225 return err;
226}
227
228
229/*
230 * conditionally insert an inline extent into the file. This
231 * does the checks required to make sure the data is small enough
232 * to fit as an inline extent.
233 */
234static int cow_file_range_inline(struct btrfs_trans_handle *trans,
235 struct btrfs_root *root,
236 struct inode *inode, u64 start, u64 end,
237 size_t compressed_size,
238 struct page **compressed_pages)
239{
240 u64 isize = i_size_read(inode);
241 u64 actual_end = min(end + 1, isize);
242 u64 inline_len = actual_end - start;
243 u64 aligned_end = (end + root->sectorsize - 1) &
244 ~((u64)root->sectorsize - 1);
245 u64 hint_byte;
246 u64 data_len = inline_len;
247 int ret;
248
249 if (compressed_size)
250 data_len = compressed_size;
251
252 if (start > 0 ||
70b99e69 253 actual_end >= PAGE_CACHE_SIZE ||
c8b97818
CM
254 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
255 (!compressed_size &&
256 (actual_end & (root->sectorsize - 1)) == 0) ||
257 end + 1 < isize ||
258 data_len > root->fs_info->max_inline) {
259 return 1;
260 }
261
c8b97818 262 ret = btrfs_drop_extents(trans, root, inode, start,
70b99e69 263 aligned_end, start, &hint_byte);
c8b97818
CM
264 BUG_ON(ret);
265
266 if (isize > actual_end)
267 inline_len = min_t(u64, isize, actual_end);
268 ret = insert_inline_extent(trans, root, inode, start,
269 inline_len, compressed_size,
270 compressed_pages);
271 BUG_ON(ret);
272 btrfs_drop_extent_cache(inode, start, aligned_end, 0);
c8b97818
CM
273 return 0;
274}
275
771ed689
CM
276struct async_extent {
277 u64 start;
278 u64 ram_size;
279 u64 compressed_size;
280 struct page **pages;
281 unsigned long nr_pages;
282 struct list_head list;
283};
284
285struct async_cow {
286 struct inode *inode;
287 struct btrfs_root *root;
288 struct page *locked_page;
289 u64 start;
290 u64 end;
291 struct list_head extents;
292 struct btrfs_work work;
293};
294
295static noinline int add_async_extent(struct async_cow *cow,
296 u64 start, u64 ram_size,
297 u64 compressed_size,
298 struct page **pages,
299 unsigned long nr_pages)
300{
301 struct async_extent *async_extent;
302
303 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
304 async_extent->start = start;
305 async_extent->ram_size = ram_size;
306 async_extent->compressed_size = compressed_size;
307 async_extent->pages = pages;
308 async_extent->nr_pages = nr_pages;
309 list_add_tail(&async_extent->list, &cow->extents);
310 return 0;
311}
312
d352ac68 313/*
771ed689
CM
314 * we create compressed extents in two phases. The first
315 * phase compresses a range of pages that have already been
316 * locked (both pages and state bits are locked).
c8b97818 317 *
771ed689
CM
318 * This is done inside an ordered work queue, and the compression
319 * is spread across many cpus. The actual IO submission is step
320 * two, and the ordered work queue takes care of making sure that
321 * happens in the same order things were put onto the queue by
322 * writepages and friends.
c8b97818 323 *
771ed689
CM
324 * If this code finds it can't get good compression, it puts an
325 * entry onto the work queue to write the uncompressed bytes. This
326 * makes sure that both compressed inodes and uncompressed inodes
327 * are written in the same order that pdflush sent them down.
d352ac68 328 */
771ed689
CM
329static noinline int compress_file_range(struct inode *inode,
330 struct page *locked_page,
331 u64 start, u64 end,
332 struct async_cow *async_cow,
333 int *num_added)
b888db2b
CM
334{
335 struct btrfs_root *root = BTRFS_I(inode)->root;
336 struct btrfs_trans_handle *trans;
db94535d 337 u64 num_bytes;
c8b97818
CM
338 u64 orig_start;
339 u64 disk_num_bytes;
db94535d 340 u64 blocksize = root->sectorsize;
c8b97818 341 u64 actual_end;
42dc7bab 342 u64 isize = i_size_read(inode);
e6dcd2dc 343 int ret = 0;
c8b97818
CM
344 struct page **pages = NULL;
345 unsigned long nr_pages;
346 unsigned long nr_pages_ret = 0;
347 unsigned long total_compressed = 0;
348 unsigned long total_in = 0;
349 unsigned long max_compressed = 128 * 1024;
771ed689 350 unsigned long max_uncompressed = 128 * 1024;
c8b97818
CM
351 int i;
352 int will_compress;
b888db2b 353
c8b97818
CM
354 orig_start = start;
355
42dc7bab 356 actual_end = min_t(u64, isize, end + 1);
c8b97818
CM
357again:
358 will_compress = 0;
359 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
360 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
be20aa9d 361
c8b97818
CM
362 total_compressed = actual_end - start;
363
364 /* we want to make sure that amount of ram required to uncompress
365 * an extent is reasonable, so we limit the total size in ram
771ed689
CM
366 * of a compressed extent to 128k. This is a crucial number
367 * because it also controls how easily we can spread reads across
368 * cpus for decompression.
369 *
370 * We also want to make sure the amount of IO required to do
371 * a random read is reasonably small, so we limit the size of
372 * a compressed extent to 128k.
c8b97818
CM
373 */
374 total_compressed = min(total_compressed, max_uncompressed);
db94535d 375 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
be20aa9d 376 num_bytes = max(blocksize, num_bytes);
c8b97818
CM
377 disk_num_bytes = num_bytes;
378 total_in = 0;
379 ret = 0;
db94535d 380
771ed689
CM
381 /*
382 * we do compression for mount -o compress and when the
383 * inode has not been flagged as nocompress. This flag can
384 * change at any time if we discover bad compression ratios.
c8b97818
CM
385 */
386 if (!btrfs_test_flag(inode, NOCOMPRESS) &&
387 btrfs_test_opt(root, COMPRESS)) {
388 WARN_ON(pages);
cfbc246e 389 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
c8b97818 390
c8b97818
CM
391 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
392 total_compressed, pages,
393 nr_pages, &nr_pages_ret,
394 &total_in,
395 &total_compressed,
396 max_compressed);
397
398 if (!ret) {
399 unsigned long offset = total_compressed &
400 (PAGE_CACHE_SIZE - 1);
401 struct page *page = pages[nr_pages_ret - 1];
402 char *kaddr;
403
404 /* zero the tail end of the last page, we might be
405 * sending it down to disk
406 */
407 if (offset) {
408 kaddr = kmap_atomic(page, KM_USER0);
409 memset(kaddr + offset, 0,
410 PAGE_CACHE_SIZE - offset);
411 kunmap_atomic(kaddr, KM_USER0);
412 }
413 will_compress = 1;
414 }
415 }
416 if (start == 0) {
771ed689
CM
417 trans = btrfs_join_transaction(root, 1);
418 BUG_ON(!trans);
419 btrfs_set_trans_block_group(trans, inode);
420
c8b97818 421 /* lets try to make an inline extent */
771ed689 422 if (ret || total_in < (actual_end - start)) {
c8b97818 423 /* we didn't compress the entire range, try
771ed689 424 * to make an uncompressed inline extent.
c8b97818
CM
425 */
426 ret = cow_file_range_inline(trans, root, inode,
427 start, end, 0, NULL);
428 } else {
771ed689 429 /* try making a compressed inline extent */
c8b97818
CM
430 ret = cow_file_range_inline(trans, root, inode,
431 start, end,
432 total_compressed, pages);
433 }
771ed689 434 btrfs_end_transaction(trans, root);
c8b97818 435 if (ret == 0) {
771ed689
CM
436 /*
437 * inline extent creation worked, we don't need
438 * to create any more async work items. Unlock
439 * and free up our temp pages.
440 */
c8b97818
CM
441 extent_clear_unlock_delalloc(inode,
442 &BTRFS_I(inode)->io_tree,
771ed689
CM
443 start, end, NULL, 1, 0,
444 0, 1, 1, 1);
c8b97818
CM
445 ret = 0;
446 goto free_pages_out;
447 }
448 }
449
450 if (will_compress) {
451 /*
452 * we aren't doing an inline extent round the compressed size
453 * up to a block size boundary so the allocator does sane
454 * things
455 */
456 total_compressed = (total_compressed + blocksize - 1) &
457 ~(blocksize - 1);
458
459 /*
460 * one last check to make sure the compression is really a
461 * win, compare the page count read with the blocks on disk
462 */
463 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
464 ~(PAGE_CACHE_SIZE - 1);
465 if (total_compressed >= total_in) {
466 will_compress = 0;
467 } else {
468 disk_num_bytes = total_compressed;
469 num_bytes = total_in;
470 }
471 }
472 if (!will_compress && pages) {
473 /*
474 * the compression code ran but failed to make things smaller,
475 * free any pages it allocated and our page pointer array
476 */
477 for (i = 0; i < nr_pages_ret; i++) {
70b99e69 478 WARN_ON(pages[i]->mapping);
c8b97818
CM
479 page_cache_release(pages[i]);
480 }
481 kfree(pages);
482 pages = NULL;
483 total_compressed = 0;
484 nr_pages_ret = 0;
485
486 /* flag the file so we don't compress in the future */
487 btrfs_set_flag(inode, NOCOMPRESS);
488 }
771ed689
CM
489 if (will_compress) {
490 *num_added += 1;
c8b97818 491
771ed689
CM
492 /* the async work queues will take care of doing actual
493 * allocation on disk for these compressed pages,
494 * and will submit them to the elevator.
495 */
496 add_async_extent(async_cow, start, num_bytes,
497 total_compressed, pages, nr_pages_ret);
179e29e4 498
42dc7bab 499 if (start + num_bytes < end && start + num_bytes < actual_end) {
771ed689
CM
500 start += num_bytes;
501 pages = NULL;
502 cond_resched();
503 goto again;
504 }
505 } else {
506 /*
507 * No compression, but we still need to write the pages in
508 * the file we've been given so far. redirty the locked
509 * page if it corresponds to our extent and set things up
510 * for the async work queue to run cow_file_range to do
511 * the normal delalloc dance
512 */
513 if (page_offset(locked_page) >= start &&
514 page_offset(locked_page) <= end) {
515 __set_page_dirty_nobuffers(locked_page);
516 /* unlocked later on in the async handlers */
517 }
518 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
519 *num_added += 1;
520 }
3b951516 521
771ed689
CM
522out:
523 return 0;
524
525free_pages_out:
526 for (i = 0; i < nr_pages_ret; i++) {
527 WARN_ON(pages[i]->mapping);
528 page_cache_release(pages[i]);
529 }
d397712b 530 kfree(pages);
771ed689
CM
531
532 goto out;
533}
534
535/*
536 * phase two of compressed writeback. This is the ordered portion
537 * of the code, which only gets called in the order the work was
538 * queued. We walk all the async extents created by compress_file_range
539 * and send them down to the disk.
540 */
541static noinline int submit_compressed_extents(struct inode *inode,
542 struct async_cow *async_cow)
543{
544 struct async_extent *async_extent;
545 u64 alloc_hint = 0;
546 struct btrfs_trans_handle *trans;
547 struct btrfs_key ins;
548 struct extent_map *em;
549 struct btrfs_root *root = BTRFS_I(inode)->root;
550 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
551 struct extent_io_tree *io_tree;
552 int ret;
553
554 if (list_empty(&async_cow->extents))
555 return 0;
556
557 trans = btrfs_join_transaction(root, 1);
558
d397712b 559 while (!list_empty(&async_cow->extents)) {
771ed689
CM
560 async_extent = list_entry(async_cow->extents.next,
561 struct async_extent, list);
562 list_del(&async_extent->list);
c8b97818 563
771ed689
CM
564 io_tree = &BTRFS_I(inode)->io_tree;
565
566 /* did the compression code fall back to uncompressed IO? */
567 if (!async_extent->pages) {
568 int page_started = 0;
569 unsigned long nr_written = 0;
570
571 lock_extent(io_tree, async_extent->start,
d397712b
CM
572 async_extent->start +
573 async_extent->ram_size - 1, GFP_NOFS);
771ed689
CM
574
575 /* allocate blocks */
576 cow_file_range(inode, async_cow->locked_page,
577 async_extent->start,
578 async_extent->start +
579 async_extent->ram_size - 1,
580 &page_started, &nr_written, 0);
581
582 /*
583 * if page_started, cow_file_range inserted an
584 * inline extent and took care of all the unlocking
585 * and IO for us. Otherwise, we need to submit
586 * all those pages down to the drive.
587 */
588 if (!page_started)
589 extent_write_locked_range(io_tree,
590 inode, async_extent->start,
d397712b 591 async_extent->start +
771ed689
CM
592 async_extent->ram_size - 1,
593 btrfs_get_extent,
594 WB_SYNC_ALL);
595 kfree(async_extent);
596 cond_resched();
597 continue;
598 }
599
600 lock_extent(io_tree, async_extent->start,
601 async_extent->start + async_extent->ram_size - 1,
602 GFP_NOFS);
c8b97818 603 /*
771ed689
CM
604 * here we're doing allocation and writeback of the
605 * compressed pages
c8b97818 606 */
771ed689
CM
607 btrfs_drop_extent_cache(inode, async_extent->start,
608 async_extent->start +
609 async_extent->ram_size - 1, 0);
610
611 ret = btrfs_reserve_extent(trans, root,
612 async_extent->compressed_size,
613 async_extent->compressed_size,
614 0, alloc_hint,
615 (u64)-1, &ins, 1);
616 BUG_ON(ret);
617 em = alloc_extent_map(GFP_NOFS);
618 em->start = async_extent->start;
619 em->len = async_extent->ram_size;
445a6944 620 em->orig_start = em->start;
c8b97818 621
771ed689
CM
622 em->block_start = ins.objectid;
623 em->block_len = ins.offset;
624 em->bdev = root->fs_info->fs_devices->latest_bdev;
625 set_bit(EXTENT_FLAG_PINNED, &em->flags);
626 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
627
d397712b 628 while (1) {
771ed689
CM
629 spin_lock(&em_tree->lock);
630 ret = add_extent_mapping(em_tree, em);
631 spin_unlock(&em_tree->lock);
632 if (ret != -EEXIST) {
633 free_extent_map(em);
634 break;
635 }
636 btrfs_drop_extent_cache(inode, async_extent->start,
637 async_extent->start +
638 async_extent->ram_size - 1, 0);
639 }
640
641 ret = btrfs_add_ordered_extent(inode, async_extent->start,
642 ins.objectid,
643 async_extent->ram_size,
644 ins.offset,
645 BTRFS_ORDERED_COMPRESSED);
646 BUG_ON(ret);
647
648 btrfs_end_transaction(trans, root);
649
650 /*
651 * clear dirty, set writeback and unlock the pages.
652 */
653 extent_clear_unlock_delalloc(inode,
654 &BTRFS_I(inode)->io_tree,
655 async_extent->start,
656 async_extent->start +
657 async_extent->ram_size - 1,
658 NULL, 1, 1, 0, 1, 1, 0);
659
660 ret = btrfs_submit_compressed_write(inode,
d397712b
CM
661 async_extent->start,
662 async_extent->ram_size,
663 ins.objectid,
664 ins.offset, async_extent->pages,
665 async_extent->nr_pages);
771ed689
CM
666
667 BUG_ON(ret);
668 trans = btrfs_join_transaction(root, 1);
669 alloc_hint = ins.objectid + ins.offset;
670 kfree(async_extent);
671 cond_resched();
672 }
673
674 btrfs_end_transaction(trans, root);
675 return 0;
676}
677
678/*
679 * when extent_io.c finds a delayed allocation range in the file,
680 * the call backs end up in this code. The basic idea is to
681 * allocate extents on disk for the range, and create ordered data structs
682 * in ram to track those extents.
683 *
684 * locked_page is the page that writepage had locked already. We use
685 * it to make sure we don't do extra locks or unlocks.
686 *
687 * *page_started is set to one if we unlock locked_page and do everything
688 * required to start IO on it. It may be clean and already done with
689 * IO when we return.
690 */
691static noinline int cow_file_range(struct inode *inode,
692 struct page *locked_page,
693 u64 start, u64 end, int *page_started,
694 unsigned long *nr_written,
695 int unlock)
696{
697 struct btrfs_root *root = BTRFS_I(inode)->root;
698 struct btrfs_trans_handle *trans;
699 u64 alloc_hint = 0;
700 u64 num_bytes;
701 unsigned long ram_size;
702 u64 disk_num_bytes;
703 u64 cur_alloc_size;
704 u64 blocksize = root->sectorsize;
705 u64 actual_end;
42dc7bab 706 u64 isize = i_size_read(inode);
771ed689
CM
707 struct btrfs_key ins;
708 struct extent_map *em;
709 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
710 int ret = 0;
711
712 trans = btrfs_join_transaction(root, 1);
713 BUG_ON(!trans);
714 btrfs_set_trans_block_group(trans, inode);
715
42dc7bab 716 actual_end = min_t(u64, isize, end + 1);
771ed689
CM
717
718 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
719 num_bytes = max(blocksize, num_bytes);
720 disk_num_bytes = num_bytes;
721 ret = 0;
722
723 if (start == 0) {
724 /* lets try to make an inline extent */
725 ret = cow_file_range_inline(trans, root, inode,
726 start, end, 0, NULL);
727 if (ret == 0) {
728 extent_clear_unlock_delalloc(inode,
729 &BTRFS_I(inode)->io_tree,
730 start, end, NULL, 1, 1,
731 1, 1, 1, 1);
732 *nr_written = *nr_written +
733 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
734 *page_started = 1;
735 ret = 0;
736 goto out;
737 }
738 }
739
740 BUG_ON(disk_num_bytes >
741 btrfs_super_total_bytes(&root->fs_info->super_copy));
742
743 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
744
d397712b 745 while (disk_num_bytes > 0) {
c8b97818 746 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
e6dcd2dc 747 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
771ed689 748 root->sectorsize, 0, alloc_hint,
e6dcd2dc 749 (u64)-1, &ins, 1);
d397712b
CM
750 BUG_ON(ret);
751
e6dcd2dc
CM
752 em = alloc_extent_map(GFP_NOFS);
753 em->start = start;
445a6944 754 em->orig_start = em->start;
c8b97818 755
771ed689
CM
756 ram_size = ins.offset;
757 em->len = ins.offset;
c8b97818 758
e6dcd2dc 759 em->block_start = ins.objectid;
c8b97818 760 em->block_len = ins.offset;
e6dcd2dc 761 em->bdev = root->fs_info->fs_devices->latest_bdev;
7f3c74fb 762 set_bit(EXTENT_FLAG_PINNED, &em->flags);
c8b97818 763
d397712b 764 while (1) {
e6dcd2dc
CM
765 spin_lock(&em_tree->lock);
766 ret = add_extent_mapping(em_tree, em);
767 spin_unlock(&em_tree->lock);
768 if (ret != -EEXIST) {
769 free_extent_map(em);
770 break;
771 }
772 btrfs_drop_extent_cache(inode, start,
c8b97818 773 start + ram_size - 1, 0);
e6dcd2dc
CM
774 }
775
98d20f67 776 cur_alloc_size = ins.offset;
e6dcd2dc 777 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
771ed689 778 ram_size, cur_alloc_size, 0);
e6dcd2dc 779 BUG_ON(ret);
c8b97818 780
17d217fe
YZ
781 if (root->root_key.objectid ==
782 BTRFS_DATA_RELOC_TREE_OBJECTID) {
783 ret = btrfs_reloc_clone_csums(inode, start,
784 cur_alloc_size);
785 BUG_ON(ret);
786 }
787
d397712b 788 if (disk_num_bytes < cur_alloc_size)
3b951516 789 break;
d397712b 790
c8b97818
CM
791 /* we're not doing compressed IO, don't unlock the first
792 * page (which the caller expects to stay locked), don't
793 * clear any dirty bits and don't set any writeback bits
794 */
795 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
796 start, start + ram_size - 1,
771ed689
CM
797 locked_page, unlock, 1,
798 1, 0, 0, 0);
c8b97818 799 disk_num_bytes -= cur_alloc_size;
c59f8951
CM
800 num_bytes -= cur_alloc_size;
801 alloc_hint = ins.objectid + ins.offset;
802 start += cur_alloc_size;
b888db2b 803 }
b888db2b 804out:
771ed689 805 ret = 0;
b888db2b 806 btrfs_end_transaction(trans, root);
c8b97818 807
be20aa9d 808 return ret;
771ed689 809}
c8b97818 810
771ed689
CM
811/*
812 * work queue call back to started compression on a file and pages
813 */
814static noinline void async_cow_start(struct btrfs_work *work)
815{
816 struct async_cow *async_cow;
817 int num_added = 0;
818 async_cow = container_of(work, struct async_cow, work);
819
820 compress_file_range(async_cow->inode, async_cow->locked_page,
821 async_cow->start, async_cow->end, async_cow,
822 &num_added);
823 if (num_added == 0)
824 async_cow->inode = NULL;
825}
826
827/*
828 * work queue call back to submit previously compressed pages
829 */
830static noinline void async_cow_submit(struct btrfs_work *work)
831{
832 struct async_cow *async_cow;
833 struct btrfs_root *root;
834 unsigned long nr_pages;
835
836 async_cow = container_of(work, struct async_cow, work);
837
838 root = async_cow->root;
839 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
840 PAGE_CACHE_SHIFT;
841
842 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
843
844 if (atomic_read(&root->fs_info->async_delalloc_pages) <
845 5 * 1042 * 1024 &&
846 waitqueue_active(&root->fs_info->async_submit_wait))
847 wake_up(&root->fs_info->async_submit_wait);
848
d397712b 849 if (async_cow->inode)
771ed689 850 submit_compressed_extents(async_cow->inode, async_cow);
771ed689 851}
c8b97818 852
771ed689
CM
853static noinline void async_cow_free(struct btrfs_work *work)
854{
855 struct async_cow *async_cow;
856 async_cow = container_of(work, struct async_cow, work);
857 kfree(async_cow);
858}
859
860static int cow_file_range_async(struct inode *inode, struct page *locked_page,
861 u64 start, u64 end, int *page_started,
862 unsigned long *nr_written)
863{
864 struct async_cow *async_cow;
865 struct btrfs_root *root = BTRFS_I(inode)->root;
866 unsigned long nr_pages;
867 u64 cur_end;
868 int limit = 10 * 1024 * 1042;
869
870 if (!btrfs_test_opt(root, COMPRESS)) {
871 return cow_file_range(inode, locked_page, start, end,
872 page_started, nr_written, 1);
873 }
874
875 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED |
876 EXTENT_DELALLOC, 1, 0, GFP_NOFS);
d397712b 877 while (start < end) {
771ed689
CM
878 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
879 async_cow->inode = inode;
880 async_cow->root = root;
881 async_cow->locked_page = locked_page;
882 async_cow->start = start;
883
884 if (btrfs_test_flag(inode, NOCOMPRESS))
885 cur_end = end;
886 else
887 cur_end = min(end, start + 512 * 1024 - 1);
888
889 async_cow->end = cur_end;
890 INIT_LIST_HEAD(&async_cow->extents);
891
892 async_cow->work.func = async_cow_start;
893 async_cow->work.ordered_func = async_cow_submit;
894 async_cow->work.ordered_free = async_cow_free;
895 async_cow->work.flags = 0;
896
771ed689
CM
897 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
898 PAGE_CACHE_SHIFT;
899 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
900
901 btrfs_queue_worker(&root->fs_info->delalloc_workers,
902 &async_cow->work);
903
904 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
905 wait_event(root->fs_info->async_submit_wait,
906 (atomic_read(&root->fs_info->async_delalloc_pages) <
907 limit));
908 }
909
d397712b 910 while (atomic_read(&root->fs_info->async_submit_draining) &&
771ed689
CM
911 atomic_read(&root->fs_info->async_delalloc_pages)) {
912 wait_event(root->fs_info->async_submit_wait,
913 (atomic_read(&root->fs_info->async_delalloc_pages) ==
914 0));
915 }
916
917 *nr_written += nr_pages;
918 start = cur_end + 1;
919 }
920 *page_started = 1;
921 return 0;
be20aa9d
CM
922}
923
d397712b 924static noinline int csum_exist_in_range(struct btrfs_root *root,
17d217fe
YZ
925 u64 bytenr, u64 num_bytes)
926{
927 int ret;
928 struct btrfs_ordered_sum *sums;
929 LIST_HEAD(list);
930
07d400a6
YZ
931 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
932 bytenr + num_bytes - 1, &list);
17d217fe
YZ
933 if (ret == 0 && list_empty(&list))
934 return 0;
935
936 while (!list_empty(&list)) {
937 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
938 list_del(&sums->list);
939 kfree(sums);
940 }
941 return 1;
942}
943
d352ac68
CM
944/*
945 * when nowcow writeback call back. This checks for snapshots or COW copies
946 * of the extents that exist in the file, and COWs the file as required.
947 *
948 * If no cow copies or snapshots exist, we write directly to the existing
949 * blocks on disk
950 */
c8b97818 951static int run_delalloc_nocow(struct inode *inode, struct page *locked_page,
771ed689
CM
952 u64 start, u64 end, int *page_started, int force,
953 unsigned long *nr_written)
be20aa9d 954{
be20aa9d 955 struct btrfs_root *root = BTRFS_I(inode)->root;
7ea394f1 956 struct btrfs_trans_handle *trans;
be20aa9d 957 struct extent_buffer *leaf;
be20aa9d 958 struct btrfs_path *path;
80ff3856 959 struct btrfs_file_extent_item *fi;
be20aa9d 960 struct btrfs_key found_key;
80ff3856
YZ
961 u64 cow_start;
962 u64 cur_offset;
963 u64 extent_end;
964 u64 disk_bytenr;
965 u64 num_bytes;
966 int extent_type;
967 int ret;
d899e052 968 int type;
80ff3856
YZ
969 int nocow;
970 int check_prev = 1;
be20aa9d
CM
971
972 path = btrfs_alloc_path();
973 BUG_ON(!path);
7ea394f1
YZ
974 trans = btrfs_join_transaction(root, 1);
975 BUG_ON(!trans);
be20aa9d 976
80ff3856
YZ
977 cow_start = (u64)-1;
978 cur_offset = start;
979 while (1) {
980 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
981 cur_offset, 0);
982 BUG_ON(ret < 0);
983 if (ret > 0 && path->slots[0] > 0 && check_prev) {
984 leaf = path->nodes[0];
985 btrfs_item_key_to_cpu(leaf, &found_key,
986 path->slots[0] - 1);
987 if (found_key.objectid == inode->i_ino &&
988 found_key.type == BTRFS_EXTENT_DATA_KEY)
989 path->slots[0]--;
990 }
991 check_prev = 0;
992next_slot:
993 leaf = path->nodes[0];
994 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
995 ret = btrfs_next_leaf(root, path);
996 if (ret < 0)
997 BUG_ON(1);
998 if (ret > 0)
999 break;
1000 leaf = path->nodes[0];
1001 }
be20aa9d 1002
80ff3856
YZ
1003 nocow = 0;
1004 disk_bytenr = 0;
17d217fe 1005 num_bytes = 0;
80ff3856
YZ
1006 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1007
1008 if (found_key.objectid > inode->i_ino ||
1009 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1010 found_key.offset > end)
1011 break;
1012
1013 if (found_key.offset > cur_offset) {
1014 extent_end = found_key.offset;
1015 goto out_check;
1016 }
1017
1018 fi = btrfs_item_ptr(leaf, path->slots[0],
1019 struct btrfs_file_extent_item);
1020 extent_type = btrfs_file_extent_type(leaf, fi);
1021
d899e052
YZ
1022 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1023 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
80ff3856
YZ
1024 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1025 extent_end = found_key.offset +
1026 btrfs_file_extent_num_bytes(leaf, fi);
1027 if (extent_end <= start) {
1028 path->slots[0]++;
1029 goto next_slot;
1030 }
17d217fe
YZ
1031 if (disk_bytenr == 0)
1032 goto out_check;
80ff3856
YZ
1033 if (btrfs_file_extent_compression(leaf, fi) ||
1034 btrfs_file_extent_encryption(leaf, fi) ||
1035 btrfs_file_extent_other_encoding(leaf, fi))
1036 goto out_check;
d899e052
YZ
1037 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1038 goto out_check;
d2fb3437 1039 if (btrfs_extent_readonly(root, disk_bytenr))
80ff3856 1040 goto out_check;
17d217fe
YZ
1041 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
1042 disk_bytenr))
1043 goto out_check;
80ff3856 1044 disk_bytenr += btrfs_file_extent_offset(leaf, fi);
17d217fe
YZ
1045 disk_bytenr += cur_offset - found_key.offset;
1046 num_bytes = min(end + 1, extent_end) - cur_offset;
1047 /*
1048 * force cow if csum exists in the range.
1049 * this ensure that csum for a given extent are
1050 * either valid or do not exist.
1051 */
1052 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1053 goto out_check;
80ff3856
YZ
1054 nocow = 1;
1055 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1056 extent_end = found_key.offset +
1057 btrfs_file_extent_inline_len(leaf, fi);
1058 extent_end = ALIGN(extent_end, root->sectorsize);
1059 } else {
1060 BUG_ON(1);
1061 }
1062out_check:
1063 if (extent_end <= start) {
1064 path->slots[0]++;
1065 goto next_slot;
1066 }
1067 if (!nocow) {
1068 if (cow_start == (u64)-1)
1069 cow_start = cur_offset;
1070 cur_offset = extent_end;
1071 if (cur_offset > end)
1072 break;
1073 path->slots[0]++;
1074 goto next_slot;
7ea394f1
YZ
1075 }
1076
1077 btrfs_release_path(root, path);
80ff3856
YZ
1078 if (cow_start != (u64)-1) {
1079 ret = cow_file_range(inode, locked_page, cow_start,
771ed689
CM
1080 found_key.offset - 1, page_started,
1081 nr_written, 1);
80ff3856
YZ
1082 BUG_ON(ret);
1083 cow_start = (u64)-1;
7ea394f1 1084 }
80ff3856 1085
d899e052
YZ
1086 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1087 struct extent_map *em;
1088 struct extent_map_tree *em_tree;
1089 em_tree = &BTRFS_I(inode)->extent_tree;
1090 em = alloc_extent_map(GFP_NOFS);
1091 em->start = cur_offset;
445a6944 1092 em->orig_start = em->start;
d899e052
YZ
1093 em->len = num_bytes;
1094 em->block_len = num_bytes;
1095 em->block_start = disk_bytenr;
1096 em->bdev = root->fs_info->fs_devices->latest_bdev;
1097 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1098 while (1) {
1099 spin_lock(&em_tree->lock);
1100 ret = add_extent_mapping(em_tree, em);
1101 spin_unlock(&em_tree->lock);
1102 if (ret != -EEXIST) {
1103 free_extent_map(em);
1104 break;
1105 }
1106 btrfs_drop_extent_cache(inode, em->start,
1107 em->start + em->len - 1, 0);
1108 }
1109 type = BTRFS_ORDERED_PREALLOC;
1110 } else {
1111 type = BTRFS_ORDERED_NOCOW;
1112 }
80ff3856
YZ
1113
1114 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
d899e052
YZ
1115 num_bytes, num_bytes, type);
1116 BUG_ON(ret);
771ed689 1117
d899e052
YZ
1118 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1119 cur_offset, cur_offset + num_bytes - 1,
771ed689 1120 locked_page, 1, 1, 1, 0, 0, 0);
80ff3856
YZ
1121 cur_offset = extent_end;
1122 if (cur_offset > end)
1123 break;
be20aa9d 1124 }
80ff3856
YZ
1125 btrfs_release_path(root, path);
1126
1127 if (cur_offset <= end && cow_start == (u64)-1)
1128 cow_start = cur_offset;
1129 if (cow_start != (u64)-1) {
1130 ret = cow_file_range(inode, locked_page, cow_start, end,
771ed689 1131 page_started, nr_written, 1);
80ff3856
YZ
1132 BUG_ON(ret);
1133 }
1134
1135 ret = btrfs_end_transaction(trans, root);
1136 BUG_ON(ret);
7ea394f1 1137 btrfs_free_path(path);
80ff3856 1138 return 0;
be20aa9d
CM
1139}
1140
d352ac68
CM
1141/*
1142 * extent_io.c call back to do delayed allocation processing
1143 */
c8b97818 1144static int run_delalloc_range(struct inode *inode, struct page *locked_page,
771ed689
CM
1145 u64 start, u64 end, int *page_started,
1146 unsigned long *nr_written)
be20aa9d 1147{
be20aa9d 1148 int ret;
a2135011 1149
17d217fe 1150 if (btrfs_test_flag(inode, NODATACOW))
c8b97818 1151 ret = run_delalloc_nocow(inode, locked_page, start, end,
d397712b 1152 page_started, 1, nr_written);
d899e052
YZ
1153 else if (btrfs_test_flag(inode, PREALLOC))
1154 ret = run_delalloc_nocow(inode, locked_page, start, end,
d397712b 1155 page_started, 0, nr_written);
be20aa9d 1156 else
771ed689 1157 ret = cow_file_range_async(inode, locked_page, start, end,
d397712b 1158 page_started, nr_written);
1832a6d5 1159
b888db2b
CM
1160 return ret;
1161}
1162
d352ac68
CM
1163/*
1164 * extent_io.c set_bit_hook, used to track delayed allocation
1165 * bytes in this file, and to maintain the list of inodes that
1166 * have pending delalloc work to be done.
1167 */
b2950863 1168static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
b0c68f8b 1169 unsigned long old, unsigned long bits)
291d673e 1170{
75eff68e
CM
1171 /*
1172 * set_bit and clear bit hooks normally require _irqsave/restore
1173 * but in this case, we are only testeing for the DELALLOC
1174 * bit, which is only set or cleared with irqs on
1175 */
b0c68f8b 1176 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
291d673e 1177 struct btrfs_root *root = BTRFS_I(inode)->root;
75eff68e 1178 spin_lock(&root->fs_info->delalloc_lock);
9069218d 1179 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
291d673e 1180 root->fs_info->delalloc_bytes += end - start + 1;
ea8c2819
CM
1181 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1182 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1183 &root->fs_info->delalloc_inodes);
1184 }
75eff68e 1185 spin_unlock(&root->fs_info->delalloc_lock);
291d673e
CM
1186 }
1187 return 0;
1188}
1189
d352ac68
CM
1190/*
1191 * extent_io.c clear_bit_hook, see set_bit_hook for why
1192 */
b2950863 1193static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
b0c68f8b 1194 unsigned long old, unsigned long bits)
291d673e 1195{
75eff68e
CM
1196 /*
1197 * set_bit and clear bit hooks normally require _irqsave/restore
1198 * but in this case, we are only testeing for the DELALLOC
1199 * bit, which is only set or cleared with irqs on
1200 */
b0c68f8b 1201 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
291d673e 1202 struct btrfs_root *root = BTRFS_I(inode)->root;
bcbfce8a 1203
75eff68e 1204 spin_lock(&root->fs_info->delalloc_lock);
b0c68f8b 1205 if (end - start + 1 > root->fs_info->delalloc_bytes) {
d397712b
CM
1206 printk(KERN_INFO "btrfs warning: delalloc account "
1207 "%llu %llu\n",
1208 (unsigned long long)end - start + 1,
1209 (unsigned long long)
1210 root->fs_info->delalloc_bytes);
b0c68f8b 1211 root->fs_info->delalloc_bytes = 0;
9069218d 1212 BTRFS_I(inode)->delalloc_bytes = 0;
b0c68f8b
CM
1213 } else {
1214 root->fs_info->delalloc_bytes -= end - start + 1;
9069218d 1215 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
b0c68f8b 1216 }
ea8c2819
CM
1217 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1218 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1219 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1220 }
75eff68e 1221 spin_unlock(&root->fs_info->delalloc_lock);
291d673e
CM
1222 }
1223 return 0;
1224}
1225
d352ac68
CM
1226/*
1227 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1228 * we don't create bios that span stripes or chunks
1229 */
239b14b3 1230int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
c8b97818
CM
1231 size_t size, struct bio *bio,
1232 unsigned long bio_flags)
239b14b3
CM
1233{
1234 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1235 struct btrfs_mapping_tree *map_tree;
a62b9401 1236 u64 logical = (u64)bio->bi_sector << 9;
239b14b3
CM
1237 u64 length = 0;
1238 u64 map_length;
239b14b3
CM
1239 int ret;
1240
771ed689
CM
1241 if (bio_flags & EXTENT_BIO_COMPRESSED)
1242 return 0;
1243
f2d8d74d 1244 length = bio->bi_size;
239b14b3
CM
1245 map_tree = &root->fs_info->mapping_tree;
1246 map_length = length;
cea9e445 1247 ret = btrfs_map_block(map_tree, READ, logical,
f188591e 1248 &map_length, NULL, 0);
cea9e445 1249
d397712b 1250 if (map_length < length + size)
239b14b3 1251 return 1;
239b14b3
CM
1252 return 0;
1253}
1254
d352ac68
CM
1255/*
1256 * in order to insert checksums into the metadata in large chunks,
1257 * we wait until bio submission time. All the pages in the bio are
1258 * checksummed and sums are attached onto the ordered extent record.
1259 *
1260 * At IO completion time the cums attached on the ordered extent record
1261 * are inserted into the btree
1262 */
d397712b
CM
1263static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1264 struct bio *bio, int mirror_num,
1265 unsigned long bio_flags)
065631f6 1266{
065631f6 1267 struct btrfs_root *root = BTRFS_I(inode)->root;
065631f6 1268 int ret = 0;
e015640f 1269
d20f7043 1270 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
44b8bd7e 1271 BUG_ON(ret);
4a69a410
CM
1272 return 0;
1273}
e015640f 1274
4a69a410
CM
1275/*
1276 * in order to insert checksums into the metadata in large chunks,
1277 * we wait until bio submission time. All the pages in the bio are
1278 * checksummed and sums are attached onto the ordered extent record.
1279 *
1280 * At IO completion time the cums attached on the ordered extent record
1281 * are inserted into the btree
1282 */
b2950863 1283static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
4a69a410
CM
1284 int mirror_num, unsigned long bio_flags)
1285{
1286 struct btrfs_root *root = BTRFS_I(inode)->root;
8b712842 1287 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
44b8bd7e
CM
1288}
1289
d352ac68 1290/*
cad321ad
CM
1291 * extent_io.c submission hook. This does the right thing for csum calculation
1292 * on write, or reading the csums from the tree before a read
d352ac68 1293 */
b2950863 1294static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
c8b97818 1295 int mirror_num, unsigned long bio_flags)
44b8bd7e
CM
1296{
1297 struct btrfs_root *root = BTRFS_I(inode)->root;
1298 int ret = 0;
19b9bdb0 1299 int skip_sum;
44b8bd7e 1300
cad321ad
CM
1301 skip_sum = btrfs_test_flag(inode, NODATASUM);
1302
e6dcd2dc
CM
1303 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1304 BUG_ON(ret);
065631f6 1305
4d1b5fb4 1306 if (!(rw & (1 << BIO_RW))) {
d20f7043 1307 if (bio_flags & EXTENT_BIO_COMPRESSED) {
c8b97818
CM
1308 return btrfs_submit_compressed_read(inode, bio,
1309 mirror_num, bio_flags);
d20f7043
CM
1310 } else if (!skip_sum)
1311 btrfs_lookup_bio_sums(root, inode, bio, NULL);
4d1b5fb4 1312 goto mapit;
19b9bdb0 1313 } else if (!skip_sum) {
17d217fe
YZ
1314 /* csum items have already been cloned */
1315 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1316 goto mapit;
19b9bdb0
CM
1317 /* we're doing a write, do the async checksumming */
1318 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
44b8bd7e 1319 inode, rw, bio, mirror_num,
4a69a410
CM
1320 bio_flags, __btrfs_submit_bio_start,
1321 __btrfs_submit_bio_done);
19b9bdb0
CM
1322 }
1323
0b86a832 1324mapit:
8b712842 1325 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
065631f6 1326}
6885f308 1327
d352ac68
CM
1328/*
1329 * given a list of ordered sums record them in the inode. This happens
1330 * at IO completion time based on sums calculated at bio submission time.
1331 */
ba1da2f4 1332static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
e6dcd2dc
CM
1333 struct inode *inode, u64 file_offset,
1334 struct list_head *list)
1335{
e6dcd2dc
CM
1336 struct btrfs_ordered_sum *sum;
1337
1338 btrfs_set_trans_block_group(trans, inode);
c6e30871
QF
1339
1340 list_for_each_entry(sum, list, list) {
d20f7043
CM
1341 btrfs_csum_file_blocks(trans,
1342 BTRFS_I(inode)->root->fs_info->csum_root, sum);
e6dcd2dc
CM
1343 }
1344 return 0;
1345}
1346
ea8c2819
CM
1347int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
1348{
d397712b 1349 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
771ed689 1350 WARN_ON(1);
ea8c2819
CM
1351 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1352 GFP_NOFS);
1353}
1354
d352ac68 1355/* see btrfs_writepage_start_hook for details on why this is required */
247e743c
CM
1356struct btrfs_writepage_fixup {
1357 struct page *page;
1358 struct btrfs_work work;
1359};
1360
b2950863 1361static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
247e743c
CM
1362{
1363 struct btrfs_writepage_fixup *fixup;
1364 struct btrfs_ordered_extent *ordered;
1365 struct page *page;
1366 struct inode *inode;
1367 u64 page_start;
1368 u64 page_end;
1369
1370 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1371 page = fixup->page;
4a096752 1372again:
247e743c
CM
1373 lock_page(page);
1374 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1375 ClearPageChecked(page);
1376 goto out_page;
1377 }
1378
1379 inode = page->mapping->host;
1380 page_start = page_offset(page);
1381 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1382
1383 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
4a096752
CM
1384
1385 /* already ordered? We're done */
1386 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
1387 EXTENT_ORDERED, 0)) {
247e743c 1388 goto out;
4a096752
CM
1389 }
1390
1391 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1392 if (ordered) {
1393 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
1394 page_end, GFP_NOFS);
1395 unlock_page(page);
1396 btrfs_start_ordered_extent(inode, ordered, 1);
1397 goto again;
1398 }
247e743c 1399
ea8c2819 1400 btrfs_set_extent_delalloc(inode, page_start, page_end);
247e743c
CM
1401 ClearPageChecked(page);
1402out:
1403 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1404out_page:
1405 unlock_page(page);
1406 page_cache_release(page);
1407}
1408
1409/*
1410 * There are a few paths in the higher layers of the kernel that directly
1411 * set the page dirty bit without asking the filesystem if it is a
1412 * good idea. This causes problems because we want to make sure COW
1413 * properly happens and the data=ordered rules are followed.
1414 *
c8b97818 1415 * In our case any range that doesn't have the ORDERED bit set
247e743c
CM
1416 * hasn't been properly setup for IO. We kick off an async process
1417 * to fix it up. The async helper will wait for ordered extents, set
1418 * the delalloc bit and make it safe to write the page.
1419 */
b2950863 1420static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
247e743c
CM
1421{
1422 struct inode *inode = page->mapping->host;
1423 struct btrfs_writepage_fixup *fixup;
1424 struct btrfs_root *root = BTRFS_I(inode)->root;
1425 int ret;
1426
1427 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1428 EXTENT_ORDERED, 0);
1429 if (ret)
1430 return 0;
1431
1432 if (PageChecked(page))
1433 return -EAGAIN;
1434
1435 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1436 if (!fixup)
1437 return -EAGAIN;
f421950f 1438
247e743c
CM
1439 SetPageChecked(page);
1440 page_cache_get(page);
1441 fixup->work.func = btrfs_writepage_fixup_worker;
1442 fixup->page = page;
1443 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1444 return -EAGAIN;
1445}
1446
d899e052
YZ
1447static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1448 struct inode *inode, u64 file_pos,
1449 u64 disk_bytenr, u64 disk_num_bytes,
1450 u64 num_bytes, u64 ram_bytes,
1451 u8 compression, u8 encryption,
1452 u16 other_encoding, int extent_type)
1453{
1454 struct btrfs_root *root = BTRFS_I(inode)->root;
1455 struct btrfs_file_extent_item *fi;
1456 struct btrfs_path *path;
1457 struct extent_buffer *leaf;
1458 struct btrfs_key ins;
1459 u64 hint;
1460 int ret;
1461
1462 path = btrfs_alloc_path();
1463 BUG_ON(!path);
1464
1465 ret = btrfs_drop_extents(trans, root, inode, file_pos,
1466 file_pos + num_bytes, file_pos, &hint);
1467 BUG_ON(ret);
1468
1469 ins.objectid = inode->i_ino;
1470 ins.offset = file_pos;
1471 ins.type = BTRFS_EXTENT_DATA_KEY;
1472 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1473 BUG_ON(ret);
1474 leaf = path->nodes[0];
1475 fi = btrfs_item_ptr(leaf, path->slots[0],
1476 struct btrfs_file_extent_item);
1477 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1478 btrfs_set_file_extent_type(leaf, fi, extent_type);
1479 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1480 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1481 btrfs_set_file_extent_offset(leaf, fi, 0);
1482 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1483 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1484 btrfs_set_file_extent_compression(leaf, fi, compression);
1485 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1486 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
1487 btrfs_mark_buffer_dirty(leaf);
1488
1489 inode_add_bytes(inode, num_bytes);
1490 btrfs_drop_extent_cache(inode, file_pos, file_pos + num_bytes - 1, 0);
1491
1492 ins.objectid = disk_bytenr;
1493 ins.offset = disk_num_bytes;
1494 ins.type = BTRFS_EXTENT_ITEM_KEY;
1495 ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
1496 root->root_key.objectid,
1497 trans->transid, inode->i_ino, &ins);
1498 BUG_ON(ret);
1499
1500 btrfs_free_path(path);
1501 return 0;
1502}
1503
d352ac68
CM
1504/* as ordered data IO finishes, this gets called so we can finish
1505 * an ordered extent if the range of bytes in the file it covers are
1506 * fully written.
1507 */
211f90e6 1508static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
e6dcd2dc 1509{
e6dcd2dc
CM
1510 struct btrfs_root *root = BTRFS_I(inode)->root;
1511 struct btrfs_trans_handle *trans;
1512 struct btrfs_ordered_extent *ordered_extent;
1513 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
d899e052 1514 int compressed = 0;
e6dcd2dc
CM
1515 int ret;
1516
1517 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
ba1da2f4 1518 if (!ret)
e6dcd2dc 1519 return 0;
e6dcd2dc 1520
f9295749 1521 trans = btrfs_join_transaction(root, 1);
e6dcd2dc
CM
1522
1523 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
1524 BUG_ON(!ordered_extent);
7ea394f1
YZ
1525 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
1526 goto nocow;
e6dcd2dc
CM
1527
1528 lock_extent(io_tree, ordered_extent->file_offset,
1529 ordered_extent->file_offset + ordered_extent->len - 1,
1530 GFP_NOFS);
1531
c8b97818 1532 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
d899e052
YZ
1533 compressed = 1;
1534 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1535 BUG_ON(compressed);
1536 ret = btrfs_mark_extent_written(trans, root, inode,
1537 ordered_extent->file_offset,
1538 ordered_extent->file_offset +
1539 ordered_extent->len);
1540 BUG_ON(ret);
1541 } else {
1542 ret = insert_reserved_file_extent(trans, inode,
1543 ordered_extent->file_offset,
1544 ordered_extent->start,
1545 ordered_extent->disk_len,
1546 ordered_extent->len,
1547 ordered_extent->len,
1548 compressed, 0, 0,
1549 BTRFS_FILE_EXTENT_REG);
1550 BUG_ON(ret);
1551 }
e6dcd2dc
CM
1552 unlock_extent(io_tree, ordered_extent->file_offset,
1553 ordered_extent->file_offset + ordered_extent->len - 1,
1554 GFP_NOFS);
7ea394f1 1555nocow:
e6dcd2dc
CM
1556 add_pending_csums(trans, inode, ordered_extent->file_offset,
1557 &ordered_extent->list);
1558
34353029 1559 mutex_lock(&BTRFS_I(inode)->extent_mutex);
dbe674a9 1560 btrfs_ordered_update_i_size(inode, ordered_extent);
e02119d5 1561 btrfs_update_inode(trans, root, inode);
e6dcd2dc 1562 btrfs_remove_ordered_extent(inode, ordered_extent);
34353029 1563 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
7f3c74fb 1564
e6dcd2dc
CM
1565 /* once for us */
1566 btrfs_put_ordered_extent(ordered_extent);
1567 /* once for the tree */
1568 btrfs_put_ordered_extent(ordered_extent);
1569
e6dcd2dc
CM
1570 btrfs_end_transaction(trans, root);
1571 return 0;
1572}
1573
b2950863 1574static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
211f90e6
CM
1575 struct extent_state *state, int uptodate)
1576{
1577 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1578}
1579
d352ac68
CM
1580/*
1581 * When IO fails, either with EIO or csum verification fails, we
1582 * try other mirrors that might have a good copy of the data. This
1583 * io_failure_record is used to record state as we go through all the
1584 * mirrors. If another mirror has good data, the page is set up to date
1585 * and things continue. If a good mirror can't be found, the original
1586 * bio end_io callback is called to indicate things have failed.
1587 */
7e38326f
CM
1588struct io_failure_record {
1589 struct page *page;
1590 u64 start;
1591 u64 len;
1592 u64 logical;
d20f7043 1593 unsigned long bio_flags;
7e38326f
CM
1594 int last_mirror;
1595};
1596
b2950863 1597static int btrfs_io_failed_hook(struct bio *failed_bio,
1259ab75
CM
1598 struct page *page, u64 start, u64 end,
1599 struct extent_state *state)
7e38326f
CM
1600{
1601 struct io_failure_record *failrec = NULL;
1602 u64 private;
1603 struct extent_map *em;
1604 struct inode *inode = page->mapping->host;
1605 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
3b951516 1606 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7e38326f
CM
1607 struct bio *bio;
1608 int num_copies;
1609 int ret;
1259ab75 1610 int rw;
7e38326f
CM
1611 u64 logical;
1612
1613 ret = get_state_private(failure_tree, start, &private);
1614 if (ret) {
7e38326f
CM
1615 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1616 if (!failrec)
1617 return -ENOMEM;
1618 failrec->start = start;
1619 failrec->len = end - start + 1;
1620 failrec->last_mirror = 0;
d20f7043 1621 failrec->bio_flags = 0;
7e38326f 1622
3b951516
CM
1623 spin_lock(&em_tree->lock);
1624 em = lookup_extent_mapping(em_tree, start, failrec->len);
1625 if (em->start > start || em->start + em->len < start) {
1626 free_extent_map(em);
1627 em = NULL;
1628 }
1629 spin_unlock(&em_tree->lock);
7e38326f
CM
1630
1631 if (!em || IS_ERR(em)) {
1632 kfree(failrec);
1633 return -EIO;
1634 }
1635 logical = start - em->start;
1636 logical = em->block_start + logical;
d20f7043
CM
1637 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1638 logical = em->block_start;
1639 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1640 }
7e38326f
CM
1641 failrec->logical = logical;
1642 free_extent_map(em);
1643 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1644 EXTENT_DIRTY, GFP_NOFS);
587f7704
CM
1645 set_state_private(failure_tree, start,
1646 (u64)(unsigned long)failrec);
7e38326f 1647 } else {
587f7704 1648 failrec = (struct io_failure_record *)(unsigned long)private;
7e38326f
CM
1649 }
1650 num_copies = btrfs_num_copies(
1651 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1652 failrec->logical, failrec->len);
1653 failrec->last_mirror++;
1654 if (!state) {
cad321ad 1655 spin_lock(&BTRFS_I(inode)->io_tree.lock);
7e38326f
CM
1656 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1657 failrec->start,
1658 EXTENT_LOCKED);
1659 if (state && state->start != failrec->start)
1660 state = NULL;
cad321ad 1661 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
7e38326f
CM
1662 }
1663 if (!state || failrec->last_mirror > num_copies) {
1664 set_state_private(failure_tree, failrec->start, 0);
1665 clear_extent_bits(failure_tree, failrec->start,
1666 failrec->start + failrec->len - 1,
1667 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1668 kfree(failrec);
1669 return -EIO;
1670 }
1671 bio = bio_alloc(GFP_NOFS, 1);
1672 bio->bi_private = state;
1673 bio->bi_end_io = failed_bio->bi_end_io;
1674 bio->bi_sector = failrec->logical >> 9;
1675 bio->bi_bdev = failed_bio->bi_bdev;
e1c4b745 1676 bio->bi_size = 0;
d20f7043 1677
7e38326f 1678 bio_add_page(bio, page, failrec->len, start - page_offset(page));
1259ab75
CM
1679 if (failed_bio->bi_rw & (1 << BIO_RW))
1680 rw = WRITE;
1681 else
1682 rw = READ;
1683
1684 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
c8b97818 1685 failrec->last_mirror,
d20f7043 1686 failrec->bio_flags);
1259ab75
CM
1687 return 0;
1688}
1689
d352ac68
CM
1690/*
1691 * each time an IO finishes, we do a fast check in the IO failure tree
1692 * to see if we need to process or clean up an io_failure_record
1693 */
b2950863 1694static int btrfs_clean_io_failures(struct inode *inode, u64 start)
1259ab75
CM
1695{
1696 u64 private;
1697 u64 private_failure;
1698 struct io_failure_record *failure;
1699 int ret;
1700
1701 private = 0;
1702 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1703 (u64)-1, 1, EXTENT_DIRTY)) {
1704 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1705 start, &private_failure);
1706 if (ret == 0) {
1707 failure = (struct io_failure_record *)(unsigned long)
1708 private_failure;
1709 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1710 failure->start, 0);
1711 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1712 failure->start,
1713 failure->start + failure->len - 1,
1714 EXTENT_DIRTY | EXTENT_LOCKED,
1715 GFP_NOFS);
1716 kfree(failure);
1717 }
1718 }
7e38326f
CM
1719 return 0;
1720}
1721
d352ac68
CM
1722/*
1723 * when reads are done, we need to check csums to verify the data is correct
1724 * if there's a match, we allow the bio to finish. If not, we go through
1725 * the io_failure_record routines to find good copies
1726 */
b2950863 1727static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
70dec807 1728 struct extent_state *state)
07157aac 1729{
35ebb934 1730 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
07157aac 1731 struct inode *inode = page->mapping->host;
d1310b2e 1732 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
07157aac 1733 char *kaddr;
aadfeb6e 1734 u64 private = ~(u32)0;
07157aac 1735 int ret;
ff79f819
CM
1736 struct btrfs_root *root = BTRFS_I(inode)->root;
1737 u32 csum = ~(u32)0;
d1310b2e 1738
d20f7043
CM
1739 if (PageChecked(page)) {
1740 ClearPageChecked(page);
1741 goto good;
1742 }
17d217fe
YZ
1743 if (btrfs_test_flag(inode, NODATASUM))
1744 return 0;
1745
1746 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
1747 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1)) {
1748 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1749 GFP_NOFS);
b6cda9bc 1750 return 0;
17d217fe 1751 }
d20f7043 1752
c2e639f0 1753 if (state && state->start == start) {
70dec807
CM
1754 private = state->private;
1755 ret = 0;
1756 } else {
1757 ret = get_state_private(io_tree, start, &private);
1758 }
9ab86c8e 1759 kaddr = kmap_atomic(page, KM_USER0);
d397712b 1760 if (ret)
07157aac 1761 goto zeroit;
d397712b 1762
ff79f819
CM
1763 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1764 btrfs_csum_final(csum, (char *)&csum);
d397712b 1765 if (csum != private)
07157aac 1766 goto zeroit;
d397712b 1767
9ab86c8e 1768 kunmap_atomic(kaddr, KM_USER0);
d20f7043 1769good:
7e38326f
CM
1770 /* if the io failure tree for this inode is non-empty,
1771 * check to see if we've recovered from a failed IO
1772 */
1259ab75 1773 btrfs_clean_io_failures(inode, start);
07157aac
CM
1774 return 0;
1775
1776zeroit:
d397712b
CM
1777 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1778 "private %llu\n", page->mapping->host->i_ino,
1779 (unsigned long long)start, csum,
1780 (unsigned long long)private);
db94535d
CM
1781 memset(kaddr + offset, 1, end - start + 1);
1782 flush_dcache_page(page);
9ab86c8e 1783 kunmap_atomic(kaddr, KM_USER0);
3b951516
CM
1784 if (private == 0)
1785 return 0;
7e38326f 1786 return -EIO;
07157aac 1787}
b888db2b 1788
7b128766
JB
1789/*
1790 * This creates an orphan entry for the given inode in case something goes
1791 * wrong in the middle of an unlink/truncate.
1792 */
1793int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
1794{
1795 struct btrfs_root *root = BTRFS_I(inode)->root;
1796 int ret = 0;
1797
bcc63abb 1798 spin_lock(&root->list_lock);
7b128766
JB
1799
1800 /* already on the orphan list, we're good */
1801 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
bcc63abb 1802 spin_unlock(&root->list_lock);
7b128766
JB
1803 return 0;
1804 }
1805
1806 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
1807
bcc63abb 1808 spin_unlock(&root->list_lock);
7b128766
JB
1809
1810 /*
1811 * insert an orphan item to track this unlinked/truncated file
1812 */
1813 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
1814
1815 return ret;
1816}
1817
1818/*
1819 * We have done the truncate/delete so we can go ahead and remove the orphan
1820 * item for this particular inode.
1821 */
1822int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
1823{
1824 struct btrfs_root *root = BTRFS_I(inode)->root;
1825 int ret = 0;
1826
bcc63abb 1827 spin_lock(&root->list_lock);
7b128766
JB
1828
1829 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
bcc63abb 1830 spin_unlock(&root->list_lock);
7b128766
JB
1831 return 0;
1832 }
1833
1834 list_del_init(&BTRFS_I(inode)->i_orphan);
1835 if (!trans) {
bcc63abb 1836 spin_unlock(&root->list_lock);
7b128766
JB
1837 return 0;
1838 }
1839
bcc63abb 1840 spin_unlock(&root->list_lock);
7b128766
JB
1841
1842 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
1843
1844 return ret;
1845}
1846
1847/*
1848 * this cleans up any orphans that may be left on the list from the last use
1849 * of this root.
1850 */
1851void btrfs_orphan_cleanup(struct btrfs_root *root)
1852{
1853 struct btrfs_path *path;
1854 struct extent_buffer *leaf;
1855 struct btrfs_item *item;
1856 struct btrfs_key key, found_key;
1857 struct btrfs_trans_handle *trans;
1858 struct inode *inode;
1859 int ret = 0, nr_unlink = 0, nr_truncate = 0;
1860
7b128766
JB
1861 path = btrfs_alloc_path();
1862 if (!path)
1863 return;
1864 path->reada = -1;
1865
1866 key.objectid = BTRFS_ORPHAN_OBJECTID;
1867 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1868 key.offset = (u64)-1;
1869
7b128766
JB
1870
1871 while (1) {
1872 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1873 if (ret < 0) {
1874 printk(KERN_ERR "Error searching slot for orphan: %d"
1875 "\n", ret);
1876 break;
1877 }
1878
1879 /*
1880 * if ret == 0 means we found what we were searching for, which
1881 * is weird, but possible, so only screw with path if we didnt
1882 * find the key and see if we have stuff that matches
1883 */
1884 if (ret > 0) {
1885 if (path->slots[0] == 0)
1886 break;
1887 path->slots[0]--;
1888 }
1889
1890 /* pull out the item */
1891 leaf = path->nodes[0];
1892 item = btrfs_item_nr(leaf, path->slots[0]);
1893 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1894
1895 /* make sure the item matches what we want */
1896 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
1897 break;
1898 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
1899 break;
1900
1901 /* release the path since we're done with it */
1902 btrfs_release_path(root, path);
1903
1904 /*
1905 * this is where we are basically btrfs_lookup, without the
1906 * crossing root thing. we store the inode number in the
1907 * offset of the orphan item.
1908 */
5b21f2ed 1909 inode = btrfs_iget_locked(root->fs_info->sb,
7b128766
JB
1910 found_key.offset, root);
1911 if (!inode)
1912 break;
1913
1914 if (inode->i_state & I_NEW) {
1915 BTRFS_I(inode)->root = root;
1916
1917 /* have to set the location manually */
1918 BTRFS_I(inode)->location.objectid = inode->i_ino;
1919 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
1920 BTRFS_I(inode)->location.offset = 0;
1921
1922 btrfs_read_locked_inode(inode);
1923 unlock_new_inode(inode);
1924 }
1925
1926 /*
1927 * add this inode to the orphan list so btrfs_orphan_del does
1928 * the proper thing when we hit it
1929 */
bcc63abb 1930 spin_lock(&root->list_lock);
7b128766 1931 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
bcc63abb 1932 spin_unlock(&root->list_lock);
7b128766
JB
1933
1934 /*
1935 * if this is a bad inode, means we actually succeeded in
1936 * removing the inode, but not the orphan record, which means
1937 * we need to manually delete the orphan since iput will just
1938 * do a destroy_inode
1939 */
1940 if (is_bad_inode(inode)) {
5b21f2ed 1941 trans = btrfs_start_transaction(root, 1);
7b128766 1942 btrfs_orphan_del(trans, inode);
5b21f2ed 1943 btrfs_end_transaction(trans, root);
7b128766
JB
1944 iput(inode);
1945 continue;
1946 }
1947
1948 /* if we have links, this was a truncate, lets do that */
1949 if (inode->i_nlink) {
1950 nr_truncate++;
1951 btrfs_truncate(inode);
1952 } else {
1953 nr_unlink++;
1954 }
1955
1956 /* this will do delete_inode and everything for us */
1957 iput(inode);
1958 }
1959
1960 if (nr_unlink)
1961 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1962 if (nr_truncate)
1963 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1964
1965 btrfs_free_path(path);
7b128766
JB
1966}
1967
d352ac68
CM
1968/*
1969 * read an inode from the btree into the in-memory inode
1970 */
39279cc3
CM
1971void btrfs_read_locked_inode(struct inode *inode)
1972{
1973 struct btrfs_path *path;
5f39d397 1974 struct extent_buffer *leaf;
39279cc3 1975 struct btrfs_inode_item *inode_item;
0b86a832 1976 struct btrfs_timespec *tspec;
39279cc3
CM
1977 struct btrfs_root *root = BTRFS_I(inode)->root;
1978 struct btrfs_key location;
1979 u64 alloc_group_block;
618e21d5 1980 u32 rdev;
39279cc3
CM
1981 int ret;
1982
1983 path = btrfs_alloc_path();
1984 BUG_ON(!path);
39279cc3 1985 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
dc17ff8f 1986
39279cc3 1987 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
5f39d397 1988 if (ret)
39279cc3 1989 goto make_bad;
39279cc3 1990
5f39d397
CM
1991 leaf = path->nodes[0];
1992 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1993 struct btrfs_inode_item);
1994
1995 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1996 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1997 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1998 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
dbe674a9 1999 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
5f39d397
CM
2000
2001 tspec = btrfs_inode_atime(inode_item);
2002 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2003 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2004
2005 tspec = btrfs_inode_mtime(inode_item);
2006 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2007 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2008
2009 tspec = btrfs_inode_ctime(inode_item);
2010 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2011 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2012
a76a3cd4 2013 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
e02119d5 2014 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
c3027eb5 2015 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
e02119d5 2016 inode->i_generation = BTRFS_I(inode)->generation;
618e21d5 2017 inode->i_rdev = 0;
5f39d397
CM
2018 rdev = btrfs_inode_rdev(leaf, inode_item);
2019
aec7477b 2020 BTRFS_I(inode)->index_cnt = (u64)-1;
d2fb3437 2021 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
aec7477b 2022
5f39d397 2023 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
d2fb3437
YZ
2024 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2025 alloc_group_block, 0);
39279cc3
CM
2026 btrfs_free_path(path);
2027 inode_item = NULL;
2028
39279cc3 2029 switch (inode->i_mode & S_IFMT) {
39279cc3
CM
2030 case S_IFREG:
2031 inode->i_mapping->a_ops = &btrfs_aops;
04160088 2032 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
d1310b2e 2033 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
39279cc3
CM
2034 inode->i_fop = &btrfs_file_operations;
2035 inode->i_op = &btrfs_file_inode_operations;
2036 break;
2037 case S_IFDIR:
2038 inode->i_fop = &btrfs_dir_file_operations;
2039 if (root == root->fs_info->tree_root)
2040 inode->i_op = &btrfs_dir_ro_inode_operations;
2041 else
2042 inode->i_op = &btrfs_dir_inode_operations;
2043 break;
2044 case S_IFLNK:
2045 inode->i_op = &btrfs_symlink_inode_operations;
2046 inode->i_mapping->a_ops = &btrfs_symlink_aops;
04160088 2047 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
39279cc3 2048 break;
618e21d5 2049 default:
0279b4cd 2050 inode->i_op = &btrfs_special_inode_operations;
618e21d5
JB
2051 init_special_inode(inode, inode->i_mode, rdev);
2052 break;
39279cc3
CM
2053 }
2054 return;
2055
2056make_bad:
39279cc3 2057 btrfs_free_path(path);
39279cc3
CM
2058 make_bad_inode(inode);
2059}
2060
d352ac68
CM
2061/*
2062 * given a leaf and an inode, copy the inode fields into the leaf
2063 */
e02119d5
CM
2064static void fill_inode_item(struct btrfs_trans_handle *trans,
2065 struct extent_buffer *leaf,
5f39d397 2066 struct btrfs_inode_item *item,
39279cc3
CM
2067 struct inode *inode)
2068{
5f39d397
CM
2069 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2070 btrfs_set_inode_gid(leaf, item, inode->i_gid);
dbe674a9 2071 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
5f39d397
CM
2072 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2073 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2074
2075 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2076 inode->i_atime.tv_sec);
2077 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2078 inode->i_atime.tv_nsec);
2079
2080 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2081 inode->i_mtime.tv_sec);
2082 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2083 inode->i_mtime.tv_nsec);
2084
2085 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2086 inode->i_ctime.tv_sec);
2087 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2088 inode->i_ctime.tv_nsec);
2089
a76a3cd4 2090 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
e02119d5 2091 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
c3027eb5 2092 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
e02119d5 2093 btrfs_set_inode_transid(leaf, item, trans->transid);
5f39d397 2094 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
b98b6767 2095 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
d2fb3437 2096 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
39279cc3
CM
2097}
2098
d352ac68
CM
2099/*
2100 * copy everything in the in-memory inode into the btree.
2101 */
d397712b
CM
2102noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2103 struct btrfs_root *root, struct inode *inode)
39279cc3
CM
2104{
2105 struct btrfs_inode_item *inode_item;
2106 struct btrfs_path *path;
5f39d397 2107 struct extent_buffer *leaf;
39279cc3
CM
2108 int ret;
2109
2110 path = btrfs_alloc_path();
2111 BUG_ON(!path);
39279cc3
CM
2112 ret = btrfs_lookup_inode(trans, root, path,
2113 &BTRFS_I(inode)->location, 1);
2114 if (ret) {
2115 if (ret > 0)
2116 ret = -ENOENT;
2117 goto failed;
2118 }
2119
5f39d397
CM
2120 leaf = path->nodes[0];
2121 inode_item = btrfs_item_ptr(leaf, path->slots[0],
39279cc3
CM
2122 struct btrfs_inode_item);
2123
e02119d5 2124 fill_inode_item(trans, leaf, inode_item, inode);
5f39d397 2125 btrfs_mark_buffer_dirty(leaf);
15ee9bc7 2126 btrfs_set_inode_last_trans(trans, inode);
39279cc3
CM
2127 ret = 0;
2128failed:
39279cc3
CM
2129 btrfs_free_path(path);
2130 return ret;
2131}
2132
2133
d352ac68
CM
2134/*
2135 * unlink helper that gets used here in inode.c and in the tree logging
2136 * recovery code. It remove a link in a directory with a given name, and
2137 * also drops the back refs in the inode to the directory
2138 */
e02119d5
CM
2139int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2140 struct btrfs_root *root,
2141 struct inode *dir, struct inode *inode,
2142 const char *name, int name_len)
39279cc3
CM
2143{
2144 struct btrfs_path *path;
39279cc3 2145 int ret = 0;
5f39d397 2146 struct extent_buffer *leaf;
39279cc3 2147 struct btrfs_dir_item *di;
5f39d397 2148 struct btrfs_key key;
aec7477b 2149 u64 index;
39279cc3
CM
2150
2151 path = btrfs_alloc_path();
54aa1f4d
CM
2152 if (!path) {
2153 ret = -ENOMEM;
2154 goto err;
2155 }
2156
39279cc3
CM
2157 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2158 name, name_len, -1);
2159 if (IS_ERR(di)) {
2160 ret = PTR_ERR(di);
2161 goto err;
2162 }
2163 if (!di) {
2164 ret = -ENOENT;
2165 goto err;
2166 }
5f39d397
CM
2167 leaf = path->nodes[0];
2168 btrfs_dir_item_key_to_cpu(leaf, di, &key);
39279cc3 2169 ret = btrfs_delete_one_dir_name(trans, root, path, di);
54aa1f4d
CM
2170 if (ret)
2171 goto err;
39279cc3
CM
2172 btrfs_release_path(root, path);
2173
aec7477b 2174 ret = btrfs_del_inode_ref(trans, root, name, name_len,
e02119d5
CM
2175 inode->i_ino,
2176 dir->i_ino, &index);
aec7477b 2177 if (ret) {
d397712b 2178 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
aec7477b 2179 "inode %lu parent %lu\n", name_len, name,
e02119d5 2180 inode->i_ino, dir->i_ino);
aec7477b
JB
2181 goto err;
2182 }
2183
39279cc3 2184 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
aec7477b 2185 index, name, name_len, -1);
39279cc3
CM
2186 if (IS_ERR(di)) {
2187 ret = PTR_ERR(di);
2188 goto err;
2189 }
2190 if (!di) {
2191 ret = -ENOENT;
2192 goto err;
2193 }
2194 ret = btrfs_delete_one_dir_name(trans, root, path, di);
925baedd 2195 btrfs_release_path(root, path);
39279cc3 2196
e02119d5
CM
2197 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2198 inode, dir->i_ino);
49eb7e46
CM
2199 BUG_ON(ret != 0 && ret != -ENOENT);
2200 if (ret != -ENOENT)
2201 BTRFS_I(dir)->log_dirty_trans = trans->transid;
e02119d5
CM
2202
2203 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2204 dir, index);
2205 BUG_ON(ret);
39279cc3
CM
2206err:
2207 btrfs_free_path(path);
e02119d5
CM
2208 if (ret)
2209 goto out;
2210
2211 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2212 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2213 btrfs_update_inode(trans, root, dir);
2214 btrfs_drop_nlink(inode);
2215 ret = btrfs_update_inode(trans, root, inode);
2216 dir->i_sb->s_dirt = 1;
2217out:
39279cc3
CM
2218 return ret;
2219}
2220
2221static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2222{
2223 struct btrfs_root *root;
2224 struct btrfs_trans_handle *trans;
7b128766 2225 struct inode *inode = dentry->d_inode;
39279cc3 2226 int ret;
1832a6d5 2227 unsigned long nr = 0;
39279cc3
CM
2228
2229 root = BTRFS_I(dir)->root;
1832a6d5
CM
2230
2231 ret = btrfs_check_free_space(root, 1, 1);
2232 if (ret)
2233 goto fail;
2234
39279cc3 2235 trans = btrfs_start_transaction(root, 1);
5f39d397 2236
39279cc3 2237 btrfs_set_trans_block_group(trans, dir);
e02119d5
CM
2238 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2239 dentry->d_name.name, dentry->d_name.len);
7b128766
JB
2240
2241 if (inode->i_nlink == 0)
2242 ret = btrfs_orphan_add(trans, inode);
2243
d3c2fdcf 2244 nr = trans->blocks_used;
5f39d397 2245
89ce8a63 2246 btrfs_end_transaction_throttle(trans, root);
1832a6d5 2247fail:
d3c2fdcf 2248 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
2249 return ret;
2250}
2251
2252static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2253{
2254 struct inode *inode = dentry->d_inode;
1832a6d5 2255 int err = 0;
39279cc3
CM
2256 int ret;
2257 struct btrfs_root *root = BTRFS_I(dir)->root;
39279cc3 2258 struct btrfs_trans_handle *trans;
1832a6d5 2259 unsigned long nr = 0;
39279cc3 2260
3394e160
CM
2261 /*
2262 * the FIRST_FREE_OBJECTID check makes sure we don't try to rmdir
2263 * the root of a subvolume or snapshot
2264 */
2265 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
2266 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
134d4512 2267 return -ENOTEMPTY;
925baedd 2268 }
134d4512 2269
1832a6d5
CM
2270 ret = btrfs_check_free_space(root, 1, 1);
2271 if (ret)
2272 goto fail;
2273
39279cc3
CM
2274 trans = btrfs_start_transaction(root, 1);
2275 btrfs_set_trans_block_group(trans, dir);
39279cc3 2276
7b128766
JB
2277 err = btrfs_orphan_add(trans, inode);
2278 if (err)
2279 goto fail_trans;
2280
39279cc3 2281 /* now the directory is empty */
e02119d5
CM
2282 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2283 dentry->d_name.name, dentry->d_name.len);
d397712b 2284 if (!err)
dbe674a9 2285 btrfs_i_size_write(inode, 0);
3954401f 2286
7b128766 2287fail_trans:
d3c2fdcf 2288 nr = trans->blocks_used;
89ce8a63 2289 ret = btrfs_end_transaction_throttle(trans, root);
1832a6d5 2290fail:
d3c2fdcf 2291 btrfs_btree_balance_dirty(root, nr);
3954401f 2292
39279cc3
CM
2293 if (ret && !err)
2294 err = ret;
2295 return err;
2296}
2297
d20f7043 2298#if 0
323ac95b
CM
2299/*
2300 * when truncating bytes in a file, it is possible to avoid reading
2301 * the leaves that contain only checksum items. This can be the
2302 * majority of the IO required to delete a large file, but it must
2303 * be done carefully.
2304 *
2305 * The keys in the level just above the leaves are checked to make sure
2306 * the lowest key in a given leaf is a csum key, and starts at an offset
2307 * after the new size.
2308 *
2309 * Then the key for the next leaf is checked to make sure it also has
2310 * a checksum item for the same file. If it does, we know our target leaf
2311 * contains only checksum items, and it can be safely freed without reading
2312 * it.
2313 *
2314 * This is just an optimization targeted at large files. It may do
2315 * nothing. It will return 0 unless things went badly.
2316 */
2317static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2318 struct btrfs_root *root,
2319 struct btrfs_path *path,
2320 struct inode *inode, u64 new_size)
2321{
2322 struct btrfs_key key;
2323 int ret;
2324 int nritems;
2325 struct btrfs_key found_key;
2326 struct btrfs_key other_key;
5b84e8d6
YZ
2327 struct btrfs_leaf_ref *ref;
2328 u64 leaf_gen;
2329 u64 leaf_start;
323ac95b
CM
2330
2331 path->lowest_level = 1;
2332 key.objectid = inode->i_ino;
2333 key.type = BTRFS_CSUM_ITEM_KEY;
2334 key.offset = new_size;
2335again:
2336 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2337 if (ret < 0)
2338 goto out;
2339
2340 if (path->nodes[1] == NULL) {
2341 ret = 0;
2342 goto out;
2343 }
2344 ret = 0;
2345 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2346 nritems = btrfs_header_nritems(path->nodes[1]);
2347
2348 if (!nritems)
2349 goto out;
2350
2351 if (path->slots[1] >= nritems)
2352 goto next_node;
2353
2354 /* did we find a key greater than anything we want to delete? */
2355 if (found_key.objectid > inode->i_ino ||
2356 (found_key.objectid == inode->i_ino && found_key.type > key.type))
2357 goto out;
2358
2359 /* we check the next key in the node to make sure the leave contains
2360 * only checksum items. This comparison doesn't work if our
2361 * leaf is the last one in the node
2362 */
2363 if (path->slots[1] + 1 >= nritems) {
2364next_node:
2365 /* search forward from the last key in the node, this
2366 * will bring us into the next node in the tree
2367 */
2368 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2369
2370 /* unlikely, but we inc below, so check to be safe */
2371 if (found_key.offset == (u64)-1)
2372 goto out;
2373
2374 /* search_forward needs a path with locks held, do the
2375 * search again for the original key. It is possible
2376 * this will race with a balance and return a path that
2377 * we could modify, but this drop is just an optimization
2378 * and is allowed to miss some leaves.
2379 */
2380 btrfs_release_path(root, path);
2381 found_key.offset++;
2382
2383 /* setup a max key for search_forward */
2384 other_key.offset = (u64)-1;
2385 other_key.type = key.type;
2386 other_key.objectid = key.objectid;
2387
2388 path->keep_locks = 1;
2389 ret = btrfs_search_forward(root, &found_key, &other_key,
2390 path, 0, 0);
2391 path->keep_locks = 0;
2392 if (ret || found_key.objectid != key.objectid ||
2393 found_key.type != key.type) {
2394 ret = 0;
2395 goto out;
2396 }
2397
2398 key.offset = found_key.offset;
2399 btrfs_release_path(root, path);
2400 cond_resched();
2401 goto again;
2402 }
2403
2404 /* we know there's one more slot after us in the tree,
2405 * read that key so we can verify it is also a checksum item
2406 */
2407 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2408
2409 if (found_key.objectid < inode->i_ino)
2410 goto next_key;
2411
2412 if (found_key.type != key.type || found_key.offset < new_size)
2413 goto next_key;
2414
2415 /*
2416 * if the key for the next leaf isn't a csum key from this objectid,
2417 * we can't be sure there aren't good items inside this leaf.
2418 * Bail out
2419 */
2420 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2421 goto out;
2422
5b84e8d6
YZ
2423 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2424 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
323ac95b
CM
2425 /*
2426 * it is safe to delete this leaf, it contains only
2427 * csum items from this inode at an offset >= new_size
2428 */
5b84e8d6 2429 ret = btrfs_del_leaf(trans, root, path, leaf_start);
323ac95b
CM
2430 BUG_ON(ret);
2431
5b84e8d6
YZ
2432 if (root->ref_cows && leaf_gen < trans->transid) {
2433 ref = btrfs_alloc_leaf_ref(root, 0);
2434 if (ref) {
2435 ref->root_gen = root->root_key.offset;
2436 ref->bytenr = leaf_start;
2437 ref->owner = 0;
2438 ref->generation = leaf_gen;
2439 ref->nritems = 0;
2440
2441 ret = btrfs_add_leaf_ref(root, ref, 0);
2442 WARN_ON(ret);
2443 btrfs_free_leaf_ref(root, ref);
2444 } else {
2445 WARN_ON(1);
2446 }
2447 }
323ac95b
CM
2448next_key:
2449 btrfs_release_path(root, path);
2450
2451 if (other_key.objectid == inode->i_ino &&
2452 other_key.type == key.type && other_key.offset > key.offset) {
2453 key.offset = other_key.offset;
2454 cond_resched();
2455 goto again;
2456 }
2457 ret = 0;
2458out:
2459 /* fixup any changes we've made to the path */
2460 path->lowest_level = 0;
2461 path->keep_locks = 0;
2462 btrfs_release_path(root, path);
2463 return ret;
2464}
2465
d20f7043
CM
2466#endif
2467
39279cc3
CM
2468/*
2469 * this can truncate away extent items, csum items and directory items.
2470 * It starts at a high offset and removes keys until it can't find
d352ac68 2471 * any higher than new_size
39279cc3
CM
2472 *
2473 * csum items that cross the new i_size are truncated to the new size
2474 * as well.
7b128766
JB
2475 *
2476 * min_type is the minimum key type to truncate down to. If set to 0, this
2477 * will kill all the items on this inode, including the INODE_ITEM_KEY.
39279cc3 2478 */
e02119d5
CM
2479noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2480 struct btrfs_root *root,
2481 struct inode *inode,
2482 u64 new_size, u32 min_type)
39279cc3
CM
2483{
2484 int ret;
2485 struct btrfs_path *path;
2486 struct btrfs_key key;
5f39d397 2487 struct btrfs_key found_key;
39279cc3 2488 u32 found_type;
5f39d397 2489 struct extent_buffer *leaf;
39279cc3
CM
2490 struct btrfs_file_extent_item *fi;
2491 u64 extent_start = 0;
db94535d 2492 u64 extent_num_bytes = 0;
39279cc3 2493 u64 item_end = 0;
7bb86316 2494 u64 root_gen = 0;
d8d5f3e1 2495 u64 root_owner = 0;
39279cc3
CM
2496 int found_extent;
2497 int del_item;
85e21bac
CM
2498 int pending_del_nr = 0;
2499 int pending_del_slot = 0;
179e29e4 2500 int extent_type = -1;
771ed689 2501 int encoding;
3b951516 2502 u64 mask = root->sectorsize - 1;
39279cc3 2503
e02119d5 2504 if (root->ref_cows)
5b21f2ed 2505 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
39279cc3 2506 path = btrfs_alloc_path();
3c69faec 2507 path->reada = -1;
39279cc3 2508 BUG_ON(!path);
5f39d397 2509
39279cc3
CM
2510 /* FIXME, add redo link to tree so we don't leak on crash */
2511 key.objectid = inode->i_ino;
2512 key.offset = (u64)-1;
5f39d397
CM
2513 key.type = (u8)-1;
2514
85e21bac 2515 btrfs_init_path(path);
323ac95b 2516
85e21bac
CM
2517search_again:
2518 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
d397712b 2519 if (ret < 0)
85e21bac 2520 goto error;
d397712b 2521
85e21bac 2522 if (ret > 0) {
e02119d5
CM
2523 /* there are no items in the tree for us to truncate, we're
2524 * done
2525 */
2526 if (path->slots[0] == 0) {
2527 ret = 0;
2528 goto error;
2529 }
85e21bac
CM
2530 path->slots[0]--;
2531 }
2532
d397712b 2533 while (1) {
39279cc3 2534 fi = NULL;
5f39d397
CM
2535 leaf = path->nodes[0];
2536 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2537 found_type = btrfs_key_type(&found_key);
771ed689 2538 encoding = 0;
39279cc3 2539
5f39d397 2540 if (found_key.objectid != inode->i_ino)
39279cc3 2541 break;
5f39d397 2542
85e21bac 2543 if (found_type < min_type)
39279cc3
CM
2544 break;
2545
5f39d397 2546 item_end = found_key.offset;
39279cc3 2547 if (found_type == BTRFS_EXTENT_DATA_KEY) {
5f39d397 2548 fi = btrfs_item_ptr(leaf, path->slots[0],
39279cc3 2549 struct btrfs_file_extent_item);
179e29e4 2550 extent_type = btrfs_file_extent_type(leaf, fi);
771ed689
CM
2551 encoding = btrfs_file_extent_compression(leaf, fi);
2552 encoding |= btrfs_file_extent_encryption(leaf, fi);
2553 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
2554
179e29e4 2555 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
5f39d397 2556 item_end +=
db94535d 2557 btrfs_file_extent_num_bytes(leaf, fi);
179e29e4 2558 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
179e29e4 2559 item_end += btrfs_file_extent_inline_len(leaf,
c8b97818 2560 fi);
39279cc3 2561 }
008630c1 2562 item_end--;
39279cc3 2563 }
e02119d5 2564 if (item_end < new_size) {
d397712b 2565 if (found_type == BTRFS_DIR_ITEM_KEY)
b888db2b 2566 found_type = BTRFS_INODE_ITEM_KEY;
d397712b 2567 else if (found_type == BTRFS_EXTENT_ITEM_KEY)
d20f7043 2568 found_type = BTRFS_EXTENT_DATA_KEY;
d397712b 2569 else if (found_type == BTRFS_EXTENT_DATA_KEY)
85e21bac 2570 found_type = BTRFS_XATTR_ITEM_KEY;
d397712b 2571 else if (found_type == BTRFS_XATTR_ITEM_KEY)
85e21bac 2572 found_type = BTRFS_INODE_REF_KEY;
d397712b 2573 else if (found_type)
b888db2b 2574 found_type--;
d397712b 2575 else
b888db2b 2576 break;
a61721d5 2577 btrfs_set_key_type(&key, found_type);
85e21bac 2578 goto next;
39279cc3 2579 }
e02119d5 2580 if (found_key.offset >= new_size)
39279cc3
CM
2581 del_item = 1;
2582 else
2583 del_item = 0;
2584 found_extent = 0;
2585
2586 /* FIXME, shrink the extent if the ref count is only 1 */
179e29e4
CM
2587 if (found_type != BTRFS_EXTENT_DATA_KEY)
2588 goto delete;
2589
2590 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
39279cc3 2591 u64 num_dec;
db94535d 2592 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
771ed689 2593 if (!del_item && !encoding) {
db94535d
CM
2594 u64 orig_num_bytes =
2595 btrfs_file_extent_num_bytes(leaf, fi);
e02119d5 2596 extent_num_bytes = new_size -
5f39d397 2597 found_key.offset + root->sectorsize - 1;
b1632b10
Y
2598 extent_num_bytes = extent_num_bytes &
2599 ~((u64)root->sectorsize - 1);
db94535d
CM
2600 btrfs_set_file_extent_num_bytes(leaf, fi,
2601 extent_num_bytes);
2602 num_dec = (orig_num_bytes -
9069218d 2603 extent_num_bytes);
e02119d5 2604 if (root->ref_cows && extent_start != 0)
a76a3cd4 2605 inode_sub_bytes(inode, num_dec);
5f39d397 2606 btrfs_mark_buffer_dirty(leaf);
39279cc3 2607 } else {
db94535d
CM
2608 extent_num_bytes =
2609 btrfs_file_extent_disk_num_bytes(leaf,
2610 fi);
39279cc3 2611 /* FIXME blocksize != 4096 */
9069218d 2612 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
39279cc3
CM
2613 if (extent_start != 0) {
2614 found_extent = 1;
e02119d5 2615 if (root->ref_cows)
a76a3cd4 2616 inode_sub_bytes(inode, num_dec);
e02119d5 2617 }
31840ae1 2618 root_gen = btrfs_header_generation(leaf);
d8d5f3e1 2619 root_owner = btrfs_header_owner(leaf);
39279cc3 2620 }
9069218d 2621 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
c8b97818
CM
2622 /*
2623 * we can't truncate inline items that have had
2624 * special encodings
2625 */
2626 if (!del_item &&
2627 btrfs_file_extent_compression(leaf, fi) == 0 &&
2628 btrfs_file_extent_encryption(leaf, fi) == 0 &&
2629 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
e02119d5
CM
2630 u32 size = new_size - found_key.offset;
2631
2632 if (root->ref_cows) {
a76a3cd4
YZ
2633 inode_sub_bytes(inode, item_end + 1 -
2634 new_size);
e02119d5
CM
2635 }
2636 size =
2637 btrfs_file_extent_calc_inline_size(size);
9069218d 2638 ret = btrfs_truncate_item(trans, root, path,
e02119d5 2639 size, 1);
9069218d 2640 BUG_ON(ret);
e02119d5 2641 } else if (root->ref_cows) {
a76a3cd4
YZ
2642 inode_sub_bytes(inode, item_end + 1 -
2643 found_key.offset);
9069218d 2644 }
39279cc3 2645 }
179e29e4 2646delete:
39279cc3 2647 if (del_item) {
85e21bac
CM
2648 if (!pending_del_nr) {
2649 /* no pending yet, add ourselves */
2650 pending_del_slot = path->slots[0];
2651 pending_del_nr = 1;
2652 } else if (pending_del_nr &&
2653 path->slots[0] + 1 == pending_del_slot) {
2654 /* hop on the pending chunk */
2655 pending_del_nr++;
2656 pending_del_slot = path->slots[0];
2657 } else {
d397712b 2658 BUG();
85e21bac 2659 }
39279cc3
CM
2660 } else {
2661 break;
2662 }
39279cc3
CM
2663 if (found_extent) {
2664 ret = btrfs_free_extent(trans, root, extent_start,
7bb86316 2665 extent_num_bytes,
31840ae1 2666 leaf->start, root_owner,
3bb1a1bc 2667 root_gen, inode->i_ino, 0);
39279cc3
CM
2668 BUG_ON(ret);
2669 }
85e21bac
CM
2670next:
2671 if (path->slots[0] == 0) {
2672 if (pending_del_nr)
2673 goto del_pending;
2674 btrfs_release_path(root, path);
2675 goto search_again;
2676 }
2677
2678 path->slots[0]--;
2679 if (pending_del_nr &&
2680 path->slots[0] + 1 != pending_del_slot) {
2681 struct btrfs_key debug;
2682del_pending:
2683 btrfs_item_key_to_cpu(path->nodes[0], &debug,
2684 pending_del_slot);
2685 ret = btrfs_del_items(trans, root, path,
2686 pending_del_slot,
2687 pending_del_nr);
2688 BUG_ON(ret);
2689 pending_del_nr = 0;
2690 btrfs_release_path(root, path);
2691 goto search_again;
2692 }
39279cc3
CM
2693 }
2694 ret = 0;
2695error:
85e21bac
CM
2696 if (pending_del_nr) {
2697 ret = btrfs_del_items(trans, root, path, pending_del_slot,
2698 pending_del_nr);
2699 }
39279cc3
CM
2700 btrfs_free_path(path);
2701 inode->i_sb->s_dirt = 1;
2702 return ret;
2703}
2704
2705/*
2706 * taken from block_truncate_page, but does cow as it zeros out
2707 * any bytes left in the last page in the file.
2708 */
2709static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
2710{
2711 struct inode *inode = mapping->host;
db94535d 2712 struct btrfs_root *root = BTRFS_I(inode)->root;
e6dcd2dc
CM
2713 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2714 struct btrfs_ordered_extent *ordered;
2715 char *kaddr;
db94535d 2716 u32 blocksize = root->sectorsize;
39279cc3
CM
2717 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2718 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2719 struct page *page;
39279cc3 2720 int ret = 0;
a52d9a80 2721 u64 page_start;
e6dcd2dc 2722 u64 page_end;
39279cc3
CM
2723
2724 if ((offset & (blocksize - 1)) == 0)
2725 goto out;
2726
2727 ret = -ENOMEM;
211c17f5 2728again:
39279cc3
CM
2729 page = grab_cache_page(mapping, index);
2730 if (!page)
2731 goto out;
e6dcd2dc
CM
2732
2733 page_start = page_offset(page);
2734 page_end = page_start + PAGE_CACHE_SIZE - 1;
2735
39279cc3 2736 if (!PageUptodate(page)) {
9ebefb18 2737 ret = btrfs_readpage(NULL, page);
39279cc3 2738 lock_page(page);
211c17f5
CM
2739 if (page->mapping != mapping) {
2740 unlock_page(page);
2741 page_cache_release(page);
2742 goto again;
2743 }
39279cc3
CM
2744 if (!PageUptodate(page)) {
2745 ret = -EIO;
89642229 2746 goto out_unlock;
39279cc3
CM
2747 }
2748 }
211c17f5 2749 wait_on_page_writeback(page);
e6dcd2dc
CM
2750
2751 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2752 set_page_extent_mapped(page);
2753
2754 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2755 if (ordered) {
2756 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2757 unlock_page(page);
2758 page_cache_release(page);
eb84ae03 2759 btrfs_start_ordered_extent(inode, ordered, 1);
e6dcd2dc
CM
2760 btrfs_put_ordered_extent(ordered);
2761 goto again;
2762 }
2763
ea8c2819 2764 btrfs_set_extent_delalloc(inode, page_start, page_end);
e6dcd2dc
CM
2765 ret = 0;
2766 if (offset != PAGE_CACHE_SIZE) {
2767 kaddr = kmap(page);
2768 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
2769 flush_dcache_page(page);
2770 kunmap(page);
2771 }
247e743c 2772 ClearPageChecked(page);
e6dcd2dc
CM
2773 set_page_dirty(page);
2774 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
39279cc3 2775
89642229 2776out_unlock:
39279cc3
CM
2777 unlock_page(page);
2778 page_cache_release(page);
2779out:
2780 return ret;
2781}
2782
9036c102 2783int btrfs_cont_expand(struct inode *inode, loff_t size)
39279cc3 2784{
9036c102
YZ
2785 struct btrfs_trans_handle *trans;
2786 struct btrfs_root *root = BTRFS_I(inode)->root;
2787 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2788 struct extent_map *em;
2789 u64 mask = root->sectorsize - 1;
2790 u64 hole_start = (inode->i_size + mask) & ~mask;
2791 u64 block_end = (size + mask) & ~mask;
2792 u64 last_byte;
2793 u64 cur_offset;
2794 u64 hole_size;
39279cc3
CM
2795 int err;
2796
9036c102
YZ
2797 if (size <= hole_start)
2798 return 0;
2799
2800 err = btrfs_check_free_space(root, 1, 0);
39279cc3
CM
2801 if (err)
2802 return err;
2803
9036c102 2804 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2bf5a725 2805
9036c102
YZ
2806 while (1) {
2807 struct btrfs_ordered_extent *ordered;
2808 btrfs_wait_ordered_range(inode, hole_start,
2809 block_end - hole_start);
2810 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2811 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
2812 if (!ordered)
2813 break;
2814 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2815 btrfs_put_ordered_extent(ordered);
2816 }
39279cc3 2817
9036c102
YZ
2818 trans = btrfs_start_transaction(root, 1);
2819 btrfs_set_trans_block_group(trans, inode);
39279cc3 2820
9036c102
YZ
2821 cur_offset = hole_start;
2822 while (1) {
2823 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
2824 block_end - cur_offset, 0);
2825 BUG_ON(IS_ERR(em) || !em);
2826 last_byte = min(extent_map_end(em), block_end);
2827 last_byte = (last_byte + mask) & ~mask;
2828 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
771ed689 2829 u64 hint_byte = 0;
9036c102 2830 hole_size = last_byte - cur_offset;
771ed689
CM
2831 err = btrfs_drop_extents(trans, root, inode,
2832 cur_offset,
2833 cur_offset + hole_size,
2834 cur_offset, &hint_byte);
2835 if (err)
2836 break;
9036c102
YZ
2837 err = btrfs_insert_file_extent(trans, root,
2838 inode->i_ino, cur_offset, 0,
2839 0, hole_size, 0, hole_size,
2840 0, 0, 0);
2841 btrfs_drop_extent_cache(inode, hole_start,
2842 last_byte - 1, 0);
2843 }
2844 free_extent_map(em);
2845 cur_offset = last_byte;
2846 if (err || cur_offset >= block_end)
2847 break;
2848 }
1832a6d5 2849
9036c102
YZ
2850 btrfs_end_transaction(trans, root);
2851 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2852 return err;
2853}
39279cc3 2854
9036c102
YZ
2855static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
2856{
2857 struct inode *inode = dentry->d_inode;
2858 int err;
39279cc3 2859
9036c102
YZ
2860 err = inode_change_ok(inode, attr);
2861 if (err)
2862 return err;
2bf5a725 2863
9036c102
YZ
2864 if (S_ISREG(inode->i_mode) &&
2865 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
2866 err = btrfs_cont_expand(inode, attr->ia_size);
54aa1f4d
CM
2867 if (err)
2868 return err;
39279cc3 2869 }
9036c102 2870
39279cc3 2871 err = inode_setattr(inode, attr);
33268eaf
JB
2872
2873 if (!err && ((attr->ia_valid & ATTR_MODE)))
2874 err = btrfs_acl_chmod(inode);
39279cc3
CM
2875 return err;
2876}
61295eb8 2877
39279cc3
CM
2878void btrfs_delete_inode(struct inode *inode)
2879{
2880 struct btrfs_trans_handle *trans;
2881 struct btrfs_root *root = BTRFS_I(inode)->root;
d3c2fdcf 2882 unsigned long nr;
39279cc3
CM
2883 int ret;
2884
2885 truncate_inode_pages(&inode->i_data, 0);
2886 if (is_bad_inode(inode)) {
7b128766 2887 btrfs_orphan_del(NULL, inode);
39279cc3
CM
2888 goto no_delete;
2889 }
4a096752 2890 btrfs_wait_ordered_range(inode, 0, (u64)-1);
5f39d397 2891
dbe674a9 2892 btrfs_i_size_write(inode, 0);
180591bc 2893 trans = btrfs_join_transaction(root, 1);
5f39d397 2894
39279cc3 2895 btrfs_set_trans_block_group(trans, inode);
e02119d5 2896 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
7b128766
JB
2897 if (ret) {
2898 btrfs_orphan_del(NULL, inode);
54aa1f4d 2899 goto no_delete_lock;
7b128766
JB
2900 }
2901
2902 btrfs_orphan_del(trans, inode);
85e21bac 2903
d3c2fdcf 2904 nr = trans->blocks_used;
85e21bac 2905 clear_inode(inode);
5f39d397 2906
39279cc3 2907 btrfs_end_transaction(trans, root);
d3c2fdcf 2908 btrfs_btree_balance_dirty(root, nr);
39279cc3 2909 return;
54aa1f4d
CM
2910
2911no_delete_lock:
d3c2fdcf 2912 nr = trans->blocks_used;
54aa1f4d 2913 btrfs_end_transaction(trans, root);
d3c2fdcf 2914 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
2915no_delete:
2916 clear_inode(inode);
2917}
2918
2919/*
2920 * this returns the key found in the dir entry in the location pointer.
2921 * If no dir entries were found, location->objectid is 0.
2922 */
2923static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
2924 struct btrfs_key *location)
2925{
2926 const char *name = dentry->d_name.name;
2927 int namelen = dentry->d_name.len;
2928 struct btrfs_dir_item *di;
2929 struct btrfs_path *path;
2930 struct btrfs_root *root = BTRFS_I(dir)->root;
0d9f7f3e 2931 int ret = 0;
39279cc3
CM
2932
2933 path = btrfs_alloc_path();
2934 BUG_ON(!path);
3954401f 2935
39279cc3
CM
2936 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
2937 namelen, 0);
0d9f7f3e
Y
2938 if (IS_ERR(di))
2939 ret = PTR_ERR(di);
d397712b
CM
2940
2941 if (!di || IS_ERR(di))
3954401f 2942 goto out_err;
d397712b 2943
5f39d397 2944 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
39279cc3 2945out:
39279cc3
CM
2946 btrfs_free_path(path);
2947 return ret;
3954401f
CM
2948out_err:
2949 location->objectid = 0;
2950 goto out;
39279cc3
CM
2951}
2952
2953/*
2954 * when we hit a tree root in a directory, the btrfs part of the inode
2955 * needs to be changed to reflect the root directory of the tree root. This
2956 * is kind of like crossing a mount point.
2957 */
2958static int fixup_tree_root_location(struct btrfs_root *root,
2959 struct btrfs_key *location,
58176a96
JB
2960 struct btrfs_root **sub_root,
2961 struct dentry *dentry)
39279cc3 2962{
39279cc3
CM
2963 struct btrfs_root_item *ri;
2964
2965 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
2966 return 0;
2967 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
2968 return 0;
2969
58176a96
JB
2970 *sub_root = btrfs_read_fs_root(root->fs_info, location,
2971 dentry->d_name.name,
2972 dentry->d_name.len);
39279cc3
CM
2973 if (IS_ERR(*sub_root))
2974 return PTR_ERR(*sub_root);
2975
2976 ri = &(*sub_root)->root_item;
2977 location->objectid = btrfs_root_dirid(ri);
39279cc3
CM
2978 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2979 location->offset = 0;
2980
39279cc3
CM
2981 return 0;
2982}
2983
e02119d5 2984static noinline void init_btrfs_i(struct inode *inode)
39279cc3 2985{
e02119d5
CM
2986 struct btrfs_inode *bi = BTRFS_I(inode);
2987
2988 bi->i_acl = NULL;
2989 bi->i_default_acl = NULL;
2990
2991 bi->generation = 0;
c3027eb5 2992 bi->sequence = 0;
e02119d5
CM
2993 bi->last_trans = 0;
2994 bi->logged_trans = 0;
2995 bi->delalloc_bytes = 0;
2996 bi->disk_i_size = 0;
2997 bi->flags = 0;
2998 bi->index_cnt = (u64)-1;
49eb7e46 2999 bi->log_dirty_trans = 0;
d1310b2e
CM
3000 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3001 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
b888db2b 3002 inode->i_mapping, GFP_NOFS);
7e38326f
CM
3003 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3004 inode->i_mapping, GFP_NOFS);
ea8c2819 3005 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
ba1da2f4 3006 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
ee6e6504 3007 mutex_init(&BTRFS_I(inode)->extent_mutex);
e02119d5
CM
3008 mutex_init(&BTRFS_I(inode)->log_mutex);
3009}
3010
3011static int btrfs_init_locked_inode(struct inode *inode, void *p)
3012{
3013 struct btrfs_iget_args *args = p;
3014 inode->i_ino = args->ino;
3015 init_btrfs_i(inode);
3016 BTRFS_I(inode)->root = args->root;
39279cc3
CM
3017 return 0;
3018}
3019
3020static int btrfs_find_actor(struct inode *inode, void *opaque)
3021{
3022 struct btrfs_iget_args *args = opaque;
d397712b
CM
3023 return args->ino == inode->i_ino &&
3024 args->root == BTRFS_I(inode)->root;
39279cc3
CM
3025}
3026
5b21f2ed
ZY
3027struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
3028 struct btrfs_root *root, int wait)
3029{
3030 struct inode *inode;
3031 struct btrfs_iget_args args;
3032 args.ino = objectid;
3033 args.root = root;
3034
3035 if (wait) {
3036 inode = ilookup5(s, objectid, btrfs_find_actor,
3037 (void *)&args);
3038 } else {
3039 inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
3040 (void *)&args);
3041 }
3042 return inode;
3043}
3044
39279cc3
CM
3045struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
3046 struct btrfs_root *root)
3047{
3048 struct inode *inode;
3049 struct btrfs_iget_args args;
3050 args.ino = objectid;
3051 args.root = root;
3052
3053 inode = iget5_locked(s, objectid, btrfs_find_actor,
3054 btrfs_init_locked_inode,
3055 (void *)&args);
3056 return inode;
3057}
3058
1a54ef8c
BR
3059/* Get an inode object given its location and corresponding root.
3060 * Returns in *is_new if the inode was read from disk
3061 */
3062struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
3063 struct btrfs_root *root, int *is_new)
3064{
3065 struct inode *inode;
3066
3067 inode = btrfs_iget_locked(s, location->objectid, root);
3068 if (!inode)
3069 return ERR_PTR(-EACCES);
3070
3071 if (inode->i_state & I_NEW) {
3072 BTRFS_I(inode)->root = root;
3073 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3074 btrfs_read_locked_inode(inode);
3075 unlock_new_inode(inode);
3076 if (is_new)
3077 *is_new = 1;
3078 } else {
3079 if (is_new)
3080 *is_new = 0;
3081 }
3082
3083 return inode;
3084}
3085
3de4586c 3086struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
39279cc3 3087{
d397712b 3088 struct inode *inode;
39279cc3
CM
3089 struct btrfs_inode *bi = BTRFS_I(dir);
3090 struct btrfs_root *root = bi->root;
3091 struct btrfs_root *sub_root = root;
3092 struct btrfs_key location;
c146afad 3093 int ret, new;
39279cc3
CM
3094
3095 if (dentry->d_name.len > BTRFS_NAME_LEN)
3096 return ERR_PTR(-ENAMETOOLONG);
5f39d397 3097
39279cc3 3098 ret = btrfs_inode_by_name(dir, dentry, &location);
5f39d397 3099
39279cc3
CM
3100 if (ret < 0)
3101 return ERR_PTR(ret);
5f39d397 3102
39279cc3
CM
3103 inode = NULL;
3104 if (location.objectid) {
58176a96
JB
3105 ret = fixup_tree_root_location(root, &location, &sub_root,
3106 dentry);
39279cc3
CM
3107 if (ret < 0)
3108 return ERR_PTR(ret);
3109 if (ret > 0)
3110 return ERR_PTR(-ENOENT);
1a54ef8c
BR
3111 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
3112 if (IS_ERR(inode))
3113 return ERR_CAST(inode);
39279cc3 3114 }
3de4586c
CM
3115 return inode;
3116}
3117
3118static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3119 struct nameidata *nd)
3120{
3121 struct inode *inode;
3122
3123 if (dentry->d_name.len > BTRFS_NAME_LEN)
3124 return ERR_PTR(-ENAMETOOLONG);
3125
3126 inode = btrfs_lookup_dentry(dir, dentry);
3127 if (IS_ERR(inode))
3128 return ERR_CAST(inode);
7b128766 3129
39279cc3
CM
3130 return d_splice_alias(inode, dentry);
3131}
3132
39279cc3
CM
3133static unsigned char btrfs_filetype_table[] = {
3134 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3135};
3136
cbdf5a24
DW
3137static int btrfs_real_readdir(struct file *filp, void *dirent,
3138 filldir_t filldir)
39279cc3 3139{
6da6abae 3140 struct inode *inode = filp->f_dentry->d_inode;
39279cc3
CM
3141 struct btrfs_root *root = BTRFS_I(inode)->root;
3142 struct btrfs_item *item;
3143 struct btrfs_dir_item *di;
3144 struct btrfs_key key;
5f39d397 3145 struct btrfs_key found_key;
39279cc3
CM
3146 struct btrfs_path *path;
3147 int ret;
3148 u32 nritems;
5f39d397 3149 struct extent_buffer *leaf;
39279cc3
CM
3150 int slot;
3151 int advance;
3152 unsigned char d_type;
3153 int over = 0;
3154 u32 di_cur;
3155 u32 di_total;
3156 u32 di_len;
3157 int key_type = BTRFS_DIR_INDEX_KEY;
5f39d397
CM
3158 char tmp_name[32];
3159 char *name_ptr;
3160 int name_len;
39279cc3
CM
3161
3162 /* FIXME, use a real flag for deciding about the key type */
3163 if (root->fs_info->tree_root == root)
3164 key_type = BTRFS_DIR_ITEM_KEY;
5f39d397 3165
3954401f
CM
3166 /* special case for "." */
3167 if (filp->f_pos == 0) {
3168 over = filldir(dirent, ".", 1,
3169 1, inode->i_ino,
3170 DT_DIR);
3171 if (over)
3172 return 0;
3173 filp->f_pos = 1;
3174 }
3954401f
CM
3175 /* special case for .., just use the back ref */
3176 if (filp->f_pos == 1) {
5ecc7e5d 3177 u64 pino = parent_ino(filp->f_path.dentry);
3954401f 3178 over = filldir(dirent, "..", 2,
5ecc7e5d 3179 2, pino, DT_DIR);
3954401f 3180 if (over)
49593bfa 3181 return 0;
3954401f
CM
3182 filp->f_pos = 2;
3183 }
49593bfa
DW
3184 path = btrfs_alloc_path();
3185 path->reada = 2;
3186
39279cc3
CM
3187 btrfs_set_key_type(&key, key_type);
3188 key.offset = filp->f_pos;
49593bfa 3189 key.objectid = inode->i_ino;
5f39d397 3190
39279cc3
CM
3191 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3192 if (ret < 0)
3193 goto err;
3194 advance = 0;
49593bfa
DW
3195
3196 while (1) {
5f39d397
CM
3197 leaf = path->nodes[0];
3198 nritems = btrfs_header_nritems(leaf);
39279cc3
CM
3199 slot = path->slots[0];
3200 if (advance || slot >= nritems) {
49593bfa 3201 if (slot >= nritems - 1) {
39279cc3
CM
3202 ret = btrfs_next_leaf(root, path);
3203 if (ret)
3204 break;
5f39d397
CM
3205 leaf = path->nodes[0];
3206 nritems = btrfs_header_nritems(leaf);
39279cc3
CM
3207 slot = path->slots[0];
3208 } else {
3209 slot++;
3210 path->slots[0]++;
3211 }
3212 }
3de4586c 3213
39279cc3 3214 advance = 1;
5f39d397
CM
3215 item = btrfs_item_nr(leaf, slot);
3216 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3217
3218 if (found_key.objectid != key.objectid)
39279cc3 3219 break;
5f39d397 3220 if (btrfs_key_type(&found_key) != key_type)
39279cc3 3221 break;
5f39d397 3222 if (found_key.offset < filp->f_pos)
39279cc3 3223 continue;
5f39d397
CM
3224
3225 filp->f_pos = found_key.offset;
49593bfa 3226
39279cc3
CM
3227 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3228 di_cur = 0;
5f39d397 3229 di_total = btrfs_item_size(leaf, item);
49593bfa
DW
3230
3231 while (di_cur < di_total) {
5f39d397
CM
3232 struct btrfs_key location;
3233
3234 name_len = btrfs_dir_name_len(leaf, di);
49593bfa 3235 if (name_len <= sizeof(tmp_name)) {
5f39d397
CM
3236 name_ptr = tmp_name;
3237 } else {
3238 name_ptr = kmalloc(name_len, GFP_NOFS);
49593bfa
DW
3239 if (!name_ptr) {
3240 ret = -ENOMEM;
3241 goto err;
3242 }
5f39d397
CM
3243 }
3244 read_extent_buffer(leaf, name_ptr,
3245 (unsigned long)(di + 1), name_len);
3246
3247 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
3248 btrfs_dir_item_key_to_cpu(leaf, di, &location);
3de4586c
CM
3249
3250 /* is this a reference to our own snapshot? If so
3251 * skip it
3252 */
3253 if (location.type == BTRFS_ROOT_ITEM_KEY &&
3254 location.objectid == root->root_key.objectid) {
3255 over = 0;
3256 goto skip;
3257 }
5f39d397 3258 over = filldir(dirent, name_ptr, name_len,
49593bfa 3259 found_key.offset, location.objectid,
39279cc3 3260 d_type);
5f39d397 3261
3de4586c 3262skip:
5f39d397
CM
3263 if (name_ptr != tmp_name)
3264 kfree(name_ptr);
3265
39279cc3
CM
3266 if (over)
3267 goto nopos;
5103e947 3268 di_len = btrfs_dir_name_len(leaf, di) +
49593bfa 3269 btrfs_dir_data_len(leaf, di) + sizeof(*di);
39279cc3
CM
3270 di_cur += di_len;
3271 di = (struct btrfs_dir_item *)((char *)di + di_len);
3272 }
3273 }
49593bfa
DW
3274
3275 /* Reached end of directory/root. Bump pos past the last item. */
5e591a07 3276 if (key_type == BTRFS_DIR_INDEX_KEY)
89f135d8 3277 filp->f_pos = INT_LIMIT(off_t);
5e591a07
YZ
3278 else
3279 filp->f_pos++;
39279cc3
CM
3280nopos:
3281 ret = 0;
3282err:
39279cc3 3283 btrfs_free_path(path);
39279cc3
CM
3284 return ret;
3285}
3286
3287int btrfs_write_inode(struct inode *inode, int wait)
3288{
3289 struct btrfs_root *root = BTRFS_I(inode)->root;
3290 struct btrfs_trans_handle *trans;
3291 int ret = 0;
3292
c146afad 3293 if (root->fs_info->btree_inode == inode)
4ca8b41e
CM
3294 return 0;
3295
39279cc3 3296 if (wait) {
f9295749 3297 trans = btrfs_join_transaction(root, 1);
39279cc3
CM
3298 btrfs_set_trans_block_group(trans, inode);
3299 ret = btrfs_commit_transaction(trans, root);
39279cc3
CM
3300 }
3301 return ret;
3302}
3303
3304/*
54aa1f4d 3305 * This is somewhat expensive, updating the tree every time the
39279cc3
CM
3306 * inode changes. But, it is most likely to find the inode in cache.
3307 * FIXME, needs more benchmarking...there are no reasons other than performance
3308 * to keep or drop this code.
3309 */
3310void btrfs_dirty_inode(struct inode *inode)
3311{
3312 struct btrfs_root *root = BTRFS_I(inode)->root;
3313 struct btrfs_trans_handle *trans;
3314
f9295749 3315 trans = btrfs_join_transaction(root, 1);
39279cc3
CM
3316 btrfs_set_trans_block_group(trans, inode);
3317 btrfs_update_inode(trans, root, inode);
3318 btrfs_end_transaction(trans, root);
39279cc3
CM
3319}
3320
d352ac68
CM
3321/*
3322 * find the highest existing sequence number in a directory
3323 * and then set the in-memory index_cnt variable to reflect
3324 * free sequence numbers
3325 */
aec7477b
JB
3326static int btrfs_set_inode_index_count(struct inode *inode)
3327{
3328 struct btrfs_root *root = BTRFS_I(inode)->root;
3329 struct btrfs_key key, found_key;
3330 struct btrfs_path *path;
3331 struct extent_buffer *leaf;
3332 int ret;
3333
3334 key.objectid = inode->i_ino;
3335 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
3336 key.offset = (u64)-1;
3337
3338 path = btrfs_alloc_path();
3339 if (!path)
3340 return -ENOMEM;
3341
3342 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3343 if (ret < 0)
3344 goto out;
3345 /* FIXME: we should be able to handle this */
3346 if (ret == 0)
3347 goto out;
3348 ret = 0;
3349
3350 /*
3351 * MAGIC NUMBER EXPLANATION:
3352 * since we search a directory based on f_pos we have to start at 2
3353 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
3354 * else has to start at 2
3355 */
3356 if (path->slots[0] == 0) {
3357 BTRFS_I(inode)->index_cnt = 2;
3358 goto out;
3359 }
3360
3361 path->slots[0]--;
3362
3363 leaf = path->nodes[0];
3364 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3365
3366 if (found_key.objectid != inode->i_ino ||
3367 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
3368 BTRFS_I(inode)->index_cnt = 2;
3369 goto out;
3370 }
3371
3372 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
3373out:
3374 btrfs_free_path(path);
3375 return ret;
3376}
3377
d352ac68
CM
3378/*
3379 * helper to find a free sequence number in a given directory. This current
3380 * code is very simple, later versions will do smarter things in the btree
3381 */
3de4586c 3382int btrfs_set_inode_index(struct inode *dir, u64 *index)
aec7477b
JB
3383{
3384 int ret = 0;
3385
3386 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
3387 ret = btrfs_set_inode_index_count(dir);
d397712b 3388 if (ret)
aec7477b
JB
3389 return ret;
3390 }
3391
00e4e6b3 3392 *index = BTRFS_I(dir)->index_cnt;
aec7477b
JB
3393 BTRFS_I(dir)->index_cnt++;
3394
3395 return ret;
3396}
3397
39279cc3
CM
3398static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
3399 struct btrfs_root *root,
aec7477b 3400 struct inode *dir,
9c58309d 3401 const char *name, int name_len,
d2fb3437
YZ
3402 u64 ref_objectid, u64 objectid,
3403 u64 alloc_hint, int mode, u64 *index)
39279cc3
CM
3404{
3405 struct inode *inode;
5f39d397 3406 struct btrfs_inode_item *inode_item;
39279cc3 3407 struct btrfs_key *location;
5f39d397 3408 struct btrfs_path *path;
9c58309d
CM
3409 struct btrfs_inode_ref *ref;
3410 struct btrfs_key key[2];
3411 u32 sizes[2];
3412 unsigned long ptr;
39279cc3
CM
3413 int ret;
3414 int owner;
3415
5f39d397
CM
3416 path = btrfs_alloc_path();
3417 BUG_ON(!path);
3418
39279cc3
CM
3419 inode = new_inode(root->fs_info->sb);
3420 if (!inode)
3421 return ERR_PTR(-ENOMEM);
3422
aec7477b 3423 if (dir) {
3de4586c 3424 ret = btrfs_set_inode_index(dir, index);
aec7477b
JB
3425 if (ret)
3426 return ERR_PTR(ret);
aec7477b
JB
3427 }
3428 /*
3429 * index_cnt is ignored for everything but a dir,
3430 * btrfs_get_inode_index_count has an explanation for the magic
3431 * number
3432 */
e02119d5 3433 init_btrfs_i(inode);
aec7477b 3434 BTRFS_I(inode)->index_cnt = 2;
39279cc3 3435 BTRFS_I(inode)->root = root;
e02119d5 3436 BTRFS_I(inode)->generation = trans->transid;
b888db2b 3437
39279cc3
CM
3438 if (mode & S_IFDIR)
3439 owner = 0;
3440 else
3441 owner = 1;
d2fb3437
YZ
3442 BTRFS_I(inode)->block_group =
3443 btrfs_find_block_group(root, 0, alloc_hint, owner);
17d217fe
YZ
3444 if ((mode & S_IFREG)) {
3445 if (btrfs_test_opt(root, NODATASUM))
3446 btrfs_set_flag(inode, NODATASUM);
3447 if (btrfs_test_opt(root, NODATACOW))
3448 btrfs_set_flag(inode, NODATACOW);
3449 }
9c58309d
CM
3450
3451 key[0].objectid = objectid;
3452 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
3453 key[0].offset = 0;
3454
3455 key[1].objectid = objectid;
3456 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
3457 key[1].offset = ref_objectid;
3458
3459 sizes[0] = sizeof(struct btrfs_inode_item);
3460 sizes[1] = name_len + sizeof(*ref);
3461
3462 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
3463 if (ret != 0)
5f39d397
CM
3464 goto fail;
3465
9c58309d
CM
3466 if (objectid > root->highest_inode)
3467 root->highest_inode = objectid;
3468
79683f2d
CM
3469 inode->i_uid = current_fsuid();
3470 inode->i_gid = current_fsgid();
39279cc3
CM
3471 inode->i_mode = mode;
3472 inode->i_ino = objectid;
a76a3cd4 3473 inode_set_bytes(inode, 0);
39279cc3 3474 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5f39d397
CM
3475 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3476 struct btrfs_inode_item);
e02119d5 3477 fill_inode_item(trans, path->nodes[0], inode_item, inode);
9c58309d
CM
3478
3479 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3480 struct btrfs_inode_ref);
3481 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
00e4e6b3 3482 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
9c58309d
CM
3483 ptr = (unsigned long)(ref + 1);
3484 write_extent_buffer(path->nodes[0], name, ptr, name_len);
3485
5f39d397
CM
3486 btrfs_mark_buffer_dirty(path->nodes[0]);
3487 btrfs_free_path(path);
3488
39279cc3
CM
3489 location = &BTRFS_I(inode)->location;
3490 location->objectid = objectid;
39279cc3
CM
3491 location->offset = 0;
3492 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
3493
39279cc3
CM
3494 insert_inode_hash(inode);
3495 return inode;
5f39d397 3496fail:
aec7477b
JB
3497 if (dir)
3498 BTRFS_I(dir)->index_cnt--;
5f39d397
CM
3499 btrfs_free_path(path);
3500 return ERR_PTR(ret);
39279cc3
CM
3501}
3502
3503static inline u8 btrfs_inode_type(struct inode *inode)
3504{
3505 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
3506}
3507
d352ac68
CM
3508/*
3509 * utility function to add 'inode' into 'parent_inode' with
3510 * a give name and a given sequence number.
3511 * if 'add_backref' is true, also insert a backref from the
3512 * inode to the parent directory.
3513 */
e02119d5
CM
3514int btrfs_add_link(struct btrfs_trans_handle *trans,
3515 struct inode *parent_inode, struct inode *inode,
3516 const char *name, int name_len, int add_backref, u64 index)
39279cc3
CM
3517{
3518 int ret;
3519 struct btrfs_key key;
e02119d5 3520 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
5f39d397 3521
39279cc3 3522 key.objectid = inode->i_ino;
39279cc3
CM
3523 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
3524 key.offset = 0;
3525
e02119d5
CM
3526 ret = btrfs_insert_dir_item(trans, root, name, name_len,
3527 parent_inode->i_ino,
aec7477b 3528 &key, btrfs_inode_type(inode),
00e4e6b3 3529 index);
39279cc3 3530 if (ret == 0) {
9c58309d
CM
3531 if (add_backref) {
3532 ret = btrfs_insert_inode_ref(trans, root,
e02119d5
CM
3533 name, name_len,
3534 inode->i_ino,
3535 parent_inode->i_ino,
3536 index);
9c58309d 3537 }
dbe674a9 3538 btrfs_i_size_write(parent_inode, parent_inode->i_size +
e02119d5 3539 name_len * 2);
79c44584 3540 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
e02119d5 3541 ret = btrfs_update_inode(trans, root, parent_inode);
39279cc3
CM
3542 }
3543 return ret;
3544}
3545
3546static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
9c58309d 3547 struct dentry *dentry, struct inode *inode,
00e4e6b3 3548 int backref, u64 index)
39279cc3 3549{
e02119d5
CM
3550 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3551 inode, dentry->d_name.name,
3552 dentry->d_name.len, backref, index);
39279cc3
CM
3553 if (!err) {
3554 d_instantiate(dentry, inode);
3555 return 0;
3556 }
3557 if (err > 0)
3558 err = -EEXIST;
3559 return err;
3560}
3561
618e21d5
JB
3562static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
3563 int mode, dev_t rdev)
3564{
3565 struct btrfs_trans_handle *trans;
3566 struct btrfs_root *root = BTRFS_I(dir)->root;
1832a6d5 3567 struct inode *inode = NULL;
618e21d5
JB
3568 int err;
3569 int drop_inode = 0;
3570 u64 objectid;
1832a6d5 3571 unsigned long nr = 0;
00e4e6b3 3572 u64 index = 0;
618e21d5
JB
3573
3574 if (!new_valid_dev(rdev))
3575 return -EINVAL;
3576
1832a6d5
CM
3577 err = btrfs_check_free_space(root, 1, 0);
3578 if (err)
3579 goto fail;
3580
618e21d5
JB
3581 trans = btrfs_start_transaction(root, 1);
3582 btrfs_set_trans_block_group(trans, dir);
3583
3584 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3585 if (err) {
3586 err = -ENOSPC;
3587 goto out_unlock;
3588 }
3589
aec7477b 3590 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
9c58309d
CM
3591 dentry->d_name.len,
3592 dentry->d_parent->d_inode->i_ino, objectid,
00e4e6b3 3593 BTRFS_I(dir)->block_group, mode, &index);
618e21d5
JB
3594 err = PTR_ERR(inode);
3595 if (IS_ERR(inode))
3596 goto out_unlock;
3597
0279b4cd 3598 err = btrfs_init_inode_security(inode, dir);
33268eaf
JB
3599 if (err) {
3600 drop_inode = 1;
3601 goto out_unlock;
3602 }
3603
618e21d5 3604 btrfs_set_trans_block_group(trans, inode);
00e4e6b3 3605 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
618e21d5
JB
3606 if (err)
3607 drop_inode = 1;
3608 else {
3609 inode->i_op = &btrfs_special_inode_operations;
3610 init_special_inode(inode, inode->i_mode, rdev);
1b4ab1bb 3611 btrfs_update_inode(trans, root, inode);
618e21d5
JB
3612 }
3613 dir->i_sb->s_dirt = 1;
3614 btrfs_update_inode_block_group(trans, inode);
3615 btrfs_update_inode_block_group(trans, dir);
3616out_unlock:
d3c2fdcf 3617 nr = trans->blocks_used;
89ce8a63 3618 btrfs_end_transaction_throttle(trans, root);
1832a6d5 3619fail:
618e21d5
JB
3620 if (drop_inode) {
3621 inode_dec_link_count(inode);
3622 iput(inode);
3623 }
d3c2fdcf 3624 btrfs_btree_balance_dirty(root, nr);
618e21d5
JB
3625 return err;
3626}
3627
39279cc3
CM
3628static int btrfs_create(struct inode *dir, struct dentry *dentry,
3629 int mode, struct nameidata *nd)
3630{
3631 struct btrfs_trans_handle *trans;
3632 struct btrfs_root *root = BTRFS_I(dir)->root;
1832a6d5 3633 struct inode *inode = NULL;
39279cc3
CM
3634 int err;
3635 int drop_inode = 0;
1832a6d5 3636 unsigned long nr = 0;
39279cc3 3637 u64 objectid;
00e4e6b3 3638 u64 index = 0;
39279cc3 3639
1832a6d5
CM
3640 err = btrfs_check_free_space(root, 1, 0);
3641 if (err)
3642 goto fail;
39279cc3
CM
3643 trans = btrfs_start_transaction(root, 1);
3644 btrfs_set_trans_block_group(trans, dir);
3645
3646 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3647 if (err) {
3648 err = -ENOSPC;
3649 goto out_unlock;
3650 }
3651
aec7477b 3652 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
9c58309d
CM
3653 dentry->d_name.len,
3654 dentry->d_parent->d_inode->i_ino,
00e4e6b3
CM
3655 objectid, BTRFS_I(dir)->block_group, mode,
3656 &index);
39279cc3
CM
3657 err = PTR_ERR(inode);
3658 if (IS_ERR(inode))
3659 goto out_unlock;
3660
0279b4cd 3661 err = btrfs_init_inode_security(inode, dir);
33268eaf
JB
3662 if (err) {
3663 drop_inode = 1;
3664 goto out_unlock;
3665 }
3666
39279cc3 3667 btrfs_set_trans_block_group(trans, inode);
00e4e6b3 3668 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
39279cc3
CM
3669 if (err)
3670 drop_inode = 1;
3671 else {
3672 inode->i_mapping->a_ops = &btrfs_aops;
04160088 3673 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
39279cc3
CM
3674 inode->i_fop = &btrfs_file_operations;
3675 inode->i_op = &btrfs_file_inode_operations;
d1310b2e 3676 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
39279cc3
CM
3677 }
3678 dir->i_sb->s_dirt = 1;
3679 btrfs_update_inode_block_group(trans, inode);
3680 btrfs_update_inode_block_group(trans, dir);
3681out_unlock:
d3c2fdcf 3682 nr = trans->blocks_used;
ab78c84d 3683 btrfs_end_transaction_throttle(trans, root);
1832a6d5 3684fail:
39279cc3
CM
3685 if (drop_inode) {
3686 inode_dec_link_count(inode);
3687 iput(inode);
3688 }
d3c2fdcf 3689 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
3690 return err;
3691}
3692
3693static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
3694 struct dentry *dentry)
3695{
3696 struct btrfs_trans_handle *trans;
3697 struct btrfs_root *root = BTRFS_I(dir)->root;
3698 struct inode *inode = old_dentry->d_inode;
00e4e6b3 3699 u64 index;
1832a6d5 3700 unsigned long nr = 0;
39279cc3
CM
3701 int err;
3702 int drop_inode = 0;
3703
3704 if (inode->i_nlink == 0)
3705 return -ENOENT;
3706
e02119d5 3707 btrfs_inc_nlink(inode);
1832a6d5
CM
3708 err = btrfs_check_free_space(root, 1, 0);
3709 if (err)
3710 goto fail;
3de4586c 3711 err = btrfs_set_inode_index(dir, &index);
aec7477b
JB
3712 if (err)
3713 goto fail;
3714
39279cc3 3715 trans = btrfs_start_transaction(root, 1);
5f39d397 3716
39279cc3
CM
3717 btrfs_set_trans_block_group(trans, dir);
3718 atomic_inc(&inode->i_count);
aec7477b 3719
00e4e6b3 3720 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
5f39d397 3721
39279cc3
CM
3722 if (err)
3723 drop_inode = 1;
5f39d397 3724
39279cc3
CM
3725 dir->i_sb->s_dirt = 1;
3726 btrfs_update_inode_block_group(trans, dir);
54aa1f4d 3727 err = btrfs_update_inode(trans, root, inode);
5f39d397 3728
54aa1f4d
CM
3729 if (err)
3730 drop_inode = 1;
39279cc3 3731
d3c2fdcf 3732 nr = trans->blocks_used;
ab78c84d 3733 btrfs_end_transaction_throttle(trans, root);
1832a6d5 3734fail:
39279cc3
CM
3735 if (drop_inode) {
3736 inode_dec_link_count(inode);
3737 iput(inode);
3738 }
d3c2fdcf 3739 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
3740 return err;
3741}
3742
39279cc3
CM
3743static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3744{
b9d86667 3745 struct inode *inode = NULL;
39279cc3
CM
3746 struct btrfs_trans_handle *trans;
3747 struct btrfs_root *root = BTRFS_I(dir)->root;
3748 int err = 0;
3749 int drop_on_err = 0;
b9d86667 3750 u64 objectid = 0;
00e4e6b3 3751 u64 index = 0;
d3c2fdcf 3752 unsigned long nr = 1;
39279cc3 3753
1832a6d5
CM
3754 err = btrfs_check_free_space(root, 1, 0);
3755 if (err)
3756 goto out_unlock;
3757
39279cc3
CM
3758 trans = btrfs_start_transaction(root, 1);
3759 btrfs_set_trans_block_group(trans, dir);
5f39d397 3760
39279cc3
CM
3761 if (IS_ERR(trans)) {
3762 err = PTR_ERR(trans);
3763 goto out_unlock;
3764 }
3765
3766 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3767 if (err) {
3768 err = -ENOSPC;
3769 goto out_unlock;
3770 }
3771
aec7477b 3772 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
9c58309d
CM
3773 dentry->d_name.len,
3774 dentry->d_parent->d_inode->i_ino, objectid,
00e4e6b3
CM
3775 BTRFS_I(dir)->block_group, S_IFDIR | mode,
3776 &index);
39279cc3
CM
3777 if (IS_ERR(inode)) {
3778 err = PTR_ERR(inode);
3779 goto out_fail;
3780 }
5f39d397 3781
39279cc3 3782 drop_on_err = 1;
33268eaf 3783
0279b4cd 3784 err = btrfs_init_inode_security(inode, dir);
33268eaf
JB
3785 if (err)
3786 goto out_fail;
3787
39279cc3
CM
3788 inode->i_op = &btrfs_dir_inode_operations;
3789 inode->i_fop = &btrfs_dir_file_operations;
3790 btrfs_set_trans_block_group(trans, inode);
3791
dbe674a9 3792 btrfs_i_size_write(inode, 0);
39279cc3
CM
3793 err = btrfs_update_inode(trans, root, inode);
3794 if (err)
3795 goto out_fail;
5f39d397 3796
e02119d5
CM
3797 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3798 inode, dentry->d_name.name,
3799 dentry->d_name.len, 0, index);
39279cc3
CM
3800 if (err)
3801 goto out_fail;
5f39d397 3802
39279cc3
CM
3803 d_instantiate(dentry, inode);
3804 drop_on_err = 0;
3805 dir->i_sb->s_dirt = 1;
3806 btrfs_update_inode_block_group(trans, inode);
3807 btrfs_update_inode_block_group(trans, dir);
3808
3809out_fail:
d3c2fdcf 3810 nr = trans->blocks_used;
ab78c84d 3811 btrfs_end_transaction_throttle(trans, root);
5f39d397 3812
39279cc3 3813out_unlock:
39279cc3
CM
3814 if (drop_on_err)
3815 iput(inode);
d3c2fdcf 3816 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
3817 return err;
3818}
3819
d352ac68
CM
3820/* helper for btfs_get_extent. Given an existing extent in the tree,
3821 * and an extent that you want to insert, deal with overlap and insert
3822 * the new extent into the tree.
3823 */
3b951516
CM
3824static int merge_extent_mapping(struct extent_map_tree *em_tree,
3825 struct extent_map *existing,
e6dcd2dc
CM
3826 struct extent_map *em,
3827 u64 map_start, u64 map_len)
3b951516
CM
3828{
3829 u64 start_diff;
3b951516 3830
e6dcd2dc
CM
3831 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
3832 start_diff = map_start - em->start;
3833 em->start = map_start;
3834 em->len = map_len;
c8b97818
CM
3835 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
3836 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
e6dcd2dc 3837 em->block_start += start_diff;
c8b97818
CM
3838 em->block_len -= start_diff;
3839 }
e6dcd2dc 3840 return add_extent_mapping(em_tree, em);
3b951516
CM
3841}
3842
c8b97818
CM
3843static noinline int uncompress_inline(struct btrfs_path *path,
3844 struct inode *inode, struct page *page,
3845 size_t pg_offset, u64 extent_offset,
3846 struct btrfs_file_extent_item *item)
3847{
3848 int ret;
3849 struct extent_buffer *leaf = path->nodes[0];
3850 char *tmp;
3851 size_t max_size;
3852 unsigned long inline_size;
3853 unsigned long ptr;
3854
3855 WARN_ON(pg_offset != 0);
3856 max_size = btrfs_file_extent_ram_bytes(leaf, item);
3857 inline_size = btrfs_file_extent_inline_item_len(leaf,
3858 btrfs_item_nr(leaf, path->slots[0]));
3859 tmp = kmalloc(inline_size, GFP_NOFS);
3860 ptr = btrfs_file_extent_inline_start(item);
3861
3862 read_extent_buffer(leaf, tmp, ptr, inline_size);
3863
5b050f04 3864 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
c8b97818
CM
3865 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
3866 inline_size, max_size);
3867 if (ret) {
3868 char *kaddr = kmap_atomic(page, KM_USER0);
3869 unsigned long copy_size = min_t(u64,
3870 PAGE_CACHE_SIZE - pg_offset,
3871 max_size - extent_offset);
3872 memset(kaddr + pg_offset, 0, copy_size);
3873 kunmap_atomic(kaddr, KM_USER0);
3874 }
3875 kfree(tmp);
3876 return 0;
3877}
3878
d352ac68
CM
3879/*
3880 * a bit scary, this does extent mapping from logical file offset to the disk.
d397712b
CM
3881 * the ugly parts come from merging extents from the disk with the in-ram
3882 * representation. This gets more complex because of the data=ordered code,
d352ac68
CM
3883 * where the in-ram extents might be locked pending data=ordered completion.
3884 *
3885 * This also copies inline extents directly into the page.
3886 */
d397712b 3887
a52d9a80 3888struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
70dec807 3889 size_t pg_offset, u64 start, u64 len,
a52d9a80
CM
3890 int create)
3891{
3892 int ret;
3893 int err = 0;
db94535d 3894 u64 bytenr;
a52d9a80
CM
3895 u64 extent_start = 0;
3896 u64 extent_end = 0;
3897 u64 objectid = inode->i_ino;
3898 u32 found_type;
f421950f 3899 struct btrfs_path *path = NULL;
a52d9a80
CM
3900 struct btrfs_root *root = BTRFS_I(inode)->root;
3901 struct btrfs_file_extent_item *item;
5f39d397
CM
3902 struct extent_buffer *leaf;
3903 struct btrfs_key found_key;
a52d9a80
CM
3904 struct extent_map *em = NULL;
3905 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
d1310b2e 3906 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
a52d9a80 3907 struct btrfs_trans_handle *trans = NULL;
c8b97818 3908 int compressed;
a52d9a80 3909
a52d9a80 3910again:
d1310b2e
CM
3911 spin_lock(&em_tree->lock);
3912 em = lookup_extent_mapping(em_tree, start, len);
a061fc8d
CM
3913 if (em)
3914 em->bdev = root->fs_info->fs_devices->latest_bdev;
d1310b2e
CM
3915 spin_unlock(&em_tree->lock);
3916
a52d9a80 3917 if (em) {
e1c4b745
CM
3918 if (em->start > start || em->start + em->len <= start)
3919 free_extent_map(em);
3920 else if (em->block_start == EXTENT_MAP_INLINE && page)
70dec807
CM
3921 free_extent_map(em);
3922 else
3923 goto out;
a52d9a80 3924 }
d1310b2e 3925 em = alloc_extent_map(GFP_NOFS);
a52d9a80 3926 if (!em) {
d1310b2e
CM
3927 err = -ENOMEM;
3928 goto out;
a52d9a80 3929 }
e6dcd2dc 3930 em->bdev = root->fs_info->fs_devices->latest_bdev;
d1310b2e 3931 em->start = EXTENT_MAP_HOLE;
445a6944 3932 em->orig_start = EXTENT_MAP_HOLE;
d1310b2e 3933 em->len = (u64)-1;
c8b97818 3934 em->block_len = (u64)-1;
f421950f
CM
3935
3936 if (!path) {
3937 path = btrfs_alloc_path();
3938 BUG_ON(!path);
3939 }
3940
179e29e4
CM
3941 ret = btrfs_lookup_file_extent(trans, root, path,
3942 objectid, start, trans != NULL);
a52d9a80
CM
3943 if (ret < 0) {
3944 err = ret;
3945 goto out;
3946 }
3947
3948 if (ret != 0) {
3949 if (path->slots[0] == 0)
3950 goto not_found;
3951 path->slots[0]--;
3952 }
3953
5f39d397
CM
3954 leaf = path->nodes[0];
3955 item = btrfs_item_ptr(leaf, path->slots[0],
a52d9a80 3956 struct btrfs_file_extent_item);
a52d9a80 3957 /* are we inside the extent that was found? */
5f39d397
CM
3958 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3959 found_type = btrfs_key_type(&found_key);
3960 if (found_key.objectid != objectid ||
a52d9a80
CM
3961 found_type != BTRFS_EXTENT_DATA_KEY) {
3962 goto not_found;
3963 }
3964
5f39d397
CM
3965 found_type = btrfs_file_extent_type(leaf, item);
3966 extent_start = found_key.offset;
c8b97818 3967 compressed = btrfs_file_extent_compression(leaf, item);
d899e052
YZ
3968 if (found_type == BTRFS_FILE_EXTENT_REG ||
3969 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
a52d9a80 3970 extent_end = extent_start +
db94535d 3971 btrfs_file_extent_num_bytes(leaf, item);
9036c102
YZ
3972 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
3973 size_t size;
3974 size = btrfs_file_extent_inline_len(leaf, item);
3975 extent_end = (extent_start + size + root->sectorsize - 1) &
3976 ~((u64)root->sectorsize - 1);
3977 }
3978
3979 if (start >= extent_end) {
3980 path->slots[0]++;
3981 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3982 ret = btrfs_next_leaf(root, path);
3983 if (ret < 0) {
3984 err = ret;
3985 goto out;
a52d9a80 3986 }
9036c102
YZ
3987 if (ret > 0)
3988 goto not_found;
3989 leaf = path->nodes[0];
a52d9a80 3990 }
9036c102
YZ
3991 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3992 if (found_key.objectid != objectid ||
3993 found_key.type != BTRFS_EXTENT_DATA_KEY)
3994 goto not_found;
3995 if (start + len <= found_key.offset)
3996 goto not_found;
3997 em->start = start;
3998 em->len = found_key.offset - start;
3999 goto not_found_em;
4000 }
4001
d899e052
YZ
4002 if (found_type == BTRFS_FILE_EXTENT_REG ||
4003 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
9036c102
YZ
4004 em->start = extent_start;
4005 em->len = extent_end - extent_start;
ff5b7ee3
YZ
4006 em->orig_start = extent_start -
4007 btrfs_file_extent_offset(leaf, item);
db94535d
CM
4008 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
4009 if (bytenr == 0) {
5f39d397 4010 em->block_start = EXTENT_MAP_HOLE;
a52d9a80
CM
4011 goto insert;
4012 }
c8b97818
CM
4013 if (compressed) {
4014 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4015 em->block_start = bytenr;
4016 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
4017 item);
4018 } else {
4019 bytenr += btrfs_file_extent_offset(leaf, item);
4020 em->block_start = bytenr;
4021 em->block_len = em->len;
d899e052
YZ
4022 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
4023 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
c8b97818 4024 }
a52d9a80
CM
4025 goto insert;
4026 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5f39d397 4027 unsigned long ptr;
a52d9a80 4028 char *map;
3326d1b0
CM
4029 size_t size;
4030 size_t extent_offset;
4031 size_t copy_size;
a52d9a80 4032
689f9346 4033 em->block_start = EXTENT_MAP_INLINE;
c8b97818 4034 if (!page || create) {
689f9346 4035 em->start = extent_start;
9036c102 4036 em->len = extent_end - extent_start;
689f9346
Y
4037 goto out;
4038 }
5f39d397 4039
9036c102
YZ
4040 size = btrfs_file_extent_inline_len(leaf, item);
4041 extent_offset = page_offset(page) + pg_offset - extent_start;
70dec807 4042 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
3326d1b0 4043 size - extent_offset);
3326d1b0 4044 em->start = extent_start + extent_offset;
70dec807
CM
4045 em->len = (copy_size + root->sectorsize - 1) &
4046 ~((u64)root->sectorsize - 1);
ff5b7ee3 4047 em->orig_start = EXTENT_MAP_INLINE;
c8b97818
CM
4048 if (compressed)
4049 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
689f9346 4050 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
179e29e4 4051 if (create == 0 && !PageUptodate(page)) {
c8b97818
CM
4052 if (btrfs_file_extent_compression(leaf, item) ==
4053 BTRFS_COMPRESS_ZLIB) {
4054 ret = uncompress_inline(path, inode, page,
4055 pg_offset,
4056 extent_offset, item);
4057 BUG_ON(ret);
4058 } else {
4059 map = kmap(page);
4060 read_extent_buffer(leaf, map + pg_offset, ptr,
4061 copy_size);
4062 kunmap(page);
4063 }
179e29e4
CM
4064 flush_dcache_page(page);
4065 } else if (create && PageUptodate(page)) {
4066 if (!trans) {
4067 kunmap(page);
4068 free_extent_map(em);
4069 em = NULL;
4070 btrfs_release_path(root, path);
f9295749 4071 trans = btrfs_join_transaction(root, 1);
179e29e4
CM
4072 goto again;
4073 }
c8b97818 4074 map = kmap(page);
70dec807 4075 write_extent_buffer(leaf, map + pg_offset, ptr,
179e29e4 4076 copy_size);
c8b97818 4077 kunmap(page);
179e29e4 4078 btrfs_mark_buffer_dirty(leaf);
a52d9a80 4079 }
d1310b2e
CM
4080 set_extent_uptodate(io_tree, em->start,
4081 extent_map_end(em) - 1, GFP_NOFS);
a52d9a80
CM
4082 goto insert;
4083 } else {
d397712b 4084 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
a52d9a80
CM
4085 WARN_ON(1);
4086 }
4087not_found:
4088 em->start = start;
d1310b2e 4089 em->len = len;
a52d9a80 4090not_found_em:
5f39d397 4091 em->block_start = EXTENT_MAP_HOLE;
9036c102 4092 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
a52d9a80
CM
4093insert:
4094 btrfs_release_path(root, path);
d1310b2e 4095 if (em->start > start || extent_map_end(em) <= start) {
d397712b
CM
4096 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
4097 "[%llu %llu]\n", (unsigned long long)em->start,
4098 (unsigned long long)em->len,
4099 (unsigned long long)start,
4100 (unsigned long long)len);
a52d9a80
CM
4101 err = -EIO;
4102 goto out;
4103 }
d1310b2e
CM
4104
4105 err = 0;
4106 spin_lock(&em_tree->lock);
a52d9a80 4107 ret = add_extent_mapping(em_tree, em);
3b951516
CM
4108 /* it is possible that someone inserted the extent into the tree
4109 * while we had the lock dropped. It is also possible that
4110 * an overlapping map exists in the tree
4111 */
a52d9a80 4112 if (ret == -EEXIST) {
3b951516 4113 struct extent_map *existing;
e6dcd2dc
CM
4114
4115 ret = 0;
4116
3b951516 4117 existing = lookup_extent_mapping(em_tree, start, len);
e1c4b745
CM
4118 if (existing && (existing->start > start ||
4119 existing->start + existing->len <= start)) {
4120 free_extent_map(existing);
4121 existing = NULL;
4122 }
3b951516
CM
4123 if (!existing) {
4124 existing = lookup_extent_mapping(em_tree, em->start,
4125 em->len);
4126 if (existing) {
4127 err = merge_extent_mapping(em_tree, existing,
e6dcd2dc
CM
4128 em, start,
4129 root->sectorsize);
3b951516
CM
4130 free_extent_map(existing);
4131 if (err) {
4132 free_extent_map(em);
4133 em = NULL;
4134 }
4135 } else {
4136 err = -EIO;
3b951516
CM
4137 free_extent_map(em);
4138 em = NULL;
4139 }
4140 } else {
4141 free_extent_map(em);
4142 em = existing;
e6dcd2dc 4143 err = 0;
a52d9a80 4144 }
a52d9a80 4145 }
d1310b2e 4146 spin_unlock(&em_tree->lock);
a52d9a80 4147out:
f421950f
CM
4148 if (path)
4149 btrfs_free_path(path);
a52d9a80
CM
4150 if (trans) {
4151 ret = btrfs_end_transaction(trans, root);
d397712b 4152 if (!err)
a52d9a80
CM
4153 err = ret;
4154 }
a52d9a80
CM
4155 if (err) {
4156 free_extent_map(em);
4157 WARN_ON(1);
4158 return ERR_PTR(err);
4159 }
4160 return em;
4161}
4162
16432985
CM
4163static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4164 const struct iovec *iov, loff_t offset,
4165 unsigned long nr_segs)
4166{
e1c4b745 4167 return -EINVAL;
16432985
CM
4168}
4169
1506fcc8
YS
4170static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4171 __u64 start, __u64 len)
4172{
4173 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
4174}
4175
a52d9a80 4176int btrfs_readpage(struct file *file, struct page *page)
9ebefb18 4177{
d1310b2e
CM
4178 struct extent_io_tree *tree;
4179 tree = &BTRFS_I(page->mapping->host)->io_tree;
a52d9a80 4180 return extent_read_full_page(tree, page, btrfs_get_extent);
9ebefb18 4181}
1832a6d5 4182
a52d9a80 4183static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
39279cc3 4184{
d1310b2e 4185 struct extent_io_tree *tree;
b888db2b
CM
4186
4187
4188 if (current->flags & PF_MEMALLOC) {
4189 redirty_page_for_writepage(wbc, page);
4190 unlock_page(page);
4191 return 0;
4192 }
d1310b2e 4193 tree = &BTRFS_I(page->mapping->host)->io_tree;
a52d9a80 4194 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
9ebefb18
CM
4195}
4196
f421950f
CM
4197int btrfs_writepages(struct address_space *mapping,
4198 struct writeback_control *wbc)
b293f02e 4199{
d1310b2e 4200 struct extent_io_tree *tree;
771ed689 4201
d1310b2e 4202 tree = &BTRFS_I(mapping->host)->io_tree;
b293f02e
CM
4203 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
4204}
4205
3ab2fb5a
CM
4206static int
4207btrfs_readpages(struct file *file, struct address_space *mapping,
4208 struct list_head *pages, unsigned nr_pages)
4209{
d1310b2e
CM
4210 struct extent_io_tree *tree;
4211 tree = &BTRFS_I(mapping->host)->io_tree;
3ab2fb5a
CM
4212 return extent_readpages(tree, mapping, pages, nr_pages,
4213 btrfs_get_extent);
4214}
e6dcd2dc 4215static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
9ebefb18 4216{
d1310b2e
CM
4217 struct extent_io_tree *tree;
4218 struct extent_map_tree *map;
a52d9a80 4219 int ret;
8c2383c3 4220
d1310b2e
CM
4221 tree = &BTRFS_I(page->mapping->host)->io_tree;
4222 map = &BTRFS_I(page->mapping->host)->extent_tree;
70dec807 4223 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
a52d9a80
CM
4224 if (ret == 1) {
4225 ClearPagePrivate(page);
4226 set_page_private(page, 0);
4227 page_cache_release(page);
39279cc3 4228 }
a52d9a80 4229 return ret;
39279cc3
CM
4230}
4231
e6dcd2dc
CM
4232static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4233{
98509cfc
CM
4234 if (PageWriteback(page) || PageDirty(page))
4235 return 0;
e6dcd2dc
CM
4236 return __btrfs_releasepage(page, gfp_flags);
4237}
4238
a52d9a80 4239static void btrfs_invalidatepage(struct page *page, unsigned long offset)
39279cc3 4240{
d1310b2e 4241 struct extent_io_tree *tree;
e6dcd2dc
CM
4242 struct btrfs_ordered_extent *ordered;
4243 u64 page_start = page_offset(page);
4244 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
39279cc3 4245
e6dcd2dc 4246 wait_on_page_writeback(page);
d1310b2e 4247 tree = &BTRFS_I(page->mapping->host)->io_tree;
e6dcd2dc
CM
4248 if (offset) {
4249 btrfs_releasepage(page, GFP_NOFS);
4250 return;
4251 }
4252
4253 lock_extent(tree, page_start, page_end, GFP_NOFS);
4254 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
4255 page_offset(page));
4256 if (ordered) {
eb84ae03
CM
4257 /*
4258 * IO on this page will never be started, so we need
4259 * to account for any ordered extents now
4260 */
e6dcd2dc
CM
4261 clear_extent_bit(tree, page_start, page_end,
4262 EXTENT_DIRTY | EXTENT_DELALLOC |
4263 EXTENT_LOCKED, 1, 0, GFP_NOFS);
211f90e6
CM
4264 btrfs_finish_ordered_io(page->mapping->host,
4265 page_start, page_end);
e6dcd2dc
CM
4266 btrfs_put_ordered_extent(ordered);
4267 lock_extent(tree, page_start, page_end, GFP_NOFS);
4268 }
4269 clear_extent_bit(tree, page_start, page_end,
4270 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4271 EXTENT_ORDERED,
4272 1, 1, GFP_NOFS);
4273 __btrfs_releasepage(page, GFP_NOFS);
4274
4a096752 4275 ClearPageChecked(page);
9ad6b7bc 4276 if (PagePrivate(page)) {
9ad6b7bc
CM
4277 ClearPagePrivate(page);
4278 set_page_private(page, 0);
4279 page_cache_release(page);
4280 }
39279cc3
CM
4281}
4282
9ebefb18
CM
4283/*
4284 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
4285 * called from a page fault handler when a page is first dirtied. Hence we must
4286 * be careful to check for EOF conditions here. We set the page up correctly
4287 * for a written page which means we get ENOSPC checking when writing into
4288 * holes and correct delalloc and unwritten extent mapping on filesystems that
4289 * support these features.
4290 *
4291 * We are not allowed to take the i_mutex here so we have to play games to
4292 * protect against truncate races as the page could now be beyond EOF. Because
4293 * vmtruncate() writes the inode size before removing pages, once we have the
4294 * page lock we can determine safely if the page is beyond EOF. If it is not
4295 * beyond EOF, then the page is guaranteed safe against truncation until we
4296 * unlock the page.
4297 */
4298int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
4299{
6da6abae 4300 struct inode *inode = fdentry(vma->vm_file)->d_inode;
1832a6d5 4301 struct btrfs_root *root = BTRFS_I(inode)->root;
e6dcd2dc
CM
4302 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4303 struct btrfs_ordered_extent *ordered;
4304 char *kaddr;
4305 unsigned long zero_start;
9ebefb18 4306 loff_t size;
1832a6d5 4307 int ret;
a52d9a80 4308 u64 page_start;
e6dcd2dc 4309 u64 page_end;
9ebefb18 4310
1832a6d5 4311 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
1832a6d5
CM
4312 if (ret)
4313 goto out;
4314
4315 ret = -EINVAL;
e6dcd2dc 4316again:
9ebefb18 4317 lock_page(page);
9ebefb18 4318 size = i_size_read(inode);
e6dcd2dc
CM
4319 page_start = page_offset(page);
4320 page_end = page_start + PAGE_CACHE_SIZE - 1;
a52d9a80 4321
9ebefb18 4322 if ((page->mapping != inode->i_mapping) ||
e6dcd2dc 4323 (page_start >= size)) {
9ebefb18
CM
4324 /* page got truncated out from underneath us */
4325 goto out_unlock;
4326 }
e6dcd2dc
CM
4327 wait_on_page_writeback(page);
4328
4329 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
4330 set_page_extent_mapped(page);
4331
eb84ae03
CM
4332 /*
4333 * we can't set the delalloc bits if there are pending ordered
4334 * extents. Drop our locks and wait for them to finish
4335 */
e6dcd2dc
CM
4336 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4337 if (ordered) {
4338 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4339 unlock_page(page);
eb84ae03 4340 btrfs_start_ordered_extent(inode, ordered, 1);
e6dcd2dc
CM
4341 btrfs_put_ordered_extent(ordered);
4342 goto again;
4343 }
4344
ea8c2819 4345 btrfs_set_extent_delalloc(inode, page_start, page_end);
e6dcd2dc 4346 ret = 0;
9ebefb18
CM
4347
4348 /* page is wholly or partially inside EOF */
a52d9a80 4349 if (page_start + PAGE_CACHE_SIZE > size)
e6dcd2dc 4350 zero_start = size & ~PAGE_CACHE_MASK;
9ebefb18 4351 else
e6dcd2dc 4352 zero_start = PAGE_CACHE_SIZE;
9ebefb18 4353
e6dcd2dc
CM
4354 if (zero_start != PAGE_CACHE_SIZE) {
4355 kaddr = kmap(page);
4356 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
4357 flush_dcache_page(page);
4358 kunmap(page);
4359 }
247e743c 4360 ClearPageChecked(page);
e6dcd2dc
CM
4361 set_page_dirty(page);
4362 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
9ebefb18
CM
4363
4364out_unlock:
4365 unlock_page(page);
1832a6d5 4366out:
9ebefb18
CM
4367 return ret;
4368}
4369
39279cc3
CM
4370static void btrfs_truncate(struct inode *inode)
4371{
4372 struct btrfs_root *root = BTRFS_I(inode)->root;
4373 int ret;
4374 struct btrfs_trans_handle *trans;
d3c2fdcf 4375 unsigned long nr;
dbe674a9 4376 u64 mask = root->sectorsize - 1;
39279cc3
CM
4377
4378 if (!S_ISREG(inode->i_mode))
4379 return;
4380 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4381 return;
4382
4383 btrfs_truncate_page(inode->i_mapping, inode->i_size);
4a096752 4384 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
39279cc3 4385
39279cc3
CM
4386 trans = btrfs_start_transaction(root, 1);
4387 btrfs_set_trans_block_group(trans, inode);
dbe674a9 4388 btrfs_i_size_write(inode, inode->i_size);
39279cc3 4389
7b128766
JB
4390 ret = btrfs_orphan_add(trans, inode);
4391 if (ret)
4392 goto out;
39279cc3 4393 /* FIXME, add redo link to tree so we don't leak on crash */
e02119d5 4394 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
85e21bac 4395 BTRFS_EXTENT_DATA_KEY);
39279cc3 4396 btrfs_update_inode(trans, root, inode);
5f39d397 4397
7b128766
JB
4398 ret = btrfs_orphan_del(trans, inode);
4399 BUG_ON(ret);
4400
4401out:
4402 nr = trans->blocks_used;
89ce8a63 4403 ret = btrfs_end_transaction_throttle(trans, root);
39279cc3 4404 BUG_ON(ret);
d3c2fdcf 4405 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
4406}
4407
d352ac68
CM
4408/*
4409 * create a new subvolume directory/inode (helper for the ioctl).
4410 */
d2fb3437
YZ
4411int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
4412 struct btrfs_root *new_root, struct dentry *dentry,
4413 u64 new_dirid, u64 alloc_hint)
39279cc3 4414{
39279cc3 4415 struct inode *inode;
cb8e7090 4416 int error;
00e4e6b3 4417 u64 index = 0;
39279cc3 4418
aec7477b 4419 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
d2fb3437 4420 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
54aa1f4d 4421 if (IS_ERR(inode))
f46b5a66 4422 return PTR_ERR(inode);
39279cc3
CM
4423 inode->i_op = &btrfs_dir_inode_operations;
4424 inode->i_fop = &btrfs_dir_file_operations;
4425
39279cc3 4426 inode->i_nlink = 1;
dbe674a9 4427 btrfs_i_size_write(inode, 0);
3b96362c 4428
cb8e7090
CH
4429 error = btrfs_update_inode(trans, new_root, inode);
4430 if (error)
4431 return error;
4432
4433 d_instantiate(dentry, inode);
4434 return 0;
39279cc3
CM
4435}
4436
d352ac68
CM
4437/* helper function for file defrag and space balancing. This
4438 * forces readahead on a given range of bytes in an inode
4439 */
edbd8d4e 4440unsigned long btrfs_force_ra(struct address_space *mapping,
86479a04
CM
4441 struct file_ra_state *ra, struct file *file,
4442 pgoff_t offset, pgoff_t last_index)
4443{
8e7bf94f 4444 pgoff_t req_size = last_index - offset + 1;
86479a04 4445
86479a04
CM
4446 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
4447 return offset + req_size;
86479a04
CM
4448}
4449
39279cc3
CM
4450struct inode *btrfs_alloc_inode(struct super_block *sb)
4451{
4452 struct btrfs_inode *ei;
4453
4454 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
4455 if (!ei)
4456 return NULL;
15ee9bc7 4457 ei->last_trans = 0;
e02119d5 4458 ei->logged_trans = 0;
e6dcd2dc 4459 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
33268eaf
JB
4460 ei->i_acl = BTRFS_ACL_NOT_CACHED;
4461 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
7b128766 4462 INIT_LIST_HEAD(&ei->i_orphan);
39279cc3
CM
4463 return &ei->vfs_inode;
4464}
4465
4466void btrfs_destroy_inode(struct inode *inode)
4467{
e6dcd2dc 4468 struct btrfs_ordered_extent *ordered;
39279cc3
CM
4469 WARN_ON(!list_empty(&inode->i_dentry));
4470 WARN_ON(inode->i_data.nrpages);
4471
33268eaf
JB
4472 if (BTRFS_I(inode)->i_acl &&
4473 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
4474 posix_acl_release(BTRFS_I(inode)->i_acl);
4475 if (BTRFS_I(inode)->i_default_acl &&
4476 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
4477 posix_acl_release(BTRFS_I(inode)->i_default_acl);
4478
bcc63abb 4479 spin_lock(&BTRFS_I(inode)->root->list_lock);
7b128766
JB
4480 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
4481 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
4482 " list\n", inode->i_ino);
4483 dump_stack();
4484 }
bcc63abb 4485 spin_unlock(&BTRFS_I(inode)->root->list_lock);
7b128766 4486
d397712b 4487 while (1) {
e6dcd2dc
CM
4488 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
4489 if (!ordered)
4490 break;
4491 else {
d397712b
CM
4492 printk(KERN_ERR "btrfs found ordered "
4493 "extent %llu %llu on inode cleanup\n",
4494 (unsigned long long)ordered->file_offset,
4495 (unsigned long long)ordered->len);
e6dcd2dc
CM
4496 btrfs_remove_ordered_extent(inode, ordered);
4497 btrfs_put_ordered_extent(ordered);
4498 btrfs_put_ordered_extent(ordered);
4499 }
4500 }
5b21f2ed 4501 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
39279cc3
CM
4502 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
4503}
4504
0ee0fda0 4505static void init_once(void *foo)
39279cc3
CM
4506{
4507 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
4508
4509 inode_init_once(&ei->vfs_inode);
4510}
4511
4512void btrfs_destroy_cachep(void)
4513{
4514 if (btrfs_inode_cachep)
4515 kmem_cache_destroy(btrfs_inode_cachep);
4516 if (btrfs_trans_handle_cachep)
4517 kmem_cache_destroy(btrfs_trans_handle_cachep);
4518 if (btrfs_transaction_cachep)
4519 kmem_cache_destroy(btrfs_transaction_cachep);
4520 if (btrfs_bit_radix_cachep)
4521 kmem_cache_destroy(btrfs_bit_radix_cachep);
4522 if (btrfs_path_cachep)
4523 kmem_cache_destroy(btrfs_path_cachep);
4524}
4525
86479a04 4526struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
92fee66d 4527 unsigned long extra_flags,
2b1f55b0 4528 void (*ctor)(void *))
92fee66d
CM
4529{
4530 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2b1f55b0 4531 SLAB_MEM_SPREAD | extra_flags), ctor);
92fee66d
CM
4532}
4533
39279cc3
CM
4534int btrfs_init_cachep(void)
4535{
86479a04 4536 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
92fee66d
CM
4537 sizeof(struct btrfs_inode),
4538 0, init_once);
39279cc3
CM
4539 if (!btrfs_inode_cachep)
4540 goto fail;
86479a04
CM
4541 btrfs_trans_handle_cachep =
4542 btrfs_cache_create("btrfs_trans_handle_cache",
4543 sizeof(struct btrfs_trans_handle),
4544 0, NULL);
39279cc3
CM
4545 if (!btrfs_trans_handle_cachep)
4546 goto fail;
86479a04 4547 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
39279cc3 4548 sizeof(struct btrfs_transaction),
92fee66d 4549 0, NULL);
39279cc3
CM
4550 if (!btrfs_transaction_cachep)
4551 goto fail;
86479a04 4552 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
23223584 4553 sizeof(struct btrfs_path),
92fee66d 4554 0, NULL);
39279cc3
CM
4555 if (!btrfs_path_cachep)
4556 goto fail;
86479a04 4557 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
92fee66d 4558 SLAB_DESTROY_BY_RCU, NULL);
39279cc3
CM
4559 if (!btrfs_bit_radix_cachep)
4560 goto fail;
4561 return 0;
4562fail:
4563 btrfs_destroy_cachep();
4564 return -ENOMEM;
4565}
4566
4567static int btrfs_getattr(struct vfsmount *mnt,
4568 struct dentry *dentry, struct kstat *stat)
4569{
4570 struct inode *inode = dentry->d_inode;
4571 generic_fillattr(inode, stat);
3394e160 4572 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
d6667462 4573 stat->blksize = PAGE_CACHE_SIZE;
a76a3cd4
YZ
4574 stat->blocks = (inode_get_bytes(inode) +
4575 BTRFS_I(inode)->delalloc_bytes) >> 9;
39279cc3
CM
4576 return 0;
4577}
4578
d397712b
CM
4579static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4580 struct inode *new_dir, struct dentry *new_dentry)
39279cc3
CM
4581{
4582 struct btrfs_trans_handle *trans;
4583 struct btrfs_root *root = BTRFS_I(old_dir)->root;
4584 struct inode *new_inode = new_dentry->d_inode;
4585 struct inode *old_inode = old_dentry->d_inode;
4586 struct timespec ctime = CURRENT_TIME;
00e4e6b3 4587 u64 index = 0;
39279cc3
CM
4588 int ret;
4589
3394e160
CM
4590 /* we're not allowed to rename between subvolumes */
4591 if (BTRFS_I(old_inode)->root->root_key.objectid !=
4592 BTRFS_I(new_dir)->root->root_key.objectid)
4593 return -EXDEV;
4594
39279cc3
CM
4595 if (S_ISDIR(old_inode->i_mode) && new_inode &&
4596 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
4597 return -ENOTEMPTY;
4598 }
5f39d397 4599
0660b5af
CM
4600 /* to rename a snapshot or subvolume, we need to juggle the
4601 * backrefs. This isn't coded yet
4602 */
4603 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
4604 return -EXDEV;
4605
1832a6d5
CM
4606 ret = btrfs_check_free_space(root, 1, 0);
4607 if (ret)
4608 goto out_unlock;
4609
39279cc3 4610 trans = btrfs_start_transaction(root, 1);
5f39d397 4611
39279cc3 4612 btrfs_set_trans_block_group(trans, new_dir);
39279cc3 4613
e02119d5 4614 btrfs_inc_nlink(old_dentry->d_inode);
39279cc3
CM
4615 old_dir->i_ctime = old_dir->i_mtime = ctime;
4616 new_dir->i_ctime = new_dir->i_mtime = ctime;
4617 old_inode->i_ctime = ctime;
5f39d397 4618
e02119d5
CM
4619 ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
4620 old_dentry->d_name.name,
4621 old_dentry->d_name.len);
39279cc3
CM
4622 if (ret)
4623 goto out_fail;
4624
4625 if (new_inode) {
4626 new_inode->i_ctime = CURRENT_TIME;
e02119d5
CM
4627 ret = btrfs_unlink_inode(trans, root, new_dir,
4628 new_dentry->d_inode,
4629 new_dentry->d_name.name,
4630 new_dentry->d_name.len);
39279cc3
CM
4631 if (ret)
4632 goto out_fail;
7b128766 4633 if (new_inode->i_nlink == 0) {
e02119d5 4634 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
7b128766
JB
4635 if (ret)
4636 goto out_fail;
4637 }
e02119d5 4638
39279cc3 4639 }
3de4586c 4640 ret = btrfs_set_inode_index(new_dir, &index);
aec7477b
JB
4641 if (ret)
4642 goto out_fail;
4643
e02119d5
CM
4644 ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
4645 old_inode, new_dentry->d_name.name,
4646 new_dentry->d_name.len, 1, index);
39279cc3
CM
4647 if (ret)
4648 goto out_fail;
4649
4650out_fail:
ab78c84d 4651 btrfs_end_transaction_throttle(trans, root);
1832a6d5 4652out_unlock:
39279cc3
CM
4653 return ret;
4654}
4655
d352ac68
CM
4656/*
4657 * some fairly slow code that needs optimization. This walks the list
4658 * of all the inodes with pending delalloc and forces them to disk.
4659 */
ea8c2819
CM
4660int btrfs_start_delalloc_inodes(struct btrfs_root *root)
4661{
4662 struct list_head *head = &root->fs_info->delalloc_inodes;
4663 struct btrfs_inode *binode;
5b21f2ed 4664 struct inode *inode;
ea8c2819 4665
c146afad
YZ
4666 if (root->fs_info->sb->s_flags & MS_RDONLY)
4667 return -EROFS;
4668
75eff68e 4669 spin_lock(&root->fs_info->delalloc_lock);
d397712b 4670 while (!list_empty(head)) {
ea8c2819
CM
4671 binode = list_entry(head->next, struct btrfs_inode,
4672 delalloc_inodes);
5b21f2ed
ZY
4673 inode = igrab(&binode->vfs_inode);
4674 if (!inode)
4675 list_del_init(&binode->delalloc_inodes);
75eff68e 4676 spin_unlock(&root->fs_info->delalloc_lock);
5b21f2ed 4677 if (inode) {
8c8bee1d 4678 filemap_flush(inode->i_mapping);
5b21f2ed
ZY
4679 iput(inode);
4680 }
4681 cond_resched();
75eff68e 4682 spin_lock(&root->fs_info->delalloc_lock);
ea8c2819 4683 }
75eff68e 4684 spin_unlock(&root->fs_info->delalloc_lock);
8c8bee1d
CM
4685
4686 /* the filemap_flush will queue IO into the worker threads, but
4687 * we have to make sure the IO is actually started and that
4688 * ordered extents get created before we return
4689 */
4690 atomic_inc(&root->fs_info->async_submit_draining);
d397712b 4691 while (atomic_read(&root->fs_info->nr_async_submits) ||
771ed689 4692 atomic_read(&root->fs_info->async_delalloc_pages)) {
8c8bee1d 4693 wait_event(root->fs_info->async_submit_wait,
771ed689
CM
4694 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
4695 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
8c8bee1d
CM
4696 }
4697 atomic_dec(&root->fs_info->async_submit_draining);
ea8c2819
CM
4698 return 0;
4699}
4700
39279cc3
CM
4701static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
4702 const char *symname)
4703{
4704 struct btrfs_trans_handle *trans;
4705 struct btrfs_root *root = BTRFS_I(dir)->root;
4706 struct btrfs_path *path;
4707 struct btrfs_key key;
1832a6d5 4708 struct inode *inode = NULL;
39279cc3
CM
4709 int err;
4710 int drop_inode = 0;
4711 u64 objectid;
00e4e6b3 4712 u64 index = 0 ;
39279cc3
CM
4713 int name_len;
4714 int datasize;
5f39d397 4715 unsigned long ptr;
39279cc3 4716 struct btrfs_file_extent_item *ei;
5f39d397 4717 struct extent_buffer *leaf;
1832a6d5 4718 unsigned long nr = 0;
39279cc3
CM
4719
4720 name_len = strlen(symname) + 1;
4721 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
4722 return -ENAMETOOLONG;
1832a6d5 4723
1832a6d5
CM
4724 err = btrfs_check_free_space(root, 1, 0);
4725 if (err)
4726 goto out_fail;
4727
39279cc3
CM
4728 trans = btrfs_start_transaction(root, 1);
4729 btrfs_set_trans_block_group(trans, dir);
4730
4731 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4732 if (err) {
4733 err = -ENOSPC;
4734 goto out_unlock;
4735 }
4736
aec7477b 4737 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
9c58309d
CM
4738 dentry->d_name.len,
4739 dentry->d_parent->d_inode->i_ino, objectid,
00e4e6b3
CM
4740 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
4741 &index);
39279cc3
CM
4742 err = PTR_ERR(inode);
4743 if (IS_ERR(inode))
4744 goto out_unlock;
4745
0279b4cd 4746 err = btrfs_init_inode_security(inode, dir);
33268eaf
JB
4747 if (err) {
4748 drop_inode = 1;
4749 goto out_unlock;
4750 }
4751
39279cc3 4752 btrfs_set_trans_block_group(trans, inode);
00e4e6b3 4753 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
39279cc3
CM
4754 if (err)
4755 drop_inode = 1;
4756 else {
4757 inode->i_mapping->a_ops = &btrfs_aops;
04160088 4758 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
39279cc3
CM
4759 inode->i_fop = &btrfs_file_operations;
4760 inode->i_op = &btrfs_file_inode_operations;
d1310b2e 4761 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
39279cc3
CM
4762 }
4763 dir->i_sb->s_dirt = 1;
4764 btrfs_update_inode_block_group(trans, inode);
4765 btrfs_update_inode_block_group(trans, dir);
4766 if (drop_inode)
4767 goto out_unlock;
4768
4769 path = btrfs_alloc_path();
4770 BUG_ON(!path);
4771 key.objectid = inode->i_ino;
4772 key.offset = 0;
39279cc3
CM
4773 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
4774 datasize = btrfs_file_extent_calc_inline_size(name_len);
4775 err = btrfs_insert_empty_item(trans, root, path, &key,
4776 datasize);
54aa1f4d
CM
4777 if (err) {
4778 drop_inode = 1;
4779 goto out_unlock;
4780 }
5f39d397
CM
4781 leaf = path->nodes[0];
4782 ei = btrfs_item_ptr(leaf, path->slots[0],
4783 struct btrfs_file_extent_item);
4784 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
4785 btrfs_set_file_extent_type(leaf, ei,
39279cc3 4786 BTRFS_FILE_EXTENT_INLINE);
c8b97818
CM
4787 btrfs_set_file_extent_encryption(leaf, ei, 0);
4788 btrfs_set_file_extent_compression(leaf, ei, 0);
4789 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
4790 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
4791
39279cc3 4792 ptr = btrfs_file_extent_inline_start(ei);
5f39d397
CM
4793 write_extent_buffer(leaf, symname, ptr, name_len);
4794 btrfs_mark_buffer_dirty(leaf);
39279cc3 4795 btrfs_free_path(path);
5f39d397 4796
39279cc3
CM
4797 inode->i_op = &btrfs_symlink_inode_operations;
4798 inode->i_mapping->a_ops = &btrfs_symlink_aops;
04160088 4799 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
d899e052 4800 inode_set_bytes(inode, name_len);
dbe674a9 4801 btrfs_i_size_write(inode, name_len - 1);
54aa1f4d
CM
4802 err = btrfs_update_inode(trans, root, inode);
4803 if (err)
4804 drop_inode = 1;
39279cc3
CM
4805
4806out_unlock:
d3c2fdcf 4807 nr = trans->blocks_used;
ab78c84d 4808 btrfs_end_transaction_throttle(trans, root);
1832a6d5 4809out_fail:
39279cc3
CM
4810 if (drop_inode) {
4811 inode_dec_link_count(inode);
4812 iput(inode);
4813 }
d3c2fdcf 4814 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
4815 return err;
4816}
16432985 4817
d899e052
YZ
4818static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
4819 u64 alloc_hint, int mode)
4820{
4821 struct btrfs_trans_handle *trans;
4822 struct btrfs_root *root = BTRFS_I(inode)->root;
4823 struct btrfs_key ins;
4824 u64 alloc_size;
4825 u64 cur_offset = start;
4826 u64 num_bytes = end - start;
4827 int ret = 0;
4828
4829 trans = btrfs_join_transaction(root, 1);
4830 BUG_ON(!trans);
4831 btrfs_set_trans_block_group(trans, inode);
4832
4833 while (num_bytes > 0) {
4834 alloc_size = min(num_bytes, root->fs_info->max_extent);
4835 ret = btrfs_reserve_extent(trans, root, alloc_size,
4836 root->sectorsize, 0, alloc_hint,
4837 (u64)-1, &ins, 1);
4838 if (ret) {
4839 WARN_ON(1);
4840 goto out;
4841 }
4842 ret = insert_reserved_file_extent(trans, inode,
4843 cur_offset, ins.objectid,
4844 ins.offset, ins.offset,
4845 ins.offset, 0, 0, 0,
4846 BTRFS_FILE_EXTENT_PREALLOC);
4847 BUG_ON(ret);
4848 num_bytes -= ins.offset;
4849 cur_offset += ins.offset;
4850 alloc_hint = ins.objectid + ins.offset;
4851 }
4852out:
4853 if (cur_offset > start) {
4854 inode->i_ctime = CURRENT_TIME;
4855 btrfs_set_flag(inode, PREALLOC);
4856 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
4857 cur_offset > i_size_read(inode))
4858 btrfs_i_size_write(inode, cur_offset);
4859 ret = btrfs_update_inode(trans, root, inode);
4860 BUG_ON(ret);
4861 }
4862
4863 btrfs_end_transaction(trans, root);
4864 return ret;
4865}
4866
4867static long btrfs_fallocate(struct inode *inode, int mode,
4868 loff_t offset, loff_t len)
4869{
4870 u64 cur_offset;
4871 u64 last_byte;
4872 u64 alloc_start;
4873 u64 alloc_end;
4874 u64 alloc_hint = 0;
4875 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
4876 struct extent_map *em;
4877 int ret;
4878
4879 alloc_start = offset & ~mask;
4880 alloc_end = (offset + len + mask) & ~mask;
4881
4882 mutex_lock(&inode->i_mutex);
4883 if (alloc_start > inode->i_size) {
4884 ret = btrfs_cont_expand(inode, alloc_start);
4885 if (ret)
4886 goto out;
4887 }
4888
4889 while (1) {
4890 struct btrfs_ordered_extent *ordered;
4891 lock_extent(&BTRFS_I(inode)->io_tree, alloc_start,
4892 alloc_end - 1, GFP_NOFS);
4893 ordered = btrfs_lookup_first_ordered_extent(inode,
4894 alloc_end - 1);
4895 if (ordered &&
4896 ordered->file_offset + ordered->len > alloc_start &&
4897 ordered->file_offset < alloc_end) {
4898 btrfs_put_ordered_extent(ordered);
4899 unlock_extent(&BTRFS_I(inode)->io_tree,
4900 alloc_start, alloc_end - 1, GFP_NOFS);
4901 btrfs_wait_ordered_range(inode, alloc_start,
4902 alloc_end - alloc_start);
4903 } else {
4904 if (ordered)
4905 btrfs_put_ordered_extent(ordered);
4906 break;
4907 }
4908 }
4909
4910 cur_offset = alloc_start;
4911 while (1) {
4912 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4913 alloc_end - cur_offset, 0);
4914 BUG_ON(IS_ERR(em) || !em);
4915 last_byte = min(extent_map_end(em), alloc_end);
4916 last_byte = (last_byte + mask) & ~mask;
4917 if (em->block_start == EXTENT_MAP_HOLE) {
4918 ret = prealloc_file_range(inode, cur_offset,
4919 last_byte, alloc_hint, mode);
4920 if (ret < 0) {
4921 free_extent_map(em);
4922 break;
4923 }
4924 }
4925 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
4926 alloc_hint = em->block_start;
4927 free_extent_map(em);
4928
4929 cur_offset = last_byte;
4930 if (cur_offset >= alloc_end) {
4931 ret = 0;
4932 break;
4933 }
4934 }
4935 unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, alloc_end - 1,
4936 GFP_NOFS);
4937out:
4938 mutex_unlock(&inode->i_mutex);
4939 return ret;
4940}
4941
e6dcd2dc
CM
4942static int btrfs_set_page_dirty(struct page *page)
4943{
e6dcd2dc
CM
4944 return __set_page_dirty_nobuffers(page);
4945}
4946
0ee0fda0 4947static int btrfs_permission(struct inode *inode, int mask)
fdebe2bd
Y
4948{
4949 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
4950 return -EACCES;
33268eaf 4951 return generic_permission(inode, mask, btrfs_check_acl);
fdebe2bd 4952}
39279cc3
CM
4953
4954static struct inode_operations btrfs_dir_inode_operations = {
3394e160 4955 .getattr = btrfs_getattr,
39279cc3
CM
4956 .lookup = btrfs_lookup,
4957 .create = btrfs_create,
4958 .unlink = btrfs_unlink,
4959 .link = btrfs_link,
4960 .mkdir = btrfs_mkdir,
4961 .rmdir = btrfs_rmdir,
4962 .rename = btrfs_rename,
4963 .symlink = btrfs_symlink,
4964 .setattr = btrfs_setattr,
618e21d5 4965 .mknod = btrfs_mknod,
95819c05
CH
4966 .setxattr = btrfs_setxattr,
4967 .getxattr = btrfs_getxattr,
5103e947 4968 .listxattr = btrfs_listxattr,
95819c05 4969 .removexattr = btrfs_removexattr,
fdebe2bd 4970 .permission = btrfs_permission,
39279cc3 4971};
39279cc3
CM
4972static struct inode_operations btrfs_dir_ro_inode_operations = {
4973 .lookup = btrfs_lookup,
fdebe2bd 4974 .permission = btrfs_permission,
39279cc3 4975};
39279cc3
CM
4976static struct file_operations btrfs_dir_file_operations = {
4977 .llseek = generic_file_llseek,
4978 .read = generic_read_dir,
cbdf5a24 4979 .readdir = btrfs_real_readdir,
34287aa3 4980 .unlocked_ioctl = btrfs_ioctl,
39279cc3 4981#ifdef CONFIG_COMPAT
34287aa3 4982 .compat_ioctl = btrfs_ioctl,
39279cc3 4983#endif
6bf13c0c 4984 .release = btrfs_release_file,
e02119d5 4985 .fsync = btrfs_sync_file,
39279cc3
CM
4986};
4987
d1310b2e 4988static struct extent_io_ops btrfs_extent_io_ops = {
07157aac 4989 .fill_delalloc = run_delalloc_range,
065631f6 4990 .submit_bio_hook = btrfs_submit_bio_hook,
239b14b3 4991 .merge_bio_hook = btrfs_merge_bio_hook,
07157aac 4992 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
e6dcd2dc 4993 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
247e743c 4994 .writepage_start_hook = btrfs_writepage_start_hook,
1259ab75 4995 .readpage_io_failed_hook = btrfs_io_failed_hook,
b0c68f8b
CM
4996 .set_bit_hook = btrfs_set_bit_hook,
4997 .clear_bit_hook = btrfs_clear_bit_hook,
07157aac
CM
4998};
4999
35054394
CM
5000/*
5001 * btrfs doesn't support the bmap operation because swapfiles
5002 * use bmap to make a mapping of extents in the file. They assume
5003 * these extents won't change over the life of the file and they
5004 * use the bmap result to do IO directly to the drive.
5005 *
5006 * the btrfs bmap call would return logical addresses that aren't
5007 * suitable for IO and they also will change frequently as COW
5008 * operations happen. So, swapfile + btrfs == corruption.
5009 *
5010 * For now we're avoiding this by dropping bmap.
5011 */
39279cc3
CM
5012static struct address_space_operations btrfs_aops = {
5013 .readpage = btrfs_readpage,
5014 .writepage = btrfs_writepage,
b293f02e 5015 .writepages = btrfs_writepages,
3ab2fb5a 5016 .readpages = btrfs_readpages,
39279cc3 5017 .sync_page = block_sync_page,
16432985 5018 .direct_IO = btrfs_direct_IO,
a52d9a80
CM
5019 .invalidatepage = btrfs_invalidatepage,
5020 .releasepage = btrfs_releasepage,
e6dcd2dc 5021 .set_page_dirty = btrfs_set_page_dirty,
39279cc3
CM
5022};
5023
5024static struct address_space_operations btrfs_symlink_aops = {
5025 .readpage = btrfs_readpage,
5026 .writepage = btrfs_writepage,
2bf5a725
CM
5027 .invalidatepage = btrfs_invalidatepage,
5028 .releasepage = btrfs_releasepage,
39279cc3
CM
5029};
5030
5031static struct inode_operations btrfs_file_inode_operations = {
5032 .truncate = btrfs_truncate,
5033 .getattr = btrfs_getattr,
5034 .setattr = btrfs_setattr,
95819c05
CH
5035 .setxattr = btrfs_setxattr,
5036 .getxattr = btrfs_getxattr,
5103e947 5037 .listxattr = btrfs_listxattr,
95819c05 5038 .removexattr = btrfs_removexattr,
fdebe2bd 5039 .permission = btrfs_permission,
d899e052 5040 .fallocate = btrfs_fallocate,
1506fcc8 5041 .fiemap = btrfs_fiemap,
39279cc3 5042};
618e21d5
JB
5043static struct inode_operations btrfs_special_inode_operations = {
5044 .getattr = btrfs_getattr,
5045 .setattr = btrfs_setattr,
fdebe2bd 5046 .permission = btrfs_permission,
95819c05
CH
5047 .setxattr = btrfs_setxattr,
5048 .getxattr = btrfs_getxattr,
33268eaf 5049 .listxattr = btrfs_listxattr,
95819c05 5050 .removexattr = btrfs_removexattr,
618e21d5 5051};
39279cc3
CM
5052static struct inode_operations btrfs_symlink_inode_operations = {
5053 .readlink = generic_readlink,
5054 .follow_link = page_follow_link_light,
5055 .put_link = page_put_link,
fdebe2bd 5056 .permission = btrfs_permission,
0279b4cd
JO
5057 .setxattr = btrfs_setxattr,
5058 .getxattr = btrfs_getxattr,
5059 .listxattr = btrfs_listxattr,
5060 .removexattr = btrfs_removexattr,
39279cc3 5061};