2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.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>
36 #include <linux/bit_spinlock.h>
37 #include <linux/version.h>
38 #include <linux/xattr.h>
41 #include "transaction.h"
42 #include "btrfs_inode.h"
44 #include "print-tree.h"
47 struct btrfs_iget_args {
49 struct btrfs_root *root;
52 static struct inode_operations btrfs_dir_inode_operations;
53 static struct inode_operations btrfs_symlink_inode_operations;
54 static struct inode_operations btrfs_dir_ro_inode_operations;
55 static struct inode_operations btrfs_special_inode_operations;
56 static struct inode_operations btrfs_file_inode_operations;
57 static struct address_space_operations btrfs_aops;
58 static struct address_space_operations btrfs_symlink_aops;
59 static struct file_operations btrfs_dir_file_operations;
60 static struct extent_io_ops btrfs_extent_io_ops;
62 static struct kmem_cache *btrfs_inode_cachep;
63 struct kmem_cache *btrfs_trans_handle_cachep;
64 struct kmem_cache *btrfs_transaction_cachep;
65 struct kmem_cache *btrfs_bit_radix_cachep;
66 struct kmem_cache *btrfs_path_cachep;
69 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
70 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
71 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
72 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
73 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
74 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
75 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
76 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
79 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
82 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
83 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
95 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
96 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
98 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
102 static int cow_file_range(struct inode *inode, u64 start, u64 end)
104 struct btrfs_root *root = BTRFS_I(inode)->root;
105 struct btrfs_trans_handle *trans;
109 u64 blocksize = root->sectorsize;
110 u64 orig_start = start;
112 struct btrfs_key ins;
115 trans = btrfs_start_transaction(root, 1);
117 btrfs_set_trans_block_group(trans, inode);
119 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
120 num_bytes = max(blocksize, num_bytes);
121 ret = btrfs_drop_extents(trans, root, inode,
122 start, start + num_bytes, start, &alloc_hint);
123 orig_num_bytes = num_bytes;
125 if (alloc_hint == EXTENT_MAP_INLINE)
128 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
130 while(num_bytes > 0) {
131 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
132 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
134 root->root_key.objectid,
136 inode->i_ino, start, 0,
137 alloc_hint, (u64)-1, &ins, 1);
142 cur_alloc_size = ins.offset;
143 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
144 start, ins.objectid, ins.offset,
146 inode->i_blocks += ins.offset >> 9;
147 btrfs_check_file(root, inode);
148 if (num_bytes < cur_alloc_size) {
149 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
153 num_bytes -= cur_alloc_size;
154 alloc_hint = ins.objectid + ins.offset;
155 start += cur_alloc_size;
157 btrfs_drop_extent_cache(inode, orig_start,
158 orig_start + orig_num_bytes - 1);
159 btrfs_add_ordered_inode(inode);
160 btrfs_update_inode(trans, root, inode);
162 btrfs_end_transaction(trans, root);
166 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
174 struct btrfs_root *root = BTRFS_I(inode)->root;
175 struct btrfs_block_group_cache *block_group;
176 struct extent_buffer *leaf;
178 struct btrfs_path *path;
179 struct btrfs_file_extent_item *item;
182 struct btrfs_key found_key;
184 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
185 path = btrfs_alloc_path();
188 ret = btrfs_lookup_file_extent(NULL, root, path,
189 inode->i_ino, start, 0);
191 btrfs_free_path(path);
197 if (path->slots[0] == 0)
202 leaf = path->nodes[0];
203 item = btrfs_item_ptr(leaf, path->slots[0],
204 struct btrfs_file_extent_item);
206 /* are we inside the extent that was found? */
207 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
208 found_type = btrfs_key_type(&found_key);
209 if (found_key.objectid != inode->i_ino ||
210 found_type != BTRFS_EXTENT_DATA_KEY) {
214 found_type = btrfs_file_extent_type(leaf, item);
215 extent_start = found_key.offset;
216 if (found_type == BTRFS_FILE_EXTENT_REG) {
217 u64 extent_num_bytes;
219 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
220 extent_end = extent_start + extent_num_bytes;
223 if (loops && start != extent_start)
226 if (start < extent_start || start >= extent_end)
229 cow_end = min(end, extent_end - 1);
230 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
234 if (btrfs_count_snapshots_in_path(root, path, inode->i_ino,
240 * we may be called by the resizer, make sure we're inside
241 * the limits of the FS
243 block_group = btrfs_lookup_block_group(root->fs_info,
245 if (!block_group || block_group->ro)
255 btrfs_free_path(path);
258 btrfs_release_path(root, path);
263 cow_file_range(inode, start, cow_end);
268 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
270 struct btrfs_root *root = BTRFS_I(inode)->root;
272 mutex_lock(&root->fs_info->fs_mutex);
273 if (btrfs_test_opt(root, NODATACOW) ||
274 btrfs_test_flag(inode, NODATACOW))
275 ret = run_delalloc_nocow(inode, start, end);
277 ret = cow_file_range(inode, start, end);
279 mutex_unlock(&root->fs_info->fs_mutex);
283 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
284 unsigned long old, unsigned long bits)
287 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
288 struct btrfs_root *root = BTRFS_I(inode)->root;
289 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
290 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
291 root->fs_info->delalloc_bytes += end - start + 1;
292 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
297 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
298 unsigned long old, unsigned long bits)
300 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
301 struct btrfs_root *root = BTRFS_I(inode)->root;
304 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
305 if (end - start + 1 > root->fs_info->delalloc_bytes) {
306 printk("warning: delalloc account %Lu %Lu\n",
307 end - start + 1, root->fs_info->delalloc_bytes);
308 root->fs_info->delalloc_bytes = 0;
309 BTRFS_I(inode)->delalloc_bytes = 0;
311 root->fs_info->delalloc_bytes -= end - start + 1;
312 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
314 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
319 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
320 size_t size, struct bio *bio)
322 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
323 struct btrfs_mapping_tree *map_tree;
324 u64 logical = bio->bi_sector << 9;
329 length = bio->bi_size;
330 map_tree = &root->fs_info->mapping_tree;
332 ret = btrfs_map_block(map_tree, READ, logical,
333 &map_length, NULL, 0);
335 if (map_length < length + size) {
341 int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
344 struct btrfs_root *root = BTRFS_I(inode)->root;
345 struct btrfs_trans_handle *trans;
349 ret = btrfs_csum_one_bio(root, bio, &sums);
352 mutex_lock(&root->fs_info->fs_mutex);
353 trans = btrfs_start_transaction(root, 1);
355 btrfs_set_trans_block_group(trans, inode);
356 btrfs_csum_file_blocks(trans, root, inode, bio, sums);
358 ret = btrfs_end_transaction(trans, root);
360 mutex_unlock(&root->fs_info->fs_mutex);
364 return btrfs_map_bio(root, rw, bio, mirror_num);
367 int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
370 struct btrfs_root *root = BTRFS_I(inode)->root;
373 if (!(rw & (1 << BIO_RW))) {
374 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
379 if (btrfs_test_opt(root, NODATASUM) ||
380 btrfs_test_flag(inode, NODATASUM)) {
384 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
385 inode, rw, bio, mirror_num,
386 __btrfs_submit_bio_hook);
388 return btrfs_map_bio(root, rw, bio, mirror_num);
391 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
394 struct inode *inode = page->mapping->host;
395 struct btrfs_root *root = BTRFS_I(inode)->root;
396 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
397 struct btrfs_csum_item *item;
398 struct btrfs_path *path = NULL;
401 if (btrfs_test_opt(root, NODATASUM) ||
402 btrfs_test_flag(inode, NODATASUM))
405 mutex_lock(&root->fs_info->fs_mutex);
406 path = btrfs_alloc_path();
407 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
410 /* a csum that isn't present is a preallocated region. */
411 if (ret == -ENOENT || ret == -EFBIG)
414 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
417 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
419 set_state_private(io_tree, start, csum);
422 btrfs_free_path(path);
423 mutex_unlock(&root->fs_info->fs_mutex);
427 struct io_failure_record {
435 int btrfs_readpage_io_failed_hook(struct bio *failed_bio,
436 struct page *page, u64 start, u64 end,
437 struct extent_state *state)
439 struct io_failure_record *failrec = NULL;
441 struct extent_map *em;
442 struct inode *inode = page->mapping->host;
443 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
444 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
450 ret = get_state_private(failure_tree, start, &private);
452 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
455 failrec->start = start;
456 failrec->len = end - start + 1;
457 failrec->last_mirror = 0;
459 spin_lock(&em_tree->lock);
460 em = lookup_extent_mapping(em_tree, start, failrec->len);
461 if (em->start > start || em->start + em->len < start) {
465 spin_unlock(&em_tree->lock);
467 if (!em || IS_ERR(em)) {
471 logical = start - em->start;
472 logical = em->block_start + logical;
473 failrec->logical = logical;
475 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
476 EXTENT_DIRTY, GFP_NOFS);
477 set_state_private(failure_tree, start,
478 (u64)(unsigned long)failrec);
480 failrec = (struct io_failure_record *)(unsigned long)private;
482 num_copies = btrfs_num_copies(
483 &BTRFS_I(inode)->root->fs_info->mapping_tree,
484 failrec->logical, failrec->len);
485 failrec->last_mirror++;
487 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
488 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
491 if (state && state->start != failrec->start)
493 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
495 if (!state || failrec->last_mirror > num_copies) {
496 set_state_private(failure_tree, failrec->start, 0);
497 clear_extent_bits(failure_tree, failrec->start,
498 failrec->start + failrec->len - 1,
499 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
503 bio = bio_alloc(GFP_NOFS, 1);
504 bio->bi_private = state;
505 bio->bi_end_io = failed_bio->bi_end_io;
506 bio->bi_sector = failrec->logical >> 9;
507 bio->bi_bdev = failed_bio->bi_bdev;
509 bio_add_page(bio, page, failrec->len, start - page_offset(page));
510 btrfs_submit_bio_hook(inode, READ, bio, failrec->last_mirror);
514 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
515 struct extent_state *state)
517 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
518 struct inode *inode = page->mapping->host;
519 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
521 u64 private = ~(u32)0;
523 struct btrfs_root *root = BTRFS_I(inode)->root;
527 if (btrfs_test_opt(root, NODATASUM) ||
528 btrfs_test_flag(inode, NODATASUM))
530 if (state && state->start == start) {
531 private = state->private;
534 ret = get_state_private(io_tree, start, &private);
536 local_irq_save(flags);
537 kaddr = kmap_atomic(page, KM_IRQ0);
541 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
542 btrfs_csum_final(csum, (char *)&csum);
543 if (csum != private) {
546 kunmap_atomic(kaddr, KM_IRQ0);
547 local_irq_restore(flags);
549 /* if the io failure tree for this inode is non-empty,
550 * check to see if we've recovered from a failed IO
553 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
554 (u64)-1, 1, EXTENT_DIRTY)) {
556 struct io_failure_record *failure;
557 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
558 start, &private_failure);
560 failure = (struct io_failure_record *)(unsigned long)
562 set_state_private(&BTRFS_I(inode)->io_failure_tree,
564 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
566 failure->start + failure->len - 1,
567 EXTENT_DIRTY | EXTENT_LOCKED,
575 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
576 page->mapping->host->i_ino, (unsigned long long)start, csum,
578 memset(kaddr + offset, 1, end - start + 1);
579 flush_dcache_page(page);
580 kunmap_atomic(kaddr, KM_IRQ0);
581 local_irq_restore(flags);
587 void btrfs_read_locked_inode(struct inode *inode)
589 struct btrfs_path *path;
590 struct extent_buffer *leaf;
591 struct btrfs_inode_item *inode_item;
592 struct btrfs_timespec *tspec;
593 struct btrfs_root *root = BTRFS_I(inode)->root;
594 struct btrfs_key location;
595 u64 alloc_group_block;
599 path = btrfs_alloc_path();
601 mutex_lock(&root->fs_info->fs_mutex);
602 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
604 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
608 leaf = path->nodes[0];
609 inode_item = btrfs_item_ptr(leaf, path->slots[0],
610 struct btrfs_inode_item);
612 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
613 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
614 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
615 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
616 inode->i_size = btrfs_inode_size(leaf, inode_item);
618 tspec = btrfs_inode_atime(inode_item);
619 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
620 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
622 tspec = btrfs_inode_mtime(inode_item);
623 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
624 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
626 tspec = btrfs_inode_ctime(inode_item);
627 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
628 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
630 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
631 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
633 rdev = btrfs_inode_rdev(leaf, inode_item);
635 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
636 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
638 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
639 if (!BTRFS_I(inode)->block_group) {
640 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
642 BTRFS_BLOCK_GROUP_METADATA, 0);
644 btrfs_free_path(path);
647 mutex_unlock(&root->fs_info->fs_mutex);
649 switch (inode->i_mode & S_IFMT) {
651 inode->i_mapping->a_ops = &btrfs_aops;
652 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
653 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
654 inode->i_fop = &btrfs_file_operations;
655 inode->i_op = &btrfs_file_inode_operations;
658 inode->i_fop = &btrfs_dir_file_operations;
659 if (root == root->fs_info->tree_root)
660 inode->i_op = &btrfs_dir_ro_inode_operations;
662 inode->i_op = &btrfs_dir_inode_operations;
665 inode->i_op = &btrfs_symlink_inode_operations;
666 inode->i_mapping->a_ops = &btrfs_symlink_aops;
667 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
670 init_special_inode(inode, inode->i_mode, rdev);
676 btrfs_release_path(root, path);
677 btrfs_free_path(path);
678 mutex_unlock(&root->fs_info->fs_mutex);
679 make_bad_inode(inode);
682 static void fill_inode_item(struct extent_buffer *leaf,
683 struct btrfs_inode_item *item,
686 btrfs_set_inode_uid(leaf, item, inode->i_uid);
687 btrfs_set_inode_gid(leaf, item, inode->i_gid);
688 btrfs_set_inode_size(leaf, item, inode->i_size);
689 btrfs_set_inode_mode(leaf, item, inode->i_mode);
690 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
692 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
693 inode->i_atime.tv_sec);
694 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
695 inode->i_atime.tv_nsec);
697 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
698 inode->i_mtime.tv_sec);
699 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
700 inode->i_mtime.tv_nsec);
702 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
703 inode->i_ctime.tv_sec);
704 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
705 inode->i_ctime.tv_nsec);
707 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
708 btrfs_set_inode_generation(leaf, item, inode->i_generation);
709 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
710 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
711 btrfs_set_inode_block_group(leaf, item,
712 BTRFS_I(inode)->block_group->key.objectid);
715 int btrfs_update_inode(struct btrfs_trans_handle *trans,
716 struct btrfs_root *root,
719 struct btrfs_inode_item *inode_item;
720 struct btrfs_path *path;
721 struct extent_buffer *leaf;
724 path = btrfs_alloc_path();
726 ret = btrfs_lookup_inode(trans, root, path,
727 &BTRFS_I(inode)->location, 1);
734 leaf = path->nodes[0];
735 inode_item = btrfs_item_ptr(leaf, path->slots[0],
736 struct btrfs_inode_item);
738 fill_inode_item(leaf, inode_item, inode);
739 btrfs_mark_buffer_dirty(leaf);
740 btrfs_set_inode_last_trans(trans, inode);
743 btrfs_release_path(root, path);
744 btrfs_free_path(path);
749 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
750 struct btrfs_root *root,
752 struct dentry *dentry)
754 struct btrfs_path *path;
755 const char *name = dentry->d_name.name;
756 int name_len = dentry->d_name.len;
758 struct extent_buffer *leaf;
759 struct btrfs_dir_item *di;
760 struct btrfs_key key;
762 path = btrfs_alloc_path();
768 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
778 leaf = path->nodes[0];
779 btrfs_dir_item_key_to_cpu(leaf, di, &key);
780 ret = btrfs_delete_one_dir_name(trans, root, path, di);
783 btrfs_release_path(root, path);
785 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
786 key.objectid, name, name_len, -1);
795 ret = btrfs_delete_one_dir_name(trans, root, path, di);
797 dentry->d_inode->i_ctime = dir->i_ctime;
798 ret = btrfs_del_inode_ref(trans, root, name, name_len,
799 dentry->d_inode->i_ino,
800 dentry->d_parent->d_inode->i_ino);
802 printk("failed to delete reference to %.*s, "
803 "inode %lu parent %lu\n", name_len, name,
804 dentry->d_inode->i_ino,
805 dentry->d_parent->d_inode->i_ino);
808 btrfs_free_path(path);
810 dir->i_size -= name_len * 2;
811 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
812 btrfs_update_inode(trans, root, dir);
813 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
814 dentry->d_inode->i_nlink--;
816 drop_nlink(dentry->d_inode);
818 ret = btrfs_update_inode(trans, root, dentry->d_inode);
819 dir->i_sb->s_dirt = 1;
824 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
826 struct btrfs_root *root;
827 struct btrfs_trans_handle *trans;
828 struct inode *inode = dentry->d_inode;
830 unsigned long nr = 0;
832 root = BTRFS_I(dir)->root;
833 mutex_lock(&root->fs_info->fs_mutex);
835 ret = btrfs_check_free_space(root, 1, 1);
839 trans = btrfs_start_transaction(root, 1);
841 btrfs_set_trans_block_group(trans, dir);
842 ret = btrfs_unlink_trans(trans, root, dir, dentry);
843 nr = trans->blocks_used;
845 if (inode->i_nlink == 0) {
847 /* if the inode isn't linked anywhere,
848 * we don't need to worry about
851 found = btrfs_del_ordered_inode(inode);
853 atomic_dec(&inode->i_count);
857 btrfs_end_transaction(trans, root);
859 mutex_unlock(&root->fs_info->fs_mutex);
860 btrfs_btree_balance_dirty(root, nr);
861 btrfs_throttle(root);
865 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
867 struct inode *inode = dentry->d_inode;
870 struct btrfs_root *root = BTRFS_I(dir)->root;
871 struct btrfs_trans_handle *trans;
872 unsigned long nr = 0;
874 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
877 mutex_lock(&root->fs_info->fs_mutex);
878 ret = btrfs_check_free_space(root, 1, 1);
882 trans = btrfs_start_transaction(root, 1);
883 btrfs_set_trans_block_group(trans, dir);
885 /* now the directory is empty */
886 err = btrfs_unlink_trans(trans, root, dir, dentry);
891 nr = trans->blocks_used;
892 ret = btrfs_end_transaction(trans, root);
894 mutex_unlock(&root->fs_info->fs_mutex);
895 btrfs_btree_balance_dirty(root, nr);
896 btrfs_throttle(root);
904 * this can truncate away extent items, csum items and directory items.
905 * It starts at a high offset and removes keys until it can't find
906 * any higher than i_size.
908 * csum items that cross the new i_size are truncated to the new size
911 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
912 struct btrfs_root *root,
917 struct btrfs_path *path;
918 struct btrfs_key key;
919 struct btrfs_key found_key;
921 struct extent_buffer *leaf;
922 struct btrfs_file_extent_item *fi;
923 u64 extent_start = 0;
924 u64 extent_num_bytes = 0;
930 int pending_del_nr = 0;
931 int pending_del_slot = 0;
932 int extent_type = -1;
933 u64 mask = root->sectorsize - 1;
935 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
936 path = btrfs_alloc_path();
940 /* FIXME, add redo link to tree so we don't leak on crash */
941 key.objectid = inode->i_ino;
942 key.offset = (u64)-1;
945 btrfs_init_path(path);
947 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
952 BUG_ON(path->slots[0] == 0);
958 leaf = path->nodes[0];
959 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
960 found_type = btrfs_key_type(&found_key);
962 if (found_key.objectid != inode->i_ino)
965 if (found_type < min_type)
968 item_end = found_key.offset;
969 if (found_type == BTRFS_EXTENT_DATA_KEY) {
970 fi = btrfs_item_ptr(leaf, path->slots[0],
971 struct btrfs_file_extent_item);
972 extent_type = btrfs_file_extent_type(leaf, fi);
973 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
975 btrfs_file_extent_num_bytes(leaf, fi);
976 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
977 struct btrfs_item *item = btrfs_item_nr(leaf,
979 item_end += btrfs_file_extent_inline_len(leaf,
984 if (found_type == BTRFS_CSUM_ITEM_KEY) {
985 ret = btrfs_csum_truncate(trans, root, path,
989 if (item_end < inode->i_size) {
990 if (found_type == BTRFS_DIR_ITEM_KEY) {
991 found_type = BTRFS_INODE_ITEM_KEY;
992 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
993 found_type = BTRFS_CSUM_ITEM_KEY;
994 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
995 found_type = BTRFS_XATTR_ITEM_KEY;
996 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
997 found_type = BTRFS_INODE_REF_KEY;
998 } else if (found_type) {
1003 btrfs_set_key_type(&key, found_type);
1006 if (found_key.offset >= inode->i_size)
1012 /* FIXME, shrink the extent if the ref count is only 1 */
1013 if (found_type != BTRFS_EXTENT_DATA_KEY)
1016 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1018 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
1020 u64 orig_num_bytes =
1021 btrfs_file_extent_num_bytes(leaf, fi);
1022 extent_num_bytes = inode->i_size -
1023 found_key.offset + root->sectorsize - 1;
1024 extent_num_bytes = extent_num_bytes &
1025 ~((u64)root->sectorsize - 1);
1026 btrfs_set_file_extent_num_bytes(leaf, fi,
1028 num_dec = (orig_num_bytes -
1030 if (extent_start != 0)
1031 dec_i_blocks(inode, num_dec);
1032 btrfs_mark_buffer_dirty(leaf);
1035 btrfs_file_extent_disk_num_bytes(leaf,
1037 /* FIXME blocksize != 4096 */
1038 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
1039 if (extent_start != 0) {
1041 dec_i_blocks(inode, num_dec);
1043 root_gen = btrfs_header_generation(leaf);
1044 root_owner = btrfs_header_owner(leaf);
1046 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1048 u32 newsize = inode->i_size - found_key.offset;
1049 dec_i_blocks(inode, item_end + 1 -
1050 found_key.offset - newsize);
1052 btrfs_file_extent_calc_inline_size(newsize);
1053 ret = btrfs_truncate_item(trans, root, path,
1057 dec_i_blocks(inode, item_end + 1 -
1063 if (!pending_del_nr) {
1064 /* no pending yet, add ourselves */
1065 pending_del_slot = path->slots[0];
1067 } else if (pending_del_nr &&
1068 path->slots[0] + 1 == pending_del_slot) {
1069 /* hop on the pending chunk */
1071 pending_del_slot = path->slots[0];
1073 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1079 ret = btrfs_free_extent(trans, root, extent_start,
1082 root_gen, inode->i_ino,
1083 found_key.offset, 0);
1087 if (path->slots[0] == 0) {
1090 btrfs_release_path(root, path);
1095 if (pending_del_nr &&
1096 path->slots[0] + 1 != pending_del_slot) {
1097 struct btrfs_key debug;
1099 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1101 ret = btrfs_del_items(trans, root, path,
1106 btrfs_release_path(root, path);
1112 if (pending_del_nr) {
1113 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1116 btrfs_release_path(root, path);
1117 btrfs_free_path(path);
1118 inode->i_sb->s_dirt = 1;
1122 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
1126 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1127 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1128 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
1131 WARN_ON(!PageLocked(page));
1132 set_page_extent_mapped(page);
1134 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1135 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1136 page_end, GFP_NOFS);
1138 if (zero_start != PAGE_CACHE_SIZE) {
1140 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
1141 flush_dcache_page(page);
1144 set_page_dirty(page);
1145 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1151 * taken from block_truncate_page, but does cow as it zeros out
1152 * any bytes left in the last page in the file.
1154 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1156 struct inode *inode = mapping->host;
1157 struct btrfs_root *root = BTRFS_I(inode)->root;
1158 u32 blocksize = root->sectorsize;
1159 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1160 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1165 if ((offset & (blocksize - 1)) == 0)
1169 page = grab_cache_page(mapping, index);
1172 if (!PageUptodate(page)) {
1173 ret = btrfs_readpage(NULL, page);
1175 if (!PageUptodate(page)) {
1180 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1182 ret = btrfs_cow_one_page(inode, page, offset);
1185 page_cache_release(page);
1190 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1192 struct inode *inode = dentry->d_inode;
1195 err = inode_change_ok(inode, attr);
1199 if (S_ISREG(inode->i_mode) &&
1200 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1201 struct btrfs_trans_handle *trans;
1202 struct btrfs_root *root = BTRFS_I(inode)->root;
1203 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1205 u64 mask = root->sectorsize - 1;
1206 u64 hole_start = (inode->i_size + mask) & ~mask;
1207 u64 block_end = (attr->ia_size + mask) & ~mask;
1211 if (attr->ia_size <= hole_start)
1214 mutex_lock(&root->fs_info->fs_mutex);
1215 err = btrfs_check_free_space(root, 1, 0);
1216 mutex_unlock(&root->fs_info->fs_mutex);
1220 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1222 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1223 hole_size = block_end - hole_start;
1225 mutex_lock(&root->fs_info->fs_mutex);
1226 trans = btrfs_start_transaction(root, 1);
1227 btrfs_set_trans_block_group(trans, inode);
1228 err = btrfs_drop_extents(trans, root, inode,
1229 hole_start, block_end, hole_start,
1232 if (alloc_hint != EXTENT_MAP_INLINE) {
1233 err = btrfs_insert_file_extent(trans, root,
1237 btrfs_drop_extent_cache(inode, hole_start,
1239 btrfs_check_file(root, inode);
1241 btrfs_end_transaction(trans, root);
1242 mutex_unlock(&root->fs_info->fs_mutex);
1243 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1248 err = inode_setattr(inode, attr);
1253 void btrfs_put_inode(struct inode *inode)
1257 if (!BTRFS_I(inode)->ordered_trans) {
1261 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1262 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1265 ret = btrfs_del_ordered_inode(inode);
1267 atomic_dec(&inode->i_count);
1271 void btrfs_delete_inode(struct inode *inode)
1273 struct btrfs_trans_handle *trans;
1274 struct btrfs_root *root = BTRFS_I(inode)->root;
1278 truncate_inode_pages(&inode->i_data, 0);
1279 if (is_bad_inode(inode)) {
1284 mutex_lock(&root->fs_info->fs_mutex);
1285 trans = btrfs_start_transaction(root, 1);
1287 btrfs_set_trans_block_group(trans, inode);
1288 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1290 goto no_delete_lock;
1292 nr = trans->blocks_used;
1295 btrfs_end_transaction(trans, root);
1296 mutex_unlock(&root->fs_info->fs_mutex);
1297 btrfs_btree_balance_dirty(root, nr);
1298 btrfs_throttle(root);
1302 nr = trans->blocks_used;
1303 btrfs_end_transaction(trans, root);
1304 mutex_unlock(&root->fs_info->fs_mutex);
1305 btrfs_btree_balance_dirty(root, nr);
1306 btrfs_throttle(root);
1312 * this returns the key found in the dir entry in the location pointer.
1313 * If no dir entries were found, location->objectid is 0.
1315 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1316 struct btrfs_key *location)
1318 const char *name = dentry->d_name.name;
1319 int namelen = dentry->d_name.len;
1320 struct btrfs_dir_item *di;
1321 struct btrfs_path *path;
1322 struct btrfs_root *root = BTRFS_I(dir)->root;
1325 if (namelen == 1 && strcmp(name, ".") == 0) {
1326 location->objectid = dir->i_ino;
1327 location->type = BTRFS_INODE_ITEM_KEY;
1328 location->offset = 0;
1331 path = btrfs_alloc_path();
1334 if (namelen == 2 && strcmp(name, "..") == 0) {
1335 struct btrfs_key key;
1336 struct extent_buffer *leaf;
1340 key.objectid = dir->i_ino;
1341 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1343 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1347 leaf = path->nodes[0];
1348 slot = path->slots[0];
1349 nritems = btrfs_header_nritems(leaf);
1350 if (slot >= nritems)
1353 btrfs_item_key_to_cpu(leaf, &key, slot);
1354 if (key.objectid != dir->i_ino ||
1355 key.type != BTRFS_INODE_REF_KEY) {
1358 location->objectid = key.offset;
1359 location->type = BTRFS_INODE_ITEM_KEY;
1360 location->offset = 0;
1364 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1368 if (!di || IS_ERR(di)) {
1371 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1373 btrfs_free_path(path);
1376 location->objectid = 0;
1381 * when we hit a tree root in a directory, the btrfs part of the inode
1382 * needs to be changed to reflect the root directory of the tree root. This
1383 * is kind of like crossing a mount point.
1385 static int fixup_tree_root_location(struct btrfs_root *root,
1386 struct btrfs_key *location,
1387 struct btrfs_root **sub_root,
1388 struct dentry *dentry)
1390 struct btrfs_path *path;
1391 struct btrfs_root_item *ri;
1393 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1395 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1398 path = btrfs_alloc_path();
1400 mutex_lock(&root->fs_info->fs_mutex);
1402 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1403 dentry->d_name.name,
1404 dentry->d_name.len);
1405 if (IS_ERR(*sub_root))
1406 return PTR_ERR(*sub_root);
1408 ri = &(*sub_root)->root_item;
1409 location->objectid = btrfs_root_dirid(ri);
1410 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1411 location->offset = 0;
1413 btrfs_free_path(path);
1414 mutex_unlock(&root->fs_info->fs_mutex);
1418 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1420 struct btrfs_iget_args *args = p;
1421 inode->i_ino = args->ino;
1422 BTRFS_I(inode)->root = args->root;
1423 BTRFS_I(inode)->delalloc_bytes = 0;
1424 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1425 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1426 inode->i_mapping, GFP_NOFS);
1427 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1428 inode->i_mapping, GFP_NOFS);
1429 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
1433 static int btrfs_find_actor(struct inode *inode, void *opaque)
1435 struct btrfs_iget_args *args = opaque;
1436 return (args->ino == inode->i_ino &&
1437 args->root == BTRFS_I(inode)->root);
1440 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1443 struct btrfs_iget_args args;
1444 args.ino = objectid;
1445 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1450 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1453 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1454 struct btrfs_root *root)
1456 struct inode *inode;
1457 struct btrfs_iget_args args;
1458 args.ino = objectid;
1461 inode = iget5_locked(s, objectid, btrfs_find_actor,
1462 btrfs_init_locked_inode,
1467 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1468 struct nameidata *nd)
1470 struct inode * inode;
1471 struct btrfs_inode *bi = BTRFS_I(dir);
1472 struct btrfs_root *root = bi->root;
1473 struct btrfs_root *sub_root = root;
1474 struct btrfs_key location;
1477 if (dentry->d_name.len > BTRFS_NAME_LEN)
1478 return ERR_PTR(-ENAMETOOLONG);
1480 mutex_lock(&root->fs_info->fs_mutex);
1481 ret = btrfs_inode_by_name(dir, dentry, &location);
1482 mutex_unlock(&root->fs_info->fs_mutex);
1485 return ERR_PTR(ret);
1488 if (location.objectid) {
1489 ret = fixup_tree_root_location(root, &location, &sub_root,
1492 return ERR_PTR(ret);
1494 return ERR_PTR(-ENOENT);
1495 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1498 return ERR_PTR(-EACCES);
1499 if (inode->i_state & I_NEW) {
1500 /* the inode and parent dir are two different roots */
1501 if (sub_root != root) {
1503 sub_root->inode = inode;
1505 BTRFS_I(inode)->root = sub_root;
1506 memcpy(&BTRFS_I(inode)->location, &location,
1508 btrfs_read_locked_inode(inode);
1509 unlock_new_inode(inode);
1512 return d_splice_alias(inode, dentry);
1515 static unsigned char btrfs_filetype_table[] = {
1516 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1519 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1521 struct inode *inode = filp->f_dentry->d_inode;
1522 struct btrfs_root *root = BTRFS_I(inode)->root;
1523 struct btrfs_item *item;
1524 struct btrfs_dir_item *di;
1525 struct btrfs_key key;
1526 struct btrfs_key found_key;
1527 struct btrfs_path *path;
1530 struct extent_buffer *leaf;
1533 unsigned char d_type;
1538 int key_type = BTRFS_DIR_INDEX_KEY;
1543 /* FIXME, use a real flag for deciding about the key type */
1544 if (root->fs_info->tree_root == root)
1545 key_type = BTRFS_DIR_ITEM_KEY;
1547 /* special case for "." */
1548 if (filp->f_pos == 0) {
1549 over = filldir(dirent, ".", 1,
1557 mutex_lock(&root->fs_info->fs_mutex);
1558 key.objectid = inode->i_ino;
1559 path = btrfs_alloc_path();
1562 /* special case for .., just use the back ref */
1563 if (filp->f_pos == 1) {
1564 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1566 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1568 leaf = path->nodes[0];
1569 slot = path->slots[0];
1570 nritems = btrfs_header_nritems(leaf);
1571 if (slot >= nritems) {
1572 btrfs_release_path(root, path);
1573 goto read_dir_items;
1575 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1576 btrfs_release_path(root, path);
1577 if (found_key.objectid != key.objectid ||
1578 found_key.type != BTRFS_INODE_REF_KEY)
1579 goto read_dir_items;
1580 over = filldir(dirent, "..", 2,
1581 2, found_key.offset, DT_DIR);
1588 btrfs_set_key_type(&key, key_type);
1589 key.offset = filp->f_pos;
1591 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1596 leaf = path->nodes[0];
1597 nritems = btrfs_header_nritems(leaf);
1598 slot = path->slots[0];
1599 if (advance || slot >= nritems) {
1600 if (slot >= nritems -1) {
1601 ret = btrfs_next_leaf(root, path);
1604 leaf = path->nodes[0];
1605 nritems = btrfs_header_nritems(leaf);
1606 slot = path->slots[0];
1613 item = btrfs_item_nr(leaf, slot);
1614 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1616 if (found_key.objectid != key.objectid)
1618 if (btrfs_key_type(&found_key) != key_type)
1620 if (found_key.offset < filp->f_pos)
1623 filp->f_pos = found_key.offset;
1625 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1627 di_total = btrfs_item_size(leaf, item);
1628 while(di_cur < di_total) {
1629 struct btrfs_key location;
1631 name_len = btrfs_dir_name_len(leaf, di);
1632 if (name_len < 32) {
1633 name_ptr = tmp_name;
1635 name_ptr = kmalloc(name_len, GFP_NOFS);
1638 read_extent_buffer(leaf, name_ptr,
1639 (unsigned long)(di + 1), name_len);
1641 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1642 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1643 over = filldir(dirent, name_ptr, name_len,
1648 if (name_ptr != tmp_name)
1653 di_len = btrfs_dir_name_len(leaf, di) +
1654 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1656 di = (struct btrfs_dir_item *)((char *)di + di_len);
1659 if (key_type == BTRFS_DIR_INDEX_KEY)
1660 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1666 btrfs_release_path(root, path);
1667 btrfs_free_path(path);
1668 mutex_unlock(&root->fs_info->fs_mutex);
1672 int btrfs_write_inode(struct inode *inode, int wait)
1674 struct btrfs_root *root = BTRFS_I(inode)->root;
1675 struct btrfs_trans_handle *trans;
1679 mutex_lock(&root->fs_info->fs_mutex);
1680 trans = btrfs_start_transaction(root, 1);
1681 btrfs_set_trans_block_group(trans, inode);
1682 ret = btrfs_commit_transaction(trans, root);
1683 mutex_unlock(&root->fs_info->fs_mutex);
1689 * This is somewhat expensive, updating the tree every time the
1690 * inode changes. But, it is most likely to find the inode in cache.
1691 * FIXME, needs more benchmarking...there are no reasons other than performance
1692 * to keep or drop this code.
1694 void btrfs_dirty_inode(struct inode *inode)
1696 struct btrfs_root *root = BTRFS_I(inode)->root;
1697 struct btrfs_trans_handle *trans;
1699 mutex_lock(&root->fs_info->fs_mutex);
1700 trans = btrfs_start_transaction(root, 1);
1701 btrfs_set_trans_block_group(trans, inode);
1702 btrfs_update_inode(trans, root, inode);
1703 btrfs_end_transaction(trans, root);
1704 mutex_unlock(&root->fs_info->fs_mutex);
1707 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1708 struct btrfs_root *root,
1709 const char *name, int name_len,
1712 struct btrfs_block_group_cache *group,
1715 struct inode *inode;
1716 struct btrfs_inode_item *inode_item;
1717 struct btrfs_block_group_cache *new_inode_group;
1718 struct btrfs_key *location;
1719 struct btrfs_path *path;
1720 struct btrfs_inode_ref *ref;
1721 struct btrfs_key key[2];
1727 path = btrfs_alloc_path();
1730 inode = new_inode(root->fs_info->sb);
1732 return ERR_PTR(-ENOMEM);
1734 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1735 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1736 inode->i_mapping, GFP_NOFS);
1737 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1738 inode->i_mapping, GFP_NOFS);
1739 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
1740 BTRFS_I(inode)->delalloc_bytes = 0;
1741 BTRFS_I(inode)->root = root;
1747 new_inode_group = btrfs_find_block_group(root, group, 0,
1748 BTRFS_BLOCK_GROUP_METADATA, owner);
1749 if (!new_inode_group) {
1750 printk("find_block group failed\n");
1751 new_inode_group = group;
1753 BTRFS_I(inode)->block_group = new_inode_group;
1754 BTRFS_I(inode)->flags = 0;
1756 key[0].objectid = objectid;
1757 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1760 key[1].objectid = objectid;
1761 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1762 key[1].offset = ref_objectid;
1764 sizes[0] = sizeof(struct btrfs_inode_item);
1765 sizes[1] = name_len + sizeof(*ref);
1767 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1771 if (objectid > root->highest_inode)
1772 root->highest_inode = objectid;
1774 inode->i_uid = current->fsuid;
1775 inode->i_gid = current->fsgid;
1776 inode->i_mode = mode;
1777 inode->i_ino = objectid;
1778 inode->i_blocks = 0;
1779 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1780 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1781 struct btrfs_inode_item);
1782 fill_inode_item(path->nodes[0], inode_item, inode);
1784 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1785 struct btrfs_inode_ref);
1786 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1787 ptr = (unsigned long)(ref + 1);
1788 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1790 btrfs_mark_buffer_dirty(path->nodes[0]);
1791 btrfs_free_path(path);
1793 location = &BTRFS_I(inode)->location;
1794 location->objectid = objectid;
1795 location->offset = 0;
1796 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1798 insert_inode_hash(inode);
1801 btrfs_free_path(path);
1802 return ERR_PTR(ret);
1805 static inline u8 btrfs_inode_type(struct inode *inode)
1807 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1810 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1811 struct dentry *dentry, struct inode *inode,
1815 struct btrfs_key key;
1816 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1817 struct inode *parent_inode;
1819 key.objectid = inode->i_ino;
1820 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1823 ret = btrfs_insert_dir_item(trans, root,
1824 dentry->d_name.name, dentry->d_name.len,
1825 dentry->d_parent->d_inode->i_ino,
1826 &key, btrfs_inode_type(inode));
1829 ret = btrfs_insert_inode_ref(trans, root,
1830 dentry->d_name.name,
1833 dentry->d_parent->d_inode->i_ino);
1835 parent_inode = dentry->d_parent->d_inode;
1836 parent_inode->i_size += dentry->d_name.len * 2;
1837 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1838 ret = btrfs_update_inode(trans, root,
1839 dentry->d_parent->d_inode);
1844 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1845 struct dentry *dentry, struct inode *inode,
1848 int err = btrfs_add_link(trans, dentry, inode, backref);
1850 d_instantiate(dentry, inode);
1858 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1859 int mode, dev_t rdev)
1861 struct btrfs_trans_handle *trans;
1862 struct btrfs_root *root = BTRFS_I(dir)->root;
1863 struct inode *inode = NULL;
1867 unsigned long nr = 0;
1869 if (!new_valid_dev(rdev))
1872 mutex_lock(&root->fs_info->fs_mutex);
1873 err = btrfs_check_free_space(root, 1, 0);
1877 trans = btrfs_start_transaction(root, 1);
1878 btrfs_set_trans_block_group(trans, dir);
1880 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1886 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1888 dentry->d_parent->d_inode->i_ino, objectid,
1889 BTRFS_I(dir)->block_group, mode);
1890 err = PTR_ERR(inode);
1894 btrfs_set_trans_block_group(trans, inode);
1895 err = btrfs_add_nondir(trans, dentry, inode, 0);
1899 inode->i_op = &btrfs_special_inode_operations;
1900 init_special_inode(inode, inode->i_mode, rdev);
1901 btrfs_update_inode(trans, root, inode);
1903 dir->i_sb->s_dirt = 1;
1904 btrfs_update_inode_block_group(trans, inode);
1905 btrfs_update_inode_block_group(trans, dir);
1907 nr = trans->blocks_used;
1908 btrfs_end_transaction(trans, root);
1910 mutex_unlock(&root->fs_info->fs_mutex);
1913 inode_dec_link_count(inode);
1916 btrfs_btree_balance_dirty(root, nr);
1917 btrfs_throttle(root);
1921 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1922 int mode, struct nameidata *nd)
1924 struct btrfs_trans_handle *trans;
1925 struct btrfs_root *root = BTRFS_I(dir)->root;
1926 struct inode *inode = NULL;
1929 unsigned long nr = 0;
1932 mutex_lock(&root->fs_info->fs_mutex);
1933 err = btrfs_check_free_space(root, 1, 0);
1936 trans = btrfs_start_transaction(root, 1);
1937 btrfs_set_trans_block_group(trans, dir);
1939 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1945 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1947 dentry->d_parent->d_inode->i_ino,
1948 objectid, BTRFS_I(dir)->block_group, mode);
1949 err = PTR_ERR(inode);
1953 btrfs_set_trans_block_group(trans, inode);
1954 err = btrfs_add_nondir(trans, dentry, inode, 0);
1958 inode->i_mapping->a_ops = &btrfs_aops;
1959 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1960 inode->i_fop = &btrfs_file_operations;
1961 inode->i_op = &btrfs_file_inode_operations;
1962 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1963 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1964 inode->i_mapping, GFP_NOFS);
1965 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1966 inode->i_mapping, GFP_NOFS);
1967 BTRFS_I(inode)->delalloc_bytes = 0;
1968 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
1969 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1971 dir->i_sb->s_dirt = 1;
1972 btrfs_update_inode_block_group(trans, inode);
1973 btrfs_update_inode_block_group(trans, dir);
1975 nr = trans->blocks_used;
1976 btrfs_end_transaction(trans, root);
1978 mutex_unlock(&root->fs_info->fs_mutex);
1981 inode_dec_link_count(inode);
1984 btrfs_btree_balance_dirty(root, nr);
1985 btrfs_throttle(root);
1989 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1990 struct dentry *dentry)
1992 struct btrfs_trans_handle *trans;
1993 struct btrfs_root *root = BTRFS_I(dir)->root;
1994 struct inode *inode = old_dentry->d_inode;
1995 unsigned long nr = 0;
1999 if (inode->i_nlink == 0)
2002 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2007 mutex_lock(&root->fs_info->fs_mutex);
2008 err = btrfs_check_free_space(root, 1, 0);
2011 trans = btrfs_start_transaction(root, 1);
2013 btrfs_set_trans_block_group(trans, dir);
2014 atomic_inc(&inode->i_count);
2015 err = btrfs_add_nondir(trans, dentry, inode, 1);
2020 dir->i_sb->s_dirt = 1;
2021 btrfs_update_inode_block_group(trans, dir);
2022 err = btrfs_update_inode(trans, root, inode);
2027 nr = trans->blocks_used;
2028 btrfs_end_transaction(trans, root);
2030 mutex_unlock(&root->fs_info->fs_mutex);
2033 inode_dec_link_count(inode);
2036 btrfs_btree_balance_dirty(root, nr);
2037 btrfs_throttle(root);
2041 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2043 struct inode *inode = NULL;
2044 struct btrfs_trans_handle *trans;
2045 struct btrfs_root *root = BTRFS_I(dir)->root;
2047 int drop_on_err = 0;
2049 unsigned long nr = 1;
2051 mutex_lock(&root->fs_info->fs_mutex);
2052 err = btrfs_check_free_space(root, 1, 0);
2056 trans = btrfs_start_transaction(root, 1);
2057 btrfs_set_trans_block_group(trans, dir);
2059 if (IS_ERR(trans)) {
2060 err = PTR_ERR(trans);
2064 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2070 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2072 dentry->d_parent->d_inode->i_ino, objectid,
2073 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2074 if (IS_ERR(inode)) {
2075 err = PTR_ERR(inode);
2080 inode->i_op = &btrfs_dir_inode_operations;
2081 inode->i_fop = &btrfs_dir_file_operations;
2082 btrfs_set_trans_block_group(trans, inode);
2085 err = btrfs_update_inode(trans, root, inode);
2089 err = btrfs_add_link(trans, dentry, inode, 0);
2093 d_instantiate(dentry, inode);
2095 dir->i_sb->s_dirt = 1;
2096 btrfs_update_inode_block_group(trans, inode);
2097 btrfs_update_inode_block_group(trans, dir);
2100 nr = trans->blocks_used;
2101 btrfs_end_transaction(trans, root);
2104 mutex_unlock(&root->fs_info->fs_mutex);
2107 btrfs_btree_balance_dirty(root, nr);
2108 btrfs_throttle(root);
2112 static int merge_extent_mapping(struct extent_map_tree *em_tree,
2113 struct extent_map *existing,
2114 struct extent_map *em)
2119 int real_blocks = existing->block_start < EXTENT_MAP_LAST_BYTE;
2121 if (real_blocks && em->block_start >= EXTENT_MAP_LAST_BYTE)
2124 if (!real_blocks && em->block_start != existing->block_start)
2127 new_end = max(existing->start + existing->len, em->start + em->len);
2129 if (existing->start >= em->start) {
2130 if (em->start + em->len < existing->start)
2133 start_diff = existing->start - em->start;
2134 if (real_blocks && em->block_start + start_diff !=
2135 existing->block_start)
2138 em->len = new_end - em->start;
2140 remove_extent_mapping(em_tree, existing);
2141 /* free for the tree */
2142 free_extent_map(existing);
2143 ret = add_extent_mapping(em_tree, em);
2145 } else if (em->start > existing->start) {
2147 if (existing->start + existing->len < em->start)
2150 start_diff = em->start - existing->start;
2151 if (real_blocks && existing->block_start + start_diff !=
2155 remove_extent_mapping(em_tree, existing);
2156 em->block_start = existing->block_start;
2157 em->start = existing->start;
2158 em->len = new_end - existing->start;
2159 free_extent_map(existing);
2161 ret = add_extent_mapping(em_tree, em);
2168 printk("invalid extent map merge [%Lu %Lu %Lu] [%Lu %Lu %Lu]\n",
2169 existing->start, existing->len, existing->block_start,
2170 em->start, em->len, em->block_start);
2174 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2175 size_t pg_offset, u64 start, u64 len,
2181 u64 extent_start = 0;
2183 u64 objectid = inode->i_ino;
2185 struct btrfs_path *path;
2186 struct btrfs_root *root = BTRFS_I(inode)->root;
2187 struct btrfs_file_extent_item *item;
2188 struct extent_buffer *leaf;
2189 struct btrfs_key found_key;
2190 struct extent_map *em = NULL;
2191 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2192 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2193 struct btrfs_trans_handle *trans = NULL;
2195 path = btrfs_alloc_path();
2197 mutex_lock(&root->fs_info->fs_mutex);
2200 spin_lock(&em_tree->lock);
2201 em = lookup_extent_mapping(em_tree, start, len);
2203 em->bdev = root->fs_info->fs_devices->latest_bdev;
2204 spin_unlock(&em_tree->lock);
2207 if (em->start > start || em->start + em->len <= start)
2208 free_extent_map(em);
2209 else if (em->block_start == EXTENT_MAP_INLINE && page)
2210 free_extent_map(em);
2214 em = alloc_extent_map(GFP_NOFS);
2220 em->start = EXTENT_MAP_HOLE;
2222 em->bdev = root->fs_info->fs_devices->latest_bdev;
2223 ret = btrfs_lookup_file_extent(trans, root, path,
2224 objectid, start, trans != NULL);
2231 if (path->slots[0] == 0)
2236 leaf = path->nodes[0];
2237 item = btrfs_item_ptr(leaf, path->slots[0],
2238 struct btrfs_file_extent_item);
2239 /* are we inside the extent that was found? */
2240 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2241 found_type = btrfs_key_type(&found_key);
2242 if (found_key.objectid != objectid ||
2243 found_type != BTRFS_EXTENT_DATA_KEY) {
2247 found_type = btrfs_file_extent_type(leaf, item);
2248 extent_start = found_key.offset;
2249 if (found_type == BTRFS_FILE_EXTENT_REG) {
2250 extent_end = extent_start +
2251 btrfs_file_extent_num_bytes(leaf, item);
2253 if (start < extent_start || start >= extent_end) {
2255 if (start < extent_start) {
2256 if (start + len <= extent_start)
2258 em->len = extent_end - extent_start;
2264 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2266 em->start = extent_start;
2267 em->len = extent_end - extent_start;
2268 em->block_start = EXTENT_MAP_HOLE;
2271 bytenr += btrfs_file_extent_offset(leaf, item);
2272 em->block_start = bytenr;
2273 em->start = extent_start;
2274 em->len = extent_end - extent_start;
2276 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2281 size_t extent_offset;
2284 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2286 extent_end = (extent_start + size + root->sectorsize - 1) &
2287 ~((u64)root->sectorsize - 1);
2288 if (start < extent_start || start >= extent_end) {
2290 if (start < extent_start) {
2291 if (start + len <= extent_start)
2293 em->len = extent_end - extent_start;
2299 em->block_start = EXTENT_MAP_INLINE;
2302 em->start = extent_start;
2307 page_start = page_offset(page) + pg_offset;
2308 extent_offset = page_start - extent_start;
2309 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2310 size - extent_offset);
2311 em->start = extent_start + extent_offset;
2312 em->len = (copy_size + root->sectorsize - 1) &
2313 ~((u64)root->sectorsize - 1);
2315 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2316 if (create == 0 && !PageUptodate(page)) {
2317 read_extent_buffer(leaf, map + pg_offset, ptr,
2319 flush_dcache_page(page);
2320 } else if (create && PageUptodate(page)) {
2323 free_extent_map(em);
2325 btrfs_release_path(root, path);
2326 trans = btrfs_start_transaction(root, 1);
2329 write_extent_buffer(leaf, map + pg_offset, ptr,
2331 btrfs_mark_buffer_dirty(leaf);
2334 set_extent_uptodate(io_tree, em->start,
2335 extent_map_end(em) - 1, GFP_NOFS);
2338 printk("unkknown found_type %d\n", found_type);
2345 em->block_start = EXTENT_MAP_HOLE;
2347 btrfs_release_path(root, path);
2348 if (em->start > start || extent_map_end(em) <= start) {
2349 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2355 spin_lock(&em_tree->lock);
2356 ret = add_extent_mapping(em_tree, em);
2357 /* it is possible that someone inserted the extent into the tree
2358 * while we had the lock dropped. It is also possible that
2359 * an overlapping map exists in the tree
2361 if (ret == -EEXIST) {
2362 struct extent_map *existing;
2363 existing = lookup_extent_mapping(em_tree, start, len);
2364 if (existing && (existing->start > start ||
2365 existing->start + existing->len <= start)) {
2366 free_extent_map(existing);
2370 existing = lookup_extent_mapping(em_tree, em->start,
2373 err = merge_extent_mapping(em_tree, existing,
2375 free_extent_map(existing);
2377 free_extent_map(em);
2382 printk("failing to insert %Lu %Lu\n",
2384 free_extent_map(em);
2388 free_extent_map(em);
2392 spin_unlock(&em_tree->lock);
2394 btrfs_free_path(path);
2396 ret = btrfs_end_transaction(trans, root);
2400 mutex_unlock(&root->fs_info->fs_mutex);
2402 free_extent_map(em);
2404 return ERR_PTR(err);
2409 #if 0 /* waiting for O_DIRECT reads */
2410 static int btrfs_get_block(struct inode *inode, sector_t iblock,
2411 struct buffer_head *bh_result, int create)
2413 struct extent_map *em;
2414 u64 start = (u64)iblock << inode->i_blkbits;
2415 struct btrfs_multi_bio *multi = NULL;
2416 struct btrfs_root *root = BTRFS_I(inode)->root;
2422 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2424 if (!em || IS_ERR(em))
2427 if (em->start > start || em->start + em->len <= start) {
2431 if (em->block_start == EXTENT_MAP_INLINE) {
2436 len = em->start + em->len - start;
2437 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2439 if (em->block_start == EXTENT_MAP_HOLE ||
2440 em->block_start == EXTENT_MAP_DELALLOC) {
2441 bh_result->b_size = len;
2445 logical = start - em->start;
2446 logical = em->block_start + logical;
2449 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2450 logical, &map_length, &multi, 0);
2452 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2453 bh_result->b_size = min(map_length, len);
2455 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2456 set_buffer_mapped(bh_result);
2459 free_extent_map(em);
2464 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2465 const struct iovec *iov, loff_t offset,
2466 unsigned long nr_segs)
2470 struct file *file = iocb->ki_filp;
2471 struct inode *inode = file->f_mapping->host;
2476 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2477 offset, nr_segs, btrfs_get_block, NULL);
2481 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2483 return extent_bmap(mapping, iblock, btrfs_get_extent);
2486 int btrfs_readpage(struct file *file, struct page *page)
2488 struct extent_io_tree *tree;
2489 tree = &BTRFS_I(page->mapping->host)->io_tree;
2490 return extent_read_full_page(tree, page, btrfs_get_extent);
2493 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2495 struct extent_io_tree *tree;
2498 if (current->flags & PF_MEMALLOC) {
2499 redirty_page_for_writepage(wbc, page);
2503 tree = &BTRFS_I(page->mapping->host)->io_tree;
2504 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2507 static int btrfs_writepages(struct address_space *mapping,
2508 struct writeback_control *wbc)
2510 struct extent_io_tree *tree;
2511 tree = &BTRFS_I(mapping->host)->io_tree;
2512 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2516 btrfs_readpages(struct file *file, struct address_space *mapping,
2517 struct list_head *pages, unsigned nr_pages)
2519 struct extent_io_tree *tree;
2520 tree = &BTRFS_I(mapping->host)->io_tree;
2521 return extent_readpages(tree, mapping, pages, nr_pages,
2525 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2527 struct extent_io_tree *tree;
2528 struct extent_map_tree *map;
2531 tree = &BTRFS_I(page->mapping->host)->io_tree;
2532 map = &BTRFS_I(page->mapping->host)->extent_tree;
2533 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
2535 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
2536 ClearPagePrivate(page);
2537 set_page_private(page, 0);
2538 page_cache_release(page);
2543 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2545 struct extent_io_tree *tree;
2547 tree = &BTRFS_I(page->mapping->host)->io_tree;
2548 extent_invalidatepage(tree, page, offset);
2549 btrfs_releasepage(page, GFP_NOFS);
2550 if (PagePrivate(page)) {
2551 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
2552 ClearPagePrivate(page);
2553 set_page_private(page, 0);
2554 page_cache_release(page);
2559 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2560 * called from a page fault handler when a page is first dirtied. Hence we must
2561 * be careful to check for EOF conditions here. We set the page up correctly
2562 * for a written page which means we get ENOSPC checking when writing into
2563 * holes and correct delalloc and unwritten extent mapping on filesystems that
2564 * support these features.
2566 * We are not allowed to take the i_mutex here so we have to play games to
2567 * protect against truncate races as the page could now be beyond EOF. Because
2568 * vmtruncate() writes the inode size before removing pages, once we have the
2569 * page lock we can determine safely if the page is beyond EOF. If it is not
2570 * beyond EOF, then the page is guaranteed safe against truncation until we
2573 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2575 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2576 struct btrfs_root *root = BTRFS_I(inode)->root;
2582 mutex_lock(&root->fs_info->fs_mutex);
2583 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2584 mutex_unlock(&root->fs_info->fs_mutex);
2591 wait_on_page_writeback(page);
2592 size = i_size_read(inode);
2593 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2595 if ((page->mapping != inode->i_mapping) ||
2596 (page_start > size)) {
2597 /* page got truncated out from underneath us */
2601 /* page is wholly or partially inside EOF */
2602 if (page_start + PAGE_CACHE_SIZE > size)
2603 end = size & ~PAGE_CACHE_MASK;
2605 end = PAGE_CACHE_SIZE;
2607 ret = btrfs_cow_one_page(inode, page, end);
2615 static void btrfs_truncate(struct inode *inode)
2617 struct btrfs_root *root = BTRFS_I(inode)->root;
2619 struct btrfs_trans_handle *trans;
2622 if (!S_ISREG(inode->i_mode))
2624 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2627 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2629 mutex_lock(&root->fs_info->fs_mutex);
2630 trans = btrfs_start_transaction(root, 1);
2631 btrfs_set_trans_block_group(trans, inode);
2633 /* FIXME, add redo link to tree so we don't leak on crash */
2634 ret = btrfs_truncate_in_trans(trans, root, inode,
2635 BTRFS_EXTENT_DATA_KEY);
2636 btrfs_update_inode(trans, root, inode);
2637 nr = trans->blocks_used;
2639 ret = btrfs_end_transaction(trans, root);
2641 mutex_unlock(&root->fs_info->fs_mutex);
2642 btrfs_btree_balance_dirty(root, nr);
2643 btrfs_throttle(root);
2646 static int noinline create_subvol(struct btrfs_root *root, char *name,
2649 struct btrfs_trans_handle *trans;
2650 struct btrfs_key key;
2651 struct btrfs_root_item root_item;
2652 struct btrfs_inode_item *inode_item;
2653 struct extent_buffer *leaf;
2654 struct btrfs_root *new_root = root;
2655 struct inode *inode;
2660 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2661 unsigned long nr = 1;
2663 mutex_lock(&root->fs_info->fs_mutex);
2664 ret = btrfs_check_free_space(root, 1, 0);
2668 trans = btrfs_start_transaction(root, 1);
2671 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2676 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2677 objectid, trans->transid, 0, 0,
2680 return PTR_ERR(leaf);
2682 btrfs_set_header_nritems(leaf, 0);
2683 btrfs_set_header_level(leaf, 0);
2684 btrfs_set_header_bytenr(leaf, leaf->start);
2685 btrfs_set_header_generation(leaf, trans->transid);
2686 btrfs_set_header_owner(leaf, objectid);
2688 write_extent_buffer(leaf, root->fs_info->fsid,
2689 (unsigned long)btrfs_header_fsid(leaf),
2691 btrfs_mark_buffer_dirty(leaf);
2693 inode_item = &root_item.inode;
2694 memset(inode_item, 0, sizeof(*inode_item));
2695 inode_item->generation = cpu_to_le64(1);
2696 inode_item->size = cpu_to_le64(3);
2697 inode_item->nlink = cpu_to_le32(1);
2698 inode_item->nblocks = cpu_to_le64(1);
2699 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2701 btrfs_set_root_bytenr(&root_item, leaf->start);
2702 btrfs_set_root_level(&root_item, 0);
2703 btrfs_set_root_refs(&root_item, 1);
2704 btrfs_set_root_used(&root_item, 0);
2706 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2707 root_item.drop_level = 0;
2709 free_extent_buffer(leaf);
2712 btrfs_set_root_dirid(&root_item, new_dirid);
2714 key.objectid = objectid;
2716 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2717 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2723 * insert the directory item
2725 key.offset = (u64)-1;
2726 dir = root->fs_info->sb->s_root->d_inode;
2727 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2728 name, namelen, dir->i_ino, &key,
2733 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2734 name, namelen, objectid,
2735 root->fs_info->sb->s_root->d_inode->i_ino);
2739 ret = btrfs_commit_transaction(trans, root);
2743 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2746 trans = btrfs_start_transaction(new_root, 1);
2749 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2751 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2754 inode->i_op = &btrfs_dir_inode_operations;
2755 inode->i_fop = &btrfs_dir_file_operations;
2756 new_root->inode = inode;
2758 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2762 ret = btrfs_update_inode(trans, new_root, inode);
2766 nr = trans->blocks_used;
2767 err = btrfs_commit_transaction(trans, new_root);
2771 mutex_unlock(&root->fs_info->fs_mutex);
2772 btrfs_btree_balance_dirty(root, nr);
2773 btrfs_throttle(root);
2777 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2779 struct btrfs_pending_snapshot *pending_snapshot;
2780 struct btrfs_trans_handle *trans;
2783 unsigned long nr = 0;
2785 if (!root->ref_cows)
2788 mutex_lock(&root->fs_info->fs_mutex);
2789 ret = btrfs_check_free_space(root, 1, 0);
2793 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2794 if (!pending_snapshot) {
2798 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
2799 if (!pending_snapshot->name) {
2801 kfree(pending_snapshot);
2804 memcpy(pending_snapshot->name, name, namelen);
2805 pending_snapshot->name[namelen] = '\0';
2806 trans = btrfs_start_transaction(root, 1);
2808 pending_snapshot->root = root;
2809 list_add(&pending_snapshot->list,
2810 &trans->transaction->pending_snapshots);
2811 ret = btrfs_update_inode(trans, root, root->inode);
2812 err = btrfs_commit_transaction(trans, root);
2815 mutex_unlock(&root->fs_info->fs_mutex);
2816 btrfs_btree_balance_dirty(root, nr);
2817 btrfs_throttle(root);
2821 unsigned long btrfs_force_ra(struct address_space *mapping,
2822 struct file_ra_state *ra, struct file *file,
2823 pgoff_t offset, pgoff_t last_index)
2825 pgoff_t req_size = last_index - offset + 1;
2827 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2828 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2831 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2832 return offset + req_size;
2836 int btrfs_defrag_file(struct file *file) {
2837 struct inode *inode = fdentry(file)->d_inode;
2838 struct btrfs_root *root = BTRFS_I(inode)->root;
2839 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2841 unsigned long last_index;
2842 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
2843 unsigned long total_read = 0;
2849 mutex_lock(&root->fs_info->fs_mutex);
2850 ret = btrfs_check_free_space(root, inode->i_size, 0);
2851 mutex_unlock(&root->fs_info->fs_mutex);
2855 mutex_lock(&inode->i_mutex);
2856 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2857 for (i = 0; i <= last_index; i++) {
2858 if (total_read % ra_pages == 0) {
2859 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
2860 min(last_index, i + ra_pages - 1));
2863 page = grab_cache_page(inode->i_mapping, i);
2866 if (!PageUptodate(page)) {
2867 btrfs_readpage(NULL, page);
2869 if (!PageUptodate(page)) {
2871 page_cache_release(page);
2876 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2877 ClearPageDirty(page);
2879 cancel_dirty_page(page, PAGE_CACHE_SIZE);
2881 wait_on_page_writeback(page);
2882 set_page_extent_mapped(page);
2884 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2885 page_end = page_start + PAGE_CACHE_SIZE - 1;
2887 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2888 set_extent_delalloc(io_tree, page_start,
2889 page_end, GFP_NOFS);
2891 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2892 set_page_dirty(page);
2894 page_cache_release(page);
2895 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2899 mutex_unlock(&inode->i_mutex);
2903 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2908 struct btrfs_ioctl_vol_args *vol_args;
2909 struct btrfs_trans_handle *trans;
2910 struct btrfs_device *device = NULL;
2912 char *devstr = NULL;
2917 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2922 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2926 namelen = strlen(vol_args->name);
2927 if (namelen > BTRFS_VOL_NAME_MAX) {
2932 mutex_lock(&root->fs_info->fs_mutex);
2933 sizestr = vol_args->name;
2934 devstr = strchr(sizestr, ':');
2937 sizestr = devstr + 1;
2939 devstr = vol_args->name;
2940 devid = simple_strtoull(devstr, &end, 10);
2941 printk("resizing devid %Lu\n", devid);
2943 device = btrfs_find_device(root, devid, NULL);
2945 printk("resizer unable to find device %Lu\n", devid);
2949 if (!strcmp(sizestr, "max"))
2950 new_size = device->bdev->bd_inode->i_size;
2952 if (sizestr[0] == '-') {
2955 } else if (sizestr[0] == '+') {
2959 new_size = btrfs_parse_size(sizestr);
2960 if (new_size == 0) {
2966 old_size = device->total_bytes;
2969 if (new_size > old_size) {
2973 new_size = old_size - new_size;
2974 } else if (mod > 0) {
2975 new_size = old_size + new_size;
2978 if (new_size < 256 * 1024 * 1024) {
2982 if (new_size > device->bdev->bd_inode->i_size) {
2987 do_div(new_size, root->sectorsize);
2988 new_size *= root->sectorsize;
2990 printk("new size for %s is %llu\n", device->name, (unsigned long long)new_size);
2992 if (new_size > old_size) {
2993 trans = btrfs_start_transaction(root, 1);
2994 ret = btrfs_grow_device(trans, device, new_size);
2995 btrfs_commit_transaction(trans, root);
2997 ret = btrfs_shrink_device(device, new_size);
3001 mutex_unlock(&root->fs_info->fs_mutex);
3007 static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
3010 struct btrfs_ioctl_vol_args *vol_args;
3011 struct btrfs_dir_item *di;
3012 struct btrfs_path *path;
3017 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
3022 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
3027 namelen = strlen(vol_args->name);
3028 if (namelen > BTRFS_VOL_NAME_MAX) {
3032 if (strchr(vol_args->name, '/')) {
3037 path = btrfs_alloc_path();
3043 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
3044 mutex_lock(&root->fs_info->fs_mutex);
3045 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
3047 vol_args->name, namelen, 0);
3048 mutex_unlock(&root->fs_info->fs_mutex);
3049 btrfs_free_path(path);
3051 if (di && !IS_ERR(di)) {
3061 if (root == root->fs_info->tree_root)
3062 ret = create_subvol(root, vol_args->name, namelen);
3064 ret = create_snapshot(root, vol_args->name, namelen);
3070 static int btrfs_ioctl_defrag(struct file *file)
3072 struct inode *inode = fdentry(file)->d_inode;
3073 struct btrfs_root *root = BTRFS_I(inode)->root;
3075 switch (inode->i_mode & S_IFMT) {
3077 mutex_lock(&root->fs_info->fs_mutex);
3078 btrfs_defrag_root(root, 0);
3079 btrfs_defrag_root(root->fs_info->extent_root, 0);
3080 mutex_unlock(&root->fs_info->fs_mutex);
3083 btrfs_defrag_file(file);
3090 long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
3092 struct btrfs_ioctl_vol_args *vol_args;
3095 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
3100 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
3104 ret = btrfs_init_new_device(root, vol_args->name);
3111 long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
3113 struct btrfs_ioctl_vol_args *vol_args;
3116 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
3121 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
3125 ret = btrfs_rm_device(root, vol_args->name);
3132 int dup_item_to_inode(struct btrfs_trans_handle *trans,
3133 struct btrfs_root *root,
3134 struct btrfs_path *path,
3135 struct extent_buffer *leaf,
3137 struct btrfs_key *key,
3141 int len = btrfs_item_size_nr(leaf, slot);
3142 struct btrfs_key ckey = *key;
3145 dup = kmalloc(len, GFP_NOFS);
3149 read_extent_buffer(leaf, dup, btrfs_item_ptr_offset(leaf, slot), len);
3150 btrfs_release_path(root, path);
3152 ckey.objectid = destino;
3153 ret = btrfs_insert_item(trans, root, &ckey, dup, len);
3158 long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
3160 struct inode *inode = fdentry(file)->d_inode;
3161 struct btrfs_root *root = BTRFS_I(inode)->root;
3162 struct file *src_file;
3164 struct btrfs_trans_handle *trans;
3167 struct btrfs_path *path;
3168 struct btrfs_key key;
3169 struct extent_buffer *leaf;
3173 src_file = fget(src_fd);
3176 src = src_file->f_dentry->d_inode;
3179 if (src->i_sb != inode->i_sb)
3183 mutex_lock(&inode->i_mutex);
3184 mutex_lock(&src->i_mutex);
3186 mutex_lock(&src->i_mutex);
3187 mutex_lock(&inode->i_mutex);
3194 /* do any pending delalloc/csum calc on src, one way or
3195 another, and lock file content */
3197 filemap_write_and_wait(src->i_mapping);
3198 lock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
3199 if (BTRFS_I(src)->delalloc_bytes == 0)
3201 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
3204 mutex_lock(&root->fs_info->fs_mutex);
3205 trans = btrfs_start_transaction(root, 0);
3206 path = btrfs_alloc_path();
3212 key.type = BTRFS_EXTENT_DATA_KEY;
3213 key.objectid = src->i_ino;
3219 * note the key will change type as we walk through the
3222 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
3226 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3227 ret = btrfs_next_leaf(root, path);
3233 leaf = path->nodes[0];
3234 slot = path->slots[0];
3235 btrfs_item_key_to_cpu(leaf, &key, slot);
3236 nritems = btrfs_header_nritems(leaf);
3238 if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY ||
3239 key.objectid != src->i_ino)
3242 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
3243 struct btrfs_file_extent_item *extent;
3246 extent = btrfs_item_ptr(leaf, slot,
3247 struct btrfs_file_extent_item);
3248 found_type = btrfs_file_extent_type(leaf, extent);
3249 if (found_type == BTRFS_FILE_EXTENT_REG) {
3250 u64 len = btrfs_file_extent_num_bytes(leaf,
3252 u64 ds = btrfs_file_extent_disk_bytenr(leaf,
3254 u64 dl = btrfs_file_extent_disk_num_bytes(leaf,
3256 u64 off = btrfs_file_extent_offset(leaf,
3258 btrfs_insert_file_extent(trans, root,
3261 /* ds == 0 means there's a hole */
3263 btrfs_inc_extent_ref(trans, root,
3265 root->root_key.objectid,
3269 pos = key.offset + len;
3270 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
3271 ret = dup_item_to_inode(trans, root, path,
3276 pos = key.offset + btrfs_item_size_nr(leaf,
3279 } else if (btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
3280 ret = dup_item_to_inode(trans, root, path, leaf,
3281 slot, &key, inode->i_ino);
3287 btrfs_release_path(root, path);
3292 btrfs_free_path(path);
3294 inode->i_blocks = src->i_blocks;
3295 i_size_write(inode, src->i_size);
3296 btrfs_update_inode(trans, root, inode);
3298 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
3300 btrfs_end_transaction(trans, root);
3301 mutex_unlock(&root->fs_info->fs_mutex);
3304 mutex_unlock(&src->i_mutex);
3305 mutex_unlock(&inode->i_mutex);
3311 long btrfs_ioctl(struct file *file, unsigned int
3312 cmd, unsigned long arg)
3314 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3317 case BTRFS_IOC_SNAP_CREATE:
3318 return btrfs_ioctl_snap_create(root, (void __user *)arg);
3319 case BTRFS_IOC_DEFRAG:
3320 return btrfs_ioctl_defrag(file);
3321 case BTRFS_IOC_RESIZE:
3322 return btrfs_ioctl_resize(root, (void __user *)arg);
3323 case BTRFS_IOC_ADD_DEV:
3324 return btrfs_ioctl_add_dev(root, (void __user *)arg);
3325 case BTRFS_IOC_RM_DEV:
3326 return btrfs_ioctl_rm_dev(root, (void __user *)arg);
3327 case BTRFS_IOC_BALANCE:
3328 return btrfs_balance(root->fs_info->dev_root);
3329 case BTRFS_IOC_CLONE:
3330 return btrfs_ioctl_clone(file, arg);
3337 * Called inside transaction, so use GFP_NOFS
3339 struct inode *btrfs_alloc_inode(struct super_block *sb)
3341 struct btrfs_inode *ei;
3343 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3347 ei->ordered_trans = 0;
3348 return &ei->vfs_inode;
3351 void btrfs_destroy_inode(struct inode *inode)
3353 WARN_ON(!list_empty(&inode->i_dentry));
3354 WARN_ON(inode->i_data.nrpages);
3356 btrfs_drop_extent_cache(inode, 0, (u64)-1);
3357 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3360 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3361 static void init_once(struct kmem_cache * cachep, void *foo)
3363 static void init_once(void * foo, struct kmem_cache * cachep,
3364 unsigned long flags)
3367 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3369 inode_init_once(&ei->vfs_inode);
3372 void btrfs_destroy_cachep(void)
3374 if (btrfs_inode_cachep)
3375 kmem_cache_destroy(btrfs_inode_cachep);
3376 if (btrfs_trans_handle_cachep)
3377 kmem_cache_destroy(btrfs_trans_handle_cachep);
3378 if (btrfs_transaction_cachep)
3379 kmem_cache_destroy(btrfs_transaction_cachep);
3380 if (btrfs_bit_radix_cachep)
3381 kmem_cache_destroy(btrfs_bit_radix_cachep);
3382 if (btrfs_path_cachep)
3383 kmem_cache_destroy(btrfs_path_cachep);
3386 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
3387 unsigned long extra_flags,
3388 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3389 void (*ctor)(struct kmem_cache *, void *)
3391 void (*ctor)(void *, struct kmem_cache *,
3396 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3397 SLAB_MEM_SPREAD | extra_flags), ctor
3398 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3404 int btrfs_init_cachep(void)
3406 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
3407 sizeof(struct btrfs_inode),
3409 if (!btrfs_inode_cachep)
3411 btrfs_trans_handle_cachep =
3412 btrfs_cache_create("btrfs_trans_handle_cache",
3413 sizeof(struct btrfs_trans_handle),
3415 if (!btrfs_trans_handle_cachep)
3417 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
3418 sizeof(struct btrfs_transaction),
3420 if (!btrfs_transaction_cachep)
3422 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
3423 sizeof(struct btrfs_path),
3425 if (!btrfs_path_cachep)
3427 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
3428 SLAB_DESTROY_BY_RCU, NULL);
3429 if (!btrfs_bit_radix_cachep)
3433 btrfs_destroy_cachep();
3437 static int btrfs_getattr(struct vfsmount *mnt,
3438 struct dentry *dentry, struct kstat *stat)
3440 struct inode *inode = dentry->d_inode;
3441 generic_fillattr(inode, stat);
3442 stat->blksize = PAGE_CACHE_SIZE;
3443 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
3447 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3448 struct inode * new_dir,struct dentry *new_dentry)
3450 struct btrfs_trans_handle *trans;
3451 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3452 struct inode *new_inode = new_dentry->d_inode;
3453 struct inode *old_inode = old_dentry->d_inode;
3454 struct timespec ctime = CURRENT_TIME;
3455 struct btrfs_path *path;
3458 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3459 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3463 mutex_lock(&root->fs_info->fs_mutex);
3464 ret = btrfs_check_free_space(root, 1, 0);
3468 trans = btrfs_start_transaction(root, 1);
3470 btrfs_set_trans_block_group(trans, new_dir);
3471 path = btrfs_alloc_path();
3477 old_dentry->d_inode->i_nlink++;
3478 old_dir->i_ctime = old_dir->i_mtime = ctime;
3479 new_dir->i_ctime = new_dir->i_mtime = ctime;
3480 old_inode->i_ctime = ctime;
3482 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3487 new_inode->i_ctime = CURRENT_TIME;
3488 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3492 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
3497 btrfs_free_path(path);
3498 btrfs_end_transaction(trans, root);
3500 mutex_unlock(&root->fs_info->fs_mutex);
3504 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3505 const char *symname)
3507 struct btrfs_trans_handle *trans;
3508 struct btrfs_root *root = BTRFS_I(dir)->root;
3509 struct btrfs_path *path;
3510 struct btrfs_key key;
3511 struct inode *inode = NULL;
3518 struct btrfs_file_extent_item *ei;
3519 struct extent_buffer *leaf;
3520 unsigned long nr = 0;
3522 name_len = strlen(symname) + 1;
3523 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3524 return -ENAMETOOLONG;
3526 mutex_lock(&root->fs_info->fs_mutex);
3527 err = btrfs_check_free_space(root, 1, 0);
3531 trans = btrfs_start_transaction(root, 1);
3532 btrfs_set_trans_block_group(trans, dir);
3534 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3540 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3542 dentry->d_parent->d_inode->i_ino, objectid,
3543 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3544 err = PTR_ERR(inode);
3548 btrfs_set_trans_block_group(trans, inode);
3549 err = btrfs_add_nondir(trans, dentry, inode, 0);
3553 inode->i_mapping->a_ops = &btrfs_aops;
3554 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3555 inode->i_fop = &btrfs_file_operations;
3556 inode->i_op = &btrfs_file_inode_operations;
3557 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3558 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
3559 inode->i_mapping, GFP_NOFS);
3560 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3561 inode->i_mapping, GFP_NOFS);
3562 BTRFS_I(inode)->delalloc_bytes = 0;
3563 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
3564 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3566 dir->i_sb->s_dirt = 1;
3567 btrfs_update_inode_block_group(trans, inode);
3568 btrfs_update_inode_block_group(trans, dir);
3572 path = btrfs_alloc_path();
3574 key.objectid = inode->i_ino;
3576 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3577 datasize = btrfs_file_extent_calc_inline_size(name_len);
3578 err = btrfs_insert_empty_item(trans, root, path, &key,
3584 leaf = path->nodes[0];
3585 ei = btrfs_item_ptr(leaf, path->slots[0],
3586 struct btrfs_file_extent_item);
3587 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3588 btrfs_set_file_extent_type(leaf, ei,
3589 BTRFS_FILE_EXTENT_INLINE);
3590 ptr = btrfs_file_extent_inline_start(ei);
3591 write_extent_buffer(leaf, symname, ptr, name_len);
3592 btrfs_mark_buffer_dirty(leaf);
3593 btrfs_free_path(path);
3595 inode->i_op = &btrfs_symlink_inode_operations;
3596 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3597 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3598 inode->i_size = name_len - 1;
3599 err = btrfs_update_inode(trans, root, inode);
3604 nr = trans->blocks_used;
3605 btrfs_end_transaction(trans, root);
3607 mutex_unlock(&root->fs_info->fs_mutex);
3609 inode_dec_link_count(inode);
3612 btrfs_btree_balance_dirty(root, nr);
3613 btrfs_throttle(root);
3617 static int btrfs_permission(struct inode *inode, int mask,
3618 struct nameidata *nd)
3620 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3622 return generic_permission(inode, mask, NULL);
3625 static struct inode_operations btrfs_dir_inode_operations = {
3626 .lookup = btrfs_lookup,
3627 .create = btrfs_create,
3628 .unlink = btrfs_unlink,
3630 .mkdir = btrfs_mkdir,
3631 .rmdir = btrfs_rmdir,
3632 .rename = btrfs_rename,
3633 .symlink = btrfs_symlink,
3634 .setattr = btrfs_setattr,
3635 .mknod = btrfs_mknod,
3636 .setxattr = generic_setxattr,
3637 .getxattr = generic_getxattr,
3638 .listxattr = btrfs_listxattr,
3639 .removexattr = generic_removexattr,
3640 .permission = btrfs_permission,
3642 static struct inode_operations btrfs_dir_ro_inode_operations = {
3643 .lookup = btrfs_lookup,
3644 .permission = btrfs_permission,
3646 static struct file_operations btrfs_dir_file_operations = {
3647 .llseek = generic_file_llseek,
3648 .read = generic_read_dir,
3649 .readdir = btrfs_readdir,
3650 .unlocked_ioctl = btrfs_ioctl,
3651 #ifdef CONFIG_COMPAT
3652 .compat_ioctl = btrfs_ioctl,
3656 static struct extent_io_ops btrfs_extent_io_ops = {
3657 .fill_delalloc = run_delalloc_range,
3658 .submit_bio_hook = btrfs_submit_bio_hook,
3659 .merge_bio_hook = btrfs_merge_bio_hook,
3660 .readpage_io_hook = btrfs_readpage_io_hook,
3661 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3662 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
3663 .set_bit_hook = btrfs_set_bit_hook,
3664 .clear_bit_hook = btrfs_clear_bit_hook,
3667 static struct address_space_operations btrfs_aops = {
3668 .readpage = btrfs_readpage,
3669 .writepage = btrfs_writepage,
3670 .writepages = btrfs_writepages,
3671 .readpages = btrfs_readpages,
3672 .sync_page = block_sync_page,
3674 .direct_IO = btrfs_direct_IO,
3675 .invalidatepage = btrfs_invalidatepage,
3676 .releasepage = btrfs_releasepage,
3677 .set_page_dirty = __set_page_dirty_nobuffers,
3680 static struct address_space_operations btrfs_symlink_aops = {
3681 .readpage = btrfs_readpage,
3682 .writepage = btrfs_writepage,
3683 .invalidatepage = btrfs_invalidatepage,
3684 .releasepage = btrfs_releasepage,
3687 static struct inode_operations btrfs_file_inode_operations = {
3688 .truncate = btrfs_truncate,
3689 .getattr = btrfs_getattr,
3690 .setattr = btrfs_setattr,
3691 .setxattr = generic_setxattr,
3692 .getxattr = generic_getxattr,
3693 .listxattr = btrfs_listxattr,
3694 .removexattr = generic_removexattr,
3695 .permission = btrfs_permission,
3697 static struct inode_operations btrfs_special_inode_operations = {
3698 .getattr = btrfs_getattr,
3699 .setattr = btrfs_setattr,
3700 .permission = btrfs_permission,
3702 static struct inode_operations btrfs_symlink_inode_operations = {
3703 .readlink = generic_readlink,
3704 .follow_link = page_follow_link_light,
3705 .put_link = page_put_link,
3706 .permission = btrfs_permission,