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
;
56 static struct kmem_cache
*btrfs_inode_cachep
;
57 struct kmem_cache
*btrfs_trans_handle_cachep
;
58 struct kmem_cache
*btrfs_transaction_cachep
;
59 struct kmem_cache
*btrfs_bit_radix_cachep
;
60 struct kmem_cache
*btrfs_path_cachep
;
63 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
64 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
65 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
66 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
67 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
68 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
69 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
70 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
73 void btrfs_read_locked_inode(struct inode
*inode
)
75 struct btrfs_path
*path
;
76 struct btrfs_inode_item
*inode_item
;
77 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
78 struct btrfs_key location
;
79 u64 alloc_group_block
;
83 path
= btrfs_alloc_path();
85 mutex_lock(&root
->fs_info
->fs_mutex
);
87 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
88 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
90 btrfs_free_path(path
);
93 inode_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
95 struct btrfs_inode_item
);
97 inode
->i_mode
= btrfs_inode_mode(inode_item
);
98 inode
->i_nlink
= btrfs_inode_nlink(inode_item
);
99 inode
->i_uid
= btrfs_inode_uid(inode_item
);
100 inode
->i_gid
= btrfs_inode_gid(inode_item
);
101 inode
->i_size
= btrfs_inode_size(inode_item
);
102 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(&inode_item
->atime
);
103 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->atime
);
104 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(&inode_item
->mtime
);
105 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->mtime
);
106 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(&inode_item
->ctime
);
107 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->ctime
);
108 inode
->i_blocks
= btrfs_inode_nblocks(inode_item
);
109 inode
->i_generation
= btrfs_inode_generation(inode_item
);
111 rdev
= btrfs_inode_rdev(inode_item
);
112 alloc_group_block
= btrfs_inode_block_group(inode_item
);
113 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
116 btrfs_free_path(path
);
119 mutex_unlock(&root
->fs_info
->fs_mutex
);
121 switch (inode
->i_mode
& S_IFMT
) {
123 inode
->i_mapping
->a_ops
= &btrfs_aops
;
124 inode
->i_fop
= &btrfs_file_operations
;
125 inode
->i_op
= &btrfs_file_inode_operations
;
128 inode
->i_fop
= &btrfs_dir_file_operations
;
129 if (root
== root
->fs_info
->tree_root
)
130 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
132 inode
->i_op
= &btrfs_dir_inode_operations
;
135 inode
->i_op
= &btrfs_symlink_inode_operations
;
136 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
139 init_special_inode(inode
, inode
->i_mode
, rdev
);
145 btrfs_release_path(root
, path
);
146 btrfs_free_path(path
);
147 mutex_unlock(&root
->fs_info
->fs_mutex
);
148 make_bad_inode(inode
);
151 static void fill_inode_item(struct btrfs_inode_item
*item
,
154 btrfs_set_inode_uid(item
, inode
->i_uid
);
155 btrfs_set_inode_gid(item
, inode
->i_gid
);
156 btrfs_set_inode_size(item
, inode
->i_size
);
157 btrfs_set_inode_mode(item
, inode
->i_mode
);
158 btrfs_set_inode_nlink(item
, inode
->i_nlink
);
159 btrfs_set_timespec_sec(&item
->atime
, inode
->i_atime
.tv_sec
);
160 btrfs_set_timespec_nsec(&item
->atime
, inode
->i_atime
.tv_nsec
);
161 btrfs_set_timespec_sec(&item
->mtime
, inode
->i_mtime
.tv_sec
);
162 btrfs_set_timespec_nsec(&item
->mtime
, inode
->i_mtime
.tv_nsec
);
163 btrfs_set_timespec_sec(&item
->ctime
, inode
->i_ctime
.tv_sec
);
164 btrfs_set_timespec_nsec(&item
->ctime
, inode
->i_ctime
.tv_nsec
);
165 btrfs_set_inode_nblocks(item
, inode
->i_blocks
);
166 btrfs_set_inode_generation(item
, inode
->i_generation
);
167 btrfs_set_inode_rdev(item
, inode
->i_rdev
);
168 btrfs_set_inode_block_group(item
,
169 BTRFS_I(inode
)->block_group
->key
.objectid
);
172 static int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
173 struct btrfs_root
*root
,
176 struct btrfs_inode_item
*inode_item
;
177 struct btrfs_path
*path
;
180 path
= btrfs_alloc_path();
182 ret
= btrfs_lookup_inode(trans
, root
, path
,
183 &BTRFS_I(inode
)->location
, 1);
190 inode_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
192 struct btrfs_inode_item
);
194 fill_inode_item(inode_item
, inode
);
195 btrfs_mark_buffer_dirty(path
->nodes
[0]);
196 btrfs_set_inode_last_trans(trans
, inode
);
199 btrfs_release_path(root
, path
);
200 btrfs_free_path(path
);
205 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
206 struct btrfs_root
*root
,
208 struct dentry
*dentry
)
210 struct btrfs_path
*path
;
211 const char *name
= dentry
->d_name
.name
;
212 int name_len
= dentry
->d_name
.len
;
215 struct btrfs_dir_item
*di
;
217 path
= btrfs_alloc_path();
223 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
233 objectid
= btrfs_disk_key_objectid(&di
->location
);
234 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
237 btrfs_release_path(root
, path
);
239 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
240 objectid
, name
, name_len
, -1);
249 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
251 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
253 btrfs_free_path(path
);
255 dir
->i_size
-= name_len
* 2;
256 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
257 btrfs_update_inode(trans
, root
, dir
);
258 drop_nlink(dentry
->d_inode
);
259 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
260 dir
->i_sb
->s_dirt
= 1;
265 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
267 struct btrfs_root
*root
;
268 struct btrfs_trans_handle
*trans
;
271 root
= BTRFS_I(dir
)->root
;
272 mutex_lock(&root
->fs_info
->fs_mutex
);
273 trans
= btrfs_start_transaction(root
, 1);
274 btrfs_set_trans_block_group(trans
, dir
);
275 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
276 btrfs_end_transaction(trans
, root
);
277 mutex_unlock(&root
->fs_info
->fs_mutex
);
278 btrfs_btree_balance_dirty(root
);
282 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
284 struct inode
*inode
= dentry
->d_inode
;
287 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
288 struct btrfs_path
*path
;
289 struct btrfs_key key
;
290 struct btrfs_trans_handle
*trans
;
291 struct btrfs_key found_key
;
293 struct btrfs_leaf
*leaf
;
294 char *goodnames
= "..";
296 path
= btrfs_alloc_path();
298 mutex_lock(&root
->fs_info
->fs_mutex
);
299 trans
= btrfs_start_transaction(root
, 1);
300 btrfs_set_trans_block_group(trans
, dir
);
301 key
.objectid
= inode
->i_ino
;
302 key
.offset
= (u64
)-1;
305 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
311 if (path
->slots
[0] == 0) {
316 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
317 btrfs_disk_key_to_cpu(&found_key
,
318 &leaf
->items
[path
->slots
[0]].key
);
319 found_type
= btrfs_key_type(&found_key
);
320 if (found_key
.objectid
!= inode
->i_ino
) {
324 if ((found_type
!= BTRFS_DIR_ITEM_KEY
&&
325 found_type
!= BTRFS_DIR_INDEX_KEY
) ||
326 (!btrfs_match_dir_item_name(root
, path
, goodnames
, 2) &&
327 !btrfs_match_dir_item_name(root
, path
, goodnames
, 1))) {
331 ret
= btrfs_del_item(trans
, root
, path
);
334 if (found_type
== BTRFS_DIR_ITEM_KEY
&& found_key
.offset
== 1)
336 btrfs_release_path(root
, path
);
339 btrfs_release_path(root
, path
);
341 /* now the directory is empty */
342 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
347 btrfs_release_path(root
, path
);
348 btrfs_free_path(path
);
349 mutex_unlock(&root
->fs_info
->fs_mutex
);
350 ret
= btrfs_end_transaction(trans
, root
);
351 btrfs_btree_balance_dirty(root
);
357 static int btrfs_free_inode(struct btrfs_trans_handle
*trans
,
358 struct btrfs_root
*root
,
361 struct btrfs_path
*path
;
366 path
= btrfs_alloc_path();
368 ret
= btrfs_lookup_inode(trans
, root
, path
,
369 &BTRFS_I(inode
)->location
, -1);
373 ret
= btrfs_del_item(trans
, root
, path
);
374 btrfs_free_path(path
);
379 * this can truncate away extent items, csum items and directory items.
380 * It starts at a high offset and removes keys until it can't find
381 * any higher than i_size.
383 * csum items that cross the new i_size are truncated to the new size
386 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
387 struct btrfs_root
*root
,
391 struct btrfs_path
*path
;
392 struct btrfs_key key
;
393 struct btrfs_disk_key
*found_key
;
395 struct btrfs_leaf
*leaf
;
396 struct btrfs_file_extent_item
*fi
;
397 u64 extent_start
= 0;
398 u64 extent_num_blocks
= 0;
403 path
= btrfs_alloc_path();
406 /* FIXME, add redo link to tree so we don't leak on crash */
407 key
.objectid
= inode
->i_ino
;
408 key
.offset
= (u64
)-1;
411 btrfs_init_path(path
);
413 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
418 BUG_ON(path
->slots
[0] == 0);
421 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
422 found_key
= &leaf
->items
[path
->slots
[0]].key
;
423 found_type
= btrfs_disk_key_type(found_key
);
425 if (btrfs_disk_key_objectid(found_key
) != inode
->i_ino
)
427 if (found_type
!= BTRFS_CSUM_ITEM_KEY
&&
428 found_type
!= BTRFS_DIR_ITEM_KEY
&&
429 found_type
!= BTRFS_DIR_INDEX_KEY
&&
430 found_type
!= BTRFS_EXTENT_DATA_KEY
)
433 item_end
= btrfs_disk_key_offset(found_key
);
434 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
435 fi
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
437 struct btrfs_file_extent_item
);
438 if (btrfs_file_extent_type(fi
) !=
439 BTRFS_FILE_EXTENT_INLINE
) {
440 item_end
+= btrfs_file_extent_num_blocks(fi
) <<
444 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
445 ret
= btrfs_csum_truncate(trans
, root
, path
,
449 if (item_end
< inode
->i_size
) {
451 btrfs_set_key_type(&key
, found_type
- 1);
456 if (btrfs_disk_key_offset(found_key
) >= inode
->i_size
)
462 /* FIXME, shrink the extent if the ref count is only 1 */
463 if (found_type
== BTRFS_EXTENT_DATA_KEY
&&
464 btrfs_file_extent_type(fi
) !=
465 BTRFS_FILE_EXTENT_INLINE
) {
468 u64 orig_num_blocks
=
469 btrfs_file_extent_num_blocks(fi
);
470 extent_num_blocks
= inode
->i_size
-
471 btrfs_disk_key_offset(found_key
) +
473 extent_num_blocks
>>= inode
->i_blkbits
;
474 btrfs_set_file_extent_num_blocks(fi
,
476 inode
->i_blocks
-= (orig_num_blocks
-
477 extent_num_blocks
) << 3;
478 btrfs_mark_buffer_dirty(path
->nodes
[0]);
481 btrfs_file_extent_disk_blocknr(fi
);
483 btrfs_file_extent_disk_num_blocks(fi
);
484 /* FIXME blocksize != 4096 */
485 num_dec
= btrfs_file_extent_num_blocks(fi
) << 3;
486 if (extent_start
!= 0) {
488 inode
->i_blocks
-= num_dec
;
493 ret
= btrfs_del_item(trans
, root
, path
);
499 btrfs_release_path(root
, path
);
501 ret
= btrfs_free_extent(trans
, root
, extent_start
,
502 extent_num_blocks
, 0);
508 btrfs_release_path(root
, path
);
509 btrfs_free_path(path
);
510 inode
->i_sb
->s_dirt
= 1;
515 * taken from block_truncate_page, but does cow as it zeros out
516 * any bytes left in the last page in the file.
518 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
520 struct inode
*inode
= mapping
->host
;
521 unsigned blocksize
= 1 << inode
->i_blkbits
;
522 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
523 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
527 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
529 struct btrfs_key ins
;
530 struct btrfs_trans_handle
*trans
;
532 if ((offset
& (blocksize
- 1)) == 0)
536 page
= grab_cache_page(mapping
, index
);
540 if (!PageUptodate(page
)) {
541 ret
= btrfs_readpage(NULL
, page
);
543 if (!PageUptodate(page
)) {
548 mutex_lock(&root
->fs_info
->fs_mutex
);
549 trans
= btrfs_start_transaction(root
, 1);
550 btrfs_set_trans_block_group(trans
, inode
);
552 ret
= btrfs_drop_extents(trans
, root
, inode
,
553 page
->index
<< PAGE_CACHE_SHIFT
,
554 (page
->index
+ 1) << PAGE_CACHE_SHIFT
,
558 ret
= btrfs_alloc_extent(trans
, root
, inode
->i_ino
, 1, 0,
559 alloc_hint
, (u64
)-1, &ins
, 1);
562 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
563 page
->index
<< PAGE_CACHE_SHIFT
,
567 SetPageChecked(page
);
569 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
570 flush_dcache_page(page
);
571 ret
= btrfs_csum_file_block(trans
, root
, inode
->i_ino
,
572 page
->index
<< PAGE_CACHE_SHIFT
,
573 kaddr
, PAGE_CACHE_SIZE
);
575 btrfs_end_transaction(trans
, root
);
576 mutex_unlock(&root
->fs_info
->fs_mutex
);
578 set_page_dirty(page
);
580 page_cache_release(page
);
585 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
587 struct inode
*inode
= dentry
->d_inode
;
590 err
= inode_change_ok(inode
, attr
);
594 if (S_ISREG(inode
->i_mode
) &&
595 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
596 struct btrfs_trans_handle
*trans
;
597 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
598 u64 mask
= root
->blocksize
- 1;
599 u64 pos
= (inode
->i_size
+ mask
) & ~mask
;
602 if (attr
->ia_size
<= pos
)
605 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
607 hole_size
= (attr
->ia_size
- pos
+ mask
) & ~mask
;
608 hole_size
>>= inode
->i_blkbits
;
610 mutex_lock(&root
->fs_info
->fs_mutex
);
611 trans
= btrfs_start_transaction(root
, 1);
612 btrfs_set_trans_block_group(trans
, inode
);
613 err
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
614 pos
, 0, 0, hole_size
);
615 btrfs_end_transaction(trans
, root
);
616 mutex_unlock(&root
->fs_info
->fs_mutex
);
621 err
= inode_setattr(inode
, attr
);
625 void btrfs_delete_inode(struct inode
*inode
)
627 struct btrfs_trans_handle
*trans
;
628 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
631 truncate_inode_pages(&inode
->i_data
, 0);
632 if (is_bad_inode(inode
)) {
636 mutex_lock(&root
->fs_info
->fs_mutex
);
637 trans
= btrfs_start_transaction(root
, 1);
638 btrfs_set_trans_block_group(trans
, inode
);
639 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
642 ret
= btrfs_free_inode(trans
, root
, inode
);
645 btrfs_end_transaction(trans
, root
);
646 mutex_unlock(&root
->fs_info
->fs_mutex
);
647 btrfs_btree_balance_dirty(root
);
651 btrfs_end_transaction(trans
, root
);
652 mutex_unlock(&root
->fs_info
->fs_mutex
);
653 btrfs_btree_balance_dirty(root
);
659 * this returns the key found in the dir entry in the location pointer.
660 * If no dir entries were found, location->objectid is 0.
662 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
663 struct btrfs_key
*location
)
665 const char *name
= dentry
->d_name
.name
;
666 int namelen
= dentry
->d_name
.len
;
667 struct btrfs_dir_item
*di
;
668 struct btrfs_path
*path
;
669 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
672 path
= btrfs_alloc_path();
674 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
676 if (!di
|| IS_ERR(di
)) {
677 location
->objectid
= 0;
681 btrfs_disk_key_to_cpu(location
, &di
->location
);
683 btrfs_release_path(root
, path
);
684 btrfs_free_path(path
);
689 * when we hit a tree root in a directory, the btrfs part of the inode
690 * needs to be changed to reflect the root directory of the tree root. This
691 * is kind of like crossing a mount point.
693 static int fixup_tree_root_location(struct btrfs_root
*root
,
694 struct btrfs_key
*location
,
695 struct btrfs_root
**sub_root
)
697 struct btrfs_path
*path
;
698 struct btrfs_root_item
*ri
;
700 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
702 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
705 path
= btrfs_alloc_path();
707 mutex_lock(&root
->fs_info
->fs_mutex
);
709 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
);
710 if (IS_ERR(*sub_root
))
711 return PTR_ERR(*sub_root
);
713 ri
= &(*sub_root
)->root_item
;
714 location
->objectid
= btrfs_root_dirid(ri
);
716 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
717 location
->offset
= 0;
719 btrfs_free_path(path
);
720 mutex_unlock(&root
->fs_info
->fs_mutex
);
724 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
726 struct btrfs_iget_args
*args
= p
;
727 inode
->i_ino
= args
->ino
;
728 BTRFS_I(inode
)->root
= args
->root
;
732 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
734 struct btrfs_iget_args
*args
= opaque
;
735 return (args
->ino
== inode
->i_ino
&&
736 args
->root
== BTRFS_I(inode
)->root
);
739 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
740 struct btrfs_root
*root
)
743 struct btrfs_iget_args args
;
747 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
748 btrfs_init_locked_inode
,
753 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
754 struct nameidata
*nd
)
756 struct inode
* inode
;
757 struct btrfs_inode
*bi
= BTRFS_I(dir
);
758 struct btrfs_root
*root
= bi
->root
;
759 struct btrfs_root
*sub_root
= root
;
760 struct btrfs_key location
;
763 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
764 return ERR_PTR(-ENAMETOOLONG
);
765 mutex_lock(&root
->fs_info
->fs_mutex
);
766 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
767 mutex_unlock(&root
->fs_info
->fs_mutex
);
771 if (location
.objectid
) {
772 ret
= fixup_tree_root_location(root
, &location
, &sub_root
);
776 return ERR_PTR(-ENOENT
);
777 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
780 return ERR_PTR(-EACCES
);
781 if (inode
->i_state
& I_NEW
) {
782 /* the inode and parent dir are two different roots */
783 if (sub_root
!= root
) {
785 sub_root
->inode
= inode
;
787 BTRFS_I(inode
)->root
= sub_root
;
788 memcpy(&BTRFS_I(inode
)->location
, &location
,
790 btrfs_read_locked_inode(inode
);
791 unlock_new_inode(inode
);
794 return d_splice_alias(inode
, dentry
);
797 static unsigned char btrfs_filetype_table
[] = {
798 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
801 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
803 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
804 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
805 struct btrfs_item
*item
;
806 struct btrfs_dir_item
*di
;
807 struct btrfs_key key
;
808 struct btrfs_path
*path
;
811 struct btrfs_leaf
*leaf
;
814 unsigned char d_type
;
819 int key_type
= BTRFS_DIR_INDEX_KEY
;
821 /* FIXME, use a real flag for deciding about the key type */
822 if (root
->fs_info
->tree_root
== root
)
823 key_type
= BTRFS_DIR_ITEM_KEY
;
824 mutex_lock(&root
->fs_info
->fs_mutex
);
825 key
.objectid
= inode
->i_ino
;
827 btrfs_set_key_type(&key
, key_type
);
828 key
.offset
= filp
->f_pos
;
829 path
= btrfs_alloc_path();
831 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
836 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
837 nritems
= btrfs_header_nritems(&leaf
->header
);
838 slot
= path
->slots
[0];
839 if (advance
|| slot
>= nritems
) {
840 if (slot
>= nritems
-1) {
841 ret
= btrfs_next_leaf(root
, path
);
844 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
845 nritems
= btrfs_header_nritems(&leaf
->header
);
846 slot
= path
->slots
[0];
853 item
= leaf
->items
+ slot
;
854 if (btrfs_disk_key_objectid(&item
->key
) != key
.objectid
)
856 if (btrfs_disk_key_type(&item
->key
) != key_type
)
858 if (btrfs_disk_key_offset(&item
->key
) < filp
->f_pos
)
860 filp
->f_pos
= btrfs_disk_key_offset(&item
->key
);
862 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
864 di_total
= btrfs_item_size(leaf
->items
+ slot
);
865 while(di_cur
< di_total
) {
866 d_type
= btrfs_filetype_table
[btrfs_dir_type(di
)];
867 over
= filldir(dirent
, (const char *)(di
+ 1),
868 btrfs_dir_name_len(di
),
869 btrfs_disk_key_offset(&item
->key
),
870 btrfs_disk_key_objectid(&di
->location
),
874 di_len
= btrfs_dir_name_len(di
) + sizeof(*di
);
876 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
883 btrfs_release_path(root
, path
);
884 btrfs_free_path(path
);
885 mutex_unlock(&root
->fs_info
->fs_mutex
);
889 int btrfs_write_inode(struct inode
*inode
, int wait
)
891 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
892 struct btrfs_trans_handle
*trans
;
896 mutex_lock(&root
->fs_info
->fs_mutex
);
897 trans
= btrfs_start_transaction(root
, 1);
898 btrfs_set_trans_block_group(trans
, inode
);
899 ret
= btrfs_commit_transaction(trans
, root
);
900 mutex_unlock(&root
->fs_info
->fs_mutex
);
906 * This is somewhat expensive, updating the tree every time the
907 * inode changes. But, it is most likely to find the inode in cache.
908 * FIXME, needs more benchmarking...there are no reasons other than performance
909 * to keep or drop this code.
911 void btrfs_dirty_inode(struct inode
*inode
)
913 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
914 struct btrfs_trans_handle
*trans
;
916 mutex_lock(&root
->fs_info
->fs_mutex
);
917 trans
= btrfs_start_transaction(root
, 1);
918 btrfs_set_trans_block_group(trans
, inode
);
919 btrfs_update_inode(trans
, root
, inode
);
920 btrfs_end_transaction(trans
, root
);
921 mutex_unlock(&root
->fs_info
->fs_mutex
);
924 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
925 struct btrfs_root
*root
,
927 struct btrfs_block_group_cache
*group
,
931 struct btrfs_inode_item inode_item
;
932 struct btrfs_key
*location
;
936 inode
= new_inode(root
->fs_info
->sb
);
938 return ERR_PTR(-ENOMEM
);
940 BTRFS_I(inode
)->root
= root
;
945 group
= btrfs_find_block_group(root
, group
, 0, 0, owner
);
946 BTRFS_I(inode
)->block_group
= group
;
948 inode
->i_uid
= current
->fsuid
;
949 inode
->i_gid
= current
->fsgid
;
950 inode
->i_mode
= mode
;
951 inode
->i_ino
= objectid
;
953 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
954 fill_inode_item(&inode_item
, inode
);
955 location
= &BTRFS_I(inode
)->location
;
956 location
->objectid
= objectid
;
958 location
->offset
= 0;
959 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
961 ret
= btrfs_insert_inode(trans
, root
, objectid
, &inode_item
);
964 insert_inode_hash(inode
);
968 static inline u8
btrfs_inode_type(struct inode
*inode
)
970 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
973 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
974 struct dentry
*dentry
, struct inode
*inode
)
977 struct btrfs_key key
;
978 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
979 struct inode
*parent_inode
;
980 key
.objectid
= inode
->i_ino
;
982 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
985 ret
= btrfs_insert_dir_item(trans
, root
,
986 dentry
->d_name
.name
, dentry
->d_name
.len
,
987 dentry
->d_parent
->d_inode
->i_ino
,
988 &key
, btrfs_inode_type(inode
));
990 parent_inode
= dentry
->d_parent
->d_inode
;
991 parent_inode
->i_size
+= dentry
->d_name
.len
* 2;
992 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
993 ret
= btrfs_update_inode(trans
, root
,
994 dentry
->d_parent
->d_inode
);
999 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
1000 struct dentry
*dentry
, struct inode
*inode
)
1002 int err
= btrfs_add_link(trans
, dentry
, inode
);
1004 d_instantiate(dentry
, inode
);
1012 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1013 int mode
, dev_t rdev
)
1015 struct btrfs_trans_handle
*trans
;
1016 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1017 struct inode
*inode
;
1022 if (!new_valid_dev(rdev
))
1025 mutex_lock(&root
->fs_info
->fs_mutex
);
1026 trans
= btrfs_start_transaction(root
, 1);
1027 btrfs_set_trans_block_group(trans
, dir
);
1029 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1035 inode
= btrfs_new_inode(trans
, root
, objectid
,
1036 BTRFS_I(dir
)->block_group
, mode
);
1037 err
= PTR_ERR(inode
);
1041 btrfs_set_trans_block_group(trans
, inode
);
1042 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1046 inode
->i_op
= &btrfs_special_inode_operations
;
1047 init_special_inode(inode
, inode
->i_mode
, rdev
);
1049 dir
->i_sb
->s_dirt
= 1;
1050 btrfs_update_inode_block_group(trans
, inode
);
1051 btrfs_update_inode_block_group(trans
, dir
);
1053 btrfs_end_transaction(trans
, root
);
1054 mutex_unlock(&root
->fs_info
->fs_mutex
);
1057 inode_dec_link_count(inode
);
1060 btrfs_btree_balance_dirty(root
);
1064 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
1065 int mode
, struct nameidata
*nd
)
1067 struct btrfs_trans_handle
*trans
;
1068 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1069 struct inode
*inode
;
1074 mutex_lock(&root
->fs_info
->fs_mutex
);
1075 trans
= btrfs_start_transaction(root
, 1);
1076 btrfs_set_trans_block_group(trans
, dir
);
1078 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1084 inode
= btrfs_new_inode(trans
, root
, objectid
,
1085 BTRFS_I(dir
)->block_group
, mode
);
1086 err
= PTR_ERR(inode
);
1090 btrfs_set_trans_block_group(trans
, inode
);
1091 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1095 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1096 inode
->i_fop
= &btrfs_file_operations
;
1097 inode
->i_op
= &btrfs_file_inode_operations
;
1099 dir
->i_sb
->s_dirt
= 1;
1100 btrfs_update_inode_block_group(trans
, inode
);
1101 btrfs_update_inode_block_group(trans
, dir
);
1103 btrfs_end_transaction(trans
, root
);
1104 mutex_unlock(&root
->fs_info
->fs_mutex
);
1107 inode_dec_link_count(inode
);
1110 btrfs_btree_balance_dirty(root
);
1114 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
1115 struct dentry
*dentry
)
1117 struct btrfs_trans_handle
*trans
;
1118 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1119 struct inode
*inode
= old_dentry
->d_inode
;
1123 if (inode
->i_nlink
== 0)
1127 mutex_lock(&root
->fs_info
->fs_mutex
);
1128 trans
= btrfs_start_transaction(root
, 1);
1129 btrfs_set_trans_block_group(trans
, dir
);
1130 atomic_inc(&inode
->i_count
);
1131 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1134 dir
->i_sb
->s_dirt
= 1;
1135 btrfs_update_inode_block_group(trans
, dir
);
1136 err
= btrfs_update_inode(trans
, root
, inode
);
1140 btrfs_end_transaction(trans
, root
);
1141 mutex_unlock(&root
->fs_info
->fs_mutex
);
1144 inode_dec_link_count(inode
);
1147 btrfs_btree_balance_dirty(root
);
1151 static int btrfs_make_empty_dir(struct btrfs_trans_handle
*trans
,
1152 struct btrfs_root
*root
,
1153 u64 objectid
, u64 dirid
)
1157 struct btrfs_key key
;
1162 key
.objectid
= objectid
;
1165 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1167 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 1, objectid
,
1168 &key
, BTRFS_FT_DIR
);
1171 key
.objectid
= dirid
;
1172 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 2, objectid
,
1173 &key
, BTRFS_FT_DIR
);
1180 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1182 struct inode
*inode
;
1183 struct btrfs_trans_handle
*trans
;
1184 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1186 int drop_on_err
= 0;
1189 mutex_lock(&root
->fs_info
->fs_mutex
);
1190 trans
= btrfs_start_transaction(root
, 1);
1191 btrfs_set_trans_block_group(trans
, dir
);
1192 if (IS_ERR(trans
)) {
1193 err
= PTR_ERR(trans
);
1197 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1203 inode
= btrfs_new_inode(trans
, root
, objectid
,
1204 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
1205 if (IS_ERR(inode
)) {
1206 err
= PTR_ERR(inode
);
1210 inode
->i_op
= &btrfs_dir_inode_operations
;
1211 inode
->i_fop
= &btrfs_dir_file_operations
;
1212 btrfs_set_trans_block_group(trans
, inode
);
1214 err
= btrfs_make_empty_dir(trans
, root
, inode
->i_ino
, dir
->i_ino
);
1219 err
= btrfs_update_inode(trans
, root
, inode
);
1222 err
= btrfs_add_link(trans
, dentry
, inode
);
1225 d_instantiate(dentry
, inode
);
1227 dir
->i_sb
->s_dirt
= 1;
1228 btrfs_update_inode_block_group(trans
, inode
);
1229 btrfs_update_inode_block_group(trans
, dir
);
1232 btrfs_end_transaction(trans
, root
);
1234 mutex_unlock(&root
->fs_info
->fs_mutex
);
1237 btrfs_btree_balance_dirty(root
);
1242 * FIBMAP and others want to pass in a fake buffer head. They need to
1243 * use BTRFS_GET_BLOCK_NO_DIRECT to make sure we don't try to memcpy
1244 * any packed file data into the fake bh
1246 #define BTRFS_GET_BLOCK_NO_CREATE 0
1247 #define BTRFS_GET_BLOCK_CREATE 1
1248 #define BTRFS_GET_BLOCK_NO_DIRECT 2
1251 * FIXME create==1 doe not work.
1253 static int btrfs_get_block_lock(struct inode
*inode
, sector_t iblock
,
1254 struct buffer_head
*result
, int create
)
1259 u64 extent_start
= 0;
1261 u64 objectid
= inode
->i_ino
;
1264 struct btrfs_path
*path
;
1265 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1266 struct btrfs_file_extent_item
*item
;
1267 struct btrfs_leaf
*leaf
;
1268 struct btrfs_disk_key
*found_key
;
1269 struct btrfs_trans_handle
*trans
= NULL
;
1271 path
= btrfs_alloc_path();
1273 if (create
& BTRFS_GET_BLOCK_CREATE
) {
1275 * danger!, this only works if the page is properly up
1278 trans
= btrfs_start_transaction(root
, 1);
1283 ret
= btrfs_drop_extents(trans
, root
, inode
,
1284 iblock
<< inode
->i_blkbits
,
1285 (iblock
+ 1) << inode
->i_blkbits
,
1290 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
1292 iblock
<< inode
->i_blkbits
, 0);
1299 if (path
->slots
[0] == 0) {
1300 btrfs_release_path(root
, path
);
1306 item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]), path
->slots
[0],
1307 struct btrfs_file_extent_item
);
1308 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
1309 blocknr
= btrfs_file_extent_disk_blocknr(item
);
1310 blocknr
+= btrfs_file_extent_offset(item
);
1312 /* are we inside the extent that was found? */
1313 found_key
= &leaf
->items
[path
->slots
[0]].key
;
1314 found_type
= btrfs_disk_key_type(found_key
);
1315 if (btrfs_disk_key_objectid(found_key
) != objectid
||
1316 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
1321 found_type
= btrfs_file_extent_type(item
);
1322 extent_start
= btrfs_disk_key_offset(&leaf
->items
[path
->slots
[0]].key
);
1323 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1324 extent_start
= extent_start
>> inode
->i_blkbits
;
1325 extent_end
= extent_start
+ btrfs_file_extent_num_blocks(item
);
1327 if (btrfs_file_extent_disk_blocknr(item
) == 0)
1329 if (iblock
>= extent_start
&& iblock
< extent_end
) {
1330 btrfs_map_bh_to_logical(root
, result
, blocknr
+
1331 iblock
- extent_start
);
1334 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1339 if (create
& BTRFS_GET_BLOCK_NO_DIRECT
) {
1343 size
= btrfs_file_extent_inline_len(leaf
->items
+
1345 extent_end
= (extent_start
+ size
) >> inode
->i_blkbits
;
1346 extent_start
>>= inode
->i_blkbits
;
1347 if (iblock
< extent_start
|| iblock
> extent_end
) {
1350 ptr
= btrfs_file_extent_inline_start(item
);
1351 map
= kmap(result
->b_page
);
1352 memcpy(map
, ptr
, size
);
1353 memset(map
+ size
, 0, PAGE_CACHE_SIZE
- size
);
1354 flush_dcache_page(result
->b_page
);
1355 kunmap(result
->b_page
);
1356 set_buffer_uptodate(result
);
1357 SetPageChecked(result
->b_page
);
1358 btrfs_map_bh_to_logical(root
, result
, 0);
1361 if (create
& BTRFS_GET_BLOCK_CREATE
) {
1362 struct btrfs_key ins
;
1363 ret
= btrfs_alloc_extent(trans
, root
, inode
->i_ino
,
1364 1, 0, alloc_hint
, (u64
)-1,
1370 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
1371 iblock
<< inode
->i_blkbits
,
1372 ins
.objectid
, ins
.offset
,
1378 btrfs_map_bh_to_logical(root
, result
, ins
.objectid
);
1382 ret
= btrfs_end_transaction(trans
, root
);
1386 btrfs_free_path(path
);
1390 int btrfs_get_block(struct inode
*inode
, sector_t iblock
,
1391 struct buffer_head
*result
, int create
)
1394 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1395 mutex_lock(&root
->fs_info
->fs_mutex
);
1396 err
= btrfs_get_block_lock(inode
, iblock
, result
, create
);
1397 mutex_unlock(&root
->fs_info
->fs_mutex
);
1401 static int btrfs_get_block_csum(struct inode
*inode
, sector_t iblock
,
1402 struct buffer_head
*result
, int create
)
1405 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1406 struct page
*page
= result
->b_page
;
1407 u64 offset
= (page
->index
<< PAGE_CACHE_SHIFT
) + bh_offset(result
);
1408 struct btrfs_csum_item
*item
;
1409 struct btrfs_path
*path
= NULL
;
1411 mutex_lock(&root
->fs_info
->fs_mutex
);
1412 ret
= btrfs_get_block_lock(inode
, iblock
, result
, create
);
1416 path
= btrfs_alloc_path();
1417 item
= btrfs_lookup_csum(NULL
, root
, path
, inode
->i_ino
, offset
, 0);
1419 ret
= PTR_ERR(item
);
1420 /* a csum that isn't present is a preallocated region. */
1421 if (ret
== -ENOENT
|| ret
== -EFBIG
)
1423 result
->b_private
= NULL
;
1426 memcpy((char *)&result
->b_private
, &item
->csum
, BTRFS_CRC32_SIZE
);
1429 btrfs_free_path(path
);
1430 mutex_unlock(&root
->fs_info
->fs_mutex
);
1434 static int btrfs_get_block_bmap(struct inode
*inode
, sector_t iblock
,
1435 struct buffer_head
*result
, int create
)
1437 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1438 mutex_lock(&root
->fs_info
->fs_mutex
);
1439 btrfs_get_block_lock(inode
, iblock
, result
, BTRFS_GET_BLOCK_NO_DIRECT
);
1440 mutex_unlock(&root
->fs_info
->fs_mutex
);
1444 static sector_t
btrfs_bmap(struct address_space
*as
, sector_t block
)
1446 return generic_block_bmap(as
, block
, btrfs_get_block_bmap
);
1449 static int btrfs_prepare_write(struct file
*file
, struct page
*page
,
1450 unsigned from
, unsigned to
)
1452 return block_prepare_write(page
, from
, to
, btrfs_get_block
);
1455 static void buffer_io_error(struct buffer_head
*bh
)
1457 char b
[BDEVNAME_SIZE
];
1459 printk(KERN_ERR
"Buffer I/O error on device %s, logical block %Lu\n",
1460 bdevname(bh
->b_bdev
, b
),
1461 (unsigned long long)bh
->b_blocknr
);
1465 * I/O completion handler for block_read_full_page() - pages
1466 * which come unlocked at the end of I/O.
1468 static void btrfs_end_buffer_async_read(struct buffer_head
*bh
, int uptodate
)
1470 unsigned long flags
;
1471 struct buffer_head
*first
;
1472 struct buffer_head
*tmp
;
1474 int page_uptodate
= 1;
1475 struct inode
*inode
;
1478 BUG_ON(!buffer_async_read(bh
));
1481 inode
= page
->mapping
->host
;
1484 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1485 if (bh
->b_private
) {
1486 char csum
[BTRFS_CRC32_SIZE
];
1487 kaddr
= kmap_atomic(page
, KM_IRQ0
);
1488 ret
= btrfs_csum_data(root
, kaddr
+ bh_offset(bh
),
1491 if (memcmp(csum
, &bh
->b_private
, BTRFS_CRC32_SIZE
)) {
1493 offset
= (page
->index
<< PAGE_CACHE_SHIFT
) +
1495 printk("btrfs csum failed ino %lu off %llu\n",
1496 page
->mapping
->host
->i_ino
,
1497 (unsigned long long)offset
);
1498 memset(kaddr
+ bh_offset(bh
), 1, bh
->b_size
);
1499 flush_dcache_page(page
);
1501 kunmap_atomic(kaddr
, KM_IRQ0
);
1503 set_buffer_uptodate(bh
);
1505 clear_buffer_uptodate(bh
);
1506 if (printk_ratelimit())
1507 buffer_io_error(bh
);
1512 * Be _very_ careful from here on. Bad things can happen if
1513 * two buffer heads end IO at almost the same time and both
1514 * decide that the page is now completely done.
1516 first
= page_buffers(page
);
1517 local_irq_save(flags
);
1518 bit_spin_lock(BH_Uptodate_Lock
, &first
->b_state
);
1519 clear_buffer_async_read(bh
);
1523 if (!buffer_uptodate(tmp
))
1525 if (buffer_async_read(tmp
)) {
1526 BUG_ON(!buffer_locked(tmp
));
1529 tmp
= tmp
->b_this_page
;
1530 } while (tmp
!= bh
);
1531 bit_spin_unlock(BH_Uptodate_Lock
, &first
->b_state
);
1532 local_irq_restore(flags
);
1535 * If none of the buffers had errors and they are all
1536 * uptodate then we can set the page uptodate.
1538 if (page_uptodate
&& !PageError(page
))
1539 SetPageUptodate(page
);
1544 bit_spin_unlock(BH_Uptodate_Lock
, &first
->b_state
);
1545 local_irq_restore(flags
);
1550 * Generic "read page" function for block devices that have the normal
1551 * get_block functionality. This is most of the block device filesystems.
1552 * Reads the page asynchronously --- the unlock_buffer() and
1553 * set/clear_buffer_uptodate() functions propagate buffer state into the
1554 * page struct once IO has completed.
1556 int btrfs_readpage(struct file
*file
, struct page
*page
)
1558 struct inode
*inode
= page
->mapping
->host
;
1559 sector_t iblock
, lblock
;
1560 struct buffer_head
*bh
, *head
, *arr
[MAX_BUF_PER_PAGE
];
1561 unsigned int blocksize
;
1563 int fully_mapped
= 1;
1565 BUG_ON(!PageLocked(page
));
1566 blocksize
= 1 << inode
->i_blkbits
;
1567 if (!page_has_buffers(page
))
1568 create_empty_buffers(page
, blocksize
, 0);
1569 head
= page_buffers(page
);
1571 iblock
= (sector_t
)page
->index
<< (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
1572 lblock
= (i_size_read(inode
)+blocksize
-1) >> inode
->i_blkbits
;
1578 if (buffer_uptodate(bh
))
1581 if (!buffer_mapped(bh
)) {
1585 if (iblock
< lblock
) {
1586 WARN_ON(bh
->b_size
!= blocksize
);
1587 err
= btrfs_get_block_csum(inode
, iblock
,
1592 if (!buffer_mapped(bh
)) {
1593 void *kaddr
= kmap_atomic(page
, KM_USER0
);
1594 memset(kaddr
+ i
* blocksize
, 0, blocksize
);
1595 flush_dcache_page(page
);
1596 kunmap_atomic(kaddr
, KM_USER0
);
1598 set_buffer_uptodate(bh
);
1602 * get_block() might have updated the buffer
1605 if (buffer_uptodate(bh
))
1609 } while (i
++, iblock
++, (bh
= bh
->b_this_page
) != head
);
1612 SetPageMappedToDisk(page
);
1616 * All buffers are uptodate - we can set the page uptodate
1617 * as well. But not if get_block() returned an error.
1619 if (!PageError(page
))
1620 SetPageUptodate(page
);
1625 /* Stage two: lock the buffers */
1626 for (i
= 0; i
< nr
; i
++) {
1629 bh
->b_end_io
= btrfs_end_buffer_async_read
;
1630 set_buffer_async_read(bh
);
1634 * Stage 3: start the IO. Check for uptodateness
1635 * inside the buffer lock in case another process reading
1636 * the underlying blockdev brought it uptodate (the sct fix).
1638 for (i
= 0; i
< nr
; i
++) {
1640 if (buffer_uptodate(bh
))
1641 btrfs_end_buffer_async_read(bh
, 1);
1643 submit_bh(READ
, bh
);
1649 * Aside from a tiny bit of packed file data handling, this is the
1650 * same as the generic code.
1652 * While block_write_full_page is writing back the dirty buffers under
1653 * the page lock, whoever dirtied the buffers may decide to clean them
1654 * again at any time. We handle that by only looking at the buffer
1655 * state inside lock_buffer().
1657 * If block_write_full_page() is called for regular writeback
1658 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1659 * locked buffer. This only can happen if someone has written the buffer
1660 * directly, with submit_bh(). At the address_space level PageWriteback
1661 * prevents this contention from occurring.
1663 static int __btrfs_write_full_page(struct inode
*inode
, struct page
*page
,
1664 struct writeback_control
*wbc
)
1668 sector_t last_block
;
1669 struct buffer_head
*bh
, *head
;
1670 const unsigned blocksize
= 1 << inode
->i_blkbits
;
1671 int nr_underway
= 0;
1672 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1674 BUG_ON(!PageLocked(page
));
1676 last_block
= (i_size_read(inode
) - 1) >> inode
->i_blkbits
;
1678 /* no csumming allowed when from PF_MEMALLOC */
1679 if (current
->flags
& PF_MEMALLOC
) {
1680 redirty_page_for_writepage(wbc
, page
);
1685 if (!page_has_buffers(page
)) {
1686 create_empty_buffers(page
, blocksize
,
1687 (1 << BH_Dirty
)|(1 << BH_Uptodate
));
1691 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1692 * here, and the (potentially unmapped) buffers may become dirty at
1693 * any time. If a buffer becomes dirty here after we've inspected it
1694 * then we just miss that fact, and the page stays dirty.
1696 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1697 * handle that here by just cleaning them.
1700 block
= (sector_t
)page
->index
<< (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
1701 head
= page_buffers(page
);
1705 * Get all the dirty buffers mapped to disk addresses and
1706 * handle any aliases from the underlying blockdev's mapping.
1709 if (block
> last_block
) {
1711 * mapped buffers outside i_size will occur, because
1712 * this page can be outside i_size when there is a
1713 * truncate in progress.
1716 * The buffer was zeroed by block_write_full_page()
1718 clear_buffer_dirty(bh
);
1719 set_buffer_uptodate(bh
);
1720 } else if (!buffer_mapped(bh
) && buffer_dirty(bh
)) {
1721 WARN_ON(bh
->b_size
!= blocksize
);
1722 err
= btrfs_get_block(inode
, block
, bh
, 0);
1726 if (buffer_new(bh
)) {
1727 /* blockdev mappings never come here */
1728 clear_buffer_new(bh
);
1731 bh
= bh
->b_this_page
;
1733 } while (bh
!= head
);
1736 if (!buffer_mapped(bh
))
1739 * If it's a fully non-blocking write attempt and we cannot
1740 * lock the buffer then redirty the page. Note that this can
1741 * potentially cause a busy-wait loop from pdflush and kswapd
1742 * activity, but those code paths have their own higher-level
1745 if (wbc
->sync_mode
!= WB_SYNC_NONE
|| !wbc
->nonblocking
) {
1747 } else if (test_set_buffer_locked(bh
)) {
1748 redirty_page_for_writepage(wbc
, page
);
1751 if (test_clear_buffer_dirty(bh
) && bh
->b_blocknr
!= 0) {
1752 struct btrfs_trans_handle
*trans
;
1754 u64 off
= page
->index
<< PAGE_CACHE_SHIFT
;
1757 off
+= bh_offset(bh
);
1758 mutex_lock(&root
->fs_info
->fs_mutex
);
1759 trans
= btrfs_start_transaction(root
, 1);
1760 btrfs_set_trans_block_group(trans
, inode
);
1762 btrfs_csum_file_block(trans
, root
, inode
->i_ino
,
1763 off
, kaddr
+ bh_offset(bh
),
1766 ret
= btrfs_end_transaction(trans
, root
);
1768 mutex_unlock(&root
->fs_info
->fs_mutex
);
1769 mark_buffer_async_write(bh
);
1773 } while ((bh
= bh
->b_this_page
) != head
);
1776 * The page and its buffers are protected by PageWriteback(), so we can
1777 * drop the bh refcounts early.
1779 BUG_ON(PageWriteback(page
));
1780 set_page_writeback(page
);
1783 struct buffer_head
*next
= bh
->b_this_page
;
1784 if (buffer_async_write(bh
)) {
1785 submit_bh(WRITE
, bh
);
1789 } while (bh
!= head
);
1794 if (nr_underway
== 0) {
1796 * The page was marked dirty, but the buffers were
1797 * clean. Someone wrote them back by hand with
1798 * ll_rw_block/submit_bh. A rare case.
1802 if (!buffer_uptodate(bh
)) {
1806 bh
= bh
->b_this_page
;
1807 } while (bh
!= head
);
1809 SetPageUptodate(page
);
1810 end_page_writeback(page
);
1816 * ENOSPC, or some other error. We may already have added some
1817 * blocks to the file, so we need to write these out to avoid
1818 * exposing stale data.
1819 * The page is currently locked and not marked for writeback
1822 /* Recovery: lock and submit the mapped buffers */
1824 if (buffer_mapped(bh
) && buffer_dirty(bh
)) {
1826 mark_buffer_async_write(bh
);
1829 * The buffer may have been set dirty during
1830 * attachment to a dirty page.
1832 clear_buffer_dirty(bh
);
1834 } while ((bh
= bh
->b_this_page
) != head
);
1836 BUG_ON(PageWriteback(page
));
1837 set_page_writeback(page
);
1839 struct buffer_head
*next
= bh
->b_this_page
;
1840 if (buffer_async_write(bh
)) {
1841 clear_buffer_dirty(bh
);
1842 submit_bh(WRITE
, bh
);
1846 } while (bh
!= head
);
1851 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
1853 struct inode
* const inode
= page
->mapping
->host
;
1854 loff_t i_size
= i_size_read(inode
);
1855 const pgoff_t end_index
= i_size
>> PAGE_CACHE_SHIFT
;
1859 /* Is the page fully inside i_size? */
1860 if (page
->index
< end_index
)
1861 return __btrfs_write_full_page(inode
, page
, wbc
);
1863 /* Is the page fully outside i_size? (truncate in progress) */
1864 offset
= i_size
& (PAGE_CACHE_SIZE
-1);
1865 if (page
->index
>= end_index
+1 || !offset
) {
1867 * The page may have dirty, unmapped buffers. For example,
1868 * they may have been added in ext3_writepage(). Make them
1869 * freeable here, so the page does not leak.
1871 block_invalidatepage(page
, 0);
1873 return 0; /* don't care */
1877 * The page straddles i_size. It must be zeroed out on each and every
1878 * writepage invokation because it may be mmapped. "A file is mapped
1879 * in multiples of the page size. For a file that is not a multiple of
1880 * the page size, the remaining memory is zeroed when mapped, and
1881 * writes to that region are not written out to the file."
1883 kaddr
= kmap_atomic(page
, KM_USER0
);
1884 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
1885 flush_dcache_page(page
);
1886 kunmap_atomic(kaddr
, KM_USER0
);
1887 return __btrfs_write_full_page(inode
, page
, wbc
);
1891 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1892 * called from a page fault handler when a page is first dirtied. Hence we must
1893 * be careful to check for EOF conditions here. We set the page up correctly
1894 * for a written page which means we get ENOSPC checking when writing into
1895 * holes and correct delalloc and unwritten extent mapping on filesystems that
1896 * support these features.
1898 * We are not allowed to take the i_mutex here so we have to play games to
1899 * protect against truncate races as the page could now be beyond EOF. Because
1900 * vmtruncate() writes the inode size before removing pages, once we have the
1901 * page lock we can determine safely if the page is beyond EOF. If it is not
1902 * beyond EOF, then the page is guaranteed safe against truncation until we
1905 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
1907 struct inode
*inode
= vma
->vm_file
->f_path
.dentry
->d_inode
;
1913 wait_on_page_writeback(page
);
1914 size
= i_size_read(inode
);
1915 if ((page
->mapping
!= inode
->i_mapping
) ||
1916 ((page
->index
<< PAGE_CACHE_SHIFT
) > size
)) {
1917 /* page got truncated out from underneath us */
1921 /* page is wholly or partially inside EOF */
1922 if (((page
->index
+ 1) << PAGE_CACHE_SHIFT
) > size
)
1923 end
= size
& ~PAGE_CACHE_MASK
;
1925 end
= PAGE_CACHE_SIZE
;
1927 ret
= btrfs_prepare_write(NULL
, page
, 0, end
);
1929 ret
= btrfs_commit_write(NULL
, page
, 0, end
);
1936 static void btrfs_truncate(struct inode
*inode
)
1938 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1940 struct btrfs_trans_handle
*trans
;
1942 if (!S_ISREG(inode
->i_mode
))
1944 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1947 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1949 mutex_lock(&root
->fs_info
->fs_mutex
);
1950 trans
= btrfs_start_transaction(root
, 1);
1951 btrfs_set_trans_block_group(trans
, inode
);
1953 /* FIXME, add redo link to tree so we don't leak on crash */
1954 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
1955 btrfs_update_inode(trans
, root
, inode
);
1956 ret
= btrfs_end_transaction(trans
, root
);
1958 mutex_unlock(&root
->fs_info
->fs_mutex
);
1959 btrfs_btree_balance_dirty(root
);
1962 int btrfs_commit_write(struct file
*file
, struct page
*page
,
1963 unsigned from
, unsigned to
)
1965 struct inode
*inode
= page
->mapping
->host
;
1966 struct buffer_head
*bh
;
1967 loff_t pos
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + to
;
1969 SetPageUptodate(page
);
1970 bh
= page_buffers(page
);
1971 set_buffer_uptodate(bh
);
1972 if (buffer_mapped(bh
) && bh
->b_blocknr
!= 0) {
1973 set_page_dirty(page
);
1975 if (pos
> inode
->i_size
) {
1976 i_size_write(inode
, pos
);
1977 mark_inode_dirty(inode
);
1982 static int create_subvol(struct btrfs_root
*root
, char *name
, int namelen
)
1984 struct btrfs_trans_handle
*trans
;
1985 struct btrfs_key key
;
1986 struct btrfs_root_item root_item
;
1987 struct btrfs_inode_item
*inode_item
;
1988 struct buffer_head
*subvol
;
1989 struct btrfs_leaf
*leaf
;
1990 struct btrfs_root
*new_root
;
1991 struct inode
*inode
;
1996 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
1998 mutex_lock(&root
->fs_info
->fs_mutex
);
1999 trans
= btrfs_start_transaction(root
, 1);
2002 subvol
= btrfs_alloc_free_block(trans
, root
, 0, 0);
2004 return PTR_ERR(subvol
);
2005 leaf
= btrfs_buffer_leaf(subvol
);
2006 btrfs_set_header_nritems(&leaf
->header
, 0);
2007 btrfs_set_header_level(&leaf
->header
, 0);
2008 btrfs_set_header_blocknr(&leaf
->header
, bh_blocknr(subvol
));
2009 btrfs_set_header_generation(&leaf
->header
, trans
->transid
);
2010 btrfs_set_header_owner(&leaf
->header
, root
->root_key
.objectid
);
2011 memcpy(leaf
->header
.fsid
, root
->fs_info
->disk_super
->fsid
,
2012 sizeof(leaf
->header
.fsid
));
2013 btrfs_mark_buffer_dirty(subvol
);
2015 inode_item
= &root_item
.inode
;
2016 memset(inode_item
, 0, sizeof(*inode_item
));
2017 btrfs_set_inode_generation(inode_item
, 1);
2018 btrfs_set_inode_size(inode_item
, 3);
2019 btrfs_set_inode_nlink(inode_item
, 1);
2020 btrfs_set_inode_nblocks(inode_item
, 1);
2021 btrfs_set_inode_mode(inode_item
, S_IFDIR
| 0755);
2023 btrfs_set_root_blocknr(&root_item
, bh_blocknr(subvol
));
2024 btrfs_set_root_refs(&root_item
, 1);
2025 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
2026 root_item
.drop_level
= 0;
2030 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2035 btrfs_set_root_dirid(&root_item
, new_dirid
);
2037 key
.objectid
= objectid
;
2040 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2041 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2047 * insert the directory item
2049 key
.offset
= (u64
)-1;
2050 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
2051 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2052 name
, namelen
, dir
->i_ino
, &key
,
2057 ret
= btrfs_commit_transaction(trans
, root
);
2061 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
);
2064 trans
= btrfs_start_transaction(new_root
, 1);
2067 inode
= btrfs_new_inode(trans
, new_root
, new_dirid
,
2068 BTRFS_I(dir
)->block_group
, S_IFDIR
| 0700);
2071 inode
->i_op
= &btrfs_dir_inode_operations
;
2072 inode
->i_fop
= &btrfs_dir_file_operations
;
2073 new_root
->inode
= inode
;
2075 ret
= btrfs_make_empty_dir(trans
, new_root
, new_dirid
, new_dirid
);
2081 ret
= btrfs_update_inode(trans
, new_root
, inode
);
2085 err
= btrfs_commit_transaction(trans
, root
);
2089 mutex_unlock(&root
->fs_info
->fs_mutex
);
2090 btrfs_btree_balance_dirty(root
);
2094 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
2096 struct btrfs_trans_handle
*trans
;
2097 struct btrfs_key key
;
2098 struct btrfs_root_item new_root_item
;
2103 if (!root
->ref_cows
)
2106 mutex_lock(&root
->fs_info
->fs_mutex
);
2107 trans
= btrfs_start_transaction(root
, 1);
2110 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
2114 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2119 memcpy(&new_root_item
, &root
->root_item
,
2120 sizeof(new_root_item
));
2122 key
.objectid
= objectid
;
2125 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2126 btrfs_set_root_blocknr(&new_root_item
, bh_blocknr(root
->node
));
2128 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2134 * insert the directory item
2136 key
.offset
= (u64
)-1;
2137 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2139 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2140 &key
, BTRFS_FT_DIR
);
2145 ret
= btrfs_inc_root_ref(trans
, root
);
2150 err
= btrfs_commit_transaction(trans
, root
);
2153 mutex_unlock(&root
->fs_info
->fs_mutex
);
2154 btrfs_btree_balance_dirty(root
);
2158 int btrfs_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int
2159 cmd
, unsigned long arg
)
2161 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2162 struct btrfs_ioctl_vol_args vol_args
;
2164 struct btrfs_dir_item
*di
;
2166 struct btrfs_path
*path
;
2170 case BTRFS_IOC_SNAP_CREATE
:
2171 if (copy_from_user(&vol_args
,
2172 (struct btrfs_ioctl_vol_args __user
*)arg
,
2175 namelen
= strlen(vol_args
.name
);
2176 if (namelen
> BTRFS_VOL_NAME_MAX
)
2178 if (strchr(vol_args
.name
, '/'))
2180 path
= btrfs_alloc_path();
2183 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2184 mutex_lock(&root
->fs_info
->fs_mutex
);
2185 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
2187 vol_args
.name
, namelen
, 0);
2188 mutex_unlock(&root
->fs_info
->fs_mutex
);
2189 btrfs_free_path(path
);
2190 if (di
&& !IS_ERR(di
))
2195 if (root
== root
->fs_info
->tree_root
)
2196 ret
= create_subvol(root
, vol_args
.name
, namelen
);
2198 ret
= create_snapshot(root
, vol_args
.name
, namelen
);
2201 case BTRFS_IOC_DEFRAG
:
2202 mutex_lock(&root
->fs_info
->fs_mutex
);
2203 btrfs_defrag_root(root
, 0);
2204 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
2205 mutex_unlock(&root
->fs_info
->fs_mutex
);
2214 #ifdef CONFIG_COMPAT
2215 long btrfs_compat_ioctl(struct file
*file
, unsigned int cmd
,
2218 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
2221 ret
= btrfs_ioctl(inode
, file
, cmd
, (unsigned long) compat_ptr(arg
));
2229 * Called inside transaction, so use GFP_NOFS
2231 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2233 struct btrfs_inode
*ei
;
2235 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2239 return &ei
->vfs_inode
;
2242 void btrfs_destroy_inode(struct inode
*inode
)
2244 WARN_ON(!list_empty(&inode
->i_dentry
));
2245 WARN_ON(inode
->i_data
.nrpages
);
2247 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2250 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2251 unsigned long flags
)
2253 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2255 inode_init_once(&ei
->vfs_inode
);
2258 void btrfs_destroy_cachep(void)
2260 if (btrfs_inode_cachep
)
2261 kmem_cache_destroy(btrfs_inode_cachep
);
2262 if (btrfs_trans_handle_cachep
)
2263 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2264 if (btrfs_transaction_cachep
)
2265 kmem_cache_destroy(btrfs_transaction_cachep
);
2266 if (btrfs_bit_radix_cachep
)
2267 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2268 if (btrfs_path_cachep
)
2269 kmem_cache_destroy(btrfs_path_cachep
);
2272 static struct kmem_cache
*cache_create(const char *name
, size_t size
,
2273 unsigned long extra_flags
,
2274 void (*ctor
)(void *, struct kmem_cache
*,
2277 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
2278 SLAB_MEM_SPREAD
| extra_flags
), ctor
2279 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2285 int btrfs_init_cachep(void)
2287 btrfs_inode_cachep
= cache_create("btrfs_inode_cache",
2288 sizeof(struct btrfs_inode
),
2290 if (!btrfs_inode_cachep
)
2292 btrfs_trans_handle_cachep
= cache_create("btrfs_trans_handle_cache",
2293 sizeof(struct btrfs_trans_handle
),
2295 if (!btrfs_trans_handle_cachep
)
2297 btrfs_transaction_cachep
= cache_create("btrfs_transaction_cache",
2298 sizeof(struct btrfs_transaction
),
2300 if (!btrfs_transaction_cachep
)
2302 btrfs_path_cachep
= cache_create("btrfs_path_cache",
2303 sizeof(struct btrfs_transaction
),
2305 if (!btrfs_path_cachep
)
2307 btrfs_bit_radix_cachep
= cache_create("btrfs_radix", 256,
2308 SLAB_DESTROY_BY_RCU
, NULL
);
2309 if (!btrfs_bit_radix_cachep
)
2313 btrfs_destroy_cachep();
2317 static int btrfs_getattr(struct vfsmount
*mnt
,
2318 struct dentry
*dentry
, struct kstat
*stat
)
2320 struct inode
*inode
= dentry
->d_inode
;
2321 generic_fillattr(inode
, stat
);
2322 stat
->blksize
= 256 * 1024;
2326 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
2327 struct inode
* new_dir
,struct dentry
*new_dentry
)
2329 struct btrfs_trans_handle
*trans
;
2330 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
2331 struct inode
*new_inode
= new_dentry
->d_inode
;
2332 struct inode
*old_inode
= old_dentry
->d_inode
;
2333 struct timespec ctime
= CURRENT_TIME
;
2334 struct btrfs_path
*path
;
2335 struct btrfs_dir_item
*di
;
2338 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
2339 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
2342 mutex_lock(&root
->fs_info
->fs_mutex
);
2343 trans
= btrfs_start_transaction(root
, 1);
2344 btrfs_set_trans_block_group(trans
, new_dir
);
2345 path
= btrfs_alloc_path();
2351 old_dentry
->d_inode
->i_nlink
++;
2352 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
2353 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
2354 old_inode
->i_ctime
= ctime
;
2355 if (S_ISDIR(old_inode
->i_mode
) && old_dir
!= new_dir
) {
2356 struct btrfs_key
*location
= &BTRFS_I(new_dir
)->location
;
2358 di
= btrfs_lookup_dir_item(trans
, root
, path
, old_inode
->i_ino
,
2368 old_parent_oid
= btrfs_disk_key_objectid(&di
->location
);
2369 ret
= btrfs_del_item(trans
, root
, path
);
2373 btrfs_release_path(root
, path
);
2375 di
= btrfs_lookup_dir_index_item(trans
, root
, path
,
2387 ret
= btrfs_del_item(trans
, root
, path
);
2391 btrfs_release_path(root
, path
);
2393 ret
= btrfs_insert_dir_item(trans
, root
, "..", 2,
2394 old_inode
->i_ino
, location
,
2401 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
2406 new_inode
->i_ctime
= CURRENT_TIME
;
2407 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
2410 if (S_ISDIR(new_inode
->i_mode
))
2411 clear_nlink(new_inode
);
2413 drop_nlink(new_inode
);
2414 ret
= btrfs_update_inode(trans
, root
, new_inode
);
2418 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
);
2423 btrfs_free_path(path
);
2424 btrfs_end_transaction(trans
, root
);
2425 mutex_unlock(&root
->fs_info
->fs_mutex
);
2429 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
2430 const char *symname
)
2432 struct btrfs_trans_handle
*trans
;
2433 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2434 struct btrfs_path
*path
;
2435 struct btrfs_key key
;
2436 struct inode
*inode
;
2443 struct btrfs_file_extent_item
*ei
;
2445 name_len
= strlen(symname
) + 1;
2446 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
2447 return -ENAMETOOLONG
;
2448 mutex_lock(&root
->fs_info
->fs_mutex
);
2449 trans
= btrfs_start_transaction(root
, 1);
2450 btrfs_set_trans_block_group(trans
, dir
);
2452 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2458 inode
= btrfs_new_inode(trans
, root
, objectid
,
2459 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
2460 err
= PTR_ERR(inode
);
2464 btrfs_set_trans_block_group(trans
, inode
);
2465 err
= btrfs_add_nondir(trans
, dentry
, inode
);
2469 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2470 inode
->i_fop
= &btrfs_file_operations
;
2471 inode
->i_op
= &btrfs_file_inode_operations
;
2473 dir
->i_sb
->s_dirt
= 1;
2474 btrfs_update_inode_block_group(trans
, inode
);
2475 btrfs_update_inode_block_group(trans
, dir
);
2479 path
= btrfs_alloc_path();
2481 key
.objectid
= inode
->i_ino
;
2484 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
2485 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
2486 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2492 ei
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
2493 path
->slots
[0], struct btrfs_file_extent_item
);
2494 btrfs_set_file_extent_generation(ei
, trans
->transid
);
2495 btrfs_set_file_extent_type(ei
,
2496 BTRFS_FILE_EXTENT_INLINE
);
2497 ptr
= btrfs_file_extent_inline_start(ei
);
2498 btrfs_memcpy(root
, path
->nodes
[0]->b_data
,
2499 ptr
, symname
, name_len
);
2500 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2501 btrfs_free_path(path
);
2502 inode
->i_op
= &btrfs_symlink_inode_operations
;
2503 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2504 inode
->i_size
= name_len
- 1;
2505 err
= btrfs_update_inode(trans
, root
, inode
);
2510 btrfs_end_transaction(trans
, root
);
2511 mutex_unlock(&root
->fs_info
->fs_mutex
);
2513 inode_dec_link_count(inode
);
2516 btrfs_btree_balance_dirty(root
);
2520 static struct inode_operations btrfs_dir_inode_operations
= {
2521 .lookup
= btrfs_lookup
,
2522 .create
= btrfs_create
,
2523 .unlink
= btrfs_unlink
,
2525 .mkdir
= btrfs_mkdir
,
2526 .rmdir
= btrfs_rmdir
,
2527 .rename
= btrfs_rename
,
2528 .symlink
= btrfs_symlink
,
2529 .setattr
= btrfs_setattr
,
2530 .mknod
= btrfs_mknod
,
2533 static struct inode_operations btrfs_dir_ro_inode_operations
= {
2534 .lookup
= btrfs_lookup
,
2537 static struct file_operations btrfs_dir_file_operations
= {
2538 .llseek
= generic_file_llseek
,
2539 .read
= generic_read_dir
,
2540 .readdir
= btrfs_readdir
,
2541 .ioctl
= btrfs_ioctl
,
2542 #ifdef CONFIG_COMPAT
2543 .compat_ioctl
= btrfs_compat_ioctl
,
2547 static struct address_space_operations btrfs_aops
= {
2548 .readpage
= btrfs_readpage
,
2549 .writepage
= btrfs_writepage
,
2550 .sync_page
= block_sync_page
,
2551 .prepare_write
= btrfs_prepare_write
,
2552 .commit_write
= btrfs_commit_write
,
2556 static struct address_space_operations btrfs_symlink_aops
= {
2557 .readpage
= btrfs_readpage
,
2558 .writepage
= btrfs_writepage
,
2561 static struct inode_operations btrfs_file_inode_operations
= {
2562 .truncate
= btrfs_truncate
,
2563 .getattr
= btrfs_getattr
,
2564 .setattr
= btrfs_setattr
,
2567 static struct inode_operations btrfs_special_inode_operations
= {
2568 .getattr
= btrfs_getattr
,
2569 .setattr
= btrfs_setattr
,
2572 static struct inode_operations btrfs_symlink_inode_operations
= {
2573 .readlink
= generic_readlink
,
2574 .follow_link
= page_follow_link_light
,
2575 .put_link
= page_put_link
,