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/bio.h>
20 #include <linux/buffer_head.h>
22 #include <linux/pagemap.h>
23 #include <linux/highmem.h>
24 #include <linux/time.h>
25 #include <linux/init.h>
26 #include <linux/string.h>
27 #include <linux/smp_lock.h>
28 #include <linux/backing-dev.h>
29 #include <linux/mpage.h>
30 #include <linux/swap.h>
31 #include <linux/writeback.h>
32 #include <linux/statfs.h>
33 #include <linux/compat.h>
34 #include <linux/bit_spinlock.h>
35 #include <linux/version.h>
36 #include <linux/xattr.h>
39 #include "transaction.h"
40 #include "btrfs_inode.h"
42 #include "print-tree.h"
44 struct btrfs_iget_args {
46 struct btrfs_root *root;
49 static struct inode_operations btrfs_dir_inode_operations;
50 static struct inode_operations btrfs_symlink_inode_operations;
51 static struct inode_operations btrfs_dir_ro_inode_operations;
52 static struct inode_operations btrfs_special_inode_operations;
53 static struct inode_operations btrfs_file_inode_operations;
54 static struct address_space_operations btrfs_aops;
55 static struct address_space_operations btrfs_symlink_aops;
56 static struct file_operations btrfs_dir_file_operations;
57 static struct extent_io_ops btrfs_extent_io_ops;
59 static struct kmem_cache *btrfs_inode_cachep;
60 struct kmem_cache *btrfs_trans_handle_cachep;
61 struct kmem_cache *btrfs_transaction_cachep;
62 struct kmem_cache *btrfs_bit_radix_cachep;
63 struct kmem_cache *btrfs_path_cachep;
66 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
67 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
68 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
69 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
70 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
71 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
72 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
73 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
76 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
79 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
80 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
91 spin_lock(&root->fs_info->delalloc_lock);
92 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
94 spin_unlock(&root->fs_info->delalloc_lock);
98 static int cow_file_range(struct inode *inode, u64 start, u64 end)
100 struct btrfs_root *root = BTRFS_I(inode)->root;
101 struct btrfs_trans_handle *trans;
105 u64 blocksize = root->sectorsize;
106 u64 orig_start = start;
108 struct btrfs_key ins;
111 trans = btrfs_start_transaction(root, 1);
113 btrfs_set_trans_block_group(trans, inode);
115 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
116 num_bytes = max(blocksize, num_bytes);
117 ret = btrfs_drop_extents(trans, root, inode,
118 start, start + num_bytes, start, &alloc_hint);
119 orig_num_bytes = num_bytes;
121 if (alloc_hint == EXTENT_MAP_INLINE)
124 while(num_bytes > 0) {
125 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
126 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
127 root->root_key.objectid,
129 inode->i_ino, start, 0,
130 alloc_hint, (u64)-1, &ins, 1);
135 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
136 start, ins.objectid, ins.offset,
138 inode->i_blocks += ins.offset >> 9;
139 btrfs_check_file(root, inode);
140 num_bytes -= cur_alloc_size;
141 alloc_hint = ins.objectid + ins.offset;
142 start += cur_alloc_size;
144 btrfs_drop_extent_cache(inode, orig_start,
145 orig_start + orig_num_bytes - 1);
146 btrfs_add_ordered_inode(inode);
147 btrfs_update_inode(trans, root, inode);
149 btrfs_end_transaction(trans, root);
153 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
161 struct btrfs_root *root = BTRFS_I(inode)->root;
162 struct extent_buffer *leaf;
164 struct btrfs_path *path;
165 struct btrfs_file_extent_item *item;
168 struct btrfs_key found_key;
170 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
171 path = btrfs_alloc_path();
174 ret = btrfs_lookup_file_extent(NULL, root, path,
175 inode->i_ino, start, 0);
177 btrfs_free_path(path);
183 if (path->slots[0] == 0)
188 leaf = path->nodes[0];
189 item = btrfs_item_ptr(leaf, path->slots[0],
190 struct btrfs_file_extent_item);
192 /* are we inside the extent that was found? */
193 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
194 found_type = btrfs_key_type(&found_key);
195 if (found_key.objectid != inode->i_ino ||
196 found_type != BTRFS_EXTENT_DATA_KEY) {
200 found_type = btrfs_file_extent_type(leaf, item);
201 extent_start = found_key.offset;
202 if (found_type == BTRFS_FILE_EXTENT_REG) {
203 u64 extent_num_bytes;
205 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
206 extent_end = extent_start + extent_num_bytes;
209 if (loops && start != extent_start)
212 if (start < extent_start || start >= extent_end)
215 cow_end = min(end, extent_end - 1);
216 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
221 * we may be called by the resizer, make sure we're inside
222 * the limits of the FS
224 if (bytenr + extent_num_bytes > total_fs_bytes)
227 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
237 btrfs_free_path(path);
240 btrfs_release_path(root, path);
245 cow_file_range(inode, start, cow_end);
250 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
252 struct btrfs_root *root = BTRFS_I(inode)->root;
254 mutex_lock(&root->fs_info->fs_mutex);
255 if (btrfs_test_opt(root, NODATACOW) ||
256 btrfs_test_flag(inode, NODATACOW))
257 ret = run_delalloc_nocow(inode, start, end);
259 ret = cow_file_range(inode, start, end);
261 mutex_unlock(&root->fs_info->fs_mutex);
265 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
266 unsigned long old, unsigned long bits)
268 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
269 struct btrfs_root *root = BTRFS_I(inode)->root;
270 spin_lock(&root->fs_info->delalloc_lock);
271 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
272 root->fs_info->delalloc_bytes += end - start + 1;
273 spin_unlock(&root->fs_info->delalloc_lock);
278 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
279 unsigned long old, unsigned long bits)
281 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
282 struct btrfs_root *root = BTRFS_I(inode)->root;
283 spin_lock(&root->fs_info->delalloc_lock);
284 if (end - start + 1 > root->fs_info->delalloc_bytes) {
285 printk("warning: delalloc account %Lu %Lu\n",
286 end - start + 1, root->fs_info->delalloc_bytes);
287 root->fs_info->delalloc_bytes = 0;
288 BTRFS_I(inode)->delalloc_bytes = 0;
290 root->fs_info->delalloc_bytes -= end - start + 1;
291 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
293 spin_unlock(&root->fs_info->delalloc_lock);
298 int btrfs_submit_bio_hook(int rw, struct bio *bio)
300 // struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
301 struct bio_vec *bvec = bio->bi_io_vec;
302 struct inode *inode = bvec->bv_page->mapping->host;
303 struct btrfs_root *root = BTRFS_I(inode)->root;
304 struct btrfs_trans_handle *trans;
310 if (btrfs_test_opt(root, NODATASUM) ||
311 btrfs_test_flag(inode, NODATASUM))
314 mutex_lock(&root->fs_info->fs_mutex);
315 trans = btrfs_start_transaction(root, 1);
316 btrfs_set_trans_block_group(trans, inode);
317 btrfs_csum_file_blocks(trans, root, inode, bio);
318 ret = btrfs_end_transaction(trans, root);
320 mutex_unlock(&root->fs_info->fs_mutex);
324 int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
326 struct inode *inode = page->mapping->host;
327 struct btrfs_root *root = BTRFS_I(inode)->root;
328 struct btrfs_trans_handle *trans;
331 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
332 size_t offset = start - page_start;
333 if (btrfs_test_opt(root, NODATASUM) ||
334 btrfs_test_flag(inode, NODATASUM))
336 mutex_lock(&root->fs_info->fs_mutex);
337 trans = btrfs_start_transaction(root, 1);
338 btrfs_set_trans_block_group(trans, inode);
340 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
341 start, kaddr + offset, end - start + 1);
343 ret = btrfs_end_transaction(trans, root);
345 mutex_unlock(&root->fs_info->fs_mutex);
349 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
352 struct inode *inode = page->mapping->host;
353 struct btrfs_root *root = BTRFS_I(inode)->root;
354 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
355 struct btrfs_csum_item *item;
356 struct btrfs_path *path = NULL;
358 if (btrfs_test_opt(root, NODATASUM) ||
359 btrfs_test_flag(inode, NODATASUM))
361 mutex_lock(&root->fs_info->fs_mutex);
362 path = btrfs_alloc_path();
363 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
366 /* a csum that isn't present is a preallocated region. */
367 if (ret == -ENOENT || ret == -EFBIG)
370 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
373 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
375 set_state_private(io_tree, start, csum);
378 btrfs_free_path(path);
379 mutex_unlock(&root->fs_info->fs_mutex);
383 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
384 struct extent_state *state)
386 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
387 struct inode *inode = page->mapping->host;
388 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
390 u64 private = ~(u32)0;
392 struct btrfs_root *root = BTRFS_I(inode)->root;
396 if (btrfs_test_opt(root, NODATASUM) ||
397 btrfs_test_flag(inode, NODATASUM))
399 if (state && state->start == start) {
400 private = state->private;
403 ret = get_state_private(io_tree, start, &private);
405 local_irq_save(flags);
406 kaddr = kmap_atomic(page, KM_IRQ0);
410 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
411 btrfs_csum_final(csum, (char *)&csum);
412 if (csum != private) {
415 kunmap_atomic(kaddr, KM_IRQ0);
416 local_irq_restore(flags);
420 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
421 page->mapping->host->i_ino, (unsigned long long)start, csum,
423 memset(kaddr + offset, 1, end - start + 1);
424 flush_dcache_page(page);
425 kunmap_atomic(kaddr, KM_IRQ0);
426 local_irq_restore(flags);
430 void btrfs_read_locked_inode(struct inode *inode)
432 struct btrfs_path *path;
433 struct extent_buffer *leaf;
434 struct btrfs_inode_item *inode_item;
435 struct btrfs_inode_timespec *tspec;
436 struct btrfs_root *root = BTRFS_I(inode)->root;
437 struct btrfs_key location;
438 u64 alloc_group_block;
442 path = btrfs_alloc_path();
444 mutex_lock(&root->fs_info->fs_mutex);
445 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
447 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
451 leaf = path->nodes[0];
452 inode_item = btrfs_item_ptr(leaf, path->slots[0],
453 struct btrfs_inode_item);
455 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
456 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
457 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
458 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
459 inode->i_size = btrfs_inode_size(leaf, inode_item);
461 tspec = btrfs_inode_atime(inode_item);
462 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
463 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
465 tspec = btrfs_inode_mtime(inode_item);
466 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
467 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
469 tspec = btrfs_inode_ctime(inode_item);
470 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
471 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
473 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
474 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
476 rdev = btrfs_inode_rdev(leaf, inode_item);
478 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
479 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
481 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
482 if (!BTRFS_I(inode)->block_group) {
483 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
486 btrfs_free_path(path);
489 mutex_unlock(&root->fs_info->fs_mutex);
491 switch (inode->i_mode & S_IFMT) {
493 inode->i_mapping->a_ops = &btrfs_aops;
494 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
495 inode->i_fop = &btrfs_file_operations;
496 inode->i_op = &btrfs_file_inode_operations;
499 inode->i_fop = &btrfs_dir_file_operations;
500 if (root == root->fs_info->tree_root)
501 inode->i_op = &btrfs_dir_ro_inode_operations;
503 inode->i_op = &btrfs_dir_inode_operations;
506 inode->i_op = &btrfs_symlink_inode_operations;
507 inode->i_mapping->a_ops = &btrfs_symlink_aops;
510 init_special_inode(inode, inode->i_mode, rdev);
516 btrfs_release_path(root, path);
517 btrfs_free_path(path);
518 mutex_unlock(&root->fs_info->fs_mutex);
519 make_bad_inode(inode);
522 static void fill_inode_item(struct extent_buffer *leaf,
523 struct btrfs_inode_item *item,
526 btrfs_set_inode_uid(leaf, item, inode->i_uid);
527 btrfs_set_inode_gid(leaf, item, inode->i_gid);
528 btrfs_set_inode_size(leaf, item, inode->i_size);
529 btrfs_set_inode_mode(leaf, item, inode->i_mode);
530 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
532 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
533 inode->i_atime.tv_sec);
534 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
535 inode->i_atime.tv_nsec);
537 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
538 inode->i_mtime.tv_sec);
539 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
540 inode->i_mtime.tv_nsec);
542 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
543 inode->i_ctime.tv_sec);
544 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
545 inode->i_ctime.tv_nsec);
547 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
548 btrfs_set_inode_generation(leaf, item, inode->i_generation);
549 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
550 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
551 btrfs_set_inode_block_group(leaf, item,
552 BTRFS_I(inode)->block_group->key.objectid);
555 int btrfs_update_inode(struct btrfs_trans_handle *trans,
556 struct btrfs_root *root,
559 struct btrfs_inode_item *inode_item;
560 struct btrfs_path *path;
561 struct extent_buffer *leaf;
564 path = btrfs_alloc_path();
566 ret = btrfs_lookup_inode(trans, root, path,
567 &BTRFS_I(inode)->location, 1);
574 leaf = path->nodes[0];
575 inode_item = btrfs_item_ptr(leaf, path->slots[0],
576 struct btrfs_inode_item);
578 fill_inode_item(leaf, inode_item, inode);
579 btrfs_mark_buffer_dirty(leaf);
580 btrfs_set_inode_last_trans(trans, inode);
583 btrfs_release_path(root, path);
584 btrfs_free_path(path);
589 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
590 struct btrfs_root *root,
592 struct dentry *dentry)
594 struct btrfs_path *path;
595 const char *name = dentry->d_name.name;
596 int name_len = dentry->d_name.len;
598 struct extent_buffer *leaf;
599 struct btrfs_dir_item *di;
600 struct btrfs_key key;
602 path = btrfs_alloc_path();
608 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
618 leaf = path->nodes[0];
619 btrfs_dir_item_key_to_cpu(leaf, di, &key);
620 ret = btrfs_delete_one_dir_name(trans, root, path, di);
623 btrfs_release_path(root, path);
625 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
626 key.objectid, name, name_len, -1);
635 ret = btrfs_delete_one_dir_name(trans, root, path, di);
637 dentry->d_inode->i_ctime = dir->i_ctime;
638 ret = btrfs_del_inode_ref(trans, root, name, name_len,
639 dentry->d_inode->i_ino,
640 dentry->d_parent->d_inode->i_ino);
642 printk("failed to delete reference to %.*s, "
643 "inode %lu parent %lu\n", name_len, name,
644 dentry->d_inode->i_ino,
645 dentry->d_parent->d_inode->i_ino);
648 btrfs_free_path(path);
650 dir->i_size -= name_len * 2;
651 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
652 btrfs_update_inode(trans, root, dir);
653 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
654 dentry->d_inode->i_nlink--;
656 drop_nlink(dentry->d_inode);
658 ret = btrfs_update_inode(trans, root, dentry->d_inode);
659 dir->i_sb->s_dirt = 1;
664 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
666 struct btrfs_root *root;
667 struct btrfs_trans_handle *trans;
668 struct inode *inode = dentry->d_inode;
670 unsigned long nr = 0;
672 root = BTRFS_I(dir)->root;
673 mutex_lock(&root->fs_info->fs_mutex);
675 ret = btrfs_check_free_space(root, 1, 1);
679 trans = btrfs_start_transaction(root, 1);
681 btrfs_set_trans_block_group(trans, dir);
682 ret = btrfs_unlink_trans(trans, root, dir, dentry);
683 nr = trans->blocks_used;
685 if (inode->i_nlink == 0) {
687 /* if the inode isn't linked anywhere,
688 * we don't need to worry about
691 found = btrfs_del_ordered_inode(inode);
693 atomic_dec(&inode->i_count);
697 btrfs_end_transaction(trans, root);
699 mutex_unlock(&root->fs_info->fs_mutex);
700 btrfs_btree_balance_dirty(root, nr);
701 btrfs_throttle(root);
705 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
707 struct inode *inode = dentry->d_inode;
710 struct btrfs_root *root = BTRFS_I(dir)->root;
711 struct btrfs_trans_handle *trans;
712 unsigned long nr = 0;
714 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
717 mutex_lock(&root->fs_info->fs_mutex);
718 ret = btrfs_check_free_space(root, 1, 1);
722 trans = btrfs_start_transaction(root, 1);
723 btrfs_set_trans_block_group(trans, dir);
725 /* now the directory is empty */
726 err = btrfs_unlink_trans(trans, root, dir, dentry);
731 nr = trans->blocks_used;
732 ret = btrfs_end_transaction(trans, root);
734 mutex_unlock(&root->fs_info->fs_mutex);
735 btrfs_btree_balance_dirty(root, nr);
736 btrfs_throttle(root);
744 * this can truncate away extent items, csum items and directory items.
745 * It starts at a high offset and removes keys until it can't find
746 * any higher than i_size.
748 * csum items that cross the new i_size are truncated to the new size
751 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
752 struct btrfs_root *root,
757 struct btrfs_path *path;
758 struct btrfs_key key;
759 struct btrfs_key found_key;
761 struct extent_buffer *leaf;
762 struct btrfs_file_extent_item *fi;
763 u64 extent_start = 0;
764 u64 extent_num_bytes = 0;
770 int pending_del_nr = 0;
771 int pending_del_slot = 0;
772 int extent_type = -1;
774 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
775 path = btrfs_alloc_path();
779 /* FIXME, add redo link to tree so we don't leak on crash */
780 key.objectid = inode->i_ino;
781 key.offset = (u64)-1;
784 btrfs_init_path(path);
786 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
791 BUG_ON(path->slots[0] == 0);
797 leaf = path->nodes[0];
798 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
799 found_type = btrfs_key_type(&found_key);
801 if (found_key.objectid != inode->i_ino)
804 if (found_type < min_type)
807 item_end = found_key.offset;
808 if (found_type == BTRFS_EXTENT_DATA_KEY) {
809 fi = btrfs_item_ptr(leaf, path->slots[0],
810 struct btrfs_file_extent_item);
811 extent_type = btrfs_file_extent_type(leaf, fi);
812 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
814 btrfs_file_extent_num_bytes(leaf, fi);
815 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
816 struct btrfs_item *item = btrfs_item_nr(leaf,
818 item_end += btrfs_file_extent_inline_len(leaf,
823 if (found_type == BTRFS_CSUM_ITEM_KEY) {
824 ret = btrfs_csum_truncate(trans, root, path,
828 if (item_end < inode->i_size) {
829 if (found_type == BTRFS_DIR_ITEM_KEY) {
830 found_type = BTRFS_INODE_ITEM_KEY;
831 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
832 found_type = BTRFS_CSUM_ITEM_KEY;
833 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
834 found_type = BTRFS_XATTR_ITEM_KEY;
835 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
836 found_type = BTRFS_INODE_REF_KEY;
837 } else if (found_type) {
842 btrfs_set_key_type(&key, found_type);
845 if (found_key.offset >= inode->i_size)
851 /* FIXME, shrink the extent if the ref count is only 1 */
852 if (found_type != BTRFS_EXTENT_DATA_KEY)
855 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
857 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
860 btrfs_file_extent_num_bytes(leaf, fi);
861 extent_num_bytes = inode->i_size -
862 found_key.offset + root->sectorsize - 1;
863 extent_num_bytes = extent_num_bytes &
864 ~((u64)root->sectorsize - 1);
865 btrfs_set_file_extent_num_bytes(leaf, fi,
867 num_dec = (orig_num_bytes -
869 if (extent_start != 0)
870 dec_i_blocks(inode, num_dec);
871 btrfs_mark_buffer_dirty(leaf);
874 btrfs_file_extent_disk_num_bytes(leaf,
876 /* FIXME blocksize != 4096 */
877 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
878 if (extent_start != 0) {
880 dec_i_blocks(inode, num_dec);
882 root_gen = btrfs_header_generation(leaf);
883 root_owner = btrfs_header_owner(leaf);
885 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
887 u32 newsize = inode->i_size - found_key.offset;
888 dec_i_blocks(inode, item_end + 1 -
889 found_key.offset - newsize);
891 btrfs_file_extent_calc_inline_size(newsize);
892 ret = btrfs_truncate_item(trans, root, path,
896 dec_i_blocks(inode, item_end + 1 -
902 if (!pending_del_nr) {
903 /* no pending yet, add ourselves */
904 pending_del_slot = path->slots[0];
906 } else if (pending_del_nr &&
907 path->slots[0] + 1 == pending_del_slot) {
908 /* hop on the pending chunk */
910 pending_del_slot = path->slots[0];
912 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
918 ret = btrfs_free_extent(trans, root, extent_start,
921 root_gen, inode->i_ino,
922 found_key.offset, 0);
926 if (path->slots[0] == 0) {
929 btrfs_release_path(root, path);
934 if (pending_del_nr &&
935 path->slots[0] + 1 != pending_del_slot) {
936 struct btrfs_key debug;
938 btrfs_item_key_to_cpu(path->nodes[0], &debug,
940 ret = btrfs_del_items(trans, root, path,
945 btrfs_release_path(root, path);
951 if (pending_del_nr) {
952 ret = btrfs_del_items(trans, root, path, pending_del_slot,
955 btrfs_release_path(root, path);
956 btrfs_free_path(path);
957 inode->i_sb->s_dirt = 1;
961 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
965 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
966 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
967 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
970 WARN_ON(!PageLocked(page));
971 set_page_extent_mapped(page);
973 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
974 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
977 if (zero_start != PAGE_CACHE_SIZE) {
979 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
980 flush_dcache_page(page);
983 set_page_dirty(page);
984 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
990 * taken from block_truncate_page, but does cow as it zeros out
991 * any bytes left in the last page in the file.
993 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
995 struct inode *inode = mapping->host;
996 struct btrfs_root *root = BTRFS_I(inode)->root;
997 u32 blocksize = root->sectorsize;
998 pgoff_t index = from >> PAGE_CACHE_SHIFT;
999 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1004 if ((offset & (blocksize - 1)) == 0)
1008 page = grab_cache_page(mapping, index);
1011 if (!PageUptodate(page)) {
1012 ret = btrfs_readpage(NULL, page);
1014 if (!PageUptodate(page)) {
1019 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1021 ret = btrfs_cow_one_page(inode, page, offset);
1024 page_cache_release(page);
1029 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1031 struct inode *inode = dentry->d_inode;
1034 err = inode_change_ok(inode, attr);
1038 if (S_ISREG(inode->i_mode) &&
1039 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1040 struct btrfs_trans_handle *trans;
1041 struct btrfs_root *root = BTRFS_I(inode)->root;
1042 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1044 u64 mask = root->sectorsize - 1;
1045 u64 hole_start = (inode->i_size + mask) & ~mask;
1046 u64 block_end = (attr->ia_size + mask) & ~mask;
1050 if (attr->ia_size <= hole_start)
1053 mutex_lock(&root->fs_info->fs_mutex);
1054 err = btrfs_check_free_space(root, 1, 0);
1055 mutex_unlock(&root->fs_info->fs_mutex);
1059 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1061 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1062 hole_size = block_end - hole_start;
1064 mutex_lock(&root->fs_info->fs_mutex);
1065 trans = btrfs_start_transaction(root, 1);
1066 btrfs_set_trans_block_group(trans, inode);
1067 err = btrfs_drop_extents(trans, root, inode,
1068 hole_start, block_end, hole_start,
1071 if (alloc_hint != EXTENT_MAP_INLINE) {
1072 err = btrfs_insert_file_extent(trans, root,
1076 btrfs_drop_extent_cache(inode, hole_start,
1078 btrfs_check_file(root, inode);
1080 btrfs_end_transaction(trans, root);
1081 mutex_unlock(&root->fs_info->fs_mutex);
1082 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1087 err = inode_setattr(inode, attr);
1092 void btrfs_put_inode(struct inode *inode)
1096 if (!BTRFS_I(inode)->ordered_trans) {
1100 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1101 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1104 ret = btrfs_del_ordered_inode(inode);
1106 atomic_dec(&inode->i_count);
1110 void btrfs_delete_inode(struct inode *inode)
1112 struct btrfs_trans_handle *trans;
1113 struct btrfs_root *root = BTRFS_I(inode)->root;
1117 truncate_inode_pages(&inode->i_data, 0);
1118 if (is_bad_inode(inode)) {
1123 mutex_lock(&root->fs_info->fs_mutex);
1124 trans = btrfs_start_transaction(root, 1);
1126 btrfs_set_trans_block_group(trans, inode);
1127 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1129 goto no_delete_lock;
1131 nr = trans->blocks_used;
1134 btrfs_end_transaction(trans, root);
1135 mutex_unlock(&root->fs_info->fs_mutex);
1136 btrfs_btree_balance_dirty(root, nr);
1137 btrfs_throttle(root);
1141 nr = trans->blocks_used;
1142 btrfs_end_transaction(trans, root);
1143 mutex_unlock(&root->fs_info->fs_mutex);
1144 btrfs_btree_balance_dirty(root, nr);
1145 btrfs_throttle(root);
1151 * this returns the key found in the dir entry in the location pointer.
1152 * If no dir entries were found, location->objectid is 0.
1154 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1155 struct btrfs_key *location)
1157 const char *name = dentry->d_name.name;
1158 int namelen = dentry->d_name.len;
1159 struct btrfs_dir_item *di;
1160 struct btrfs_path *path;
1161 struct btrfs_root *root = BTRFS_I(dir)->root;
1164 if (namelen == 1 && strcmp(name, ".") == 0) {
1165 location->objectid = dir->i_ino;
1166 location->type = BTRFS_INODE_ITEM_KEY;
1167 location->offset = 0;
1170 path = btrfs_alloc_path();
1173 if (namelen == 2 && strcmp(name, "..") == 0) {
1174 struct btrfs_key key;
1175 struct extent_buffer *leaf;
1179 key.objectid = dir->i_ino;
1180 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1182 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1186 leaf = path->nodes[0];
1187 slot = path->slots[0];
1188 nritems = btrfs_header_nritems(leaf);
1189 if (slot >= nritems)
1192 btrfs_item_key_to_cpu(leaf, &key, slot);
1193 if (key.objectid != dir->i_ino ||
1194 key.type != BTRFS_INODE_REF_KEY) {
1197 location->objectid = key.offset;
1198 location->type = BTRFS_INODE_ITEM_KEY;
1199 location->offset = 0;
1203 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1207 if (!di || IS_ERR(di)) {
1210 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1212 btrfs_free_path(path);
1215 location->objectid = 0;
1220 * when we hit a tree root in a directory, the btrfs part of the inode
1221 * needs to be changed to reflect the root directory of the tree root. This
1222 * is kind of like crossing a mount point.
1224 static int fixup_tree_root_location(struct btrfs_root *root,
1225 struct btrfs_key *location,
1226 struct btrfs_root **sub_root,
1227 struct dentry *dentry)
1229 struct btrfs_path *path;
1230 struct btrfs_root_item *ri;
1232 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1234 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1237 path = btrfs_alloc_path();
1239 mutex_lock(&root->fs_info->fs_mutex);
1241 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1242 dentry->d_name.name,
1243 dentry->d_name.len);
1244 if (IS_ERR(*sub_root))
1245 return PTR_ERR(*sub_root);
1247 ri = &(*sub_root)->root_item;
1248 location->objectid = btrfs_root_dirid(ri);
1249 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1250 location->offset = 0;
1252 btrfs_free_path(path);
1253 mutex_unlock(&root->fs_info->fs_mutex);
1257 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1259 struct btrfs_iget_args *args = p;
1260 inode->i_ino = args->ino;
1261 BTRFS_I(inode)->root = args->root;
1262 BTRFS_I(inode)->delalloc_bytes = 0;
1263 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1264 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1265 inode->i_mapping, GFP_NOFS);
1269 static int btrfs_find_actor(struct inode *inode, void *opaque)
1271 struct btrfs_iget_args *args = opaque;
1272 return (args->ino == inode->i_ino &&
1273 args->root == BTRFS_I(inode)->root);
1276 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1279 struct btrfs_iget_args args;
1280 args.ino = objectid;
1281 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1286 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1289 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1290 struct btrfs_root *root)
1292 struct inode *inode;
1293 struct btrfs_iget_args args;
1294 args.ino = objectid;
1297 inode = iget5_locked(s, objectid, btrfs_find_actor,
1298 btrfs_init_locked_inode,
1303 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1304 struct nameidata *nd)
1306 struct inode * inode;
1307 struct btrfs_inode *bi = BTRFS_I(dir);
1308 struct btrfs_root *root = bi->root;
1309 struct btrfs_root *sub_root = root;
1310 struct btrfs_key location;
1313 if (dentry->d_name.len > BTRFS_NAME_LEN)
1314 return ERR_PTR(-ENAMETOOLONG);
1316 mutex_lock(&root->fs_info->fs_mutex);
1317 ret = btrfs_inode_by_name(dir, dentry, &location);
1318 mutex_unlock(&root->fs_info->fs_mutex);
1321 return ERR_PTR(ret);
1324 if (location.objectid) {
1325 ret = fixup_tree_root_location(root, &location, &sub_root,
1328 return ERR_PTR(ret);
1330 return ERR_PTR(-ENOENT);
1331 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1334 return ERR_PTR(-EACCES);
1335 if (inode->i_state & I_NEW) {
1336 /* the inode and parent dir are two different roots */
1337 if (sub_root != root) {
1339 sub_root->inode = inode;
1341 BTRFS_I(inode)->root = sub_root;
1342 memcpy(&BTRFS_I(inode)->location, &location,
1344 btrfs_read_locked_inode(inode);
1345 unlock_new_inode(inode);
1348 return d_splice_alias(inode, dentry);
1351 static unsigned char btrfs_filetype_table[] = {
1352 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1355 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1357 struct inode *inode = filp->f_dentry->d_inode;
1358 struct btrfs_root *root = BTRFS_I(inode)->root;
1359 struct btrfs_item *item;
1360 struct btrfs_dir_item *di;
1361 struct btrfs_key key;
1362 struct btrfs_key found_key;
1363 struct btrfs_path *path;
1366 struct extent_buffer *leaf;
1369 unsigned char d_type;
1374 int key_type = BTRFS_DIR_INDEX_KEY;
1379 /* FIXME, use a real flag for deciding about the key type */
1380 if (root->fs_info->tree_root == root)
1381 key_type = BTRFS_DIR_ITEM_KEY;
1383 /* special case for "." */
1384 if (filp->f_pos == 0) {
1385 over = filldir(dirent, ".", 1,
1393 mutex_lock(&root->fs_info->fs_mutex);
1394 key.objectid = inode->i_ino;
1395 path = btrfs_alloc_path();
1398 /* special case for .., just use the back ref */
1399 if (filp->f_pos == 1) {
1400 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1402 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1404 leaf = path->nodes[0];
1405 slot = path->slots[0];
1406 nritems = btrfs_header_nritems(leaf);
1407 if (slot >= nritems) {
1408 btrfs_release_path(root, path);
1409 goto read_dir_items;
1411 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1412 btrfs_release_path(root, path);
1413 if (found_key.objectid != key.objectid ||
1414 found_key.type != BTRFS_INODE_REF_KEY)
1415 goto read_dir_items;
1416 over = filldir(dirent, "..", 2,
1417 2, found_key.offset, DT_DIR);
1424 btrfs_set_key_type(&key, key_type);
1425 key.offset = filp->f_pos;
1427 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1432 leaf = path->nodes[0];
1433 nritems = btrfs_header_nritems(leaf);
1434 slot = path->slots[0];
1435 if (advance || slot >= nritems) {
1436 if (slot >= nritems -1) {
1437 ret = btrfs_next_leaf(root, path);
1440 leaf = path->nodes[0];
1441 nritems = btrfs_header_nritems(leaf);
1442 slot = path->slots[0];
1449 item = btrfs_item_nr(leaf, slot);
1450 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1452 if (found_key.objectid != key.objectid)
1454 if (btrfs_key_type(&found_key) != key_type)
1456 if (found_key.offset < filp->f_pos)
1459 filp->f_pos = found_key.offset;
1461 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1463 di_total = btrfs_item_size(leaf, item);
1464 while(di_cur < di_total) {
1465 struct btrfs_key location;
1467 name_len = btrfs_dir_name_len(leaf, di);
1468 if (name_len < 32) {
1469 name_ptr = tmp_name;
1471 name_ptr = kmalloc(name_len, GFP_NOFS);
1474 read_extent_buffer(leaf, name_ptr,
1475 (unsigned long)(di + 1), name_len);
1477 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1478 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1479 over = filldir(dirent, name_ptr, name_len,
1484 if (name_ptr != tmp_name)
1489 di_len = btrfs_dir_name_len(leaf, di) +
1490 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1492 di = (struct btrfs_dir_item *)((char *)di + di_len);
1495 if (key_type == BTRFS_DIR_INDEX_KEY)
1496 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1502 btrfs_release_path(root, path);
1503 btrfs_free_path(path);
1504 mutex_unlock(&root->fs_info->fs_mutex);
1508 int btrfs_write_inode(struct inode *inode, int wait)
1510 struct btrfs_root *root = BTRFS_I(inode)->root;
1511 struct btrfs_trans_handle *trans;
1515 mutex_lock(&root->fs_info->fs_mutex);
1516 trans = btrfs_start_transaction(root, 1);
1517 btrfs_set_trans_block_group(trans, inode);
1518 ret = btrfs_commit_transaction(trans, root);
1519 mutex_unlock(&root->fs_info->fs_mutex);
1525 * This is somewhat expensive, updating the tree every time the
1526 * inode changes. But, it is most likely to find the inode in cache.
1527 * FIXME, needs more benchmarking...there are no reasons other than performance
1528 * to keep or drop this code.
1530 void btrfs_dirty_inode(struct inode *inode)
1532 struct btrfs_root *root = BTRFS_I(inode)->root;
1533 struct btrfs_trans_handle *trans;
1535 mutex_lock(&root->fs_info->fs_mutex);
1536 trans = btrfs_start_transaction(root, 1);
1537 btrfs_set_trans_block_group(trans, inode);
1538 btrfs_update_inode(trans, root, inode);
1539 btrfs_end_transaction(trans, root);
1540 mutex_unlock(&root->fs_info->fs_mutex);
1543 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1544 struct btrfs_root *root,
1545 const char *name, int name_len,
1548 struct btrfs_block_group_cache *group,
1551 struct inode *inode;
1552 struct btrfs_inode_item *inode_item;
1553 struct btrfs_key *location;
1554 struct btrfs_path *path;
1555 struct btrfs_inode_ref *ref;
1556 struct btrfs_key key[2];
1562 path = btrfs_alloc_path();
1565 inode = new_inode(root->fs_info->sb);
1567 return ERR_PTR(-ENOMEM);
1569 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1570 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1571 inode->i_mapping, GFP_NOFS);
1572 BTRFS_I(inode)->delalloc_bytes = 0;
1573 BTRFS_I(inode)->root = root;
1579 group = btrfs_find_block_group(root, group, 0, 0, owner);
1580 BTRFS_I(inode)->block_group = group;
1581 BTRFS_I(inode)->flags = 0;
1583 key[0].objectid = objectid;
1584 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1587 key[1].objectid = objectid;
1588 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1589 key[1].offset = ref_objectid;
1591 sizes[0] = sizeof(struct btrfs_inode_item);
1592 sizes[1] = name_len + sizeof(*ref);
1594 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1598 if (objectid > root->highest_inode)
1599 root->highest_inode = objectid;
1601 inode->i_uid = current->fsuid;
1602 inode->i_gid = current->fsgid;
1603 inode->i_mode = mode;
1604 inode->i_ino = objectid;
1605 inode->i_blocks = 0;
1606 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1607 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1608 struct btrfs_inode_item);
1609 fill_inode_item(path->nodes[0], inode_item, inode);
1611 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1612 struct btrfs_inode_ref);
1613 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1614 ptr = (unsigned long)(ref + 1);
1615 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1617 btrfs_mark_buffer_dirty(path->nodes[0]);
1618 btrfs_free_path(path);
1620 location = &BTRFS_I(inode)->location;
1621 location->objectid = objectid;
1622 location->offset = 0;
1623 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1625 insert_inode_hash(inode);
1628 btrfs_free_path(path);
1629 return ERR_PTR(ret);
1632 static inline u8 btrfs_inode_type(struct inode *inode)
1634 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1637 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1638 struct dentry *dentry, struct inode *inode,
1642 struct btrfs_key key;
1643 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1644 struct inode *parent_inode;
1646 key.objectid = inode->i_ino;
1647 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1650 ret = btrfs_insert_dir_item(trans, root,
1651 dentry->d_name.name, dentry->d_name.len,
1652 dentry->d_parent->d_inode->i_ino,
1653 &key, btrfs_inode_type(inode));
1656 ret = btrfs_insert_inode_ref(trans, root,
1657 dentry->d_name.name,
1660 dentry->d_parent->d_inode->i_ino);
1662 parent_inode = dentry->d_parent->d_inode;
1663 parent_inode->i_size += dentry->d_name.len * 2;
1664 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1665 ret = btrfs_update_inode(trans, root,
1666 dentry->d_parent->d_inode);
1671 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1672 struct dentry *dentry, struct inode *inode,
1675 int err = btrfs_add_link(trans, dentry, inode, backref);
1677 d_instantiate(dentry, inode);
1685 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1686 int mode, dev_t rdev)
1688 struct btrfs_trans_handle *trans;
1689 struct btrfs_root *root = BTRFS_I(dir)->root;
1690 struct inode *inode = NULL;
1694 unsigned long nr = 0;
1696 if (!new_valid_dev(rdev))
1699 mutex_lock(&root->fs_info->fs_mutex);
1700 err = btrfs_check_free_space(root, 1, 0);
1704 trans = btrfs_start_transaction(root, 1);
1705 btrfs_set_trans_block_group(trans, dir);
1707 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1713 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1715 dentry->d_parent->d_inode->i_ino, objectid,
1716 BTRFS_I(dir)->block_group, mode);
1717 err = PTR_ERR(inode);
1721 btrfs_set_trans_block_group(trans, inode);
1722 err = btrfs_add_nondir(trans, dentry, inode, 0);
1726 inode->i_op = &btrfs_special_inode_operations;
1727 init_special_inode(inode, inode->i_mode, rdev);
1728 btrfs_update_inode(trans, root, inode);
1730 dir->i_sb->s_dirt = 1;
1731 btrfs_update_inode_block_group(trans, inode);
1732 btrfs_update_inode_block_group(trans, dir);
1734 nr = trans->blocks_used;
1735 btrfs_end_transaction(trans, root);
1737 mutex_unlock(&root->fs_info->fs_mutex);
1740 inode_dec_link_count(inode);
1743 btrfs_btree_balance_dirty(root, nr);
1744 btrfs_throttle(root);
1748 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1749 int mode, struct nameidata *nd)
1751 struct btrfs_trans_handle *trans;
1752 struct btrfs_root *root = BTRFS_I(dir)->root;
1753 struct inode *inode = NULL;
1756 unsigned long nr = 0;
1759 mutex_lock(&root->fs_info->fs_mutex);
1760 err = btrfs_check_free_space(root, 1, 0);
1763 trans = btrfs_start_transaction(root, 1);
1764 btrfs_set_trans_block_group(trans, dir);
1766 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1772 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1774 dentry->d_parent->d_inode->i_ino,
1775 objectid, BTRFS_I(dir)->block_group, mode);
1776 err = PTR_ERR(inode);
1780 btrfs_set_trans_block_group(trans, inode);
1781 err = btrfs_add_nondir(trans, dentry, inode, 0);
1785 inode->i_mapping->a_ops = &btrfs_aops;
1786 inode->i_fop = &btrfs_file_operations;
1787 inode->i_op = &btrfs_file_inode_operations;
1788 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1789 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1790 inode->i_mapping, GFP_NOFS);
1791 BTRFS_I(inode)->delalloc_bytes = 0;
1792 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1794 dir->i_sb->s_dirt = 1;
1795 btrfs_update_inode_block_group(trans, inode);
1796 btrfs_update_inode_block_group(trans, dir);
1798 nr = trans->blocks_used;
1799 btrfs_end_transaction(trans, root);
1801 mutex_unlock(&root->fs_info->fs_mutex);
1804 inode_dec_link_count(inode);
1807 btrfs_btree_balance_dirty(root, nr);
1808 btrfs_throttle(root);
1812 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1813 struct dentry *dentry)
1815 struct btrfs_trans_handle *trans;
1816 struct btrfs_root *root = BTRFS_I(dir)->root;
1817 struct inode *inode = old_dentry->d_inode;
1818 unsigned long nr = 0;
1822 if (inode->i_nlink == 0)
1825 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1830 mutex_lock(&root->fs_info->fs_mutex);
1831 err = btrfs_check_free_space(root, 1, 0);
1834 trans = btrfs_start_transaction(root, 1);
1836 btrfs_set_trans_block_group(trans, dir);
1837 atomic_inc(&inode->i_count);
1838 err = btrfs_add_nondir(trans, dentry, inode, 1);
1843 dir->i_sb->s_dirt = 1;
1844 btrfs_update_inode_block_group(trans, dir);
1845 err = btrfs_update_inode(trans, root, inode);
1850 nr = trans->blocks_used;
1851 btrfs_end_transaction(trans, root);
1853 mutex_unlock(&root->fs_info->fs_mutex);
1856 inode_dec_link_count(inode);
1859 btrfs_btree_balance_dirty(root, nr);
1860 btrfs_throttle(root);
1864 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1866 struct inode *inode;
1867 struct btrfs_trans_handle *trans;
1868 struct btrfs_root *root = BTRFS_I(dir)->root;
1870 int drop_on_err = 0;
1872 unsigned long nr = 1;
1874 mutex_lock(&root->fs_info->fs_mutex);
1875 err = btrfs_check_free_space(root, 1, 0);
1879 trans = btrfs_start_transaction(root, 1);
1880 btrfs_set_trans_block_group(trans, dir);
1882 if (IS_ERR(trans)) {
1883 err = PTR_ERR(trans);
1887 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1893 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1895 dentry->d_parent->d_inode->i_ino, objectid,
1896 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1897 if (IS_ERR(inode)) {
1898 err = PTR_ERR(inode);
1903 inode->i_op = &btrfs_dir_inode_operations;
1904 inode->i_fop = &btrfs_dir_file_operations;
1905 btrfs_set_trans_block_group(trans, inode);
1908 err = btrfs_update_inode(trans, root, inode);
1912 err = btrfs_add_link(trans, dentry, inode, 0);
1916 d_instantiate(dentry, inode);
1918 dir->i_sb->s_dirt = 1;
1919 btrfs_update_inode_block_group(trans, inode);
1920 btrfs_update_inode_block_group(trans, dir);
1923 nr = trans->blocks_used;
1924 btrfs_end_transaction(trans, root);
1927 mutex_unlock(&root->fs_info->fs_mutex);
1930 btrfs_btree_balance_dirty(root, nr);
1931 btrfs_throttle(root);
1935 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1936 size_t pg_offset, u64 start, u64 len,
1942 u64 extent_start = 0;
1944 u64 objectid = inode->i_ino;
1946 struct btrfs_path *path;
1947 struct btrfs_root *root = BTRFS_I(inode)->root;
1948 struct btrfs_file_extent_item *item;
1949 struct extent_buffer *leaf;
1950 struct btrfs_key found_key;
1951 struct extent_map *em = NULL;
1952 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1953 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1954 struct btrfs_trans_handle *trans = NULL;
1956 path = btrfs_alloc_path();
1958 mutex_lock(&root->fs_info->fs_mutex);
1961 spin_lock(&em_tree->lock);
1962 em = lookup_extent_mapping(em_tree, start, len);
1963 spin_unlock(&em_tree->lock);
1966 if (em->start > start) {
1967 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
1968 start, len, em->start, em->len);
1971 if (em->block_start == EXTENT_MAP_INLINE && page)
1972 free_extent_map(em);
1976 em = alloc_extent_map(GFP_NOFS);
1982 em->start = EXTENT_MAP_HOLE;
1984 em->bdev = inode->i_sb->s_bdev;
1985 ret = btrfs_lookup_file_extent(trans, root, path,
1986 objectid, start, trans != NULL);
1993 if (path->slots[0] == 0)
1998 leaf = path->nodes[0];
1999 item = btrfs_item_ptr(leaf, path->slots[0],
2000 struct btrfs_file_extent_item);
2001 /* are we inside the extent that was found? */
2002 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2003 found_type = btrfs_key_type(&found_key);
2004 if (found_key.objectid != objectid ||
2005 found_type != BTRFS_EXTENT_DATA_KEY) {
2009 found_type = btrfs_file_extent_type(leaf, item);
2010 extent_start = found_key.offset;
2011 if (found_type == BTRFS_FILE_EXTENT_REG) {
2012 extent_end = extent_start +
2013 btrfs_file_extent_num_bytes(leaf, item);
2015 if (start < extent_start || start >= extent_end) {
2017 if (start < extent_start) {
2018 if (start + len <= extent_start)
2020 em->len = extent_end - extent_start;
2026 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2028 em->start = extent_start;
2029 em->len = extent_end - extent_start;
2030 em->block_start = EXTENT_MAP_HOLE;
2033 bytenr += btrfs_file_extent_offset(leaf, item);
2034 em->block_start = bytenr;
2035 em->start = extent_start;
2036 em->len = extent_end - extent_start;
2038 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2043 size_t extent_offset;
2046 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2048 extent_end = (extent_start + size + root->sectorsize - 1) &
2049 ~((u64)root->sectorsize - 1);
2050 if (start < extent_start || start >= extent_end) {
2052 if (start < extent_start) {
2053 if (start + len <= extent_start)
2055 em->len = extent_end - extent_start;
2061 em->block_start = EXTENT_MAP_INLINE;
2064 em->start = extent_start;
2069 page_start = page_offset(page) + pg_offset;
2070 extent_offset = page_start - extent_start;
2071 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2072 size - extent_offset);
2073 em->start = extent_start + extent_offset;
2074 em->len = (copy_size + root->sectorsize - 1) &
2075 ~((u64)root->sectorsize - 1);
2077 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2078 if (create == 0 && !PageUptodate(page)) {
2079 read_extent_buffer(leaf, map + pg_offset, ptr,
2081 flush_dcache_page(page);
2082 } else if (create && PageUptodate(page)) {
2085 free_extent_map(em);
2087 btrfs_release_path(root, path);
2088 trans = btrfs_start_transaction(root, 1);
2091 write_extent_buffer(leaf, map + pg_offset, ptr,
2093 btrfs_mark_buffer_dirty(leaf);
2096 set_extent_uptodate(io_tree, em->start,
2097 extent_map_end(em) - 1, GFP_NOFS);
2100 printk("unkknown found_type %d\n", found_type);
2107 em->block_start = EXTENT_MAP_HOLE;
2109 btrfs_release_path(root, path);
2110 if (em->start > start || extent_map_end(em) <= start) {
2111 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2117 spin_lock(&em_tree->lock);
2118 ret = add_extent_mapping(em_tree, em);
2119 if (ret == -EEXIST) {
2120 free_extent_map(em);
2121 em = lookup_extent_mapping(em_tree, start, len);
2124 printk("failing to insert %Lu %Lu\n", start, len);
2127 spin_unlock(&em_tree->lock);
2129 btrfs_free_path(path);
2131 ret = btrfs_end_transaction(trans, root);
2135 mutex_unlock(&root->fs_info->fs_mutex);
2137 free_extent_map(em);
2139 return ERR_PTR(err);
2144 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2146 return extent_bmap(mapping, iblock, btrfs_get_extent);
2149 int btrfs_readpage(struct file *file, struct page *page)
2151 struct extent_io_tree *tree;
2152 tree = &BTRFS_I(page->mapping->host)->io_tree;
2153 return extent_read_full_page(tree, page, btrfs_get_extent);
2156 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2158 struct extent_io_tree *tree;
2161 if (current->flags & PF_MEMALLOC) {
2162 redirty_page_for_writepage(wbc, page);
2166 tree = &BTRFS_I(page->mapping->host)->io_tree;
2167 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2170 static int btrfs_writepages(struct address_space *mapping,
2171 struct writeback_control *wbc)
2173 struct extent_io_tree *tree;
2174 tree = &BTRFS_I(mapping->host)->io_tree;
2175 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2179 btrfs_readpages(struct file *file, struct address_space *mapping,
2180 struct list_head *pages, unsigned nr_pages)
2182 struct extent_io_tree *tree;
2183 tree = &BTRFS_I(mapping->host)->io_tree;
2184 return extent_readpages(tree, mapping, pages, nr_pages,
2188 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2190 struct extent_io_tree *tree;
2191 struct extent_map_tree *map;
2194 tree = &BTRFS_I(page->mapping->host)->io_tree;
2195 map = &BTRFS_I(page->mapping->host)->extent_tree;
2196 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
2198 ClearPagePrivate(page);
2199 set_page_private(page, 0);
2200 page_cache_release(page);
2205 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2207 struct extent_io_tree *tree;
2209 tree = &BTRFS_I(page->mapping->host)->io_tree;
2210 extent_invalidatepage(tree, page, offset);
2211 btrfs_releasepage(page, GFP_NOFS);
2215 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2216 * called from a page fault handler when a page is first dirtied. Hence we must
2217 * be careful to check for EOF conditions here. We set the page up correctly
2218 * for a written page which means we get ENOSPC checking when writing into
2219 * holes and correct delalloc and unwritten extent mapping on filesystems that
2220 * support these features.
2222 * We are not allowed to take the i_mutex here so we have to play games to
2223 * protect against truncate races as the page could now be beyond EOF. Because
2224 * vmtruncate() writes the inode size before removing pages, once we have the
2225 * page lock we can determine safely if the page is beyond EOF. If it is not
2226 * beyond EOF, then the page is guaranteed safe against truncation until we
2229 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2231 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2232 struct btrfs_root *root = BTRFS_I(inode)->root;
2238 mutex_lock(&root->fs_info->fs_mutex);
2239 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2240 mutex_unlock(&root->fs_info->fs_mutex);
2247 wait_on_page_writeback(page);
2248 size = i_size_read(inode);
2249 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2251 if ((page->mapping != inode->i_mapping) ||
2252 (page_start > size)) {
2253 /* page got truncated out from underneath us */
2257 /* page is wholly or partially inside EOF */
2258 if (page_start + PAGE_CACHE_SIZE > size)
2259 end = size & ~PAGE_CACHE_MASK;
2261 end = PAGE_CACHE_SIZE;
2263 ret = btrfs_cow_one_page(inode, page, end);
2271 static void btrfs_truncate(struct inode *inode)
2273 struct btrfs_root *root = BTRFS_I(inode)->root;
2275 struct btrfs_trans_handle *trans;
2278 if (!S_ISREG(inode->i_mode))
2280 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2283 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2285 mutex_lock(&root->fs_info->fs_mutex);
2286 trans = btrfs_start_transaction(root, 1);
2287 btrfs_set_trans_block_group(trans, inode);
2289 /* FIXME, add redo link to tree so we don't leak on crash */
2290 ret = btrfs_truncate_in_trans(trans, root, inode,
2291 BTRFS_EXTENT_DATA_KEY);
2292 btrfs_update_inode(trans, root, inode);
2293 nr = trans->blocks_used;
2295 ret = btrfs_end_transaction(trans, root);
2297 mutex_unlock(&root->fs_info->fs_mutex);
2298 btrfs_btree_balance_dirty(root, nr);
2299 btrfs_throttle(root);
2302 static int noinline create_subvol(struct btrfs_root *root, char *name,
2305 struct btrfs_trans_handle *trans;
2306 struct btrfs_key key;
2307 struct btrfs_root_item root_item;
2308 struct btrfs_inode_item *inode_item;
2309 struct extent_buffer *leaf;
2310 struct btrfs_root *new_root = root;
2311 struct inode *inode;
2316 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2317 unsigned long nr = 1;
2319 mutex_lock(&root->fs_info->fs_mutex);
2320 ret = btrfs_check_free_space(root, 1, 0);
2324 trans = btrfs_start_transaction(root, 1);
2327 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2332 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2333 objectid, trans->transid, 0, 0,
2336 return PTR_ERR(leaf);
2338 btrfs_set_header_nritems(leaf, 0);
2339 btrfs_set_header_level(leaf, 0);
2340 btrfs_set_header_bytenr(leaf, leaf->start);
2341 btrfs_set_header_generation(leaf, trans->transid);
2342 btrfs_set_header_owner(leaf, objectid);
2344 write_extent_buffer(leaf, root->fs_info->fsid,
2345 (unsigned long)btrfs_header_fsid(leaf),
2347 btrfs_mark_buffer_dirty(leaf);
2349 inode_item = &root_item.inode;
2350 memset(inode_item, 0, sizeof(*inode_item));
2351 inode_item->generation = cpu_to_le64(1);
2352 inode_item->size = cpu_to_le64(3);
2353 inode_item->nlink = cpu_to_le32(1);
2354 inode_item->nblocks = cpu_to_le64(1);
2355 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2357 btrfs_set_root_bytenr(&root_item, leaf->start);
2358 btrfs_set_root_level(&root_item, 0);
2359 btrfs_set_root_refs(&root_item, 1);
2360 btrfs_set_root_used(&root_item, 0);
2362 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2363 root_item.drop_level = 0;
2365 free_extent_buffer(leaf);
2368 btrfs_set_root_dirid(&root_item, new_dirid);
2370 key.objectid = objectid;
2372 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2373 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2379 * insert the directory item
2381 key.offset = (u64)-1;
2382 dir = root->fs_info->sb->s_root->d_inode;
2383 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2384 name, namelen, dir->i_ino, &key,
2389 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2390 name, namelen, objectid,
2391 root->fs_info->sb->s_root->d_inode->i_ino);
2395 ret = btrfs_commit_transaction(trans, root);
2399 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2402 trans = btrfs_start_transaction(new_root, 1);
2405 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2407 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2410 inode->i_op = &btrfs_dir_inode_operations;
2411 inode->i_fop = &btrfs_dir_file_operations;
2412 new_root->inode = inode;
2414 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2418 ret = btrfs_update_inode(trans, new_root, inode);
2422 nr = trans->blocks_used;
2423 err = btrfs_commit_transaction(trans, new_root);
2427 mutex_unlock(&root->fs_info->fs_mutex);
2428 btrfs_btree_balance_dirty(root, nr);
2429 btrfs_throttle(root);
2433 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2435 struct btrfs_pending_snapshot *pending_snapshot;
2436 struct btrfs_trans_handle *trans;
2439 unsigned long nr = 0;
2441 if (!root->ref_cows)
2444 mutex_lock(&root->fs_info->fs_mutex);
2445 ret = btrfs_check_free_space(root, 1, 0);
2449 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2450 if (!pending_snapshot) {
2454 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
2455 if (!pending_snapshot->name) {
2457 kfree(pending_snapshot);
2460 memcpy(pending_snapshot->name, name, namelen);
2461 pending_snapshot->name[namelen] = '\0';
2462 trans = btrfs_start_transaction(root, 1);
2464 pending_snapshot->root = root;
2465 list_add(&pending_snapshot->list,
2466 &trans->transaction->pending_snapshots);
2467 ret = btrfs_update_inode(trans, root, root->inode);
2468 err = btrfs_commit_transaction(trans, root);
2471 mutex_unlock(&root->fs_info->fs_mutex);
2472 btrfs_btree_balance_dirty(root, nr);
2473 btrfs_throttle(root);
2477 unsigned long btrfs_force_ra(struct address_space *mapping,
2478 struct file_ra_state *ra, struct file *file,
2479 pgoff_t offset, pgoff_t last_index)
2483 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2484 req_size = last_index - offset + 1;
2485 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2488 req_size = min(last_index - offset + 1, (pgoff_t)128);
2489 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2490 return offset + req_size;
2494 int btrfs_defrag_file(struct file *file) {
2495 struct inode *inode = fdentry(file)->d_inode;
2496 struct btrfs_root *root = BTRFS_I(inode)->root;
2497 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2499 unsigned long last_index;
2500 unsigned long ra_index = 0;
2506 mutex_lock(&root->fs_info->fs_mutex);
2507 ret = btrfs_check_free_space(root, inode->i_size, 0);
2508 mutex_unlock(&root->fs_info->fs_mutex);
2512 mutex_lock(&inode->i_mutex);
2513 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2514 for (i = 0; i <= last_index; i++) {
2515 if (i == ra_index) {
2516 ra_index = btrfs_force_ra(inode->i_mapping,
2518 file, ra_index, last_index);
2520 page = grab_cache_page(inode->i_mapping, i);
2523 if (!PageUptodate(page)) {
2524 btrfs_readpage(NULL, page);
2526 if (!PageUptodate(page)) {
2528 page_cache_release(page);
2532 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2533 page_end = page_start + PAGE_CACHE_SIZE - 1;
2535 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2536 set_extent_delalloc(io_tree, page_start,
2537 page_end, GFP_NOFS);
2539 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2540 set_page_dirty(page);
2542 page_cache_release(page);
2543 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2547 mutex_unlock(&inode->i_mutex);
2551 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2555 struct btrfs_ioctl_vol_args *vol_args;
2556 struct btrfs_trans_handle *trans;
2562 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2567 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2571 namelen = strlen(vol_args->name);
2572 if (namelen > BTRFS_VOL_NAME_MAX) {
2577 sizestr = vol_args->name;
2578 if (!strcmp(sizestr, "max"))
2579 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2581 if (sizestr[0] == '-') {
2584 } else if (sizestr[0] == '+') {
2588 new_size = btrfs_parse_size(sizestr);
2589 if (new_size == 0) {
2595 mutex_lock(&root->fs_info->fs_mutex);
2596 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2599 if (new_size > old_size) {
2603 new_size = old_size - new_size;
2604 } else if (mod > 0) {
2605 new_size = old_size + new_size;
2608 if (new_size < 256 * 1024 * 1024) {
2612 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2617 do_div(new_size, root->sectorsize);
2618 new_size *= root->sectorsize;
2620 printk("new size is %Lu\n", new_size);
2621 if (new_size > old_size) {
2622 trans = btrfs_start_transaction(root, 1);
2623 ret = btrfs_grow_extent_tree(trans, root, new_size);
2624 btrfs_commit_transaction(trans, root);
2626 ret = btrfs_shrink_extent_tree(root, new_size);
2630 mutex_unlock(&root->fs_info->fs_mutex);
2636 static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2639 struct btrfs_ioctl_vol_args *vol_args;
2640 struct btrfs_dir_item *di;
2641 struct btrfs_path *path;
2646 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2651 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2656 namelen = strlen(vol_args->name);
2657 if (namelen > BTRFS_VOL_NAME_MAX) {
2661 if (strchr(vol_args->name, '/')) {
2666 path = btrfs_alloc_path();
2672 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2673 mutex_lock(&root->fs_info->fs_mutex);
2674 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2676 vol_args->name, namelen, 0);
2677 mutex_unlock(&root->fs_info->fs_mutex);
2678 btrfs_free_path(path);
2680 if (di && !IS_ERR(di)) {
2690 if (root == root->fs_info->tree_root)
2691 ret = create_subvol(root, vol_args->name, namelen);
2693 ret = create_snapshot(root, vol_args->name, namelen);
2699 static int btrfs_ioctl_defrag(struct file *file)
2701 struct inode *inode = fdentry(file)->d_inode;
2702 struct btrfs_root *root = BTRFS_I(inode)->root;
2704 switch (inode->i_mode & S_IFMT) {
2706 mutex_lock(&root->fs_info->fs_mutex);
2707 btrfs_defrag_root(root, 0);
2708 btrfs_defrag_root(root->fs_info->extent_root, 0);
2709 mutex_unlock(&root->fs_info->fs_mutex);
2712 btrfs_defrag_file(file);
2719 long btrfs_ioctl(struct file *file, unsigned int
2720 cmd, unsigned long arg)
2722 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
2725 case BTRFS_IOC_SNAP_CREATE:
2726 return btrfs_ioctl_snap_create(root, (void __user *)arg);
2727 case BTRFS_IOC_DEFRAG:
2728 return btrfs_ioctl_defrag(file);
2729 case BTRFS_IOC_RESIZE:
2730 return btrfs_ioctl_resize(root, (void __user *)arg);
2737 * Called inside transaction, so use GFP_NOFS
2739 struct inode *btrfs_alloc_inode(struct super_block *sb)
2741 struct btrfs_inode *ei;
2743 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2747 ei->ordered_trans = 0;
2748 return &ei->vfs_inode;
2751 void btrfs_destroy_inode(struct inode *inode)
2753 WARN_ON(!list_empty(&inode->i_dentry));
2754 WARN_ON(inode->i_data.nrpages);
2756 btrfs_drop_extent_cache(inode, 0, (u64)-1);
2757 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2760 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2761 static void init_once(struct kmem_cache * cachep, void *foo)
2763 static void init_once(void * foo, struct kmem_cache * cachep,
2764 unsigned long flags)
2767 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2769 inode_init_once(&ei->vfs_inode);
2772 void btrfs_destroy_cachep(void)
2774 if (btrfs_inode_cachep)
2775 kmem_cache_destroy(btrfs_inode_cachep);
2776 if (btrfs_trans_handle_cachep)
2777 kmem_cache_destroy(btrfs_trans_handle_cachep);
2778 if (btrfs_transaction_cachep)
2779 kmem_cache_destroy(btrfs_transaction_cachep);
2780 if (btrfs_bit_radix_cachep)
2781 kmem_cache_destroy(btrfs_bit_radix_cachep);
2782 if (btrfs_path_cachep)
2783 kmem_cache_destroy(btrfs_path_cachep);
2786 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
2787 unsigned long extra_flags,
2788 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2789 void (*ctor)(struct kmem_cache *, void *)
2791 void (*ctor)(void *, struct kmem_cache *,
2796 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2797 SLAB_MEM_SPREAD | extra_flags), ctor
2798 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2804 int btrfs_init_cachep(void)
2806 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
2807 sizeof(struct btrfs_inode),
2809 if (!btrfs_inode_cachep)
2811 btrfs_trans_handle_cachep =
2812 btrfs_cache_create("btrfs_trans_handle_cache",
2813 sizeof(struct btrfs_trans_handle),
2815 if (!btrfs_trans_handle_cachep)
2817 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
2818 sizeof(struct btrfs_transaction),
2820 if (!btrfs_transaction_cachep)
2822 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
2823 sizeof(struct btrfs_path),
2825 if (!btrfs_path_cachep)
2827 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
2828 SLAB_DESTROY_BY_RCU, NULL);
2829 if (!btrfs_bit_radix_cachep)
2833 btrfs_destroy_cachep();
2837 static int btrfs_getattr(struct vfsmount *mnt,
2838 struct dentry *dentry, struct kstat *stat)
2840 struct inode *inode = dentry->d_inode;
2841 generic_fillattr(inode, stat);
2842 stat->blksize = PAGE_CACHE_SIZE;
2843 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
2847 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2848 struct inode * new_dir,struct dentry *new_dentry)
2850 struct btrfs_trans_handle *trans;
2851 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2852 struct inode *new_inode = new_dentry->d_inode;
2853 struct inode *old_inode = old_dentry->d_inode;
2854 struct timespec ctime = CURRENT_TIME;
2855 struct btrfs_path *path;
2858 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2859 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2863 mutex_lock(&root->fs_info->fs_mutex);
2864 ret = btrfs_check_free_space(root, 1, 0);
2868 trans = btrfs_start_transaction(root, 1);
2870 btrfs_set_trans_block_group(trans, new_dir);
2871 path = btrfs_alloc_path();
2877 old_dentry->d_inode->i_nlink++;
2878 old_dir->i_ctime = old_dir->i_mtime = ctime;
2879 new_dir->i_ctime = new_dir->i_mtime = ctime;
2880 old_inode->i_ctime = ctime;
2882 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2887 new_inode->i_ctime = CURRENT_TIME;
2888 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2892 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
2897 btrfs_free_path(path);
2898 btrfs_end_transaction(trans, root);
2900 mutex_unlock(&root->fs_info->fs_mutex);
2904 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2905 const char *symname)
2907 struct btrfs_trans_handle *trans;
2908 struct btrfs_root *root = BTRFS_I(dir)->root;
2909 struct btrfs_path *path;
2910 struct btrfs_key key;
2911 struct inode *inode = NULL;
2918 struct btrfs_file_extent_item *ei;
2919 struct extent_buffer *leaf;
2920 unsigned long nr = 0;
2922 name_len = strlen(symname) + 1;
2923 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2924 return -ENAMETOOLONG;
2926 mutex_lock(&root->fs_info->fs_mutex);
2927 err = btrfs_check_free_space(root, 1, 0);
2931 trans = btrfs_start_transaction(root, 1);
2932 btrfs_set_trans_block_group(trans, dir);
2934 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2940 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2942 dentry->d_parent->d_inode->i_ino, objectid,
2943 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2944 err = PTR_ERR(inode);
2948 btrfs_set_trans_block_group(trans, inode);
2949 err = btrfs_add_nondir(trans, dentry, inode, 0);
2953 inode->i_mapping->a_ops = &btrfs_aops;
2954 inode->i_fop = &btrfs_file_operations;
2955 inode->i_op = &btrfs_file_inode_operations;
2956 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2957 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2958 inode->i_mapping, GFP_NOFS);
2959 BTRFS_I(inode)->delalloc_bytes = 0;
2960 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2962 dir->i_sb->s_dirt = 1;
2963 btrfs_update_inode_block_group(trans, inode);
2964 btrfs_update_inode_block_group(trans, dir);
2968 path = btrfs_alloc_path();
2970 key.objectid = inode->i_ino;
2972 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2973 datasize = btrfs_file_extent_calc_inline_size(name_len);
2974 err = btrfs_insert_empty_item(trans, root, path, &key,
2980 leaf = path->nodes[0];
2981 ei = btrfs_item_ptr(leaf, path->slots[0],
2982 struct btrfs_file_extent_item);
2983 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2984 btrfs_set_file_extent_type(leaf, ei,
2985 BTRFS_FILE_EXTENT_INLINE);
2986 ptr = btrfs_file_extent_inline_start(ei);
2987 write_extent_buffer(leaf, symname, ptr, name_len);
2988 btrfs_mark_buffer_dirty(leaf);
2989 btrfs_free_path(path);
2991 inode->i_op = &btrfs_symlink_inode_operations;
2992 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2993 inode->i_size = name_len - 1;
2994 err = btrfs_update_inode(trans, root, inode);
2999 nr = trans->blocks_used;
3000 btrfs_end_transaction(trans, root);
3002 mutex_unlock(&root->fs_info->fs_mutex);
3004 inode_dec_link_count(inode);
3007 btrfs_btree_balance_dirty(root, nr);
3008 btrfs_throttle(root);
3011 static int btrfs_permission(struct inode *inode, int mask,
3012 struct nameidata *nd)
3014 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3016 return generic_permission(inode, mask, NULL);
3019 static struct inode_operations btrfs_dir_inode_operations = {
3020 .lookup = btrfs_lookup,
3021 .create = btrfs_create,
3022 .unlink = btrfs_unlink,
3024 .mkdir = btrfs_mkdir,
3025 .rmdir = btrfs_rmdir,
3026 .rename = btrfs_rename,
3027 .symlink = btrfs_symlink,
3028 .setattr = btrfs_setattr,
3029 .mknod = btrfs_mknod,
3030 .setxattr = generic_setxattr,
3031 .getxattr = generic_getxattr,
3032 .listxattr = btrfs_listxattr,
3033 .removexattr = generic_removexattr,
3034 .permission = btrfs_permission,
3036 static struct inode_operations btrfs_dir_ro_inode_operations = {
3037 .lookup = btrfs_lookup,
3038 .permission = btrfs_permission,
3040 static struct file_operations btrfs_dir_file_operations = {
3041 .llseek = generic_file_llseek,
3042 .read = generic_read_dir,
3043 .readdir = btrfs_readdir,
3044 .unlocked_ioctl = btrfs_ioctl,
3045 #ifdef CONFIG_COMPAT
3046 .compat_ioctl = btrfs_ioctl,
3050 static struct extent_io_ops btrfs_extent_io_ops = {
3051 .fill_delalloc = run_delalloc_range,
3052 // .writepage_io_hook = btrfs_writepage_io_hook,
3053 .submit_bio_hook = btrfs_submit_bio_hook,
3054 .readpage_io_hook = btrfs_readpage_io_hook,
3055 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3056 .set_bit_hook = btrfs_set_bit_hook,
3057 .clear_bit_hook = btrfs_clear_bit_hook,
3060 static struct address_space_operations btrfs_aops = {
3061 .readpage = btrfs_readpage,
3062 .writepage = btrfs_writepage,
3063 .writepages = btrfs_writepages,
3064 .readpages = btrfs_readpages,
3065 .sync_page = block_sync_page,
3067 .invalidatepage = btrfs_invalidatepage,
3068 .releasepage = btrfs_releasepage,
3069 .set_page_dirty = __set_page_dirty_nobuffers,
3072 static struct address_space_operations btrfs_symlink_aops = {
3073 .readpage = btrfs_readpage,
3074 .writepage = btrfs_writepage,
3075 .invalidatepage = btrfs_invalidatepage,
3076 .releasepage = btrfs_releasepage,
3079 static struct inode_operations btrfs_file_inode_operations = {
3080 .truncate = btrfs_truncate,
3081 .getattr = btrfs_getattr,
3082 .setattr = btrfs_setattr,
3083 .setxattr = generic_setxattr,
3084 .getxattr = generic_getxattr,
3085 .listxattr = btrfs_listxattr,
3086 .removexattr = generic_removexattr,
3087 .permission = btrfs_permission,
3089 static struct inode_operations btrfs_special_inode_operations = {
3090 .getattr = btrfs_getattr,
3091 .setattr = btrfs_setattr,
3092 .permission = btrfs_permission,
3094 static struct inode_operations btrfs_symlink_inode_operations = {
3095 .readlink = generic_readlink,
3096 .follow_link = page_follow_link_light,
3097 .put_link = page_put_link,
3098 .permission = btrfs_permission,