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 static int run_delalloc_range(struct inode
*inode
, u64 start
, u64 end
)
75 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
76 struct btrfs_trans_handle
*trans
;
81 u64 blocksize
= 1 << inode
->i_blkbits
;
83 mutex_lock(&root
->fs_info
->fs_mutex
);
84 trans
= btrfs_start_transaction(root
, 1);
85 btrfs_set_trans_block_group(trans
, inode
);
87 num_blocks
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
88 ret
= btrfs_drop_extents(trans
, root
, inode
,
89 start
, start
+ num_blocks
, &alloc_hint
);
90 num_blocks
= num_blocks
>> inode
->i_blkbits
;
91 ret
= btrfs_alloc_extent(trans
, root
, inode
->i_ino
, num_blocks
, 0,
92 alloc_hint
, (u64
)-1, &ins
, 1);
97 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
98 start
, ins
.objectid
, ins
.offset
,
101 btrfs_end_transaction(trans
, root
);
102 mutex_unlock(&root
->fs_info
->fs_mutex
);
107 void btrfs_read_locked_inode(struct inode
*inode
)
109 struct btrfs_path
*path
;
110 struct btrfs_inode_item
*inode_item
;
111 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
112 struct btrfs_key location
;
113 u64 alloc_group_block
;
117 path
= btrfs_alloc_path();
119 mutex_lock(&root
->fs_info
->fs_mutex
);
121 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
122 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
124 btrfs_free_path(path
);
127 inode_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
129 struct btrfs_inode_item
);
131 inode
->i_mode
= btrfs_inode_mode(inode_item
);
132 inode
->i_nlink
= btrfs_inode_nlink(inode_item
);
133 inode
->i_uid
= btrfs_inode_uid(inode_item
);
134 inode
->i_gid
= btrfs_inode_gid(inode_item
);
135 inode
->i_size
= btrfs_inode_size(inode_item
);
136 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(&inode_item
->atime
);
137 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->atime
);
138 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(&inode_item
->mtime
);
139 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->mtime
);
140 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(&inode_item
->ctime
);
141 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->ctime
);
142 inode
->i_blocks
= btrfs_inode_nblocks(inode_item
);
143 inode
->i_generation
= btrfs_inode_generation(inode_item
);
145 rdev
= btrfs_inode_rdev(inode_item
);
146 alloc_group_block
= btrfs_inode_block_group(inode_item
);
147 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
150 btrfs_free_path(path
);
153 mutex_unlock(&root
->fs_info
->fs_mutex
);
155 switch (inode
->i_mode
& S_IFMT
) {
157 inode
->i_mapping
->a_ops
= &btrfs_aops
;
158 BTRFS_I(inode
)->extent_tree
.fill_delalloc
= run_delalloc_range
;
159 inode
->i_fop
= &btrfs_file_operations
;
160 inode
->i_op
= &btrfs_file_inode_operations
;
163 inode
->i_fop
= &btrfs_dir_file_operations
;
164 if (root
== root
->fs_info
->tree_root
)
165 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
167 inode
->i_op
= &btrfs_dir_inode_operations
;
170 inode
->i_op
= &btrfs_symlink_inode_operations
;
171 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
174 init_special_inode(inode
, inode
->i_mode
, rdev
);
180 btrfs_release_path(root
, path
);
181 btrfs_free_path(path
);
182 mutex_unlock(&root
->fs_info
->fs_mutex
);
183 make_bad_inode(inode
);
186 static void fill_inode_item(struct btrfs_inode_item
*item
,
189 btrfs_set_inode_uid(item
, inode
->i_uid
);
190 btrfs_set_inode_gid(item
, inode
->i_gid
);
191 btrfs_set_inode_size(item
, inode
->i_size
);
192 btrfs_set_inode_mode(item
, inode
->i_mode
);
193 btrfs_set_inode_nlink(item
, inode
->i_nlink
);
194 btrfs_set_timespec_sec(&item
->atime
, inode
->i_atime
.tv_sec
);
195 btrfs_set_timespec_nsec(&item
->atime
, inode
->i_atime
.tv_nsec
);
196 btrfs_set_timespec_sec(&item
->mtime
, inode
->i_mtime
.tv_sec
);
197 btrfs_set_timespec_nsec(&item
->mtime
, inode
->i_mtime
.tv_nsec
);
198 btrfs_set_timespec_sec(&item
->ctime
, inode
->i_ctime
.tv_sec
);
199 btrfs_set_timespec_nsec(&item
->ctime
, inode
->i_ctime
.tv_nsec
);
200 btrfs_set_inode_nblocks(item
, inode
->i_blocks
);
201 btrfs_set_inode_generation(item
, inode
->i_generation
);
202 btrfs_set_inode_rdev(item
, inode
->i_rdev
);
203 btrfs_set_inode_block_group(item
,
204 BTRFS_I(inode
)->block_group
->key
.objectid
);
207 int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
208 struct btrfs_root
*root
,
211 struct btrfs_inode_item
*inode_item
;
212 struct btrfs_path
*path
;
215 path
= btrfs_alloc_path();
217 ret
= btrfs_lookup_inode(trans
, root
, path
,
218 &BTRFS_I(inode
)->location
, 1);
225 inode_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
227 struct btrfs_inode_item
);
229 fill_inode_item(inode_item
, inode
);
230 btrfs_mark_buffer_dirty(path
->nodes
[0]);
231 btrfs_set_inode_last_trans(trans
, inode
);
234 btrfs_release_path(root
, path
);
235 btrfs_free_path(path
);
240 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
241 struct btrfs_root
*root
,
243 struct dentry
*dentry
)
245 struct btrfs_path
*path
;
246 const char *name
= dentry
->d_name
.name
;
247 int name_len
= dentry
->d_name
.len
;
250 struct btrfs_dir_item
*di
;
252 path
= btrfs_alloc_path();
258 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
268 objectid
= btrfs_disk_key_objectid(&di
->location
);
269 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
272 btrfs_release_path(root
, path
);
274 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
275 objectid
, name
, name_len
, -1);
284 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
286 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
288 btrfs_free_path(path
);
290 dir
->i_size
-= name_len
* 2;
291 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
292 btrfs_update_inode(trans
, root
, dir
);
293 drop_nlink(dentry
->d_inode
);
294 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
295 dir
->i_sb
->s_dirt
= 1;
300 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
302 struct btrfs_root
*root
;
303 struct btrfs_trans_handle
*trans
;
306 root
= BTRFS_I(dir
)->root
;
307 mutex_lock(&root
->fs_info
->fs_mutex
);
308 trans
= btrfs_start_transaction(root
, 1);
309 btrfs_set_trans_block_group(trans
, dir
);
310 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
311 btrfs_end_transaction(trans
, root
);
312 mutex_unlock(&root
->fs_info
->fs_mutex
);
313 btrfs_btree_balance_dirty(root
);
317 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
319 struct inode
*inode
= dentry
->d_inode
;
322 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
323 struct btrfs_path
*path
;
324 struct btrfs_key key
;
325 struct btrfs_trans_handle
*trans
;
326 struct btrfs_key found_key
;
328 struct btrfs_leaf
*leaf
;
329 char *goodnames
= "..";
331 path
= btrfs_alloc_path();
333 mutex_lock(&root
->fs_info
->fs_mutex
);
334 trans
= btrfs_start_transaction(root
, 1);
335 btrfs_set_trans_block_group(trans
, dir
);
336 key
.objectid
= inode
->i_ino
;
337 key
.offset
= (u64
)-1;
340 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
346 if (path
->slots
[0] == 0) {
351 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
352 btrfs_disk_key_to_cpu(&found_key
,
353 &leaf
->items
[path
->slots
[0]].key
);
354 found_type
= btrfs_key_type(&found_key
);
355 if (found_key
.objectid
!= inode
->i_ino
) {
359 if ((found_type
!= BTRFS_DIR_ITEM_KEY
&&
360 found_type
!= BTRFS_DIR_INDEX_KEY
) ||
361 (!btrfs_match_dir_item_name(root
, path
, goodnames
, 2) &&
362 !btrfs_match_dir_item_name(root
, path
, goodnames
, 1))) {
366 ret
= btrfs_del_item(trans
, root
, path
);
369 if (found_type
== BTRFS_DIR_ITEM_KEY
&& found_key
.offset
== 1)
371 btrfs_release_path(root
, path
);
374 btrfs_release_path(root
, path
);
376 /* now the directory is empty */
377 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
382 btrfs_release_path(root
, path
);
383 btrfs_free_path(path
);
384 mutex_unlock(&root
->fs_info
->fs_mutex
);
385 ret
= btrfs_end_transaction(trans
, root
);
386 btrfs_btree_balance_dirty(root
);
392 static int btrfs_free_inode(struct btrfs_trans_handle
*trans
,
393 struct btrfs_root
*root
,
396 struct btrfs_path
*path
;
401 path
= btrfs_alloc_path();
403 ret
= btrfs_lookup_inode(trans
, root
, path
,
404 &BTRFS_I(inode
)->location
, -1);
408 ret
= btrfs_del_item(trans
, root
, path
);
409 btrfs_free_path(path
);
414 * this can truncate away extent items, csum items and directory items.
415 * It starts at a high offset and removes keys until it can't find
416 * any higher than i_size.
418 * csum items that cross the new i_size are truncated to the new size
421 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
422 struct btrfs_root
*root
,
426 struct btrfs_path
*path
;
427 struct btrfs_key key
;
428 struct btrfs_disk_key
*found_key
;
430 struct btrfs_leaf
*leaf
;
431 struct btrfs_file_extent_item
*fi
;
432 u64 extent_start
= 0;
433 u64 extent_num_blocks
= 0;
438 btrfs_drop_extent_cache(inode
, inode
->i_size
, (u64
)-1);
439 path
= btrfs_alloc_path();
442 /* FIXME, add redo link to tree so we don't leak on crash */
443 key
.objectid
= inode
->i_ino
;
444 key
.offset
= (u64
)-1;
447 btrfs_init_path(path
);
449 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
454 BUG_ON(path
->slots
[0] == 0);
457 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
458 found_key
= &leaf
->items
[path
->slots
[0]].key
;
459 found_type
= btrfs_disk_key_type(found_key
);
461 if (btrfs_disk_key_objectid(found_key
) != inode
->i_ino
)
463 if (found_type
!= BTRFS_CSUM_ITEM_KEY
&&
464 found_type
!= BTRFS_DIR_ITEM_KEY
&&
465 found_type
!= BTRFS_DIR_INDEX_KEY
&&
466 found_type
!= BTRFS_EXTENT_DATA_KEY
)
469 item_end
= btrfs_disk_key_offset(found_key
);
470 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
471 fi
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
473 struct btrfs_file_extent_item
);
474 if (btrfs_file_extent_type(fi
) !=
475 BTRFS_FILE_EXTENT_INLINE
) {
476 item_end
+= btrfs_file_extent_num_blocks(fi
) <<
480 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
481 ret
= btrfs_csum_truncate(trans
, root
, path
,
485 if (item_end
< inode
->i_size
) {
486 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
487 found_type
= BTRFS_INODE_ITEM_KEY
;
488 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
489 found_type
= BTRFS_CSUM_ITEM_KEY
;
490 } else if (found_type
) {
495 btrfs_set_key_type(&key
, found_type
- 1);
498 if (btrfs_disk_key_offset(found_key
) >= inode
->i_size
)
504 /* FIXME, shrink the extent if the ref count is only 1 */
505 if (found_type
== BTRFS_EXTENT_DATA_KEY
&&
506 btrfs_file_extent_type(fi
) !=
507 BTRFS_FILE_EXTENT_INLINE
) {
510 u64 orig_num_blocks
=
511 btrfs_file_extent_num_blocks(fi
);
512 extent_num_blocks
= inode
->i_size
-
513 btrfs_disk_key_offset(found_key
) +
515 extent_num_blocks
>>= inode
->i_blkbits
;
516 btrfs_set_file_extent_num_blocks(fi
,
518 inode
->i_blocks
-= (orig_num_blocks
-
519 extent_num_blocks
) << 3;
520 btrfs_mark_buffer_dirty(path
->nodes
[0]);
523 btrfs_file_extent_disk_blocknr(fi
);
525 btrfs_file_extent_disk_num_blocks(fi
);
526 /* FIXME blocksize != 4096 */
527 num_dec
= btrfs_file_extent_num_blocks(fi
) << 3;
528 if (extent_start
!= 0) {
530 inode
->i_blocks
-= num_dec
;
535 ret
= btrfs_del_item(trans
, root
, path
);
541 btrfs_release_path(root
, path
);
543 ret
= btrfs_free_extent(trans
, root
, extent_start
,
544 extent_num_blocks
, 0);
550 btrfs_release_path(root
, path
);
551 btrfs_free_path(path
);
552 inode
->i_sb
->s_dirt
= 1;
556 static int btrfs_cow_one_page(struct inode
*inode
, struct page
*page
,
561 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
562 u64 page_start
= page
->index
<< PAGE_CACHE_SHIFT
;
563 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
565 if (!PagePrivate(page
)) {
566 SetPagePrivate(page
);
567 set_page_private(page
, 1);
568 WARN_ON(!page
->mapping
->a_ops
->invalidatepage
);
569 page_cache_get(page
);
572 lock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
573 set_extent_delalloc(&BTRFS_I(inode
)->extent_tree
, page_start
,
575 if (zero_start
!= PAGE_CACHE_SIZE
) {
577 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
578 flush_dcache_page(page
);
581 set_page_dirty(page
);
582 unlock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
588 * taken from block_truncate_page, but does cow as it zeros out
589 * any bytes left in the last page in the file.
591 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
593 struct inode
*inode
= mapping
->host
;
594 unsigned blocksize
= 1 << inode
->i_blkbits
;
595 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
596 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
601 if ((offset
& (blocksize
- 1)) == 0)
605 page
= grab_cache_page(mapping
, index
);
608 if (!PageUptodate(page
)) {
609 ret
= btrfs_readpage(NULL
, page
);
611 if (!PageUptodate(page
)) {
616 page_start
= page
->index
<< PAGE_CACHE_SHIFT
;
618 ret
= btrfs_cow_one_page(inode
, page
, offset
);
621 page_cache_release(page
);
626 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
628 struct inode
*inode
= dentry
->d_inode
;
631 err
= inode_change_ok(inode
, attr
);
635 if (S_ISREG(inode
->i_mode
) &&
636 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
637 struct btrfs_trans_handle
*trans
;
638 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
639 u64 mask
= root
->blocksize
- 1;
640 u64 pos
= (inode
->i_size
+ mask
) & ~mask
;
643 if (attr
->ia_size
<= pos
)
646 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
648 hole_size
= (attr
->ia_size
- pos
+ mask
) & ~mask
;
649 hole_size
>>= inode
->i_blkbits
;
651 mutex_lock(&root
->fs_info
->fs_mutex
);
652 trans
= btrfs_start_transaction(root
, 1);
653 btrfs_set_trans_block_group(trans
, inode
);
654 err
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
655 pos
, 0, 0, hole_size
);
656 btrfs_end_transaction(trans
, root
);
657 mutex_unlock(&root
->fs_info
->fs_mutex
);
662 err
= inode_setattr(inode
, attr
);
666 void btrfs_delete_inode(struct inode
*inode
)
668 struct btrfs_trans_handle
*trans
;
669 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
672 truncate_inode_pages(&inode
->i_data
, 0);
673 if (is_bad_inode(inode
)) {
677 mutex_lock(&root
->fs_info
->fs_mutex
);
678 trans
= btrfs_start_transaction(root
, 1);
679 btrfs_set_trans_block_group(trans
, inode
);
680 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
683 ret
= btrfs_free_inode(trans
, root
, inode
);
686 btrfs_end_transaction(trans
, root
);
687 mutex_unlock(&root
->fs_info
->fs_mutex
);
688 btrfs_btree_balance_dirty(root
);
692 btrfs_end_transaction(trans
, root
);
693 mutex_unlock(&root
->fs_info
->fs_mutex
);
694 btrfs_btree_balance_dirty(root
);
700 * this returns the key found in the dir entry in the location pointer.
701 * If no dir entries were found, location->objectid is 0.
703 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
704 struct btrfs_key
*location
)
706 const char *name
= dentry
->d_name
.name
;
707 int namelen
= dentry
->d_name
.len
;
708 struct btrfs_dir_item
*di
;
709 struct btrfs_path
*path
;
710 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
713 path
= btrfs_alloc_path();
715 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
717 if (!di
|| IS_ERR(di
)) {
718 location
->objectid
= 0;
722 btrfs_disk_key_to_cpu(location
, &di
->location
);
724 btrfs_release_path(root
, path
);
725 btrfs_free_path(path
);
730 * when we hit a tree root in a directory, the btrfs part of the inode
731 * needs to be changed to reflect the root directory of the tree root. This
732 * is kind of like crossing a mount point.
734 static int fixup_tree_root_location(struct btrfs_root
*root
,
735 struct btrfs_key
*location
,
736 struct btrfs_root
**sub_root
,
737 struct dentry
*dentry
)
739 struct btrfs_path
*path
;
740 struct btrfs_root_item
*ri
;
742 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
744 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
747 path
= btrfs_alloc_path();
749 mutex_lock(&root
->fs_info
->fs_mutex
);
751 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
754 if (IS_ERR(*sub_root
))
755 return PTR_ERR(*sub_root
);
757 ri
= &(*sub_root
)->root_item
;
758 location
->objectid
= btrfs_root_dirid(ri
);
760 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
761 location
->offset
= 0;
763 btrfs_free_path(path
);
764 mutex_unlock(&root
->fs_info
->fs_mutex
);
768 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
770 struct btrfs_iget_args
*args
= p
;
771 inode
->i_ino
= args
->ino
;
772 BTRFS_I(inode
)->root
= args
->root
;
773 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
774 inode
->i_mapping
, GFP_NOFS
);
778 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
780 struct btrfs_iget_args
*args
= opaque
;
781 return (args
->ino
== inode
->i_ino
&&
782 args
->root
== BTRFS_I(inode
)->root
);
785 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
786 struct btrfs_root
*root
)
789 struct btrfs_iget_args args
;
793 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
794 btrfs_init_locked_inode
,
799 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
800 struct nameidata
*nd
)
802 struct inode
* inode
;
803 struct btrfs_inode
*bi
= BTRFS_I(dir
);
804 struct btrfs_root
*root
= bi
->root
;
805 struct btrfs_root
*sub_root
= root
;
806 struct btrfs_key location
;
809 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
810 return ERR_PTR(-ENAMETOOLONG
);
811 mutex_lock(&root
->fs_info
->fs_mutex
);
812 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
813 mutex_unlock(&root
->fs_info
->fs_mutex
);
817 if (location
.objectid
) {
818 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
823 return ERR_PTR(-ENOENT
);
824 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
827 return ERR_PTR(-EACCES
);
828 if (inode
->i_state
& I_NEW
) {
829 /* the inode and parent dir are two different roots */
830 if (sub_root
!= root
) {
832 sub_root
->inode
= inode
;
834 BTRFS_I(inode
)->root
= sub_root
;
835 memcpy(&BTRFS_I(inode
)->location
, &location
,
837 btrfs_read_locked_inode(inode
);
838 unlock_new_inode(inode
);
841 return d_splice_alias(inode
, dentry
);
844 static unsigned char btrfs_filetype_table
[] = {
845 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
848 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
850 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
851 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
852 struct btrfs_item
*item
;
853 struct btrfs_dir_item
*di
;
854 struct btrfs_key key
;
855 struct btrfs_path
*path
;
858 struct btrfs_leaf
*leaf
;
861 unsigned char d_type
;
866 int key_type
= BTRFS_DIR_INDEX_KEY
;
868 /* FIXME, use a real flag for deciding about the key type */
869 if (root
->fs_info
->tree_root
== root
)
870 key_type
= BTRFS_DIR_ITEM_KEY
;
871 mutex_lock(&root
->fs_info
->fs_mutex
);
872 key
.objectid
= inode
->i_ino
;
874 btrfs_set_key_type(&key
, key_type
);
875 key
.offset
= filp
->f_pos
;
876 path
= btrfs_alloc_path();
878 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
883 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
884 nritems
= btrfs_header_nritems(&leaf
->header
);
885 slot
= path
->slots
[0];
886 if (advance
|| slot
>= nritems
) {
887 if (slot
>= nritems
-1) {
888 ret
= btrfs_next_leaf(root
, path
);
891 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
892 nritems
= btrfs_header_nritems(&leaf
->header
);
893 slot
= path
->slots
[0];
900 item
= leaf
->items
+ slot
;
901 if (btrfs_disk_key_objectid(&item
->key
) != key
.objectid
)
903 if (btrfs_disk_key_type(&item
->key
) != key_type
)
905 if (btrfs_disk_key_offset(&item
->key
) < filp
->f_pos
)
907 filp
->f_pos
= btrfs_disk_key_offset(&item
->key
);
909 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
911 di_total
= btrfs_item_size(leaf
->items
+ slot
);
912 while(di_cur
< di_total
) {
913 d_type
= btrfs_filetype_table
[btrfs_dir_type(di
)];
914 over
= filldir(dirent
, (const char *)(di
+ 1),
915 btrfs_dir_name_len(di
),
916 btrfs_disk_key_offset(&item
->key
),
917 btrfs_disk_key_objectid(&di
->location
),
921 di_len
= btrfs_dir_name_len(di
) + sizeof(*di
);
923 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
930 btrfs_release_path(root
, path
);
931 btrfs_free_path(path
);
932 mutex_unlock(&root
->fs_info
->fs_mutex
);
936 int btrfs_write_inode(struct inode
*inode
, int wait
)
938 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
939 struct btrfs_trans_handle
*trans
;
943 mutex_lock(&root
->fs_info
->fs_mutex
);
944 trans
= btrfs_start_transaction(root
, 1);
945 btrfs_set_trans_block_group(trans
, inode
);
946 ret
= btrfs_commit_transaction(trans
, root
);
947 mutex_unlock(&root
->fs_info
->fs_mutex
);
953 * This is somewhat expensive, updating the tree every time the
954 * inode changes. But, it is most likely to find the inode in cache.
955 * FIXME, needs more benchmarking...there are no reasons other than performance
956 * to keep or drop this code.
958 void btrfs_dirty_inode(struct inode
*inode
)
960 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
961 struct btrfs_trans_handle
*trans
;
963 mutex_lock(&root
->fs_info
->fs_mutex
);
964 trans
= btrfs_start_transaction(root
, 1);
965 btrfs_set_trans_block_group(trans
, inode
);
966 btrfs_update_inode(trans
, root
, inode
);
967 btrfs_end_transaction(trans
, root
);
968 mutex_unlock(&root
->fs_info
->fs_mutex
);
971 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
972 struct btrfs_root
*root
,
974 struct btrfs_block_group_cache
*group
,
978 struct btrfs_inode_item inode_item
;
979 struct btrfs_key
*location
;
983 inode
= new_inode(root
->fs_info
->sb
);
985 return ERR_PTR(-ENOMEM
);
987 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
988 inode
->i_mapping
, GFP_NOFS
);
989 BTRFS_I(inode
)->root
= root
;
995 group
= btrfs_find_block_group(root
, group
, 0, 0, owner
);
996 BTRFS_I(inode
)->block_group
= group
;
998 inode
->i_uid
= current
->fsuid
;
999 inode
->i_gid
= current
->fsgid
;
1000 inode
->i_mode
= mode
;
1001 inode
->i_ino
= objectid
;
1002 inode
->i_blocks
= 0;
1003 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1004 fill_inode_item(&inode_item
, inode
);
1005 location
= &BTRFS_I(inode
)->location
;
1006 location
->objectid
= objectid
;
1007 location
->flags
= 0;
1008 location
->offset
= 0;
1009 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1011 ret
= btrfs_insert_inode(trans
, root
, objectid
, &inode_item
);
1013 return ERR_PTR(ret
);
1014 insert_inode_hash(inode
);
1018 static inline u8
btrfs_inode_type(struct inode
*inode
)
1020 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
1023 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
1024 struct dentry
*dentry
, struct inode
*inode
)
1027 struct btrfs_key key
;
1028 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
1029 struct inode
*parent_inode
;
1030 key
.objectid
= inode
->i_ino
;
1032 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1035 ret
= btrfs_insert_dir_item(trans
, root
,
1036 dentry
->d_name
.name
, dentry
->d_name
.len
,
1037 dentry
->d_parent
->d_inode
->i_ino
,
1038 &key
, btrfs_inode_type(inode
));
1040 parent_inode
= dentry
->d_parent
->d_inode
;
1041 parent_inode
->i_size
+= dentry
->d_name
.len
* 2;
1042 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
1043 ret
= btrfs_update_inode(trans
, root
,
1044 dentry
->d_parent
->d_inode
);
1049 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
1050 struct dentry
*dentry
, struct inode
*inode
)
1052 int err
= btrfs_add_link(trans
, dentry
, inode
);
1054 d_instantiate(dentry
, inode
);
1062 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1063 int mode
, dev_t rdev
)
1065 struct btrfs_trans_handle
*trans
;
1066 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1067 struct inode
*inode
;
1072 if (!new_valid_dev(rdev
))
1075 mutex_lock(&root
->fs_info
->fs_mutex
);
1076 trans
= btrfs_start_transaction(root
, 1);
1077 btrfs_set_trans_block_group(trans
, dir
);
1079 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1085 inode
= btrfs_new_inode(trans
, root
, objectid
,
1086 BTRFS_I(dir
)->block_group
, mode
);
1087 err
= PTR_ERR(inode
);
1091 btrfs_set_trans_block_group(trans
, inode
);
1092 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1096 inode
->i_op
= &btrfs_special_inode_operations
;
1097 init_special_inode(inode
, inode
->i_mode
, rdev
);
1098 btrfs_update_inode(trans
, root
, inode
);
1100 dir
->i_sb
->s_dirt
= 1;
1101 btrfs_update_inode_block_group(trans
, inode
);
1102 btrfs_update_inode_block_group(trans
, dir
);
1104 btrfs_end_transaction(trans
, root
);
1105 mutex_unlock(&root
->fs_info
->fs_mutex
);
1108 inode_dec_link_count(inode
);
1111 btrfs_btree_balance_dirty(root
);
1115 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
1116 int mode
, struct nameidata
*nd
)
1118 struct btrfs_trans_handle
*trans
;
1119 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1120 struct inode
*inode
;
1125 mutex_lock(&root
->fs_info
->fs_mutex
);
1126 trans
= btrfs_start_transaction(root
, 1);
1127 btrfs_set_trans_block_group(trans
, dir
);
1129 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1135 inode
= btrfs_new_inode(trans
, root
, objectid
,
1136 BTRFS_I(dir
)->block_group
, mode
);
1137 err
= PTR_ERR(inode
);
1141 btrfs_set_trans_block_group(trans
, inode
);
1142 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1146 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1147 inode
->i_fop
= &btrfs_file_operations
;
1148 inode
->i_op
= &btrfs_file_inode_operations
;
1149 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1150 inode
->i_mapping
, GFP_NOFS
);
1151 BTRFS_I(inode
)->extent_tree
.fill_delalloc
= run_delalloc_range
;
1153 dir
->i_sb
->s_dirt
= 1;
1154 btrfs_update_inode_block_group(trans
, inode
);
1155 btrfs_update_inode_block_group(trans
, dir
);
1157 btrfs_end_transaction(trans
, root
);
1158 mutex_unlock(&root
->fs_info
->fs_mutex
);
1161 inode_dec_link_count(inode
);
1164 btrfs_btree_balance_dirty(root
);
1168 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
1169 struct dentry
*dentry
)
1171 struct btrfs_trans_handle
*trans
;
1172 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1173 struct inode
*inode
= old_dentry
->d_inode
;
1177 if (inode
->i_nlink
== 0)
1181 mutex_lock(&root
->fs_info
->fs_mutex
);
1182 trans
= btrfs_start_transaction(root
, 1);
1183 btrfs_set_trans_block_group(trans
, dir
);
1184 atomic_inc(&inode
->i_count
);
1185 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1188 dir
->i_sb
->s_dirt
= 1;
1189 btrfs_update_inode_block_group(trans
, dir
);
1190 err
= btrfs_update_inode(trans
, root
, inode
);
1194 btrfs_end_transaction(trans
, root
);
1195 mutex_unlock(&root
->fs_info
->fs_mutex
);
1198 inode_dec_link_count(inode
);
1201 btrfs_btree_balance_dirty(root
);
1205 static int btrfs_make_empty_dir(struct btrfs_trans_handle
*trans
,
1206 struct btrfs_root
*root
,
1207 u64 objectid
, u64 dirid
)
1211 struct btrfs_key key
;
1216 key
.objectid
= objectid
;
1219 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1221 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 1, objectid
,
1222 &key
, BTRFS_FT_DIR
);
1225 key
.objectid
= dirid
;
1226 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 2, objectid
,
1227 &key
, BTRFS_FT_DIR
);
1234 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1236 struct inode
*inode
;
1237 struct btrfs_trans_handle
*trans
;
1238 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1240 int drop_on_err
= 0;
1243 mutex_lock(&root
->fs_info
->fs_mutex
);
1244 trans
= btrfs_start_transaction(root
, 1);
1245 btrfs_set_trans_block_group(trans
, dir
);
1246 if (IS_ERR(trans
)) {
1247 err
= PTR_ERR(trans
);
1251 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1257 inode
= btrfs_new_inode(trans
, root
, objectid
,
1258 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
1259 if (IS_ERR(inode
)) {
1260 err
= PTR_ERR(inode
);
1264 inode
->i_op
= &btrfs_dir_inode_operations
;
1265 inode
->i_fop
= &btrfs_dir_file_operations
;
1266 btrfs_set_trans_block_group(trans
, inode
);
1268 err
= btrfs_make_empty_dir(trans
, root
, inode
->i_ino
, dir
->i_ino
);
1273 err
= btrfs_update_inode(trans
, root
, inode
);
1276 err
= btrfs_add_link(trans
, dentry
, inode
);
1279 d_instantiate(dentry
, inode
);
1281 dir
->i_sb
->s_dirt
= 1;
1282 btrfs_update_inode_block_group(trans
, inode
);
1283 btrfs_update_inode_block_group(trans
, dir
);
1286 btrfs_end_transaction(trans
, root
);
1288 mutex_unlock(&root
->fs_info
->fs_mutex
);
1291 btrfs_btree_balance_dirty(root
);
1295 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
1296 size_t page_offset
, u64 start
, u64 end
,
1302 u64 extent_start
= 0;
1304 u64 objectid
= inode
->i_ino
;
1306 int failed_insert
= 0;
1307 struct btrfs_path
*path
;
1308 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1309 struct btrfs_file_extent_item
*item
;
1310 struct btrfs_leaf
*leaf
;
1311 struct btrfs_disk_key
*found_key
;
1312 struct extent_map
*em
= NULL
;
1313 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1314 struct btrfs_trans_handle
*trans
= NULL
;
1316 path
= btrfs_alloc_path();
1318 mutex_lock(&root
->fs_info
->fs_mutex
);
1321 em
= lookup_extent_mapping(em_tree
, start
, end
);
1326 em
= alloc_extent_map(GFP_NOFS
);
1334 em
->bdev
= inode
->i_sb
->s_bdev
;
1335 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
1336 objectid
, start
, 0);
1343 if (path
->slots
[0] == 0)
1348 item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]), path
->slots
[0],
1349 struct btrfs_file_extent_item
);
1350 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
1351 blocknr
= btrfs_file_extent_disk_blocknr(item
);
1352 blocknr
+= btrfs_file_extent_offset(item
);
1354 /* are we inside the extent that was found? */
1355 found_key
= &leaf
->items
[path
->slots
[0]].key
;
1356 found_type
= btrfs_disk_key_type(found_key
);
1357 if (btrfs_disk_key_objectid(found_key
) != objectid
||
1358 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
1362 found_type
= btrfs_file_extent_type(item
);
1363 extent_start
= btrfs_disk_key_offset(&leaf
->items
[path
->slots
[0]].key
);
1364 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1365 extent_end
= extent_start
+
1366 (btrfs_file_extent_num_blocks(item
) << inode
->i_blkbits
);
1368 if (start
< extent_start
|| start
>= extent_end
) {
1370 if (start
< extent_start
) {
1371 if (end
< extent_start
)
1373 em
->end
= extent_end
- 1;
1379 if (btrfs_file_extent_disk_blocknr(item
) == 0) {
1380 em
->start
= extent_start
;
1381 em
->end
= extent_end
- 1;
1382 em
->block_start
= 0;
1386 em
->block_start
= blocknr
<< inode
->i_blkbits
;
1387 em
->block_end
= em
->block_start
+
1388 (btrfs_file_extent_num_blocks(item
) <<
1389 inode
->i_blkbits
) - 1;
1390 em
->start
= extent_start
;
1391 em
->end
= extent_end
- 1;
1393 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1398 size
= btrfs_file_extent_inline_len(leaf
->items
+
1400 extent_end
= extent_start
+ size
;
1401 if (start
< extent_start
|| start
>= extent_end
) {
1403 if (start
< extent_start
) {
1404 if (end
< extent_start
)
1406 em
->end
= extent_end
- 1;
1412 em
->block_start
= EXTENT_MAP_INLINE
;
1413 em
->block_end
= EXTENT_MAP_INLINE
;
1414 em
->start
= extent_start
;
1415 em
->end
= extent_end
- 1;
1419 ptr
= btrfs_file_extent_inline_start(item
);
1421 memcpy(map
+ page_offset
, ptr
, size
);
1422 flush_dcache_page(result
->b_page
);
1424 set_extent_uptodate(em_tree
, extent_start
,
1425 extent_end
, GFP_NOFS
);
1428 printk("unkknown found_type %d\n", found_type
);
1435 em
->block_start
= 0;
1438 btrfs_release_path(root
, path
);
1439 if (em
->start
> start
|| em
->end
< start
) {
1440 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->end
, start
, end
);
1444 ret
= add_extent_mapping(em_tree
, em
);
1445 if (ret
== -EEXIST
) {
1446 free_extent_map(em
);
1448 if (failed_insert
> 5) {
1449 printk("failing to insert %Lu %Lu\n", start
, end
);
1458 btrfs_free_path(path
);
1460 ret
= btrfs_end_transaction(trans
, root
);
1464 mutex_unlock(&root
->fs_info
->fs_mutex
);
1466 free_extent_map(em
);
1468 return ERR_PTR(err
);
1475 * FIBMAP and others want to pass in a fake buffer head. They need to
1476 * use BTRFS_GET_BLOCK_NO_DIRECT to make sure we don't try to memcpy
1477 * any packed file data into the fake bh
1479 #define BTRFS_GET_BLOCK_NO_CREATE 0
1480 #define BTRFS_GET_BLOCK_CREATE 1
1481 #define BTRFS_GET_BLOCK_NO_DIRECT 2
1484 * FIXME create==1 doe not work.
1486 static int btrfs_get_block_lock(struct inode
*inode
, sector_t iblock
,
1487 struct buffer_head
*result
, int create
)
1492 u64 extent_start
= 0;
1494 u64 objectid
= inode
->i_ino
;
1497 struct btrfs_path
*path
;
1498 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1499 struct btrfs_file_extent_item
*item
;
1500 struct btrfs_leaf
*leaf
;
1501 struct btrfs_disk_key
*found_key
;
1502 struct btrfs_trans_handle
*trans
= NULL
;
1504 path
= btrfs_alloc_path();
1506 if (create
& BTRFS_GET_BLOCK_CREATE
) {
1508 * danger!, this only works if the page is properly up
1511 trans
= btrfs_start_transaction(root
, 1);
1516 ret
= btrfs_drop_extents(trans
, root
, inode
,
1517 iblock
<< inode
->i_blkbits
,
1518 (iblock
+ 1) << inode
->i_blkbits
,
1523 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
1525 iblock
<< inode
->i_blkbits
, 0);
1532 if (path
->slots
[0] == 0) {
1533 btrfs_release_path(root
, path
);
1539 item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]), path
->slots
[0],
1540 struct btrfs_file_extent_item
);
1541 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
1542 blocknr
= btrfs_file_extent_disk_blocknr(item
);
1543 blocknr
+= btrfs_file_extent_offset(item
);
1545 /* are we inside the extent that was found? */
1546 found_key
= &leaf
->items
[path
->slots
[0]].key
;
1547 found_type
= btrfs_disk_key_type(found_key
);
1548 if (btrfs_disk_key_objectid(found_key
) != objectid
||
1549 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
1554 found_type
= btrfs_file_extent_type(item
);
1555 extent_start
= btrfs_disk_key_offset(&leaf
->items
[path
->slots
[0]].key
);
1556 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1557 extent_start
= extent_start
>> inode
->i_blkbits
;
1558 extent_end
= extent_start
+ btrfs_file_extent_num_blocks(item
);
1560 if (btrfs_file_extent_disk_blocknr(item
) == 0)
1562 if (iblock
>= extent_start
&& iblock
< extent_end
) {
1563 btrfs_map_bh_to_logical(root
, result
, blocknr
+
1564 iblock
- extent_start
);
1567 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1572 if (create
& BTRFS_GET_BLOCK_NO_DIRECT
) {
1576 size
= btrfs_file_extent_inline_len(leaf
->items
+
1578 extent_end
= (extent_start
+ size
) >> inode
->i_blkbits
;
1579 extent_start
>>= inode
->i_blkbits
;
1580 if (iblock
< extent_start
|| iblock
> extent_end
) {
1583 ptr
= btrfs_file_extent_inline_start(item
);
1584 map
= kmap(result
->b_page
);
1585 memcpy(map
, ptr
, size
);
1586 memset(map
+ size
, 0, PAGE_CACHE_SIZE
- size
);
1587 flush_dcache_page(result
->b_page
);
1588 kunmap(result
->b_page
);
1589 set_buffer_uptodate(result
);
1590 SetPageChecked(result
->b_page
);
1591 btrfs_map_bh_to_logical(root
, result
, 0);
1594 if (create
& BTRFS_GET_BLOCK_CREATE
) {
1595 struct btrfs_key ins
;
1596 ret
= btrfs_alloc_extent(trans
, root
, inode
->i_ino
,
1597 1, 0, alloc_hint
, (u64
)-1,
1603 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
1604 iblock
<< inode
->i_blkbits
,
1605 ins
.objectid
, ins
.offset
,
1611 btrfs_map_bh_to_logical(root
, result
, ins
.objectid
);
1615 ret
= btrfs_end_transaction(trans
, root
);
1619 btrfs_free_path(path
);
1623 int btrfs_get_block(struct inode
*inode
, sector_t iblock
,
1624 struct buffer_head
*result
, int create
)
1627 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1628 mutex_lock(&root
->fs_info
->fs_mutex
);
1629 err
= btrfs_get_block_lock(inode
, iblock
, result
, create
);
1630 mutex_unlock(&root
->fs_info
->fs_mutex
);
1634 static int btrfs_get_block_bmap(struct inode
*inode
, sector_t iblock
,
1635 struct buffer_head
*result
, int create
)
1637 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1638 u64 start
= iblock
<< inode
->i_blkbits
;
1639 u64 end
= start
+ root
->blocksize
-1;
1640 struct extent_map
*em
;
1642 em
= btrfs_get_extent(inode
, NULL
, 0, start
, end
, 0);
1643 if (em
&& !IS_ERR(em
) && em
->block_start
!= EXTENT_MAP_INLINE
&&
1644 em
->block_start
!= 0) {
1646 offset
= start
- em
->start
;
1647 start
= (em
->block_start
+ offset
) >> inode
->i_blkbits
;
1648 btrfs_map_bh_to_logical(root
, result
, start
);
1653 static sector_t
btrfs_bmap(struct address_space
*as
, sector_t block
)
1655 return generic_block_bmap(as
, block
, btrfs_get_block_bmap
);
1658 static int btrfs_prepare_write(struct file
*file
, struct page
*page
,
1659 unsigned from
, unsigned to
)
1661 return extent_prepare_write(&BTRFS_I(page
->mapping
->host
)->extent_tree
,
1662 page
->mapping
->host
, page
, from
, to
,
1666 int btrfs_readpage(struct file
*file
, struct page
*page
)
1668 struct extent_map_tree
*tree
;
1669 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1670 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
1672 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
1674 struct extent_map_tree
*tree
;
1677 if (current
->flags
& PF_MEMALLOC
) {
1678 redirty_page_for_writepage(wbc
, page
);
1682 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1683 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
1686 static int btrfs_releasepage(struct page
*page
, gfp_t unused_gfp_flags
)
1688 struct extent_map_tree
*tree
;
1691 if (page
->private != 1) {
1693 return try_to_free_buffers(page
);
1695 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1696 ret
= try_release_extent_mapping(tree
, page
);
1698 ClearPagePrivate(page
);
1699 set_page_private(page
, 0);
1700 page_cache_release(page
);
1705 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
1707 struct extent_map_tree
*tree
;
1709 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1710 extent_invalidatepage(tree
, page
, offset
);
1711 btrfs_releasepage(page
, GFP_NOFS
);
1715 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1716 * called from a page fault handler when a page is first dirtied. Hence we must
1717 * be careful to check for EOF conditions here. We set the page up correctly
1718 * for a written page which means we get ENOSPC checking when writing into
1719 * holes and correct delalloc and unwritten extent mapping on filesystems that
1720 * support these features.
1722 * We are not allowed to take the i_mutex here so we have to play games to
1723 * protect against truncate races as the page could now be beyond EOF. Because
1724 * vmtruncate() writes the inode size before removing pages, once we have the
1725 * page lock we can determine safely if the page is beyond EOF. If it is not
1726 * beyond EOF, then the page is guaranteed safe against truncation until we
1729 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
1731 struct inode
*inode
= vma
->vm_file
->f_path
.dentry
->d_inode
;
1738 wait_on_page_writeback(page
);
1739 size
= i_size_read(inode
);
1740 page_start
= page
->index
<< PAGE_CACHE_SHIFT
;
1742 if ((page
->mapping
!= inode
->i_mapping
) ||
1743 (page_start
> size
)) {
1744 /* page got truncated out from underneath us */
1748 /* page is wholly or partially inside EOF */
1749 if (page_start
+ PAGE_CACHE_SIZE
> size
)
1750 end
= size
& ~PAGE_CACHE_MASK
;
1752 end
= PAGE_CACHE_SIZE
;
1754 ret
= btrfs_cow_one_page(inode
, page
, end
);
1761 static void btrfs_truncate(struct inode
*inode
)
1763 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1765 struct btrfs_trans_handle
*trans
;
1767 if (!S_ISREG(inode
->i_mode
))
1769 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1772 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1774 mutex_lock(&root
->fs_info
->fs_mutex
);
1775 trans
= btrfs_start_transaction(root
, 1);
1776 btrfs_set_trans_block_group(trans
, inode
);
1778 /* FIXME, add redo link to tree so we don't leak on crash */
1779 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
1780 btrfs_update_inode(trans
, root
, inode
);
1781 ret
= btrfs_end_transaction(trans
, root
);
1783 mutex_unlock(&root
->fs_info
->fs_mutex
);
1784 btrfs_btree_balance_dirty(root
);
1787 int btrfs_commit_write(struct file
*file
, struct page
*page
,
1788 unsigned from
, unsigned to
)
1790 return extent_commit_write(&BTRFS_I(page
->mapping
->host
)->extent_tree
,
1791 page
->mapping
->host
, page
, from
, to
);
1794 static int create_subvol(struct btrfs_root
*root
, char *name
, int namelen
)
1796 struct btrfs_trans_handle
*trans
;
1797 struct btrfs_key key
;
1798 struct btrfs_root_item root_item
;
1799 struct btrfs_inode_item
*inode_item
;
1800 struct buffer_head
*subvol
;
1801 struct btrfs_leaf
*leaf
;
1802 struct btrfs_root
*new_root
;
1803 struct inode
*inode
;
1808 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
1810 mutex_lock(&root
->fs_info
->fs_mutex
);
1811 trans
= btrfs_start_transaction(root
, 1);
1814 subvol
= btrfs_alloc_free_block(trans
, root
, 0, 0);
1816 return PTR_ERR(subvol
);
1817 leaf
= btrfs_buffer_leaf(subvol
);
1818 btrfs_set_header_nritems(&leaf
->header
, 0);
1819 btrfs_set_header_level(&leaf
->header
, 0);
1820 btrfs_set_header_blocknr(&leaf
->header
, bh_blocknr(subvol
));
1821 btrfs_set_header_generation(&leaf
->header
, trans
->transid
);
1822 btrfs_set_header_owner(&leaf
->header
, root
->root_key
.objectid
);
1823 memcpy(leaf
->header
.fsid
, root
->fs_info
->disk_super
->fsid
,
1824 sizeof(leaf
->header
.fsid
));
1825 btrfs_mark_buffer_dirty(subvol
);
1827 inode_item
= &root_item
.inode
;
1828 memset(inode_item
, 0, sizeof(*inode_item
));
1829 btrfs_set_inode_generation(inode_item
, 1);
1830 btrfs_set_inode_size(inode_item
, 3);
1831 btrfs_set_inode_nlink(inode_item
, 1);
1832 btrfs_set_inode_nblocks(inode_item
, 1);
1833 btrfs_set_inode_mode(inode_item
, S_IFDIR
| 0755);
1835 btrfs_set_root_blocknr(&root_item
, bh_blocknr(subvol
));
1836 btrfs_set_root_refs(&root_item
, 1);
1837 btrfs_set_root_blocks_used(&root_item
, 0);
1838 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
1839 root_item
.drop_level
= 0;
1843 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
1848 btrfs_set_root_dirid(&root_item
, new_dirid
);
1850 key
.objectid
= objectid
;
1853 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
1854 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
1860 * insert the directory item
1862 key
.offset
= (u64
)-1;
1863 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
1864 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
1865 name
, namelen
, dir
->i_ino
, &key
,
1870 ret
= btrfs_commit_transaction(trans
, root
);
1874 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
, name
, namelen
);
1877 trans
= btrfs_start_transaction(new_root
, 1);
1880 inode
= btrfs_new_inode(trans
, new_root
, new_dirid
,
1881 BTRFS_I(dir
)->block_group
, S_IFDIR
| 0700);
1884 inode
->i_op
= &btrfs_dir_inode_operations
;
1885 inode
->i_fop
= &btrfs_dir_file_operations
;
1886 new_root
->inode
= inode
;
1888 ret
= btrfs_make_empty_dir(trans
, new_root
, new_dirid
, new_dirid
);
1894 ret
= btrfs_update_inode(trans
, new_root
, inode
);
1898 err
= btrfs_commit_transaction(trans
, root
);
1902 mutex_unlock(&root
->fs_info
->fs_mutex
);
1903 btrfs_btree_balance_dirty(root
);
1907 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
1909 struct btrfs_trans_handle
*trans
;
1910 struct btrfs_key key
;
1911 struct btrfs_root_item new_root_item
;
1912 struct buffer_head
*tmp
;
1917 if (!root
->ref_cows
)
1920 mutex_lock(&root
->fs_info
->fs_mutex
);
1921 trans
= btrfs_start_transaction(root
, 1);
1924 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
1928 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
1933 memcpy(&new_root_item
, &root
->root_item
,
1934 sizeof(new_root_item
));
1936 key
.objectid
= objectid
;
1939 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
1940 btrfs_cow_block(trans
, root
, root
->node
, NULL
, 0, &tmp
);
1941 btrfs_set_root_blocknr(&new_root_item
, bh_blocknr(root
->node
));
1943 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
1949 * insert the directory item
1951 key
.offset
= (u64
)-1;
1952 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
1954 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
1955 &key
, BTRFS_FT_DIR
);
1960 ret
= btrfs_inc_root_ref(trans
, root
);
1965 err
= btrfs_commit_transaction(trans
, root
);
1968 mutex_unlock(&root
->fs_info
->fs_mutex
);
1969 btrfs_btree_balance_dirty(root
);
1973 int btrfs_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int
1974 cmd
, unsigned long arg
)
1976 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1977 struct btrfs_ioctl_vol_args vol_args
;
1979 struct btrfs_dir_item
*di
;
1981 struct btrfs_path
*path
;
1985 case BTRFS_IOC_SNAP_CREATE
:
1986 if (copy_from_user(&vol_args
,
1987 (struct btrfs_ioctl_vol_args __user
*)arg
,
1990 namelen
= strlen(vol_args
.name
);
1991 if (namelen
> BTRFS_VOL_NAME_MAX
)
1993 if (strchr(vol_args
.name
, '/'))
1995 path
= btrfs_alloc_path();
1998 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
1999 mutex_lock(&root
->fs_info
->fs_mutex
);
2000 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
2002 vol_args
.name
, namelen
, 0);
2003 mutex_unlock(&root
->fs_info
->fs_mutex
);
2004 btrfs_free_path(path
);
2005 if (di
&& !IS_ERR(di
))
2010 if (root
== root
->fs_info
->tree_root
)
2011 ret
= create_subvol(root
, vol_args
.name
, namelen
);
2013 ret
= create_snapshot(root
, vol_args
.name
, namelen
);
2016 case BTRFS_IOC_DEFRAG
:
2017 mutex_lock(&root
->fs_info
->fs_mutex
);
2018 btrfs_defrag_root(root
, 0);
2019 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
2020 mutex_unlock(&root
->fs_info
->fs_mutex
);
2029 #ifdef CONFIG_COMPAT
2030 long btrfs_compat_ioctl(struct file
*file
, unsigned int cmd
,
2033 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
2036 ret
= btrfs_ioctl(inode
, file
, cmd
, (unsigned long) compat_ptr(arg
));
2044 * Called inside transaction, so use GFP_NOFS
2046 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2048 struct btrfs_inode
*ei
;
2050 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2054 return &ei
->vfs_inode
;
2057 void btrfs_destroy_inode(struct inode
*inode
)
2059 WARN_ON(!list_empty(&inode
->i_dentry
));
2060 WARN_ON(inode
->i_data
.nrpages
);
2062 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2065 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2066 unsigned long flags
)
2068 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2070 inode_init_once(&ei
->vfs_inode
);
2073 void btrfs_destroy_cachep(void)
2075 if (btrfs_inode_cachep
)
2076 kmem_cache_destroy(btrfs_inode_cachep
);
2077 if (btrfs_trans_handle_cachep
)
2078 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2079 if (btrfs_transaction_cachep
)
2080 kmem_cache_destroy(btrfs_transaction_cachep
);
2081 if (btrfs_bit_radix_cachep
)
2082 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2083 if (btrfs_path_cachep
)
2084 kmem_cache_destroy(btrfs_path_cachep
);
2087 static struct kmem_cache
*cache_create(const char *name
, size_t size
,
2088 unsigned long extra_flags
,
2089 void (*ctor
)(void *, struct kmem_cache
*,
2092 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
2093 SLAB_MEM_SPREAD
| extra_flags
), ctor
2094 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2100 int btrfs_init_cachep(void)
2102 btrfs_inode_cachep
= cache_create("btrfs_inode_cache",
2103 sizeof(struct btrfs_inode
),
2105 if (!btrfs_inode_cachep
)
2107 btrfs_trans_handle_cachep
= cache_create("btrfs_trans_handle_cache",
2108 sizeof(struct btrfs_trans_handle
),
2110 if (!btrfs_trans_handle_cachep
)
2112 btrfs_transaction_cachep
= cache_create("btrfs_transaction_cache",
2113 sizeof(struct btrfs_transaction
),
2115 if (!btrfs_transaction_cachep
)
2117 btrfs_path_cachep
= cache_create("btrfs_path_cache",
2118 sizeof(struct btrfs_transaction
),
2120 if (!btrfs_path_cachep
)
2122 btrfs_bit_radix_cachep
= cache_create("btrfs_radix", 256,
2123 SLAB_DESTROY_BY_RCU
, NULL
);
2124 if (!btrfs_bit_radix_cachep
)
2128 btrfs_destroy_cachep();
2132 static int btrfs_getattr(struct vfsmount
*mnt
,
2133 struct dentry
*dentry
, struct kstat
*stat
)
2135 struct inode
*inode
= dentry
->d_inode
;
2136 generic_fillattr(inode
, stat
);
2137 stat
->blksize
= 256 * 1024;
2141 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
2142 struct inode
* new_dir
,struct dentry
*new_dentry
)
2144 struct btrfs_trans_handle
*trans
;
2145 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
2146 struct inode
*new_inode
= new_dentry
->d_inode
;
2147 struct inode
*old_inode
= old_dentry
->d_inode
;
2148 struct timespec ctime
= CURRENT_TIME
;
2149 struct btrfs_path
*path
;
2150 struct btrfs_dir_item
*di
;
2153 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
2154 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
2157 mutex_lock(&root
->fs_info
->fs_mutex
);
2158 trans
= btrfs_start_transaction(root
, 1);
2159 btrfs_set_trans_block_group(trans
, new_dir
);
2160 path
= btrfs_alloc_path();
2166 old_dentry
->d_inode
->i_nlink
++;
2167 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
2168 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
2169 old_inode
->i_ctime
= ctime
;
2170 if (S_ISDIR(old_inode
->i_mode
) && old_dir
!= new_dir
) {
2171 struct btrfs_key
*location
= &BTRFS_I(new_dir
)->location
;
2173 di
= btrfs_lookup_dir_item(trans
, root
, path
, old_inode
->i_ino
,
2183 old_parent_oid
= btrfs_disk_key_objectid(&di
->location
);
2184 ret
= btrfs_del_item(trans
, root
, path
);
2188 btrfs_release_path(root
, path
);
2190 di
= btrfs_lookup_dir_index_item(trans
, root
, path
,
2202 ret
= btrfs_del_item(trans
, root
, path
);
2206 btrfs_release_path(root
, path
);
2208 ret
= btrfs_insert_dir_item(trans
, root
, "..", 2,
2209 old_inode
->i_ino
, location
,
2216 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
2221 new_inode
->i_ctime
= CURRENT_TIME
;
2222 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
2225 if (S_ISDIR(new_inode
->i_mode
))
2226 clear_nlink(new_inode
);
2228 drop_nlink(new_inode
);
2229 ret
= btrfs_update_inode(trans
, root
, new_inode
);
2233 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
);
2238 btrfs_free_path(path
);
2239 btrfs_end_transaction(trans
, root
);
2240 mutex_unlock(&root
->fs_info
->fs_mutex
);
2244 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
2245 const char *symname
)
2247 struct btrfs_trans_handle
*trans
;
2248 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2249 struct btrfs_path
*path
;
2250 struct btrfs_key key
;
2251 struct inode
*inode
;
2258 struct btrfs_file_extent_item
*ei
;
2260 name_len
= strlen(symname
) + 1;
2261 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
2262 return -ENAMETOOLONG
;
2263 mutex_lock(&root
->fs_info
->fs_mutex
);
2264 trans
= btrfs_start_transaction(root
, 1);
2265 btrfs_set_trans_block_group(trans
, dir
);
2267 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2273 inode
= btrfs_new_inode(trans
, root
, objectid
,
2274 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
2275 err
= PTR_ERR(inode
);
2279 btrfs_set_trans_block_group(trans
, inode
);
2280 err
= btrfs_add_nondir(trans
, dentry
, inode
);
2284 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2285 inode
->i_fop
= &btrfs_file_operations
;
2286 inode
->i_op
= &btrfs_file_inode_operations
;
2287 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
2288 inode
->i_mapping
, GFP_NOFS
);
2289 BTRFS_I(inode
)->extent_tree
.fill_delalloc
= run_delalloc_range
;
2291 dir
->i_sb
->s_dirt
= 1;
2292 btrfs_update_inode_block_group(trans
, inode
);
2293 btrfs_update_inode_block_group(trans
, dir
);
2297 path
= btrfs_alloc_path();
2299 key
.objectid
= inode
->i_ino
;
2302 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
2303 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
2304 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2310 ei
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
2311 path
->slots
[0], struct btrfs_file_extent_item
);
2312 btrfs_set_file_extent_generation(ei
, trans
->transid
);
2313 btrfs_set_file_extent_type(ei
,
2314 BTRFS_FILE_EXTENT_INLINE
);
2315 ptr
= btrfs_file_extent_inline_start(ei
);
2316 btrfs_memcpy(root
, path
->nodes
[0]->b_data
,
2317 ptr
, symname
, name_len
);
2318 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2319 btrfs_free_path(path
);
2320 inode
->i_op
= &btrfs_symlink_inode_operations
;
2321 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2322 inode
->i_size
= name_len
- 1;
2323 err
= btrfs_update_inode(trans
, root
, inode
);
2328 btrfs_end_transaction(trans
, root
);
2329 mutex_unlock(&root
->fs_info
->fs_mutex
);
2331 inode_dec_link_count(inode
);
2334 btrfs_btree_balance_dirty(root
);
2338 static struct inode_operations btrfs_dir_inode_operations
= {
2339 .lookup
= btrfs_lookup
,
2340 .create
= btrfs_create
,
2341 .unlink
= btrfs_unlink
,
2343 .mkdir
= btrfs_mkdir
,
2344 .rmdir
= btrfs_rmdir
,
2345 .rename
= btrfs_rename
,
2346 .symlink
= btrfs_symlink
,
2347 .setattr
= btrfs_setattr
,
2348 .mknod
= btrfs_mknod
,
2351 static struct inode_operations btrfs_dir_ro_inode_operations
= {
2352 .lookup
= btrfs_lookup
,
2355 static struct file_operations btrfs_dir_file_operations
= {
2356 .llseek
= generic_file_llseek
,
2357 .read
= generic_read_dir
,
2358 .readdir
= btrfs_readdir
,
2359 .ioctl
= btrfs_ioctl
,
2360 #ifdef CONFIG_COMPAT
2361 .compat_ioctl
= btrfs_compat_ioctl
,
2365 static struct address_space_operations btrfs_aops
= {
2366 .readpage
= btrfs_readpage
,
2367 .writepage
= btrfs_writepage
,
2368 .sync_page
= block_sync_page
,
2369 .prepare_write
= btrfs_prepare_write
,
2370 .commit_write
= btrfs_commit_write
,
2372 .invalidatepage
= btrfs_invalidatepage
,
2373 .releasepage
= btrfs_releasepage
,
2374 .set_page_dirty
= __set_page_dirty_nobuffers
,
2377 static struct address_space_operations btrfs_symlink_aops
= {
2378 .readpage
= btrfs_readpage
,
2379 .writepage
= btrfs_writepage
,
2382 static struct inode_operations btrfs_file_inode_operations
= {
2383 .truncate
= btrfs_truncate
,
2384 .getattr
= btrfs_getattr
,
2385 .setattr
= btrfs_setattr
,
2388 static struct inode_operations btrfs_special_inode_operations
= {
2389 .getattr
= btrfs_getattr
,
2390 .setattr
= btrfs_setattr
,
2393 static struct inode_operations btrfs_symlink_inode_operations
= {
2394 .readlink
= generic_readlink
,
2395 .follow_link
= page_follow_link_light
,
2396 .put_link
= page_put_link
,