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 = root->sectorsize;
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_bytes = (end - start + blocksize) & ~(blocksize - 1);
89 ret = btrfs_drop_extents(trans, root, inode,
90 start, start + num_bytes, start, &alloc_hint);
92 ret = btrfs_alloc_extent(trans, root, inode->i_ino, num_bytes, 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 size_t offset = start - (page->index << PAGE_CACHE_SHIFT);
163 struct inode *inode = page->mapping->host;
164 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
169 ret = get_state_private(em_tree, start, &private);
170 kaddr = kmap_atomic(page, KM_IRQ0);
175 struct btrfs_root *root = BTRFS_I(inode)->root;
176 char csum[BTRFS_CRC32_SIZE];
177 ret = btrfs_csum_data(root, kaddr + offset, end - start + 1, csum);
179 if (memcmp(csum, &private, BTRFS_CRC32_SIZE)) {
183 kunmap_atomic(kaddr, KM_IRQ0);
187 printk("btrfs csum failed ino %lu off %llu\n",
188 page->mapping->host->i_ino, (unsigned long long)start);
189 memset(kaddr + offset, 1, end - start + 1);
190 flush_dcache_page(page);
191 kunmap_atomic(kaddr, KM_IRQ0);
195 void btrfs_read_locked_inode(struct inode *inode)
197 struct btrfs_path *path;
198 struct extent_buffer *leaf;
199 struct btrfs_inode_item *inode_item;
200 struct btrfs_inode_timespec *tspec;
201 struct btrfs_root *root = BTRFS_I(inode)->root;
202 struct btrfs_key location;
203 u64 alloc_group_block;
207 path = btrfs_alloc_path();
209 mutex_lock(&root->fs_info->fs_mutex);
211 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
212 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
216 leaf = path->nodes[0];
217 inode_item = btrfs_item_ptr(leaf, path->slots[0],
218 struct btrfs_inode_item);
220 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
221 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
222 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
223 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
224 inode->i_size = btrfs_inode_size(leaf, inode_item);
226 tspec = btrfs_inode_atime(inode_item);
227 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
228 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
230 tspec = btrfs_inode_mtime(inode_item);
231 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
232 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
234 tspec = btrfs_inode_ctime(inode_item);
235 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
236 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
238 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
239 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
241 rdev = btrfs_inode_rdev(leaf, inode_item);
243 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
244 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
247 btrfs_free_path(path);
250 mutex_unlock(&root->fs_info->fs_mutex);
252 switch (inode->i_mode & S_IFMT) {
254 inode->i_mapping->a_ops = &btrfs_aops;
255 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
256 inode->i_fop = &btrfs_file_operations;
257 inode->i_op = &btrfs_file_inode_operations;
260 inode->i_fop = &btrfs_dir_file_operations;
261 if (root == root->fs_info->tree_root)
262 inode->i_op = &btrfs_dir_ro_inode_operations;
264 inode->i_op = &btrfs_dir_inode_operations;
267 inode->i_op = &btrfs_symlink_inode_operations;
268 inode->i_mapping->a_ops = &btrfs_symlink_aops;
271 init_special_inode(inode, inode->i_mode, rdev);
277 btrfs_release_path(root, path);
278 btrfs_free_path(path);
279 mutex_unlock(&root->fs_info->fs_mutex);
280 make_bad_inode(inode);
283 static void fill_inode_item(struct extent_buffer *leaf,
284 struct btrfs_inode_item *item,
287 btrfs_set_inode_uid(leaf, item, inode->i_uid);
288 btrfs_set_inode_gid(leaf, item, inode->i_gid);
289 btrfs_set_inode_size(leaf, item, inode->i_size);
290 btrfs_set_inode_mode(leaf, item, inode->i_mode);
291 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
293 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
294 inode->i_atime.tv_sec);
295 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
296 inode->i_atime.tv_nsec);
298 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
299 inode->i_mtime.tv_sec);
300 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
301 inode->i_mtime.tv_nsec);
303 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
304 inode->i_ctime.tv_sec);
305 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
306 inode->i_ctime.tv_nsec);
308 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
309 btrfs_set_inode_generation(leaf, item, inode->i_generation);
310 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
311 btrfs_set_inode_block_group(leaf, item,
312 BTRFS_I(inode)->block_group->key.objectid);
315 int btrfs_update_inode(struct btrfs_trans_handle *trans,
316 struct btrfs_root *root,
319 struct btrfs_inode_item *inode_item;
320 struct btrfs_path *path;
321 struct extent_buffer *leaf;
324 path = btrfs_alloc_path();
326 ret = btrfs_lookup_inode(trans, root, path,
327 &BTRFS_I(inode)->location, 1);
334 leaf = path->nodes[0];
335 inode_item = btrfs_item_ptr(leaf, path->slots[0],
336 struct btrfs_inode_item);
338 fill_inode_item(leaf, inode_item, inode);
339 btrfs_mark_buffer_dirty(leaf);
340 btrfs_set_inode_last_trans(trans, inode);
343 btrfs_release_path(root, path);
344 btrfs_free_path(path);
349 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
350 struct btrfs_root *root,
352 struct dentry *dentry)
354 struct btrfs_path *path;
355 const char *name = dentry->d_name.name;
356 int name_len = dentry->d_name.len;
358 struct extent_buffer *leaf;
359 struct btrfs_dir_item *di;
360 struct btrfs_key key;
362 path = btrfs_alloc_path();
368 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
378 leaf = path->nodes[0];
379 btrfs_dir_item_key_to_cpu(leaf, di, &key);
380 ret = btrfs_delete_one_dir_name(trans, root, path, di);
383 btrfs_release_path(root, path);
385 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
386 key.objectid, name, name_len, -1);
395 ret = btrfs_delete_one_dir_name(trans, root, path, di);
397 dentry->d_inode->i_ctime = dir->i_ctime;
399 btrfs_free_path(path);
401 dir->i_size -= name_len * 2;
402 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
403 btrfs_update_inode(trans, root, dir);
404 drop_nlink(dentry->d_inode);
405 ret = btrfs_update_inode(trans, root, dentry->d_inode);
406 dir->i_sb->s_dirt = 1;
411 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
413 struct btrfs_root *root;
414 struct btrfs_trans_handle *trans;
418 root = BTRFS_I(dir)->root;
419 mutex_lock(&root->fs_info->fs_mutex);
420 trans = btrfs_start_transaction(root, 1);
422 btrfs_set_trans_block_group(trans, dir);
423 ret = btrfs_unlink_trans(trans, root, dir, dentry);
424 nr = trans->blocks_used;
426 btrfs_end_transaction(trans, root);
427 mutex_unlock(&root->fs_info->fs_mutex);
428 btrfs_btree_balance_dirty(root, nr);
433 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
435 struct inode *inode = dentry->d_inode;
438 struct btrfs_root *root = BTRFS_I(dir)->root;
439 struct btrfs_path *path;
440 struct btrfs_key key;
441 struct btrfs_trans_handle *trans;
442 struct btrfs_key found_key;
444 struct extent_buffer *leaf;
445 char *goodnames = "..";
448 path = btrfs_alloc_path();
450 mutex_lock(&root->fs_info->fs_mutex);
451 trans = btrfs_start_transaction(root, 1);
453 btrfs_set_trans_block_group(trans, dir);
454 key.objectid = inode->i_ino;
455 key.offset = (u64)-1;
458 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
464 if (path->slots[0] == 0) {
469 leaf = path->nodes[0];
470 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
471 found_type = btrfs_key_type(&found_key);
472 if (found_key.objectid != inode->i_ino) {
476 if ((found_type != BTRFS_DIR_ITEM_KEY &&
477 found_type != BTRFS_DIR_INDEX_KEY) ||
478 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
479 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
483 ret = btrfs_del_item(trans, root, path);
486 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
488 btrfs_release_path(root, path);
491 btrfs_release_path(root, path);
493 /* now the directory is empty */
494 err = btrfs_unlink_trans(trans, root, dir, dentry);
499 btrfs_release_path(root, path);
500 btrfs_free_path(path);
501 mutex_unlock(&root->fs_info->fs_mutex);
502 nr = trans->blocks_used;
503 ret = btrfs_end_transaction(trans, root);
504 btrfs_btree_balance_dirty(root, nr);
510 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
511 struct btrfs_root *root,
514 struct btrfs_path *path;
519 path = btrfs_alloc_path();
521 ret = btrfs_lookup_inode(trans, root, path,
522 &BTRFS_I(inode)->location, -1);
526 ret = btrfs_del_item(trans, root, path);
527 btrfs_free_path(path);
532 * this can truncate away extent items, csum items and directory items.
533 * It starts at a high offset and removes keys until it can't find
534 * any higher than i_size.
536 * csum items that cross the new i_size are truncated to the new size
539 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
540 struct btrfs_root *root,
544 struct btrfs_path *path;
545 struct btrfs_key key;
546 struct btrfs_key found_key;
548 struct extent_buffer *leaf;
549 struct btrfs_file_extent_item *fi;
550 u64 extent_start = 0;
551 u64 extent_num_bytes = 0;
556 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
557 path = btrfs_alloc_path();
561 /* FIXME, add redo link to tree so we don't leak on crash */
562 key.objectid = inode->i_ino;
563 key.offset = (u64)-1;
567 btrfs_init_path(path);
569 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
574 BUG_ON(path->slots[0] == 0);
577 leaf = path->nodes[0];
578 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
579 found_type = btrfs_key_type(&found_key);
581 if (found_key.objectid != inode->i_ino)
584 if (found_type != BTRFS_CSUM_ITEM_KEY &&
585 found_type != BTRFS_DIR_ITEM_KEY &&
586 found_type != BTRFS_DIR_INDEX_KEY &&
587 found_type != BTRFS_EXTENT_DATA_KEY)
590 item_end = found_key.offset;
591 if (found_type == BTRFS_EXTENT_DATA_KEY) {
592 fi = btrfs_item_ptr(leaf, path->slots[0],
593 struct btrfs_file_extent_item);
594 if (btrfs_file_extent_type(leaf, fi) !=
595 BTRFS_FILE_EXTENT_INLINE) {
597 btrfs_file_extent_num_bytes(leaf, fi);
600 if (found_type == BTRFS_CSUM_ITEM_KEY) {
601 ret = btrfs_csum_truncate(trans, root, path,
605 if (item_end < inode->i_size) {
606 if (found_type == BTRFS_DIR_ITEM_KEY) {
607 found_type = BTRFS_INODE_ITEM_KEY;
608 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
609 found_type = BTRFS_CSUM_ITEM_KEY;
610 } else if (found_type) {
615 btrfs_set_key_type(&key, found_type);
618 if (found_key.offset >= inode->i_size)
624 /* FIXME, shrink the extent if the ref count is only 1 */
625 if (found_type == BTRFS_EXTENT_DATA_KEY &&
626 btrfs_file_extent_type(leaf, fi) !=
627 BTRFS_FILE_EXTENT_INLINE) {
629 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
632 btrfs_file_extent_num_bytes(leaf, fi);
633 extent_num_bytes = inode->i_size -
634 found_key.offset + root->sectorsize - 1;
635 btrfs_set_file_extent_num_bytes(leaf, fi,
637 num_dec = (orig_num_bytes -
638 extent_num_bytes) >> 9;
639 if (extent_start != 0) {
640 inode->i_blocks -= num_dec;
642 btrfs_mark_buffer_dirty(leaf);
645 btrfs_file_extent_disk_num_bytes(leaf,
647 /* FIXME blocksize != 4096 */
648 num_dec = btrfs_file_extent_num_bytes(leaf,
650 if (extent_start != 0) {
652 inode->i_blocks -= num_dec;
657 ret = btrfs_del_item(trans, root, path);
663 btrfs_release_path(root, path);
665 ret = btrfs_free_extent(trans, root, extent_start,
666 extent_num_bytes, 0);
672 btrfs_release_path(root, path);
673 btrfs_free_path(path);
674 inode->i_sb->s_dirt = 1;
678 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
683 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
684 u64 page_start = page->index << PAGE_CACHE_SHIFT;
685 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
687 set_page_extent_mapped(page);
689 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
690 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
692 if (zero_start != PAGE_CACHE_SIZE) {
694 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
695 flush_dcache_page(page);
698 set_page_dirty(page);
699 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
705 * taken from block_truncate_page, but does cow as it zeros out
706 * any bytes left in the last page in the file.
708 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
710 struct inode *inode = mapping->host;
711 struct btrfs_root *root = BTRFS_I(inode)->root;
712 u32 blocksize = root->sectorsize;
713 pgoff_t index = from >> PAGE_CACHE_SHIFT;
714 unsigned offset = from & (PAGE_CACHE_SIZE-1);
719 if ((offset & (blocksize - 1)) == 0)
722 down_read(&root->snap_sem);
724 page = grab_cache_page(mapping, index);
727 if (!PageUptodate(page)) {
728 ret = btrfs_readpage(NULL, page);
730 if (!PageUptodate(page)) {
735 page_start = page->index << PAGE_CACHE_SHIFT;
737 ret = btrfs_cow_one_page(inode, page, offset);
740 page_cache_release(page);
741 up_read(&BTRFS_I(inode)->root->snap_sem);
746 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
748 struct inode *inode = dentry->d_inode;
751 err = inode_change_ok(inode, attr);
755 if (S_ISREG(inode->i_mode) &&
756 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
757 struct btrfs_trans_handle *trans;
758 struct btrfs_root *root = BTRFS_I(inode)->root;
759 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
761 u64 mask = root->sectorsize - 1;
762 u64 pos = (inode->i_size + mask) & ~mask;
763 u64 block_end = attr->ia_size | mask;
767 if (attr->ia_size <= pos)
770 btrfs_truncate_page(inode->i_mapping, inode->i_size);
772 lock_extent(em_tree, pos, block_end, GFP_NOFS);
773 hole_size = (attr->ia_size - pos + mask) & ~mask;
775 mutex_lock(&root->fs_info->fs_mutex);
776 trans = btrfs_start_transaction(root, 1);
777 btrfs_set_trans_block_group(trans, inode);
778 err = btrfs_drop_extents(trans, root, inode,
779 pos, pos + hole_size, pos,
782 err = btrfs_insert_file_extent(trans, root, inode->i_ino,
783 pos, 0, 0, hole_size);
784 btrfs_end_transaction(trans, root);
785 mutex_unlock(&root->fs_info->fs_mutex);
786 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
791 err = inode_setattr(inode, attr);
795 void btrfs_delete_inode(struct inode *inode)
797 struct btrfs_trans_handle *trans;
798 struct btrfs_root *root = BTRFS_I(inode)->root;
802 truncate_inode_pages(&inode->i_data, 0);
803 if (is_bad_inode(inode)) {
808 mutex_lock(&root->fs_info->fs_mutex);
809 trans = btrfs_start_transaction(root, 1);
811 btrfs_set_trans_block_group(trans, inode);
812 ret = btrfs_truncate_in_trans(trans, root, inode);
815 ret = btrfs_free_inode(trans, root, inode);
818 nr = trans->blocks_used;
820 btrfs_end_transaction(trans, root);
821 mutex_unlock(&root->fs_info->fs_mutex);
822 btrfs_btree_balance_dirty(root, nr);
826 nr = trans->blocks_used;
827 btrfs_end_transaction(trans, root);
828 mutex_unlock(&root->fs_info->fs_mutex);
829 btrfs_btree_balance_dirty(root, nr);
835 * this returns the key found in the dir entry in the location pointer.
836 * If no dir entries were found, location->objectid is 0.
838 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
839 struct btrfs_key *location)
841 const char *name = dentry->d_name.name;
842 int namelen = dentry->d_name.len;
843 struct btrfs_dir_item *di;
844 struct btrfs_path *path;
845 struct btrfs_root *root = BTRFS_I(dir)->root;
848 path = btrfs_alloc_path();
850 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
852 if (!di || IS_ERR(di)) {
853 location->objectid = 0;
857 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
859 btrfs_release_path(root, path);
860 btrfs_free_path(path);
865 * when we hit a tree root in a directory, the btrfs part of the inode
866 * needs to be changed to reflect the root directory of the tree root. This
867 * is kind of like crossing a mount point.
869 static int fixup_tree_root_location(struct btrfs_root *root,
870 struct btrfs_key *location,
871 struct btrfs_root **sub_root,
872 struct dentry *dentry)
874 struct btrfs_path *path;
875 struct btrfs_root_item *ri;
877 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
879 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
882 path = btrfs_alloc_path();
884 mutex_lock(&root->fs_info->fs_mutex);
886 *sub_root = btrfs_read_fs_root(root->fs_info, location,
889 if (IS_ERR(*sub_root))
890 return PTR_ERR(*sub_root);
892 ri = &(*sub_root)->root_item;
893 location->objectid = btrfs_root_dirid(ri);
894 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
895 location->offset = 0;
897 btrfs_free_path(path);
898 mutex_unlock(&root->fs_info->fs_mutex);
902 static int btrfs_init_locked_inode(struct inode *inode, void *p)
904 struct btrfs_iget_args *args = p;
905 inode->i_ino = args->ino;
906 BTRFS_I(inode)->root = args->root;
907 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
908 inode->i_mapping, GFP_NOFS);
912 static int btrfs_find_actor(struct inode *inode, void *opaque)
914 struct btrfs_iget_args *args = opaque;
915 return (args->ino == inode->i_ino &&
916 args->root == BTRFS_I(inode)->root);
919 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
920 struct btrfs_root *root)
923 struct btrfs_iget_args args;
927 inode = iget5_locked(s, objectid, btrfs_find_actor,
928 btrfs_init_locked_inode,
933 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
934 struct nameidata *nd)
936 struct inode * inode;
937 struct btrfs_inode *bi = BTRFS_I(dir);
938 struct btrfs_root *root = bi->root;
939 struct btrfs_root *sub_root = root;
940 struct btrfs_key location;
943 if (dentry->d_name.len > BTRFS_NAME_LEN)
944 return ERR_PTR(-ENAMETOOLONG);
946 mutex_lock(&root->fs_info->fs_mutex);
947 ret = btrfs_inode_by_name(dir, dentry, &location);
948 mutex_unlock(&root->fs_info->fs_mutex);
954 if (location.objectid) {
955 ret = fixup_tree_root_location(root, &location, &sub_root,
960 return ERR_PTR(-ENOENT);
961 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
964 return ERR_PTR(-EACCES);
965 if (inode->i_state & I_NEW) {
966 /* the inode and parent dir are two different roots */
967 if (sub_root != root) {
969 sub_root->inode = inode;
971 BTRFS_I(inode)->root = sub_root;
972 memcpy(&BTRFS_I(inode)->location, &location,
974 btrfs_read_locked_inode(inode);
975 unlock_new_inode(inode);
978 return d_splice_alias(inode, dentry);
981 static unsigned char btrfs_filetype_table[] = {
982 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
985 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
987 struct inode *inode = filp->f_path.dentry->d_inode;
988 struct btrfs_root *root = BTRFS_I(inode)->root;
989 struct btrfs_item *item;
990 struct btrfs_dir_item *di;
991 struct btrfs_key key;
992 struct btrfs_key found_key;
993 struct btrfs_path *path;
996 struct extent_buffer *leaf;
999 unsigned char d_type;
1004 int key_type = BTRFS_DIR_INDEX_KEY;
1009 /* FIXME, use a real flag for deciding about the key type */
1010 if (root->fs_info->tree_root == root)
1011 key_type = BTRFS_DIR_ITEM_KEY;
1013 mutex_lock(&root->fs_info->fs_mutex);
1014 key.objectid = inode->i_ino;
1015 btrfs_set_key_type(&key, key_type);
1016 key.offset = filp->f_pos;
1018 path = btrfs_alloc_path();
1020 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1025 leaf = path->nodes[0];
1026 nritems = btrfs_header_nritems(leaf);
1027 slot = path->slots[0];
1028 if (advance || slot >= nritems) {
1029 if (slot >= nritems -1) {
1030 ret = btrfs_next_leaf(root, path);
1033 leaf = path->nodes[0];
1034 nritems = btrfs_header_nritems(leaf);
1035 slot = path->slots[0];
1042 item = btrfs_item_nr(leaf, slot);
1043 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1045 if (found_key.objectid != key.objectid)
1047 if (btrfs_key_type(&found_key) != key_type)
1049 if (found_key.offset < filp->f_pos)
1052 filp->f_pos = found_key.offset;
1054 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1056 di_total = btrfs_item_size(leaf, item);
1057 while(di_cur < di_total) {
1058 struct btrfs_key location;
1060 name_len = btrfs_dir_name_len(leaf, di);
1061 if (name_len < 32) {
1062 name_ptr = tmp_name;
1064 name_ptr = kmalloc(name_len, GFP_NOFS);
1067 read_extent_buffer(leaf, name_ptr,
1068 (unsigned long)(di + 1), name_len);
1070 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1071 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1073 over = filldir(dirent, name_ptr, name_len,
1078 if (name_ptr != tmp_name)
1083 di_len = btrfs_dir_name_len(leaf, di) + sizeof(*di);
1085 di = (struct btrfs_dir_item *)((char *)di + di_len);
1092 btrfs_release_path(root, path);
1093 btrfs_free_path(path);
1094 mutex_unlock(&root->fs_info->fs_mutex);
1098 int btrfs_write_inode(struct inode *inode, int wait)
1100 struct btrfs_root *root = BTRFS_I(inode)->root;
1101 struct btrfs_trans_handle *trans;
1105 mutex_lock(&root->fs_info->fs_mutex);
1106 trans = btrfs_start_transaction(root, 1);
1107 btrfs_set_trans_block_group(trans, inode);
1108 ret = btrfs_commit_transaction(trans, root);
1109 mutex_unlock(&root->fs_info->fs_mutex);
1115 * This is somewhat expensive, updating the tree every time the
1116 * inode changes. But, it is most likely to find the inode in cache.
1117 * FIXME, needs more benchmarking...there are no reasons other than performance
1118 * to keep or drop this code.
1120 void btrfs_dirty_inode(struct inode *inode)
1122 struct btrfs_root *root = BTRFS_I(inode)->root;
1123 struct btrfs_trans_handle *trans;
1125 mutex_lock(&root->fs_info->fs_mutex);
1126 trans = btrfs_start_transaction(root, 1);
1127 btrfs_set_trans_block_group(trans, inode);
1128 btrfs_update_inode(trans, root, inode);
1129 btrfs_end_transaction(trans, root);
1130 mutex_unlock(&root->fs_info->fs_mutex);
1133 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1134 struct btrfs_root *root,
1136 struct btrfs_block_group_cache *group,
1139 struct inode *inode;
1140 struct btrfs_inode_item *inode_item;
1141 struct btrfs_key *location;
1142 struct btrfs_path *path;
1146 path = btrfs_alloc_path();
1149 inode = new_inode(root->fs_info->sb);
1151 return ERR_PTR(-ENOMEM);
1153 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1154 inode->i_mapping, GFP_NOFS);
1155 BTRFS_I(inode)->root = root;
1161 group = btrfs_find_block_group(root, group, 0, 0, owner);
1162 BTRFS_I(inode)->block_group = group;
1164 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1168 inode->i_uid = current->fsuid;
1169 inode->i_gid = current->fsgid;
1170 inode->i_mode = mode;
1171 inode->i_ino = objectid;
1172 inode->i_blocks = 0;
1173 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1174 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1175 struct btrfs_inode_item);
1176 fill_inode_item(path->nodes[0], inode_item, inode);
1177 btrfs_mark_buffer_dirty(path->nodes[0]);
1178 btrfs_free_path(path);
1180 location = &BTRFS_I(inode)->location;
1181 location->objectid = objectid;
1182 location->offset = 0;
1183 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1185 insert_inode_hash(inode);
1188 btrfs_free_path(path);
1189 return ERR_PTR(ret);
1192 static inline u8 btrfs_inode_type(struct inode *inode)
1194 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1197 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1198 struct dentry *dentry, struct inode *inode)
1201 struct btrfs_key key;
1202 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1203 struct inode *parent_inode;
1205 key.objectid = inode->i_ino;
1206 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1209 ret = btrfs_insert_dir_item(trans, root,
1210 dentry->d_name.name, dentry->d_name.len,
1211 dentry->d_parent->d_inode->i_ino,
1212 &key, btrfs_inode_type(inode));
1214 parent_inode = dentry->d_parent->d_inode;
1215 parent_inode->i_size += dentry->d_name.len * 2;
1216 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1217 ret = btrfs_update_inode(trans, root,
1218 dentry->d_parent->d_inode);
1223 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1224 struct dentry *dentry, struct inode *inode)
1226 int err = btrfs_add_link(trans, dentry, inode);
1228 d_instantiate(dentry, inode);
1236 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1237 int mode, dev_t rdev)
1239 struct btrfs_trans_handle *trans;
1240 struct btrfs_root *root = BTRFS_I(dir)->root;
1241 struct inode *inode;
1247 if (!new_valid_dev(rdev))
1250 mutex_lock(&root->fs_info->fs_mutex);
1251 trans = btrfs_start_transaction(root, 1);
1252 btrfs_set_trans_block_group(trans, dir);
1254 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1260 inode = btrfs_new_inode(trans, root, objectid,
1261 BTRFS_I(dir)->block_group, mode);
1262 err = PTR_ERR(inode);
1266 btrfs_set_trans_block_group(trans, inode);
1267 err = btrfs_add_nondir(trans, dentry, inode);
1271 inode->i_op = &btrfs_special_inode_operations;
1272 init_special_inode(inode, inode->i_mode, rdev);
1273 btrfs_update_inode(trans, root, inode);
1275 dir->i_sb->s_dirt = 1;
1276 btrfs_update_inode_block_group(trans, inode);
1277 btrfs_update_inode_block_group(trans, dir);
1279 nr = trans->blocks_used;
1280 btrfs_end_transaction(trans, root);
1281 mutex_unlock(&root->fs_info->fs_mutex);
1284 inode_dec_link_count(inode);
1287 btrfs_btree_balance_dirty(root, nr);
1291 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1292 int mode, struct nameidata *nd)
1294 struct btrfs_trans_handle *trans;
1295 struct btrfs_root *root = BTRFS_I(dir)->root;
1296 struct inode *inode;
1302 mutex_lock(&root->fs_info->fs_mutex);
1303 trans = btrfs_start_transaction(root, 1);
1304 btrfs_set_trans_block_group(trans, dir);
1306 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1312 inode = btrfs_new_inode(trans, root, objectid,
1313 BTRFS_I(dir)->block_group, mode);
1314 err = PTR_ERR(inode);
1318 btrfs_set_trans_block_group(trans, inode);
1319 err = btrfs_add_nondir(trans, dentry, inode);
1323 inode->i_mapping->a_ops = &btrfs_aops;
1324 inode->i_fop = &btrfs_file_operations;
1325 inode->i_op = &btrfs_file_inode_operations;
1326 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1327 inode->i_mapping, GFP_NOFS);
1328 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
1330 dir->i_sb->s_dirt = 1;
1331 btrfs_update_inode_block_group(trans, inode);
1332 btrfs_update_inode_block_group(trans, dir);
1334 nr = trans->blocks_used;
1335 btrfs_end_transaction(trans, root);
1336 mutex_unlock(&root->fs_info->fs_mutex);
1339 inode_dec_link_count(inode);
1342 btrfs_btree_balance_dirty(root, nr);
1346 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1347 struct dentry *dentry)
1349 struct btrfs_trans_handle *trans;
1350 struct btrfs_root *root = BTRFS_I(dir)->root;
1351 struct inode *inode = old_dentry->d_inode;
1356 if (inode->i_nlink == 0)
1360 mutex_lock(&root->fs_info->fs_mutex);
1361 trans = btrfs_start_transaction(root, 1);
1363 btrfs_set_trans_block_group(trans, dir);
1364 atomic_inc(&inode->i_count);
1365 err = btrfs_add_nondir(trans, dentry, inode);
1370 dir->i_sb->s_dirt = 1;
1371 btrfs_update_inode_block_group(trans, dir);
1372 err = btrfs_update_inode(trans, root, inode);
1377 nr = trans->blocks_used;
1378 btrfs_end_transaction(trans, root);
1379 mutex_unlock(&root->fs_info->fs_mutex);
1382 inode_dec_link_count(inode);
1385 btrfs_btree_balance_dirty(root, nr);
1389 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
1390 struct btrfs_root *root,
1391 u64 objectid, u64 dirid)
1395 struct btrfs_key key;
1400 key.objectid = objectid;
1402 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1404 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
1405 &key, BTRFS_FT_DIR);
1409 key.objectid = dirid;
1410 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
1411 &key, BTRFS_FT_DIR);
1418 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1420 struct inode *inode;
1421 struct btrfs_trans_handle *trans;
1422 struct btrfs_root *root = BTRFS_I(dir)->root;
1424 int drop_on_err = 0;
1426 unsigned long nr = 1;
1428 mutex_lock(&root->fs_info->fs_mutex);
1429 trans = btrfs_start_transaction(root, 1);
1430 btrfs_set_trans_block_group(trans, dir);
1432 if (IS_ERR(trans)) {
1433 err = PTR_ERR(trans);
1437 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1443 inode = btrfs_new_inode(trans, root, objectid,
1444 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1445 if (IS_ERR(inode)) {
1446 err = PTR_ERR(inode);
1451 inode->i_op = &btrfs_dir_inode_operations;
1452 inode->i_fop = &btrfs_dir_file_operations;
1453 btrfs_set_trans_block_group(trans, inode);
1455 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1460 err = btrfs_update_inode(trans, root, inode);
1464 err = btrfs_add_link(trans, dentry, inode);
1468 d_instantiate(dentry, inode);
1470 dir->i_sb->s_dirt = 1;
1471 btrfs_update_inode_block_group(trans, inode);
1472 btrfs_update_inode_block_group(trans, dir);
1475 nr = trans->blocks_used;
1476 btrfs_end_transaction(trans, root);
1479 mutex_unlock(&root->fs_info->fs_mutex);
1482 btrfs_btree_balance_dirty(root, nr);
1486 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1487 size_t page_offset, u64 start, u64 end,
1493 u64 extent_start = 0;
1495 u64 objectid = inode->i_ino;
1497 int failed_insert = 0;
1498 struct btrfs_path *path;
1499 struct btrfs_root *root = BTRFS_I(inode)->root;
1500 struct btrfs_file_extent_item *item;
1501 struct extent_buffer *leaf;
1502 struct btrfs_key found_key;
1503 struct extent_map *em = NULL;
1504 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1505 struct btrfs_trans_handle *trans = NULL;
1507 path = btrfs_alloc_path();
1509 mutex_lock(&root->fs_info->fs_mutex);
1512 em = lookup_extent_mapping(em_tree, start, end);
1517 em = alloc_extent_map(GFP_NOFS);
1522 em->start = EXTENT_MAP_HOLE;
1523 em->end = EXTENT_MAP_HOLE;
1525 em->bdev = inode->i_sb->s_bdev;
1526 ret = btrfs_lookup_file_extent(NULL, root, path,
1527 objectid, start, 0);
1534 if (path->slots[0] == 0)
1539 leaf = path->nodes[0];
1540 item = btrfs_item_ptr(leaf, path->slots[0],
1541 struct btrfs_file_extent_item);
1542 /* are we inside the extent that was found? */
1543 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1544 found_type = btrfs_key_type(&found_key);
1545 if (found_key.objectid != objectid ||
1546 found_type != BTRFS_EXTENT_DATA_KEY) {
1550 found_type = btrfs_file_extent_type(leaf, item);
1551 extent_start = found_key.offset;
1552 if (found_type == BTRFS_FILE_EXTENT_REG) {
1553 extent_end = extent_start +
1554 btrfs_file_extent_num_bytes(leaf, item);
1556 if (start < extent_start || start >= extent_end) {
1558 if (start < extent_start) {
1559 if (end < extent_start)
1561 em->end = extent_end - 1;
1567 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1569 em->start = extent_start;
1570 em->end = extent_end - 1;
1571 em->block_start = EXTENT_MAP_HOLE;
1572 em->block_end = EXTENT_MAP_HOLE;
1575 bytenr += btrfs_file_extent_offset(leaf, item);
1576 em->block_start = bytenr;
1577 em->block_end = em->block_start +
1578 btrfs_file_extent_num_bytes(leaf, item) - 1;
1579 em->start = extent_start;
1580 em->end = extent_end - 1;
1582 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1586 size_t extent_offset;
1589 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1592 extent_end = (extent_start + size) |
1593 ((u64)root->sectorsize - 1);
1594 if (start < extent_start || start >= extent_end) {
1596 if (start < extent_start) {
1597 if (end < extent_start)
1599 em->end = extent_end;
1606 extent_offset = (page->index << PAGE_CACHE_SHIFT) -
1608 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
1610 copy_size = min(PAGE_CACHE_SIZE - page_offset,
1611 size - extent_offset);
1613 em->block_start = EXTENT_MAP_INLINE;
1614 em->block_end = EXTENT_MAP_INLINE;
1615 em->start = extent_start + extent_offset;
1616 em->end = (em->start + copy_size -1) |
1617 ((u64)root->sectorsize -1);
1623 read_extent_buffer(leaf, map + page_offset, ptr, copy_size);
1625 memset(map + page_offset + copy_size, 0,
1626 PAGE_CACHE_SIZE - copy_size - page_offset);
1628 flush_dcache_page(page);
1630 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
1633 printk("unkknown found_type %d\n", found_type);
1640 em->block_start = EXTENT_MAP_HOLE;
1641 em->block_end = EXTENT_MAP_HOLE;
1643 btrfs_release_path(root, path);
1644 if (em->start > start || em->end < start) {
1645 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
1649 ret = add_extent_mapping(em_tree, em);
1650 if (ret == -EEXIST) {
1651 free_extent_map(em);
1654 if (failed_insert > 5) {
1655 printk("failing to insert %Lu %Lu\n", start, end);
1663 btrfs_free_path(path);
1665 ret = btrfs_end_transaction(trans, root);
1669 mutex_unlock(&root->fs_info->fs_mutex);
1671 free_extent_map(em);
1673 return ERR_PTR(err);
1678 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
1680 return extent_bmap(mapping, iblock, btrfs_get_extent);
1683 static int btrfs_prepare_write(struct file *file, struct page *page,
1684 unsigned from, unsigned to)
1686 return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree,
1687 page->mapping->host, page, from, to,
1691 int btrfs_readpage(struct file *file, struct page *page)
1693 struct extent_map_tree *tree;
1694 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1695 return extent_read_full_page(tree, page, btrfs_get_extent);
1697 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1699 struct extent_map_tree *tree;
1702 if (current->flags & PF_MEMALLOC) {
1703 redirty_page_for_writepage(wbc, page);
1707 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1708 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
1711 static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
1713 struct extent_map_tree *tree;
1716 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1717 ret = try_release_extent_mapping(tree, page);
1719 ClearPagePrivate(page);
1720 set_page_private(page, 0);
1721 page_cache_release(page);
1726 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
1728 struct extent_map_tree *tree;
1730 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1731 extent_invalidatepage(tree, page, offset);
1732 btrfs_releasepage(page, GFP_NOFS);
1736 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1737 * called from a page fault handler when a page is first dirtied. Hence we must
1738 * be careful to check for EOF conditions here. We set the page up correctly
1739 * for a written page which means we get ENOSPC checking when writing into
1740 * holes and correct delalloc and unwritten extent mapping on filesystems that
1741 * support these features.
1743 * We are not allowed to take the i_mutex here so we have to play games to
1744 * protect against truncate races as the page could now be beyond EOF. Because
1745 * vmtruncate() writes the inode size before removing pages, once we have the
1746 * page lock we can determine safely if the page is beyond EOF. If it is not
1747 * beyond EOF, then the page is guaranteed safe against truncation until we
1750 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1752 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1758 down_read(&BTRFS_I(inode)->root->snap_sem);
1760 wait_on_page_writeback(page);
1761 size = i_size_read(inode);
1762 page_start = page->index << PAGE_CACHE_SHIFT;
1764 if ((page->mapping != inode->i_mapping) ||
1765 (page_start > size)) {
1766 /* page got truncated out from underneath us */
1770 /* page is wholly or partially inside EOF */
1771 if (page_start + PAGE_CACHE_SIZE > size)
1772 end = size & ~PAGE_CACHE_MASK;
1774 end = PAGE_CACHE_SIZE;
1776 ret = btrfs_cow_one_page(inode, page, end);
1779 up_read(&BTRFS_I(inode)->root->snap_sem);
1784 static void btrfs_truncate(struct inode *inode)
1786 struct btrfs_root *root = BTRFS_I(inode)->root;
1788 struct btrfs_trans_handle *trans;
1791 if (!S_ISREG(inode->i_mode))
1793 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1796 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1798 mutex_lock(&root->fs_info->fs_mutex);
1799 trans = btrfs_start_transaction(root, 1);
1800 btrfs_set_trans_block_group(trans, inode);
1802 /* FIXME, add redo link to tree so we don't leak on crash */
1803 ret = btrfs_truncate_in_trans(trans, root, inode);
1804 btrfs_update_inode(trans, root, inode);
1805 nr = trans->blocks_used;
1807 ret = btrfs_end_transaction(trans, root);
1809 mutex_unlock(&root->fs_info->fs_mutex);
1810 btrfs_btree_balance_dirty(root, nr);
1813 int btrfs_commit_write(struct file *file, struct page *page,
1814 unsigned from, unsigned to)
1816 return extent_commit_write(&BTRFS_I(page->mapping->host)->extent_tree,
1817 page->mapping->host, page, from, to);
1820 static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1822 struct btrfs_trans_handle *trans;
1823 struct btrfs_key key;
1824 struct btrfs_root_item root_item;
1825 struct btrfs_inode_item *inode_item;
1826 struct extent_buffer *leaf;
1827 struct btrfs_root *new_root;
1828 struct inode *inode;
1833 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
1834 unsigned long nr = 1;
1836 mutex_lock(&root->fs_info->fs_mutex);
1837 trans = btrfs_start_transaction(root, 1);
1840 leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0, 0);
1842 return PTR_ERR(leaf);
1844 btrfs_set_header_nritems(leaf, 0);
1845 btrfs_set_header_level(leaf, 0);
1846 btrfs_set_header_bytenr(leaf, leaf->start);
1847 btrfs_set_header_generation(leaf, trans->transid);
1848 btrfs_set_header_owner(leaf, root->root_key.objectid);
1849 write_extent_buffer(leaf, root->fs_info->fsid,
1850 (unsigned long)btrfs_header_fsid(leaf),
1852 btrfs_mark_buffer_dirty(leaf);
1854 inode_item = &root_item.inode;
1855 memset(inode_item, 0, sizeof(*inode_item));
1856 inode_item->generation = cpu_to_le64(1);
1857 inode_item->size = cpu_to_le64(3);
1858 inode_item->nlink = cpu_to_le32(1);
1859 inode_item->nblocks = cpu_to_le64(1);
1860 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
1862 btrfs_set_root_bytenr(&root_item, leaf->start);
1863 btrfs_set_root_level(&root_item, 0);
1864 btrfs_set_root_refs(&root_item, 1);
1865 btrfs_set_root_used(&root_item, 0);
1867 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
1868 root_item.drop_level = 0;
1870 free_extent_buffer(leaf);
1873 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1878 btrfs_set_root_dirid(&root_item, new_dirid);
1880 key.objectid = objectid;
1882 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1883 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1889 * insert the directory item
1891 key.offset = (u64)-1;
1892 dir = root->fs_info->sb->s_root->d_inode;
1893 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1894 name, namelen, dir->i_ino, &key,
1899 ret = btrfs_commit_transaction(trans, root);
1903 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
1906 trans = btrfs_start_transaction(new_root, 1);
1909 inode = btrfs_new_inode(trans, new_root, new_dirid,
1910 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
1913 inode->i_op = &btrfs_dir_inode_operations;
1914 inode->i_fop = &btrfs_dir_file_operations;
1915 new_root->inode = inode;
1917 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
1923 ret = btrfs_update_inode(trans, new_root, inode);
1927 nr = trans->blocks_used;
1928 err = btrfs_commit_transaction(trans, root);
1932 mutex_unlock(&root->fs_info->fs_mutex);
1933 btrfs_btree_balance_dirty(root, nr);
1937 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
1939 struct btrfs_trans_handle *trans;
1940 struct btrfs_key key;
1941 struct btrfs_root_item new_root_item;
1942 struct extent_buffer *tmp;
1948 if (!root->ref_cows)
1951 down_write(&root->snap_sem);
1952 freeze_bdev(root->fs_info->sb->s_bdev);
1953 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
1955 mutex_lock(&root->fs_info->fs_mutex);
1956 trans = btrfs_start_transaction(root, 1);
1959 ret = btrfs_update_inode(trans, root, root->inode);
1963 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1968 memcpy(&new_root_item, &root->root_item,
1969 sizeof(new_root_item));
1971 key.objectid = objectid;
1973 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1975 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
1976 btrfs_set_root_bytenr(&new_root_item, root->node->start);
1977 btrfs_set_root_level(&new_root_item, btrfs_header_level(root->node));
1979 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1985 * insert the directory item
1987 key.offset = (u64)-1;
1988 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1990 root->fs_info->sb->s_root->d_inode->i_ino,
1991 &key, BTRFS_FT_DIR);
1996 ret = btrfs_inc_root_ref(trans, root);
2000 nr = trans->blocks_used;
2001 err = btrfs_commit_transaction(trans, root);
2006 mutex_unlock(&root->fs_info->fs_mutex);
2007 up_write(&root->snap_sem);
2008 btrfs_btree_balance_dirty(root, nr);
2012 static unsigned long force_ra(struct address_space *mapping,
2013 struct file_ra_state *ra, struct file *file,
2014 pgoff_t offset, pgoff_t last_index)
2018 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2019 req_size = last_index - offset + 1;
2020 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2023 req_size = min(last_index - offset + 1, (pgoff_t)128);
2024 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2025 return offset + req_size;
2029 int btrfs_defrag_file(struct file *file) {
2030 struct inode *inode = file->f_path.dentry->d_inode;
2031 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2033 unsigned long last_index;
2034 unsigned long ra_index = 0;
2039 mutex_lock(&inode->i_mutex);
2040 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2041 for (i = 0; i <= last_index; i++) {
2042 if (i == ra_index) {
2043 ra_index = force_ra(inode->i_mapping, &file->f_ra,
2044 file, ra_index, last_index);
2046 page = grab_cache_page(inode->i_mapping, i);
2049 if (!PageUptodate(page)) {
2050 btrfs_readpage(NULL, page);
2052 if (!PageUptodate(page)) {
2054 page_cache_release(page);
2058 page_start = page->index << PAGE_CACHE_SHIFT;
2059 page_end = page_start + PAGE_CACHE_SIZE - 1;
2061 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2062 set_extent_delalloc(em_tree, page_start,
2063 page_end, GFP_NOFS);
2064 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2065 set_page_dirty(page);
2067 page_cache_release(page);
2068 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2072 mutex_unlock(&inode->i_mutex);
2076 static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg)
2078 struct btrfs_ioctl_vol_args vol_args;
2079 struct btrfs_dir_item *di;
2080 struct btrfs_path *path;
2084 if (copy_from_user(&vol_args, arg, sizeof(vol_args)))
2087 namelen = strlen(vol_args.name);
2088 if (namelen > BTRFS_VOL_NAME_MAX)
2090 if (strchr(vol_args.name, '/'))
2093 path = btrfs_alloc_path();
2097 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2098 mutex_lock(&root->fs_info->fs_mutex);
2099 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2101 vol_args.name, namelen, 0);
2102 mutex_unlock(&root->fs_info->fs_mutex);
2103 btrfs_free_path(path);
2104 if (di && !IS_ERR(di))
2109 if (root == root->fs_info->tree_root)
2110 return create_subvol(root, vol_args.name, namelen);
2111 return create_snapshot(root, vol_args.name, namelen);
2114 static int btrfs_ioctl_defrag(struct file *file)
2116 struct inode *inode = file->f_path.dentry->d_inode;
2117 struct btrfs_root *root = BTRFS_I(inode)->root;
2119 switch (inode->i_mode & S_IFMT) {
2121 mutex_lock(&root->fs_info->fs_mutex);
2122 btrfs_defrag_root(root, 0);
2123 btrfs_defrag_root(root->fs_info->extent_root, 0);
2124 mutex_unlock(&root->fs_info->fs_mutex);
2127 btrfs_defrag_file(file);
2134 long btrfs_ioctl(struct file *file, unsigned int
2135 cmd, unsigned long arg)
2137 struct btrfs_root *root = BTRFS_I(file->f_path.dentry->d_inode)->root;
2140 case BTRFS_IOC_SNAP_CREATE:
2141 return btrfs_ioctl_snap_create(root, (void __user *)arg);
2142 case BTRFS_IOC_DEFRAG:
2143 return btrfs_ioctl_defrag(file);
2150 * Called inside transaction, so use GFP_NOFS
2152 struct inode *btrfs_alloc_inode(struct super_block *sb)
2154 struct btrfs_inode *ei;
2156 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2160 return &ei->vfs_inode;
2163 void btrfs_destroy_inode(struct inode *inode)
2165 WARN_ON(!list_empty(&inode->i_dentry));
2166 WARN_ON(inode->i_data.nrpages);
2168 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2171 static void init_once(void * foo, struct kmem_cache * cachep,
2172 unsigned long flags)
2174 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2176 inode_init_once(&ei->vfs_inode);
2179 void btrfs_destroy_cachep(void)
2181 if (btrfs_inode_cachep)
2182 kmem_cache_destroy(btrfs_inode_cachep);
2183 if (btrfs_trans_handle_cachep)
2184 kmem_cache_destroy(btrfs_trans_handle_cachep);
2185 if (btrfs_transaction_cachep)
2186 kmem_cache_destroy(btrfs_transaction_cachep);
2187 if (btrfs_bit_radix_cachep)
2188 kmem_cache_destroy(btrfs_bit_radix_cachep);
2189 if (btrfs_path_cachep)
2190 kmem_cache_destroy(btrfs_path_cachep);
2193 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
2194 unsigned long extra_flags,
2195 void (*ctor)(void *, struct kmem_cache *,
2198 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2199 SLAB_MEM_SPREAD | extra_flags), ctor
2200 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2206 int btrfs_init_cachep(void)
2208 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
2209 sizeof(struct btrfs_inode),
2211 if (!btrfs_inode_cachep)
2213 btrfs_trans_handle_cachep =
2214 btrfs_cache_create("btrfs_trans_handle_cache",
2215 sizeof(struct btrfs_trans_handle),
2217 if (!btrfs_trans_handle_cachep)
2219 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
2220 sizeof(struct btrfs_transaction),
2222 if (!btrfs_transaction_cachep)
2224 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
2225 sizeof(struct btrfs_path),
2227 if (!btrfs_path_cachep)
2229 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
2230 SLAB_DESTROY_BY_RCU, NULL);
2231 if (!btrfs_bit_radix_cachep)
2235 btrfs_destroy_cachep();
2239 static int btrfs_getattr(struct vfsmount *mnt,
2240 struct dentry *dentry, struct kstat *stat)
2242 struct inode *inode = dentry->d_inode;
2243 generic_fillattr(inode, stat);
2244 stat->blksize = 256 * 1024;
2248 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2249 struct inode * new_dir,struct dentry *new_dentry)
2251 struct btrfs_trans_handle *trans;
2252 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2253 struct inode *new_inode = new_dentry->d_inode;
2254 struct inode *old_inode = old_dentry->d_inode;
2255 struct timespec ctime = CURRENT_TIME;
2256 struct btrfs_path *path;
2257 struct btrfs_dir_item *di;
2260 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2261 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2265 mutex_lock(&root->fs_info->fs_mutex);
2266 trans = btrfs_start_transaction(root, 1);
2268 btrfs_set_trans_block_group(trans, new_dir);
2269 path = btrfs_alloc_path();
2275 old_dentry->d_inode->i_nlink++;
2276 old_dir->i_ctime = old_dir->i_mtime = ctime;
2277 new_dir->i_ctime = new_dir->i_mtime = ctime;
2278 old_inode->i_ctime = ctime;
2280 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2281 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
2282 struct btrfs_key old_parent_key;
2283 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2293 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &old_parent_key);
2294 ret = btrfs_del_item(trans, root, path);
2298 btrfs_release_path(root, path);
2300 di = btrfs_lookup_dir_index_item(trans, root, path,
2302 old_parent_key.objectid,
2312 ret = btrfs_del_item(trans, root, path);
2316 btrfs_release_path(root, path);
2318 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2319 old_inode->i_ino, location,
2326 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2331 new_inode->i_ctime = CURRENT_TIME;
2332 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2336 ret = btrfs_add_link(trans, new_dentry, old_inode);
2341 btrfs_free_path(path);
2342 btrfs_end_transaction(trans, root);
2343 mutex_unlock(&root->fs_info->fs_mutex);
2347 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2348 const char *symname)
2350 struct btrfs_trans_handle *trans;
2351 struct btrfs_root *root = BTRFS_I(dir)->root;
2352 struct btrfs_path *path;
2353 struct btrfs_key key;
2354 struct inode *inode;
2361 struct btrfs_file_extent_item *ei;
2362 struct extent_buffer *leaf;
2365 name_len = strlen(symname) + 1;
2366 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2367 return -ENAMETOOLONG;
2368 mutex_lock(&root->fs_info->fs_mutex);
2369 trans = btrfs_start_transaction(root, 1);
2370 btrfs_set_trans_block_group(trans, dir);
2372 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2378 inode = btrfs_new_inode(trans, root, objectid,
2379 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2380 err = PTR_ERR(inode);
2384 btrfs_set_trans_block_group(trans, inode);
2385 err = btrfs_add_nondir(trans, dentry, inode);
2389 inode->i_mapping->a_ops = &btrfs_aops;
2390 inode->i_fop = &btrfs_file_operations;
2391 inode->i_op = &btrfs_file_inode_operations;
2392 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2393 inode->i_mapping, GFP_NOFS);
2394 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
2396 dir->i_sb->s_dirt = 1;
2397 btrfs_update_inode_block_group(trans, inode);
2398 btrfs_update_inode_block_group(trans, dir);
2402 path = btrfs_alloc_path();
2404 key.objectid = inode->i_ino;
2406 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2407 datasize = btrfs_file_extent_calc_inline_size(name_len);
2408 err = btrfs_insert_empty_item(trans, root, path, &key,
2414 leaf = path->nodes[0];
2415 ei = btrfs_item_ptr(leaf, path->slots[0],
2416 struct btrfs_file_extent_item);
2417 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2418 btrfs_set_file_extent_type(leaf, ei,
2419 BTRFS_FILE_EXTENT_INLINE);
2420 ptr = btrfs_file_extent_inline_start(ei);
2421 write_extent_buffer(leaf, symname, ptr, name_len);
2422 btrfs_mark_buffer_dirty(leaf);
2423 btrfs_free_path(path);
2425 inode->i_op = &btrfs_symlink_inode_operations;
2426 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2427 inode->i_size = name_len - 1;
2428 err = btrfs_update_inode(trans, root, inode);
2433 nr = trans->blocks_used;
2434 btrfs_end_transaction(trans, root);
2435 mutex_unlock(&root->fs_info->fs_mutex);
2437 inode_dec_link_count(inode);
2440 btrfs_btree_balance_dirty(root, nr);
2444 static struct inode_operations btrfs_dir_inode_operations = {
2445 .lookup = btrfs_lookup,
2446 .create = btrfs_create,
2447 .unlink = btrfs_unlink,
2449 .mkdir = btrfs_mkdir,
2450 .rmdir = btrfs_rmdir,
2451 .rename = btrfs_rename,
2452 .symlink = btrfs_symlink,
2453 .setattr = btrfs_setattr,
2454 .mknod = btrfs_mknod,
2457 static struct inode_operations btrfs_dir_ro_inode_operations = {
2458 .lookup = btrfs_lookup,
2461 static struct file_operations btrfs_dir_file_operations = {
2462 .llseek = generic_file_llseek,
2463 .read = generic_read_dir,
2464 .readdir = btrfs_readdir,
2465 .unlocked_ioctl = btrfs_ioctl,
2466 #ifdef CONFIG_COMPAT
2467 .compat_ioctl = btrfs_ioctl,
2471 static struct extent_map_ops btrfs_extent_map_ops = {
2472 .fill_delalloc = run_delalloc_range,
2473 .writepage_io_hook = btrfs_writepage_io_hook,
2474 .readpage_io_hook = btrfs_readpage_io_hook,
2475 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2478 static struct address_space_operations btrfs_aops = {
2479 .readpage = btrfs_readpage,
2480 .writepage = btrfs_writepage,
2481 .sync_page = block_sync_page,
2482 .prepare_write = btrfs_prepare_write,
2483 .commit_write = btrfs_commit_write,
2485 .invalidatepage = btrfs_invalidatepage,
2486 .releasepage = btrfs_releasepage,
2487 .set_page_dirty = __set_page_dirty_nobuffers,
2490 static struct address_space_operations btrfs_symlink_aops = {
2491 .readpage = btrfs_readpage,
2492 .writepage = btrfs_writepage,
2493 .invalidatepage = btrfs_invalidatepage,
2494 .releasepage = btrfs_releasepage,
2497 static struct inode_operations btrfs_file_inode_operations = {
2498 .truncate = btrfs_truncate,
2499 .getattr = btrfs_getattr,
2500 .setattr = btrfs_setattr,
2503 static struct inode_operations btrfs_special_inode_operations = {
2504 .getattr = btrfs_getattr,
2505 .setattr = btrfs_setattr,
2508 static struct inode_operations btrfs_symlink_inode_operations = {
2509 .readlink = generic_readlink,
2510 .follow_link = page_follow_link_light,
2511 .put_link = page_put_link,