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/buffer_head.h>
21 #include <linux/pagemap.h>
22 #include <linux/highmem.h>
23 #include <linux/time.h>
24 #include <linux/init.h>
25 #include <linux/string.h>
26 #include <linux/smp_lock.h>
27 #include <linux/backing-dev.h>
28 #include <linux/mpage.h>
29 #include <linux/swap.h>
30 #include <linux/writeback.h>
31 #include <linux/statfs.h>
32 #include <linux/compat.h>
33 #include <linux/bit_spinlock.h>
34 #include <linux/version.h>
37 #include "transaction.h"
38 #include "btrfs_inode.h"
40 #include "print-tree.h"
42 struct btrfs_iget_args {
44 struct btrfs_root *root;
47 static struct inode_operations btrfs_dir_inode_operations;
48 static struct inode_operations btrfs_symlink_inode_operations;
49 static struct inode_operations btrfs_dir_ro_inode_operations;
50 static struct inode_operations btrfs_special_inode_operations;
51 static struct inode_operations btrfs_file_inode_operations;
52 static struct address_space_operations btrfs_aops;
53 static struct address_space_operations btrfs_symlink_aops;
54 static struct file_operations btrfs_dir_file_operations;
55 static struct extent_map_ops btrfs_extent_map_ops;
57 static struct kmem_cache *btrfs_inode_cachep;
58 struct kmem_cache *btrfs_trans_handle_cachep;
59 struct kmem_cache *btrfs_transaction_cachep;
60 struct kmem_cache *btrfs_bit_radix_cachep;
61 struct kmem_cache *btrfs_path_cachep;
64 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
65 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
66 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
67 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
68 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
69 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
70 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
71 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
74 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
76 struct btrfs_root *root = BTRFS_I(inode)->root;
77 struct btrfs_trans_handle *trans;
82 u64 blocksize = 1 << inode->i_blkbits;
84 mutex_lock(&root->fs_info->fs_mutex);
85 trans = btrfs_start_transaction(root, 1);
86 btrfs_set_trans_block_group(trans, inode);
88 num_blocks = (end - start + blocksize) & ~(blocksize - 1);
89 ret = btrfs_drop_extents(trans, root, inode,
90 start, start + num_blocks, &alloc_hint);
91 num_blocks = num_blocks >> inode->i_blkbits;
92 ret = btrfs_alloc_extent(trans, root, inode->i_ino, num_blocks, 0,
93 alloc_hint, (u64)-1, &ins, 1);
98 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
99 start, ins.objectid, ins.offset,
102 btrfs_end_transaction(trans, root);
103 mutex_unlock(&root->fs_info->fs_mutex);
107 int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
109 struct inode *inode = page->mapping->host;
110 struct btrfs_root *root = BTRFS_I(inode)->root;
111 struct btrfs_trans_handle *trans;
114 u64 page_start = page->index << PAGE_CACHE_SHIFT;
115 size_t offset = start - page_start;
117 mutex_lock(&root->fs_info->fs_mutex);
118 trans = btrfs_start_transaction(root, 1);
119 btrfs_set_trans_block_group(trans, inode);
121 btrfs_csum_file_block(trans, root, inode->i_ino,
122 start, kaddr + offset, end - start + 1);
124 ret = btrfs_end_transaction(trans, root);
126 mutex_unlock(&root->fs_info->fs_mutex);
130 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
133 struct inode *inode = page->mapping->host;
134 struct btrfs_root *root = BTRFS_I(inode)->root;
135 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
136 struct btrfs_csum_item *item;
137 struct btrfs_path *path = NULL;
140 mutex_lock(&root->fs_info->fs_mutex);
141 path = btrfs_alloc_path();
142 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
145 /* a csum that isn't present is a preallocated region. */
146 if (ret == -ENOENT || ret == -EFBIG)
151 memcpy((char *)&private, &item->csum, BTRFS_CRC32_SIZE);
152 set_state_private(em_tree, start, private);
155 btrfs_free_path(path);
156 mutex_unlock(&root->fs_info->fs_mutex);
160 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
162 char csum[BTRFS_CRC32_SIZE];
163 size_t offset = start - (page->index << PAGE_CACHE_SHIFT);
164 struct inode *inode = page->mapping->host;
165 struct btrfs_root *root = BTRFS_I(inode)->root;
166 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
171 ret = get_state_private(em_tree, start, &private);
172 kaddr = kmap_atomic(page, KM_IRQ0);
176 ret = btrfs_csum_data(root, kaddr + offset, end - start + 1, csum);
178 if (memcmp(csum, &private, BTRFS_CRC32_SIZE)) {
181 kunmap_atomic(kaddr, KM_IRQ0);
185 printk("btrfs csum failed ino %lu off %llu\n",
186 page->mapping->host->i_ino, (unsigned long long)start);
187 memset(kaddr + offset, 1, end - start + 1); flush_dcache_page(page);
188 kunmap_atomic(kaddr, KM_IRQ0);
192 void btrfs_read_locked_inode(struct inode *inode)
194 struct btrfs_path *path;
195 struct btrfs_inode_item *inode_item;
196 struct btrfs_root *root = BTRFS_I(inode)->root;
197 struct btrfs_key location;
198 u64 alloc_group_block;
202 path = btrfs_alloc_path();
204 mutex_lock(&root->fs_info->fs_mutex);
206 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
207 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
211 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
213 struct btrfs_inode_item);
215 inode->i_mode = btrfs_inode_mode(inode_item);
216 inode->i_nlink = btrfs_inode_nlink(inode_item);
217 inode->i_uid = btrfs_inode_uid(inode_item);
218 inode->i_gid = btrfs_inode_gid(inode_item);
219 inode->i_size = btrfs_inode_size(inode_item);
220 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
221 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
222 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
223 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
224 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
225 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
226 inode->i_blocks = btrfs_inode_nblocks(inode_item);
227 inode->i_generation = btrfs_inode_generation(inode_item);
229 rdev = btrfs_inode_rdev(inode_item);
230 alloc_group_block = btrfs_inode_block_group(inode_item);
231 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
234 btrfs_free_path(path);
237 mutex_unlock(&root->fs_info->fs_mutex);
239 switch (inode->i_mode & S_IFMT) {
241 inode->i_mapping->a_ops = &btrfs_aops;
242 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
243 inode->i_fop = &btrfs_file_operations;
244 inode->i_op = &btrfs_file_inode_operations;
247 inode->i_fop = &btrfs_dir_file_operations;
248 if (root == root->fs_info->tree_root)
249 inode->i_op = &btrfs_dir_ro_inode_operations;
251 inode->i_op = &btrfs_dir_inode_operations;
254 inode->i_op = &btrfs_symlink_inode_operations;
255 inode->i_mapping->a_ops = &btrfs_symlink_aops;
258 init_special_inode(inode, inode->i_mode, rdev);
264 btrfs_release_path(root, path);
265 btrfs_free_path(path);
266 mutex_unlock(&root->fs_info->fs_mutex);
267 make_bad_inode(inode);
270 static void fill_inode_item(struct btrfs_inode_item *item,
273 btrfs_set_inode_uid(item, inode->i_uid);
274 btrfs_set_inode_gid(item, inode->i_gid);
275 btrfs_set_inode_size(item, inode->i_size);
276 btrfs_set_inode_mode(item, inode->i_mode);
277 btrfs_set_inode_nlink(item, inode->i_nlink);
278 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
279 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
280 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
281 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
282 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
283 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
284 btrfs_set_inode_nblocks(item, inode->i_blocks);
285 btrfs_set_inode_generation(item, inode->i_generation);
286 btrfs_set_inode_rdev(item, inode->i_rdev);
287 btrfs_set_inode_block_group(item,
288 BTRFS_I(inode)->block_group->key.objectid);
291 int btrfs_update_inode(struct btrfs_trans_handle *trans,
292 struct btrfs_root *root,
295 struct btrfs_inode_item *inode_item;
296 struct btrfs_path *path;
299 path = btrfs_alloc_path();
301 ret = btrfs_lookup_inode(trans, root, path,
302 &BTRFS_I(inode)->location, 1);
309 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
311 struct btrfs_inode_item);
313 fill_inode_item(inode_item, inode);
314 btrfs_mark_buffer_dirty(path->nodes[0]);
315 btrfs_set_inode_last_trans(trans, inode);
318 btrfs_release_path(root, path);
319 btrfs_free_path(path);
324 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
325 struct btrfs_root *root,
327 struct dentry *dentry)
329 struct btrfs_path *path;
330 const char *name = dentry->d_name.name;
331 int name_len = dentry->d_name.len;
334 struct btrfs_dir_item *di;
336 path = btrfs_alloc_path();
342 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
352 objectid = btrfs_disk_key_objectid(&di->location);
353 ret = btrfs_delete_one_dir_name(trans, root, path, di);
356 btrfs_release_path(root, path);
358 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
359 objectid, name, name_len, -1);
368 ret = btrfs_delete_one_dir_name(trans, root, path, di);
370 dentry->d_inode->i_ctime = dir->i_ctime;
372 btrfs_free_path(path);
374 dir->i_size -= name_len * 2;
375 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
376 btrfs_update_inode(trans, root, dir);
377 drop_nlink(dentry->d_inode);
378 ret = btrfs_update_inode(trans, root, dentry->d_inode);
379 dir->i_sb->s_dirt = 1;
384 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
386 struct btrfs_root *root;
387 struct btrfs_trans_handle *trans;
391 root = BTRFS_I(dir)->root;
392 mutex_lock(&root->fs_info->fs_mutex);
393 trans = btrfs_start_transaction(root, 1);
394 btrfs_set_trans_block_group(trans, dir);
395 ret = btrfs_unlink_trans(trans, root, dir, dentry);
396 nr = trans->blocks_used;
397 btrfs_end_transaction(trans, root);
398 mutex_unlock(&root->fs_info->fs_mutex);
399 btrfs_btree_balance_dirty(root, nr);
403 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
405 struct inode *inode = dentry->d_inode;
408 struct btrfs_root *root = BTRFS_I(dir)->root;
409 struct btrfs_path *path;
410 struct btrfs_key key;
411 struct btrfs_trans_handle *trans;
412 struct btrfs_key found_key;
414 struct btrfs_leaf *leaf;
415 char *goodnames = "..";
418 path = btrfs_alloc_path();
420 mutex_lock(&root->fs_info->fs_mutex);
421 trans = btrfs_start_transaction(root, 1);
422 btrfs_set_trans_block_group(trans, dir);
423 key.objectid = inode->i_ino;
424 key.offset = (u64)-1;
427 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
433 if (path->slots[0] == 0) {
438 leaf = btrfs_buffer_leaf(path->nodes[0]);
439 btrfs_disk_key_to_cpu(&found_key,
440 &leaf->items[path->slots[0]].key);
441 found_type = btrfs_key_type(&found_key);
442 if (found_key.objectid != inode->i_ino) {
446 if ((found_type != BTRFS_DIR_ITEM_KEY &&
447 found_type != BTRFS_DIR_INDEX_KEY) ||
448 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
449 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
453 ret = btrfs_del_item(trans, root, path);
456 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
458 btrfs_release_path(root, path);
461 btrfs_release_path(root, path);
463 /* now the directory is empty */
464 err = btrfs_unlink_trans(trans, root, dir, dentry);
469 btrfs_release_path(root, path);
470 btrfs_free_path(path);
471 mutex_unlock(&root->fs_info->fs_mutex);
472 nr = trans->blocks_used;
473 ret = btrfs_end_transaction(trans, root);
474 btrfs_btree_balance_dirty(root, nr);
480 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
481 struct btrfs_root *root,
484 struct btrfs_path *path;
489 path = btrfs_alloc_path();
491 ret = btrfs_lookup_inode(trans, root, path,
492 &BTRFS_I(inode)->location, -1);
496 ret = btrfs_del_item(trans, root, path);
497 btrfs_free_path(path);
502 * this can truncate away extent items, csum items and directory items.
503 * It starts at a high offset and removes keys until it can't find
504 * any higher than i_size.
506 * csum items that cross the new i_size are truncated to the new size
509 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
510 struct btrfs_root *root,
514 struct btrfs_path *path;
515 struct btrfs_key key;
516 struct btrfs_disk_key *found_key;
518 struct btrfs_leaf *leaf;
519 struct btrfs_file_extent_item *fi;
520 u64 extent_start = 0;
521 u64 extent_num_blocks = 0;
526 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
527 path = btrfs_alloc_path();
530 /* FIXME, add redo link to tree so we don't leak on crash */
531 key.objectid = inode->i_ino;
532 key.offset = (u64)-1;
535 btrfs_init_path(path);
537 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
542 BUG_ON(path->slots[0] == 0);
545 leaf = btrfs_buffer_leaf(path->nodes[0]);
546 found_key = &leaf->items[path->slots[0]].key;
547 found_type = btrfs_disk_key_type(found_key);
549 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
551 if (found_type != BTRFS_CSUM_ITEM_KEY &&
552 found_type != BTRFS_DIR_ITEM_KEY &&
553 found_type != BTRFS_DIR_INDEX_KEY &&
554 found_type != BTRFS_EXTENT_DATA_KEY)
557 item_end = btrfs_disk_key_offset(found_key);
558 if (found_type == BTRFS_EXTENT_DATA_KEY) {
559 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
561 struct btrfs_file_extent_item);
562 if (btrfs_file_extent_type(fi) !=
563 BTRFS_FILE_EXTENT_INLINE) {
564 item_end += btrfs_file_extent_num_blocks(fi) <<
568 if (found_type == BTRFS_CSUM_ITEM_KEY) {
569 ret = btrfs_csum_truncate(trans, root, path,
573 if (item_end < inode->i_size) {
574 if (found_type == BTRFS_DIR_ITEM_KEY) {
575 found_type = BTRFS_INODE_ITEM_KEY;
576 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
577 found_type = BTRFS_CSUM_ITEM_KEY;
578 } else if (found_type) {
583 btrfs_set_key_type(&key, found_type);
586 if (btrfs_disk_key_offset(found_key) >= inode->i_size)
592 /* FIXME, shrink the extent if the ref count is only 1 */
593 if (found_type == BTRFS_EXTENT_DATA_KEY &&
594 btrfs_file_extent_type(fi) !=
595 BTRFS_FILE_EXTENT_INLINE) {
597 extent_start = btrfs_file_extent_disk_blocknr(fi);
599 u64 orig_num_blocks =
600 btrfs_file_extent_num_blocks(fi);
601 extent_num_blocks = inode->i_size -
602 btrfs_disk_key_offset(found_key) +
604 extent_num_blocks >>= inode->i_blkbits;
605 btrfs_set_file_extent_num_blocks(fi,
607 num_dec = (orig_num_blocks -
608 extent_num_blocks) << 3;
609 if (extent_start != 0) {
610 inode->i_blocks -= num_dec;
612 btrfs_mark_buffer_dirty(path->nodes[0]);
615 btrfs_file_extent_disk_num_blocks(fi);
616 /* FIXME blocksize != 4096 */
617 num_dec = btrfs_file_extent_num_blocks(fi) << 3;
618 if (extent_start != 0) {
620 inode->i_blocks -= num_dec;
625 ret = btrfs_del_item(trans, root, path);
631 btrfs_release_path(root, path);
633 ret = btrfs_free_extent(trans, root, extent_start,
634 extent_num_blocks, 0);
640 btrfs_release_path(root, path);
641 btrfs_free_path(path);
642 inode->i_sb->s_dirt = 1;
646 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
651 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
652 u64 page_start = page->index << PAGE_CACHE_SHIFT;
653 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
655 if (!PagePrivate(page)) {
656 SetPagePrivate(page);
657 set_page_private(page, 1);
658 WARN_ON(!page->mapping->a_ops->invalidatepage);
659 page_cache_get(page);
662 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
663 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
665 if (zero_start != PAGE_CACHE_SIZE) {
667 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
668 flush_dcache_page(page);
671 set_page_dirty(page);
672 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
678 * taken from block_truncate_page, but does cow as it zeros out
679 * any bytes left in the last page in the file.
681 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
683 struct inode *inode = mapping->host;
684 unsigned blocksize = 1 << inode->i_blkbits;
685 pgoff_t index = from >> PAGE_CACHE_SHIFT;
686 unsigned offset = from & (PAGE_CACHE_SIZE-1);
691 if ((offset & (blocksize - 1)) == 0)
694 down_read(&BTRFS_I(inode)->root->snap_sem);
696 page = grab_cache_page(mapping, index);
699 if (!PageUptodate(page)) {
700 ret = btrfs_readpage(NULL, page);
702 if (!PageUptodate(page)) {
707 page_start = page->index << PAGE_CACHE_SHIFT;
709 ret = btrfs_cow_one_page(inode, page, offset);
712 page_cache_release(page);
713 up_read(&BTRFS_I(inode)->root->snap_sem);
718 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
720 struct inode *inode = dentry->d_inode;
723 err = inode_change_ok(inode, attr);
727 if (S_ISREG(inode->i_mode) &&
728 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
729 struct btrfs_trans_handle *trans;
730 struct btrfs_root *root = BTRFS_I(inode)->root;
731 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
733 u64 mask = root->blocksize - 1;
734 u64 pos = (inode->i_size + mask) & ~mask;
735 u64 block_end = attr->ia_size | mask;
739 if (attr->ia_size <= pos)
742 btrfs_truncate_page(inode->i_mapping, inode->i_size);
744 lock_extent(em_tree, pos, block_end, GFP_NOFS);
745 hole_size = (attr->ia_size - pos + mask) & ~mask;
747 mutex_lock(&root->fs_info->fs_mutex);
748 trans = btrfs_start_transaction(root, 1);
749 btrfs_set_trans_block_group(trans, inode);
750 err = btrfs_drop_extents(trans, root, inode,
751 pos, pos + hole_size, &alloc_hint);
753 hole_size >>= inode->i_blkbits;
755 err = btrfs_insert_file_extent(trans, root, inode->i_ino,
756 pos, 0, 0, hole_size);
757 btrfs_end_transaction(trans, root);
758 mutex_unlock(&root->fs_info->fs_mutex);
759 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
764 err = inode_setattr(inode, attr);
768 void btrfs_delete_inode(struct inode *inode)
770 struct btrfs_trans_handle *trans;
771 struct btrfs_root *root = BTRFS_I(inode)->root;
775 truncate_inode_pages(&inode->i_data, 0);
776 if (is_bad_inode(inode)) {
780 mutex_lock(&root->fs_info->fs_mutex);
781 trans = btrfs_start_transaction(root, 1);
782 btrfs_set_trans_block_group(trans, inode);
783 ret = btrfs_truncate_in_trans(trans, root, inode);
786 ret = btrfs_free_inode(trans, root, inode);
789 nr = trans->blocks_used;
790 btrfs_end_transaction(trans, root);
791 mutex_unlock(&root->fs_info->fs_mutex);
792 btrfs_btree_balance_dirty(root, nr);
796 nr = trans->blocks_used;
797 btrfs_end_transaction(trans, root);
798 mutex_unlock(&root->fs_info->fs_mutex);
799 btrfs_btree_balance_dirty(root, nr);
805 * this returns the key found in the dir entry in the location pointer.
806 * If no dir entries were found, location->objectid is 0.
808 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
809 struct btrfs_key *location)
811 const char *name = dentry->d_name.name;
812 int namelen = dentry->d_name.len;
813 struct btrfs_dir_item *di;
814 struct btrfs_path *path;
815 struct btrfs_root *root = BTRFS_I(dir)->root;
818 path = btrfs_alloc_path();
820 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
822 if (!di || IS_ERR(di)) {
823 location->objectid = 0;
827 btrfs_disk_key_to_cpu(location, &di->location);
829 btrfs_release_path(root, path);
830 btrfs_free_path(path);
835 * when we hit a tree root in a directory, the btrfs part of the inode
836 * needs to be changed to reflect the root directory of the tree root. This
837 * is kind of like crossing a mount point.
839 static int fixup_tree_root_location(struct btrfs_root *root,
840 struct btrfs_key *location,
841 struct btrfs_root **sub_root,
842 struct dentry *dentry)
844 struct btrfs_path *path;
845 struct btrfs_root_item *ri;
847 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
849 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
852 path = btrfs_alloc_path();
854 mutex_lock(&root->fs_info->fs_mutex);
856 *sub_root = btrfs_read_fs_root(root->fs_info, location,
859 if (IS_ERR(*sub_root))
860 return PTR_ERR(*sub_root);
862 ri = &(*sub_root)->root_item;
863 location->objectid = btrfs_root_dirid(ri);
865 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
866 location->offset = 0;
868 btrfs_free_path(path);
869 mutex_unlock(&root->fs_info->fs_mutex);
873 static int btrfs_init_locked_inode(struct inode *inode, void *p)
875 struct btrfs_iget_args *args = p;
876 inode->i_ino = args->ino;
877 BTRFS_I(inode)->root = args->root;
878 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
879 inode->i_mapping, GFP_NOFS);
883 static int btrfs_find_actor(struct inode *inode, void *opaque)
885 struct btrfs_iget_args *args = opaque;
886 return (args->ino == inode->i_ino &&
887 args->root == BTRFS_I(inode)->root);
890 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
891 struct btrfs_root *root)
894 struct btrfs_iget_args args;
898 inode = iget5_locked(s, objectid, btrfs_find_actor,
899 btrfs_init_locked_inode,
904 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
905 struct nameidata *nd)
907 struct inode * inode;
908 struct btrfs_inode *bi = BTRFS_I(dir);
909 struct btrfs_root *root = bi->root;
910 struct btrfs_root *sub_root = root;
911 struct btrfs_key location;
914 if (dentry->d_name.len > BTRFS_NAME_LEN)
915 return ERR_PTR(-ENAMETOOLONG);
916 mutex_lock(&root->fs_info->fs_mutex);
917 ret = btrfs_inode_by_name(dir, dentry, &location);
918 mutex_unlock(&root->fs_info->fs_mutex);
922 if (location.objectid) {
923 ret = fixup_tree_root_location(root, &location, &sub_root,
928 return ERR_PTR(-ENOENT);
929 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
932 return ERR_PTR(-EACCES);
933 if (inode->i_state & I_NEW) {
934 /* the inode and parent dir are two different roots */
935 if (sub_root != root) {
937 sub_root->inode = inode;
939 BTRFS_I(inode)->root = sub_root;
940 memcpy(&BTRFS_I(inode)->location, &location,
942 btrfs_read_locked_inode(inode);
943 unlock_new_inode(inode);
946 return d_splice_alias(inode, dentry);
949 static unsigned char btrfs_filetype_table[] = {
950 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
953 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
955 struct inode *inode = filp->f_path.dentry->d_inode;
956 struct btrfs_root *root = BTRFS_I(inode)->root;
957 struct btrfs_item *item;
958 struct btrfs_dir_item *di;
959 struct btrfs_key key;
960 struct btrfs_path *path;
963 struct btrfs_leaf *leaf;
966 unsigned char d_type;
971 int key_type = BTRFS_DIR_INDEX_KEY;
973 /* FIXME, use a real flag for deciding about the key type */
974 if (root->fs_info->tree_root == root)
975 key_type = BTRFS_DIR_ITEM_KEY;
976 mutex_lock(&root->fs_info->fs_mutex);
977 key.objectid = inode->i_ino;
979 btrfs_set_key_type(&key, key_type);
980 key.offset = filp->f_pos;
981 path = btrfs_alloc_path();
983 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
988 leaf = btrfs_buffer_leaf(path->nodes[0]);
989 nritems = btrfs_header_nritems(&leaf->header);
990 slot = path->slots[0];
991 if (advance || slot >= nritems) {
992 if (slot >= nritems -1) {
993 ret = btrfs_next_leaf(root, path);
996 leaf = btrfs_buffer_leaf(path->nodes[0]);
997 nritems = btrfs_header_nritems(&leaf->header);
998 slot = path->slots[0];
1005 item = leaf->items + slot;
1006 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
1008 if (btrfs_disk_key_type(&item->key) != key_type)
1010 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
1012 filp->f_pos = btrfs_disk_key_offset(&item->key);
1014 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1016 di_total = btrfs_item_size(leaf->items + slot);
1017 while(di_cur < di_total) {
1018 d_type = btrfs_filetype_table[btrfs_dir_type(di)];
1019 over = filldir(dirent, (const char *)(di + 1),
1020 btrfs_dir_name_len(di),
1021 btrfs_disk_key_offset(&item->key),
1022 btrfs_disk_key_objectid(&di->location),
1026 di_len = btrfs_dir_name_len(di) + sizeof(*di);
1028 di = (struct btrfs_dir_item *)((char *)di + di_len);
1035 btrfs_release_path(root, path);
1036 btrfs_free_path(path);
1037 mutex_unlock(&root->fs_info->fs_mutex);
1041 int btrfs_write_inode(struct inode *inode, int wait)
1043 struct btrfs_root *root = BTRFS_I(inode)->root;
1044 struct btrfs_trans_handle *trans;
1048 mutex_lock(&root->fs_info->fs_mutex);
1049 trans = btrfs_start_transaction(root, 1);
1050 btrfs_set_trans_block_group(trans, inode);
1051 ret = btrfs_commit_transaction(trans, root);
1052 mutex_unlock(&root->fs_info->fs_mutex);
1058 * This is somewhat expensive, updating the tree every time the
1059 * inode changes. But, it is most likely to find the inode in cache.
1060 * FIXME, needs more benchmarking...there are no reasons other than performance
1061 * to keep or drop this code.
1063 void btrfs_dirty_inode(struct inode *inode)
1065 struct btrfs_root *root = BTRFS_I(inode)->root;
1066 struct btrfs_trans_handle *trans;
1068 mutex_lock(&root->fs_info->fs_mutex);
1069 trans = btrfs_start_transaction(root, 1);
1070 btrfs_set_trans_block_group(trans, inode);
1071 btrfs_update_inode(trans, root, inode);
1072 btrfs_end_transaction(trans, root);
1073 mutex_unlock(&root->fs_info->fs_mutex);
1076 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1077 struct btrfs_root *root,
1079 struct btrfs_block_group_cache *group,
1082 struct inode *inode;
1083 struct btrfs_inode_item inode_item;
1084 struct btrfs_key *location;
1088 inode = new_inode(root->fs_info->sb);
1090 return ERR_PTR(-ENOMEM);
1092 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1093 inode->i_mapping, GFP_NOFS);
1094 BTRFS_I(inode)->root = root;
1100 group = btrfs_find_block_group(root, group, 0, 0, owner);
1101 BTRFS_I(inode)->block_group = group;
1103 inode->i_uid = current->fsuid;
1104 inode->i_gid = current->fsgid;
1105 inode->i_mode = mode;
1106 inode->i_ino = objectid;
1107 inode->i_blocks = 0;
1108 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1109 fill_inode_item(&inode_item, inode);
1110 location = &BTRFS_I(inode)->location;
1111 location->objectid = objectid;
1112 location->flags = 0;
1113 location->offset = 0;
1114 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1116 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
1118 return ERR_PTR(ret);
1119 insert_inode_hash(inode);
1123 static inline u8 btrfs_inode_type(struct inode *inode)
1125 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1128 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1129 struct dentry *dentry, struct inode *inode)
1132 struct btrfs_key key;
1133 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1134 struct inode *parent_inode;
1135 key.objectid = inode->i_ino;
1137 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1140 ret = btrfs_insert_dir_item(trans, root,
1141 dentry->d_name.name, dentry->d_name.len,
1142 dentry->d_parent->d_inode->i_ino,
1143 &key, btrfs_inode_type(inode));
1145 parent_inode = dentry->d_parent->d_inode;
1146 parent_inode->i_size += dentry->d_name.len * 2;
1147 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1148 ret = btrfs_update_inode(trans, root,
1149 dentry->d_parent->d_inode);
1154 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1155 struct dentry *dentry, struct inode *inode)
1157 int err = btrfs_add_link(trans, dentry, inode);
1159 d_instantiate(dentry, inode);
1167 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1168 int mode, dev_t rdev)
1170 struct btrfs_trans_handle *trans;
1171 struct btrfs_root *root = BTRFS_I(dir)->root;
1172 struct inode *inode;
1178 if (!new_valid_dev(rdev))
1181 mutex_lock(&root->fs_info->fs_mutex);
1182 trans = btrfs_start_transaction(root, 1);
1183 btrfs_set_trans_block_group(trans, dir);
1185 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1191 inode = btrfs_new_inode(trans, root, objectid,
1192 BTRFS_I(dir)->block_group, mode);
1193 err = PTR_ERR(inode);
1197 btrfs_set_trans_block_group(trans, inode);
1198 err = btrfs_add_nondir(trans, dentry, inode);
1202 inode->i_op = &btrfs_special_inode_operations;
1203 init_special_inode(inode, inode->i_mode, rdev);
1204 btrfs_update_inode(trans, root, inode);
1206 dir->i_sb->s_dirt = 1;
1207 btrfs_update_inode_block_group(trans, inode);
1208 btrfs_update_inode_block_group(trans, dir);
1210 nr = trans->blocks_used;
1211 btrfs_end_transaction(trans, root);
1212 mutex_unlock(&root->fs_info->fs_mutex);
1215 inode_dec_link_count(inode);
1218 btrfs_btree_balance_dirty(root, nr);
1222 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1223 int mode, struct nameidata *nd)
1225 struct btrfs_trans_handle *trans;
1226 struct btrfs_root *root = BTRFS_I(dir)->root;
1227 struct inode *inode;
1233 mutex_lock(&root->fs_info->fs_mutex);
1234 trans = btrfs_start_transaction(root, 1);
1235 btrfs_set_trans_block_group(trans, dir);
1237 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1243 inode = btrfs_new_inode(trans, root, objectid,
1244 BTRFS_I(dir)->block_group, mode);
1245 err = PTR_ERR(inode);
1249 btrfs_set_trans_block_group(trans, inode);
1250 err = btrfs_add_nondir(trans, dentry, inode);
1254 inode->i_mapping->a_ops = &btrfs_aops;
1255 inode->i_fop = &btrfs_file_operations;
1256 inode->i_op = &btrfs_file_inode_operations;
1257 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1258 inode->i_mapping, GFP_NOFS);
1259 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
1261 dir->i_sb->s_dirt = 1;
1262 btrfs_update_inode_block_group(trans, inode);
1263 btrfs_update_inode_block_group(trans, dir);
1265 nr = trans->blocks_used;
1266 btrfs_end_transaction(trans, root);
1267 mutex_unlock(&root->fs_info->fs_mutex);
1270 inode_dec_link_count(inode);
1273 btrfs_btree_balance_dirty(root, nr);
1277 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1278 struct dentry *dentry)
1280 struct btrfs_trans_handle *trans;
1281 struct btrfs_root *root = BTRFS_I(dir)->root;
1282 struct inode *inode = old_dentry->d_inode;
1287 if (inode->i_nlink == 0)
1291 mutex_lock(&root->fs_info->fs_mutex);
1292 trans = btrfs_start_transaction(root, 1);
1293 btrfs_set_trans_block_group(trans, dir);
1294 atomic_inc(&inode->i_count);
1295 err = btrfs_add_nondir(trans, dentry, inode);
1298 dir->i_sb->s_dirt = 1;
1299 btrfs_update_inode_block_group(trans, dir);
1300 err = btrfs_update_inode(trans, root, inode);
1304 nr = trans->blocks_used;
1305 btrfs_end_transaction(trans, root);
1306 mutex_unlock(&root->fs_info->fs_mutex);
1309 inode_dec_link_count(inode);
1312 btrfs_btree_balance_dirty(root, nr);
1316 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
1317 struct btrfs_root *root,
1318 u64 objectid, u64 dirid)
1322 struct btrfs_key key;
1327 key.objectid = objectid;
1330 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1332 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
1333 &key, BTRFS_FT_DIR);
1336 key.objectid = dirid;
1337 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
1338 &key, BTRFS_FT_DIR);
1345 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1347 struct inode *inode;
1348 struct btrfs_trans_handle *trans;
1349 struct btrfs_root *root = BTRFS_I(dir)->root;
1351 int drop_on_err = 0;
1353 unsigned long nr = 1;
1355 mutex_lock(&root->fs_info->fs_mutex);
1356 trans = btrfs_start_transaction(root, 1);
1357 btrfs_set_trans_block_group(trans, dir);
1358 if (IS_ERR(trans)) {
1359 err = PTR_ERR(trans);
1363 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1369 inode = btrfs_new_inode(trans, root, objectid,
1370 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1371 if (IS_ERR(inode)) {
1372 err = PTR_ERR(inode);
1376 inode->i_op = &btrfs_dir_inode_operations;
1377 inode->i_fop = &btrfs_dir_file_operations;
1378 btrfs_set_trans_block_group(trans, inode);
1380 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1385 err = btrfs_update_inode(trans, root, inode);
1388 err = btrfs_add_link(trans, dentry, inode);
1391 d_instantiate(dentry, inode);
1393 dir->i_sb->s_dirt = 1;
1394 btrfs_update_inode_block_group(trans, inode);
1395 btrfs_update_inode_block_group(trans, dir);
1398 nr = trans->blocks_used;
1399 btrfs_end_transaction(trans, root);
1401 mutex_unlock(&root->fs_info->fs_mutex);
1404 btrfs_btree_balance_dirty(root, nr);
1408 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1409 size_t page_offset, u64 start, u64 end,
1415 u64 extent_start = 0;
1417 u64 objectid = inode->i_ino;
1419 int failed_insert = 0;
1420 struct btrfs_path *path;
1421 struct btrfs_root *root = BTRFS_I(inode)->root;
1422 struct btrfs_file_extent_item *item;
1423 struct btrfs_leaf *leaf;
1424 struct btrfs_disk_key *found_key;
1425 struct extent_map *em = NULL;
1426 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1427 struct btrfs_trans_handle *trans = NULL;
1429 path = btrfs_alloc_path();
1431 mutex_lock(&root->fs_info->fs_mutex);
1434 em = lookup_extent_mapping(em_tree, start, end);
1439 em = alloc_extent_map(GFP_NOFS);
1447 em->bdev = inode->i_sb->s_bdev;
1448 ret = btrfs_lookup_file_extent(NULL, root, path,
1449 objectid, start, 0);
1456 if (path->slots[0] == 0)
1461 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
1462 struct btrfs_file_extent_item);
1463 leaf = btrfs_buffer_leaf(path->nodes[0]);
1464 blocknr = btrfs_file_extent_disk_blocknr(item);
1465 blocknr += btrfs_file_extent_offset(item);
1467 /* are we inside the extent that was found? */
1468 found_key = &leaf->items[path->slots[0]].key;
1469 found_type = btrfs_disk_key_type(found_key);
1470 if (btrfs_disk_key_objectid(found_key) != objectid ||
1471 found_type != BTRFS_EXTENT_DATA_KEY) {
1475 found_type = btrfs_file_extent_type(item);
1476 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
1477 if (found_type == BTRFS_FILE_EXTENT_REG) {
1478 extent_end = extent_start +
1479 (btrfs_file_extent_num_blocks(item) << inode->i_blkbits);
1481 if (start < extent_start || start >= extent_end) {
1483 if (start < extent_start) {
1484 if (end < extent_start)
1486 em->end = extent_end - 1;
1492 if (btrfs_file_extent_disk_blocknr(item) == 0) {
1493 em->start = extent_start;
1494 em->end = extent_end - 1;
1495 em->block_start = 0;
1499 em->block_start = blocknr << inode->i_blkbits;
1500 em->block_end = em->block_start +
1501 (btrfs_file_extent_num_blocks(item) <<
1502 inode->i_blkbits) - 1;
1503 em->start = extent_start;
1504 em->end = extent_end - 1;
1506 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1511 size = btrfs_file_extent_inline_len(leaf->items +
1513 extent_end = extent_start + size;
1514 if (start < extent_start || start >= extent_end) {
1516 if (start < extent_start) {
1517 if (end < extent_start)
1519 em->end = extent_end - 1;
1525 em->block_start = EXTENT_MAP_INLINE;
1526 em->block_end = EXTENT_MAP_INLINE;
1527 em->start = extent_start;
1528 em->end = extent_end - 1;
1532 ptr = btrfs_file_extent_inline_start(item);
1534 memcpy(map + page_offset, ptr, size);
1535 flush_dcache_page(result->b_page);
1537 set_extent_uptodate(em_tree, extent_start,
1538 extent_end - 1, GFP_NOFS);
1541 printk("unkknown found_type %d\n", found_type);
1548 em->block_start = 0;
1551 btrfs_release_path(root, path);
1552 if (em->start > start || em->end < start) {
1553 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
1557 ret = add_extent_mapping(em_tree, em);
1558 if (ret == -EEXIST) {
1559 free_extent_map(em);
1562 if (failed_insert > 5) {
1563 printk("failing to insert %Lu %Lu\n", start, end);
1571 btrfs_free_path(path);
1573 ret = btrfs_end_transaction(trans, root);
1577 mutex_unlock(&root->fs_info->fs_mutex);
1579 free_extent_map(em);
1581 return ERR_PTR(err);
1586 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
1588 return extent_bmap(mapping, iblock, btrfs_get_extent);
1591 static int btrfs_prepare_write(struct file *file, struct page *page,
1592 unsigned from, unsigned to)
1594 return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree,
1595 page->mapping->host, page, from, to,
1599 int btrfs_readpage(struct file *file, struct page *page)
1601 struct extent_map_tree *tree;
1602 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1603 return extent_read_full_page(tree, page, btrfs_get_extent);
1605 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1607 struct extent_map_tree *tree;
1610 if (current->flags & PF_MEMALLOC) {
1611 redirty_page_for_writepage(wbc, page);
1615 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1616 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
1619 static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
1621 struct extent_map_tree *tree;
1624 if (page->private != 1) {
1626 return try_to_free_buffers(page);
1628 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1629 ret = try_release_extent_mapping(tree, page);
1631 ClearPagePrivate(page);
1632 set_page_private(page, 0);
1633 page_cache_release(page);
1638 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
1640 struct extent_map_tree *tree;
1642 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1643 extent_invalidatepage(tree, page, offset);
1644 btrfs_releasepage(page, GFP_NOFS);
1648 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1649 * called from a page fault handler when a page is first dirtied. Hence we must
1650 * be careful to check for EOF conditions here. We set the page up correctly
1651 * for a written page which means we get ENOSPC checking when writing into
1652 * holes and correct delalloc and unwritten extent mapping on filesystems that
1653 * support these features.
1655 * We are not allowed to take the i_mutex here so we have to play games to
1656 * protect against truncate races as the page could now be beyond EOF. Because
1657 * vmtruncate() writes the inode size before removing pages, once we have the
1658 * page lock we can determine safely if the page is beyond EOF. If it is not
1659 * beyond EOF, then the page is guaranteed safe against truncation until we
1662 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1664 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1670 down_read(&BTRFS_I(inode)->root->snap_sem);
1672 wait_on_page_writeback(page);
1673 size = i_size_read(inode);
1674 page_start = page->index << PAGE_CACHE_SHIFT;
1676 if ((page->mapping != inode->i_mapping) ||
1677 (page_start > size)) {
1678 /* page got truncated out from underneath us */
1682 /* page is wholly or partially inside EOF */
1683 if (page_start + PAGE_CACHE_SIZE > size)
1684 end = size & ~PAGE_CACHE_MASK;
1686 end = PAGE_CACHE_SIZE;
1688 ret = btrfs_cow_one_page(inode, page, end);
1691 up_read(&BTRFS_I(inode)->root->snap_sem);
1696 static void btrfs_truncate(struct inode *inode)
1698 struct btrfs_root *root = BTRFS_I(inode)->root;
1700 struct btrfs_trans_handle *trans;
1703 if (!S_ISREG(inode->i_mode))
1705 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1708 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1710 mutex_lock(&root->fs_info->fs_mutex);
1711 trans = btrfs_start_transaction(root, 1);
1712 btrfs_set_trans_block_group(trans, inode);
1714 /* FIXME, add redo link to tree so we don't leak on crash */
1715 ret = btrfs_truncate_in_trans(trans, root, inode);
1716 btrfs_update_inode(trans, root, inode);
1717 nr = trans->blocks_used;
1718 ret = btrfs_end_transaction(trans, root);
1720 mutex_unlock(&root->fs_info->fs_mutex);
1721 btrfs_btree_balance_dirty(root, nr);
1724 int btrfs_commit_write(struct file *file, struct page *page,
1725 unsigned from, unsigned to)
1727 return extent_commit_write(&BTRFS_I(page->mapping->host)->extent_tree,
1728 page->mapping->host, page, from, to);
1731 static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1733 struct btrfs_trans_handle *trans;
1734 struct btrfs_key key;
1735 struct btrfs_root_item root_item;
1736 struct btrfs_inode_item *inode_item;
1737 struct buffer_head *subvol;
1738 struct btrfs_leaf *leaf;
1739 struct btrfs_root *new_root;
1740 struct inode *inode;
1745 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
1746 unsigned long nr = 1;
1748 mutex_lock(&root->fs_info->fs_mutex);
1749 trans = btrfs_start_transaction(root, 1);
1752 subvol = btrfs_alloc_free_block(trans, root, 0, 0);
1754 return PTR_ERR(subvol);
1755 leaf = btrfs_buffer_leaf(subvol);
1756 btrfs_set_header_nritems(&leaf->header, 0);
1757 btrfs_set_header_level(&leaf->header, 0);
1758 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
1759 btrfs_set_header_generation(&leaf->header, trans->transid);
1760 btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
1761 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
1762 sizeof(leaf->header.fsid));
1763 btrfs_mark_buffer_dirty(subvol);
1765 inode_item = &root_item.inode;
1766 memset(inode_item, 0, sizeof(*inode_item));
1767 btrfs_set_inode_generation(inode_item, 1);
1768 btrfs_set_inode_size(inode_item, 3);
1769 btrfs_set_inode_nlink(inode_item, 1);
1770 btrfs_set_inode_nblocks(inode_item, 1);
1771 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
1773 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
1774 btrfs_set_root_refs(&root_item, 1);
1775 btrfs_set_root_blocks_used(&root_item, 0);
1776 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
1777 root_item.drop_level = 0;
1781 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1786 btrfs_set_root_dirid(&root_item, new_dirid);
1788 key.objectid = objectid;
1791 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1792 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1798 * insert the directory item
1800 key.offset = (u64)-1;
1801 dir = root->fs_info->sb->s_root->d_inode;
1802 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1803 name, namelen, dir->i_ino, &key,
1808 ret = btrfs_commit_transaction(trans, root);
1812 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
1815 trans = btrfs_start_transaction(new_root, 1);
1818 inode = btrfs_new_inode(trans, new_root, new_dirid,
1819 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
1822 inode->i_op = &btrfs_dir_inode_operations;
1823 inode->i_fop = &btrfs_dir_file_operations;
1824 new_root->inode = inode;
1826 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
1832 ret = btrfs_update_inode(trans, new_root, inode);
1836 nr = trans->blocks_used;
1837 err = btrfs_commit_transaction(trans, root);
1841 mutex_unlock(&root->fs_info->fs_mutex);
1842 btrfs_btree_balance_dirty(root, nr);
1846 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
1848 struct btrfs_trans_handle *trans;
1849 struct btrfs_key key;
1850 struct btrfs_root_item new_root_item;
1851 struct buffer_head *tmp;
1857 if (!root->ref_cows)
1860 down_write(&root->snap_sem);
1861 freeze_bdev(root->fs_info->sb->s_bdev);
1862 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
1864 mutex_lock(&root->fs_info->fs_mutex);
1865 trans = btrfs_start_transaction(root, 1);
1868 ret = btrfs_update_inode(trans, root, root->inode);
1872 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1877 memcpy(&new_root_item, &root->root_item,
1878 sizeof(new_root_item));
1880 key.objectid = objectid;
1883 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1884 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
1885 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
1887 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1893 * insert the directory item
1895 key.offset = (u64)-1;
1896 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1898 root->fs_info->sb->s_root->d_inode->i_ino,
1899 &key, BTRFS_FT_DIR);
1904 ret = btrfs_inc_root_ref(trans, root);
1908 nr = trans->blocks_used;
1909 err = btrfs_commit_transaction(trans, root);
1912 mutex_unlock(&root->fs_info->fs_mutex);
1913 up_write(&root->snap_sem);
1914 btrfs_btree_balance_dirty(root, nr);
1918 static unsigned long force_ra(struct address_space *mapping,
1919 struct file_ra_state *ra, struct file *file,
1920 pgoff_t offset, pgoff_t last_index)
1924 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
1925 req_size = last_index - offset + 1;
1926 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
1929 req_size = min(last_index - offset + 1, (pgoff_t)128);
1930 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
1931 return offset + req_size;
1935 int btrfs_defrag_file(struct file *file) {
1936 struct inode *inode = file->f_path.dentry->d_inode;
1937 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1939 unsigned long last_index;
1940 unsigned long ra_index = 0;
1945 mutex_lock(&inode->i_mutex);
1946 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
1947 for (i = 0; i <= last_index; i++) {
1948 if (i == ra_index) {
1949 ra_index = force_ra(inode->i_mapping, &file->f_ra,
1950 file, ra_index, last_index);
1952 page = grab_cache_page(inode->i_mapping, i);
1955 if (!PageUptodate(page)) {
1956 btrfs_readpage(NULL, page);
1958 if (!PageUptodate(page)) {
1960 page_cache_release(page);
1964 page_start = page->index << PAGE_CACHE_SHIFT;
1965 page_end = page_start + PAGE_CACHE_SIZE - 1;
1967 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
1968 set_extent_delalloc(em_tree, page_start,
1969 page_end, GFP_NOFS);
1970 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
1971 set_page_dirty(page);
1973 page_cache_release(page);
1974 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
1978 mutex_unlock(&inode->i_mutex);
1982 static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg)
1984 struct btrfs_ioctl_vol_args vol_args;
1985 struct btrfs_dir_item *di;
1986 struct btrfs_path *path;
1990 if (copy_from_user(&vol_args, arg, sizeof(vol_args)))
1993 namelen = strlen(vol_args.name);
1994 if (namelen > BTRFS_VOL_NAME_MAX)
1996 if (strchr(vol_args.name, '/'))
1999 path = btrfs_alloc_path();
2003 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2004 mutex_lock(&root->fs_info->fs_mutex);
2005 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2007 vol_args.name, namelen, 0);
2008 mutex_unlock(&root->fs_info->fs_mutex);
2009 btrfs_free_path(path);
2010 if (di && !IS_ERR(di))
2015 if (root == root->fs_info->tree_root)
2016 return create_subvol(root, vol_args.name, namelen);
2017 return create_snapshot(root, vol_args.name, namelen);
2020 static int btrfs_ioctl_defrag(struct file *file)
2022 struct inode *inode = file->f_path.dentry->d_inode;
2023 struct btrfs_root *root = BTRFS_I(inode)->root;
2025 switch (inode->i_mode & S_IFMT) {
2027 mutex_lock(&root->fs_info->fs_mutex);
2028 btrfs_defrag_root(root, 0);
2029 btrfs_defrag_root(root->fs_info->extent_root, 0);
2030 mutex_unlock(&root->fs_info->fs_mutex);
2033 btrfs_defrag_file(file);
2040 long btrfs_ioctl(struct file *file, unsigned int
2041 cmd, unsigned long arg)
2043 struct btrfs_root *root = BTRFS_I(file->f_path.dentry->d_inode)->root;
2046 case BTRFS_IOC_SNAP_CREATE:
2047 return btrfs_ioctl_snap_create(root, (void __user *)arg);
2048 case BTRFS_IOC_DEFRAG:
2049 return btrfs_ioctl_defrag(file);
2056 * Called inside transaction, so use GFP_NOFS
2058 struct inode *btrfs_alloc_inode(struct super_block *sb)
2060 struct btrfs_inode *ei;
2062 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2066 return &ei->vfs_inode;
2069 void btrfs_destroy_inode(struct inode *inode)
2071 WARN_ON(!list_empty(&inode->i_dentry));
2072 WARN_ON(inode->i_data.nrpages);
2074 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2077 static void init_once(void * foo, struct kmem_cache * cachep,
2078 unsigned long flags)
2080 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2082 inode_init_once(&ei->vfs_inode);
2085 void btrfs_destroy_cachep(void)
2087 if (btrfs_inode_cachep)
2088 kmem_cache_destroy(btrfs_inode_cachep);
2089 if (btrfs_trans_handle_cachep)
2090 kmem_cache_destroy(btrfs_trans_handle_cachep);
2091 if (btrfs_transaction_cachep)
2092 kmem_cache_destroy(btrfs_transaction_cachep);
2093 if (btrfs_bit_radix_cachep)
2094 kmem_cache_destroy(btrfs_bit_radix_cachep);
2095 if (btrfs_path_cachep)
2096 kmem_cache_destroy(btrfs_path_cachep);
2099 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
2100 unsigned long extra_flags,
2101 void (*ctor)(void *, struct kmem_cache *,
2104 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2105 SLAB_MEM_SPREAD | extra_flags), ctor
2106 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2112 int btrfs_init_cachep(void)
2114 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
2115 sizeof(struct btrfs_inode),
2117 if (!btrfs_inode_cachep)
2119 btrfs_trans_handle_cachep =
2120 btrfs_cache_create("btrfs_trans_handle_cache",
2121 sizeof(struct btrfs_trans_handle),
2123 if (!btrfs_trans_handle_cachep)
2125 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
2126 sizeof(struct btrfs_transaction),
2128 if (!btrfs_transaction_cachep)
2130 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
2131 sizeof(struct btrfs_path),
2133 if (!btrfs_path_cachep)
2135 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
2136 SLAB_DESTROY_BY_RCU, NULL);
2137 if (!btrfs_bit_radix_cachep)
2141 btrfs_destroy_cachep();
2145 static int btrfs_getattr(struct vfsmount *mnt,
2146 struct dentry *dentry, struct kstat *stat)
2148 struct inode *inode = dentry->d_inode;
2149 generic_fillattr(inode, stat);
2150 stat->blksize = 256 * 1024;
2154 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2155 struct inode * new_dir,struct dentry *new_dentry)
2157 struct btrfs_trans_handle *trans;
2158 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2159 struct inode *new_inode = new_dentry->d_inode;
2160 struct inode *old_inode = old_dentry->d_inode;
2161 struct timespec ctime = CURRENT_TIME;
2162 struct btrfs_path *path;
2163 struct btrfs_dir_item *di;
2166 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2167 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2170 mutex_lock(&root->fs_info->fs_mutex);
2171 trans = btrfs_start_transaction(root, 1);
2172 btrfs_set_trans_block_group(trans, new_dir);
2173 path = btrfs_alloc_path();
2179 old_dentry->d_inode->i_nlink++;
2180 old_dir->i_ctime = old_dir->i_mtime = ctime;
2181 new_dir->i_ctime = new_dir->i_mtime = ctime;
2182 old_inode->i_ctime = ctime;
2183 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2184 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
2186 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2196 old_parent_oid = btrfs_disk_key_objectid(&di->location);
2197 ret = btrfs_del_item(trans, root, path);
2201 btrfs_release_path(root, path);
2203 di = btrfs_lookup_dir_index_item(trans, root, path,
2215 ret = btrfs_del_item(trans, root, path);
2219 btrfs_release_path(root, path);
2221 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2222 old_inode->i_ino, location,
2229 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2234 new_inode->i_ctime = CURRENT_TIME;
2235 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2239 ret = btrfs_add_link(trans, new_dentry, old_inode);
2244 btrfs_free_path(path);
2245 btrfs_end_transaction(trans, root);
2246 mutex_unlock(&root->fs_info->fs_mutex);
2250 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2251 const char *symname)
2253 struct btrfs_trans_handle *trans;
2254 struct btrfs_root *root = BTRFS_I(dir)->root;
2255 struct btrfs_path *path;
2256 struct btrfs_key key;
2257 struct inode *inode;
2264 struct btrfs_file_extent_item *ei;
2267 name_len = strlen(symname) + 1;
2268 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2269 return -ENAMETOOLONG;
2270 mutex_lock(&root->fs_info->fs_mutex);
2271 trans = btrfs_start_transaction(root, 1);
2272 btrfs_set_trans_block_group(trans, dir);
2274 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2280 inode = btrfs_new_inode(trans, root, objectid,
2281 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2282 err = PTR_ERR(inode);
2286 btrfs_set_trans_block_group(trans, inode);
2287 err = btrfs_add_nondir(trans, dentry, inode);
2291 inode->i_mapping->a_ops = &btrfs_aops;
2292 inode->i_fop = &btrfs_file_operations;
2293 inode->i_op = &btrfs_file_inode_operations;
2294 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2295 inode->i_mapping, GFP_NOFS);
2296 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
2298 dir->i_sb->s_dirt = 1;
2299 btrfs_update_inode_block_group(trans, inode);
2300 btrfs_update_inode_block_group(trans, dir);
2304 path = btrfs_alloc_path();
2306 key.objectid = inode->i_ino;
2309 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2310 datasize = btrfs_file_extent_calc_inline_size(name_len);
2311 err = btrfs_insert_empty_item(trans, root, path, &key,
2317 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2318 path->slots[0], struct btrfs_file_extent_item);
2319 btrfs_set_file_extent_generation(ei, trans->transid);
2320 btrfs_set_file_extent_type(ei,
2321 BTRFS_FILE_EXTENT_INLINE);
2322 ptr = btrfs_file_extent_inline_start(ei);
2323 btrfs_memcpy(root, path->nodes[0]->b_data,
2324 ptr, symname, name_len);
2325 btrfs_mark_buffer_dirty(path->nodes[0]);
2326 btrfs_free_path(path);
2327 inode->i_op = &btrfs_symlink_inode_operations;
2328 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2329 inode->i_size = name_len - 1;
2330 err = btrfs_update_inode(trans, root, inode);
2335 nr = trans->blocks_used;
2336 btrfs_end_transaction(trans, root);
2337 mutex_unlock(&root->fs_info->fs_mutex);
2339 inode_dec_link_count(inode);
2342 btrfs_btree_balance_dirty(root, nr);
2346 static struct inode_operations btrfs_dir_inode_operations = {
2347 .lookup = btrfs_lookup,
2348 .create = btrfs_create,
2349 .unlink = btrfs_unlink,
2351 .mkdir = btrfs_mkdir,
2352 .rmdir = btrfs_rmdir,
2353 .rename = btrfs_rename,
2354 .symlink = btrfs_symlink,
2355 .setattr = btrfs_setattr,
2356 .mknod = btrfs_mknod,
2359 static struct inode_operations btrfs_dir_ro_inode_operations = {
2360 .lookup = btrfs_lookup,
2363 static struct file_operations btrfs_dir_file_operations = {
2364 .llseek = generic_file_llseek,
2365 .read = generic_read_dir,
2366 .readdir = btrfs_readdir,
2367 .unlocked_ioctl = btrfs_ioctl,
2368 #ifdef CONFIG_COMPAT
2369 .compat_ioctl = btrfs_ioctl,
2373 static struct extent_map_ops btrfs_extent_map_ops = {
2374 .fill_delalloc = run_delalloc_range,
2375 .writepage_io_hook = btrfs_writepage_io_hook,
2376 .readpage_io_hook = btrfs_readpage_io_hook,
2377 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2380 static struct address_space_operations btrfs_aops = {
2381 .readpage = btrfs_readpage,
2382 .writepage = btrfs_writepage,
2383 .sync_page = block_sync_page,
2384 .prepare_write = btrfs_prepare_write,
2385 .commit_write = btrfs_commit_write,
2387 .invalidatepage = btrfs_invalidatepage,
2388 .releasepage = btrfs_releasepage,
2389 .set_page_dirty = __set_page_dirty_nobuffers,
2392 static struct address_space_operations btrfs_symlink_aops = {
2393 .readpage = btrfs_readpage,
2394 .writepage = btrfs_writepage,
2395 .invalidatepage = btrfs_invalidatepage,
2396 .releasepage = btrfs_releasepage,
2399 static struct inode_operations btrfs_file_inode_operations = {
2400 .truncate = btrfs_truncate,
2401 .getattr = btrfs_getattr,
2402 .setattr = btrfs_setattr,
2405 static struct inode_operations btrfs_special_inode_operations = {
2406 .getattr = btrfs_getattr,
2407 .setattr = btrfs_setattr,
2410 static struct inode_operations btrfs_symlink_inode_operations = {
2411 .readlink = generic_readlink,
2412 .follow_link = page_follow_link_light,
2413 .put_link = page_put_link,