2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/buffer_head.h>
21 #include <linux/pagemap.h>
22 #include <linux/highmem.h>
23 #include <linux/time.h>
24 #include <linux/init.h>
25 #include <linux/string.h>
26 #include <linux/smp_lock.h>
27 #include <linux/backing-dev.h>
28 #include <linux/mpage.h>
29 #include <linux/swap.h>
30 #include <linux/writeback.h>
31 #include <linux/statfs.h>
32 #include <linux/compat.h>
33 #include <linux/bit_spinlock.h>
34 #include <linux/version.h>
37 #include "transaction.h"
38 #include "btrfs_inode.h"
40 #include "print-tree.h"
42 struct btrfs_iget_args
{
44 struct btrfs_root
*root
;
47 static struct inode_operations btrfs_dir_inode_operations
;
48 static struct inode_operations btrfs_symlink_inode_operations
;
49 static struct inode_operations btrfs_dir_ro_inode_operations
;
50 static struct inode_operations btrfs_special_inode_operations
;
51 static struct inode_operations btrfs_file_inode_operations
;
52 static struct address_space_operations btrfs_aops
;
53 static struct address_space_operations btrfs_symlink_aops
;
54 static struct file_operations btrfs_dir_file_operations
;
55 static struct extent_map_ops btrfs_extent_map_ops
;
57 static struct kmem_cache
*btrfs_inode_cachep
;
58 struct kmem_cache
*btrfs_trans_handle_cachep
;
59 struct kmem_cache
*btrfs_transaction_cachep
;
60 struct kmem_cache
*btrfs_bit_radix_cachep
;
61 struct kmem_cache
*btrfs_path_cachep
;
64 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
65 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
66 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
67 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
68 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
69 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
70 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
71 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
74 static int run_delalloc_range(struct inode
*inode
, u64 start
, u64 end
)
76 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
77 struct btrfs_trans_handle
*trans
;
82 u64 blocksize
= 1 << inode
->i_blkbits
;
84 mutex_lock(&root
->fs_info
->fs_mutex
);
85 trans
= btrfs_start_transaction(root
, 1);
86 btrfs_set_trans_block_group(trans
, inode
);
88 num_blocks
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
89 ret
= btrfs_drop_extents(trans
, root
, inode
,
90 start
, start
+ num_blocks
, &alloc_hint
);
91 num_blocks
= num_blocks
>> inode
->i_blkbits
;
92 ret
= btrfs_alloc_extent(trans
, root
, inode
->i_ino
, num_blocks
, 0,
93 alloc_hint
, (u64
)-1, &ins
, 1);
98 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
99 start
, ins
.objectid
, ins
.offset
,
102 btrfs_end_transaction(trans
, root
);
103 mutex_unlock(&root
->fs_info
->fs_mutex
);
107 int btrfs_writepage_io_hook(struct page
*page
, u64 start
, u64 end
)
109 struct inode
*inode
= page
->mapping
->host
;
110 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
111 struct btrfs_trans_handle
*trans
;
114 u64 page_start
= page
->index
<< PAGE_CACHE_SHIFT
;
115 size_t offset
= start
- page_start
;
117 mutex_lock(&root
->fs_info
->fs_mutex
);
118 trans
= btrfs_start_transaction(root
, 1);
119 btrfs_set_trans_block_group(trans
, inode
);
121 btrfs_csum_file_block(trans
, root
, inode
->i_ino
,
122 start
, kaddr
+ offset
, end
- start
+ 1);
124 ret
= btrfs_end_transaction(trans
, root
);
126 mutex_unlock(&root
->fs_info
->fs_mutex
);
130 int btrfs_readpage_io_hook(struct page
*page
, u64 start
, u64 end
)
133 struct inode
*inode
= page
->mapping
->host
;
134 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
135 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
136 struct btrfs_csum_item
*item
;
137 struct btrfs_path
*path
= NULL
;
140 mutex_lock(&root
->fs_info
->fs_mutex
);
141 path
= btrfs_alloc_path();
142 item
= btrfs_lookup_csum(NULL
, root
, path
, inode
->i_ino
, start
, 0);
145 /* a csum that isn't present is a preallocated region. */
146 if (ret
== -ENOENT
|| ret
== -EFBIG
)
151 memcpy((char *)&private, &item
->csum
, BTRFS_CRC32_SIZE
);
152 set_state_private(em_tree
, start
, private);
155 btrfs_free_path(path
);
156 mutex_unlock(&root
->fs_info
->fs_mutex
);
160 int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
)
162 char csum
[BTRFS_CRC32_SIZE
];
163 size_t offset
= start
- (page
->index
<< PAGE_CACHE_SHIFT
);
164 struct inode
*inode
= page
->mapping
->host
;
165 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
166 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
171 ret
= get_state_private(em_tree
, start
, &private);
172 kaddr
= kmap_atomic(page
, KM_IRQ0
);
176 ret
= btrfs_csum_data(root
, kaddr
+ offset
, end
- start
+ 1, csum
);
178 if (memcmp(csum
, &private, BTRFS_CRC32_SIZE
)) {
181 kunmap_atomic(kaddr
, KM_IRQ0
);
185 printk("btrfs csum failed ino %lu off %llu\n",
186 page
->mapping
->host
->i_ino
, (unsigned long long)start
);
187 memset(kaddr
+ offset
, 1, end
- start
+ 1); flush_dcache_page(page
);
188 kunmap_atomic(kaddr
, KM_IRQ0
);
192 void btrfs_read_locked_inode(struct inode
*inode
)
194 struct btrfs_path
*path
;
195 struct btrfs_inode_item
*inode_item
;
196 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
197 struct btrfs_key location
;
198 u64 alloc_group_block
;
202 path
= btrfs_alloc_path();
204 mutex_lock(&root
->fs_info
->fs_mutex
);
206 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
207 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
209 btrfs_free_path(path
);
212 inode_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
214 struct btrfs_inode_item
);
216 inode
->i_mode
= btrfs_inode_mode(inode_item
);
217 inode
->i_nlink
= btrfs_inode_nlink(inode_item
);
218 inode
->i_uid
= btrfs_inode_uid(inode_item
);
219 inode
->i_gid
= btrfs_inode_gid(inode_item
);
220 inode
->i_size
= btrfs_inode_size(inode_item
);
221 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(&inode_item
->atime
);
222 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->atime
);
223 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(&inode_item
->mtime
);
224 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->mtime
);
225 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(&inode_item
->ctime
);
226 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->ctime
);
227 inode
->i_blocks
= btrfs_inode_nblocks(inode_item
);
228 inode
->i_generation
= btrfs_inode_generation(inode_item
);
230 rdev
= btrfs_inode_rdev(inode_item
);
231 alloc_group_block
= btrfs_inode_block_group(inode_item
);
232 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
235 btrfs_free_path(path
);
238 mutex_unlock(&root
->fs_info
->fs_mutex
);
240 switch (inode
->i_mode
& S_IFMT
) {
242 inode
->i_mapping
->a_ops
= &btrfs_aops
;
243 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
244 inode
->i_fop
= &btrfs_file_operations
;
245 inode
->i_op
= &btrfs_file_inode_operations
;
248 inode
->i_fop
= &btrfs_dir_file_operations
;
249 if (root
== root
->fs_info
->tree_root
)
250 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
252 inode
->i_op
= &btrfs_dir_inode_operations
;
255 inode
->i_op
= &btrfs_symlink_inode_operations
;
256 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
259 init_special_inode(inode
, inode
->i_mode
, rdev
);
265 btrfs_release_path(root
, path
);
266 btrfs_free_path(path
);
267 mutex_unlock(&root
->fs_info
->fs_mutex
);
268 make_bad_inode(inode
);
271 static void fill_inode_item(struct btrfs_inode_item
*item
,
274 btrfs_set_inode_uid(item
, inode
->i_uid
);
275 btrfs_set_inode_gid(item
, inode
->i_gid
);
276 btrfs_set_inode_size(item
, inode
->i_size
);
277 btrfs_set_inode_mode(item
, inode
->i_mode
);
278 btrfs_set_inode_nlink(item
, inode
->i_nlink
);
279 btrfs_set_timespec_sec(&item
->atime
, inode
->i_atime
.tv_sec
);
280 btrfs_set_timespec_nsec(&item
->atime
, inode
->i_atime
.tv_nsec
);
281 btrfs_set_timespec_sec(&item
->mtime
, inode
->i_mtime
.tv_sec
);
282 btrfs_set_timespec_nsec(&item
->mtime
, inode
->i_mtime
.tv_nsec
);
283 btrfs_set_timespec_sec(&item
->ctime
, inode
->i_ctime
.tv_sec
);
284 btrfs_set_timespec_nsec(&item
->ctime
, inode
->i_ctime
.tv_nsec
);
285 btrfs_set_inode_nblocks(item
, inode
->i_blocks
);
286 btrfs_set_inode_generation(item
, inode
->i_generation
);
287 btrfs_set_inode_rdev(item
, inode
->i_rdev
);
288 btrfs_set_inode_block_group(item
,
289 BTRFS_I(inode
)->block_group
->key
.objectid
);
292 int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
293 struct btrfs_root
*root
,
296 struct btrfs_inode_item
*inode_item
;
297 struct btrfs_path
*path
;
300 path
= btrfs_alloc_path();
302 ret
= btrfs_lookup_inode(trans
, root
, path
,
303 &BTRFS_I(inode
)->location
, 1);
310 inode_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
312 struct btrfs_inode_item
);
314 fill_inode_item(inode_item
, inode
);
315 btrfs_mark_buffer_dirty(path
->nodes
[0]);
316 btrfs_set_inode_last_trans(trans
, inode
);
319 btrfs_release_path(root
, path
);
320 btrfs_free_path(path
);
325 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
326 struct btrfs_root
*root
,
328 struct dentry
*dentry
)
330 struct btrfs_path
*path
;
331 const char *name
= dentry
->d_name
.name
;
332 int name_len
= dentry
->d_name
.len
;
335 struct btrfs_dir_item
*di
;
337 path
= btrfs_alloc_path();
343 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
353 objectid
= btrfs_disk_key_objectid(&di
->location
);
354 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
357 btrfs_release_path(root
, path
);
359 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
360 objectid
, name
, name_len
, -1);
369 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
371 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
373 btrfs_free_path(path
);
375 dir
->i_size
-= name_len
* 2;
376 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
377 btrfs_update_inode(trans
, root
, dir
);
378 drop_nlink(dentry
->d_inode
);
379 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
380 dir
->i_sb
->s_dirt
= 1;
385 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
387 struct btrfs_root
*root
;
388 struct btrfs_trans_handle
*trans
;
391 root
= BTRFS_I(dir
)->root
;
392 mutex_lock(&root
->fs_info
->fs_mutex
);
393 trans
= btrfs_start_transaction(root
, 1);
394 btrfs_set_trans_block_group(trans
, dir
);
395 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
396 btrfs_end_transaction(trans
, root
);
397 mutex_unlock(&root
->fs_info
->fs_mutex
);
398 btrfs_btree_balance_dirty(root
);
402 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
404 struct inode
*inode
= dentry
->d_inode
;
407 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
408 struct btrfs_path
*path
;
409 struct btrfs_key key
;
410 struct btrfs_trans_handle
*trans
;
411 struct btrfs_key found_key
;
413 struct btrfs_leaf
*leaf
;
414 char *goodnames
= "..";
416 path
= btrfs_alloc_path();
418 mutex_lock(&root
->fs_info
->fs_mutex
);
419 trans
= btrfs_start_transaction(root
, 1);
420 btrfs_set_trans_block_group(trans
, dir
);
421 key
.objectid
= inode
->i_ino
;
422 key
.offset
= (u64
)-1;
425 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
431 if (path
->slots
[0] == 0) {
436 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
437 btrfs_disk_key_to_cpu(&found_key
,
438 &leaf
->items
[path
->slots
[0]].key
);
439 found_type
= btrfs_key_type(&found_key
);
440 if (found_key
.objectid
!= inode
->i_ino
) {
444 if ((found_type
!= BTRFS_DIR_ITEM_KEY
&&
445 found_type
!= BTRFS_DIR_INDEX_KEY
) ||
446 (!btrfs_match_dir_item_name(root
, path
, goodnames
, 2) &&
447 !btrfs_match_dir_item_name(root
, path
, goodnames
, 1))) {
451 ret
= btrfs_del_item(trans
, root
, path
);
454 if (found_type
== BTRFS_DIR_ITEM_KEY
&& found_key
.offset
== 1)
456 btrfs_release_path(root
, path
);
459 btrfs_release_path(root
, path
);
461 /* now the directory is empty */
462 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
467 btrfs_release_path(root
, path
);
468 btrfs_free_path(path
);
469 mutex_unlock(&root
->fs_info
->fs_mutex
);
470 ret
= btrfs_end_transaction(trans
, root
);
471 btrfs_btree_balance_dirty(root
);
477 static int btrfs_free_inode(struct btrfs_trans_handle
*trans
,
478 struct btrfs_root
*root
,
481 struct btrfs_path
*path
;
486 path
= btrfs_alloc_path();
488 ret
= btrfs_lookup_inode(trans
, root
, path
,
489 &BTRFS_I(inode
)->location
, -1);
493 ret
= btrfs_del_item(trans
, root
, path
);
494 btrfs_free_path(path
);
499 * this can truncate away extent items, csum items and directory items.
500 * It starts at a high offset and removes keys until it can't find
501 * any higher than i_size.
503 * csum items that cross the new i_size are truncated to the new size
506 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
507 struct btrfs_root
*root
,
511 struct btrfs_path
*path
;
512 struct btrfs_key key
;
513 struct btrfs_disk_key
*found_key
;
515 struct btrfs_leaf
*leaf
;
516 struct btrfs_file_extent_item
*fi
;
517 u64 extent_start
= 0;
518 u64 extent_num_blocks
= 0;
523 btrfs_drop_extent_cache(inode
, inode
->i_size
, (u64
)-1);
524 path
= btrfs_alloc_path();
527 /* FIXME, add redo link to tree so we don't leak on crash */
528 key
.objectid
= inode
->i_ino
;
529 key
.offset
= (u64
)-1;
532 btrfs_init_path(path
);
534 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
539 BUG_ON(path
->slots
[0] == 0);
542 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
543 found_key
= &leaf
->items
[path
->slots
[0]].key
;
544 found_type
= btrfs_disk_key_type(found_key
);
546 if (btrfs_disk_key_objectid(found_key
) != inode
->i_ino
)
548 if (found_type
!= BTRFS_CSUM_ITEM_KEY
&&
549 found_type
!= BTRFS_DIR_ITEM_KEY
&&
550 found_type
!= BTRFS_DIR_INDEX_KEY
&&
551 found_type
!= BTRFS_EXTENT_DATA_KEY
)
554 item_end
= btrfs_disk_key_offset(found_key
);
555 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
556 fi
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
558 struct btrfs_file_extent_item
);
559 if (btrfs_file_extent_type(fi
) !=
560 BTRFS_FILE_EXTENT_INLINE
) {
561 item_end
+= btrfs_file_extent_num_blocks(fi
) <<
565 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
566 ret
= btrfs_csum_truncate(trans
, root
, path
,
570 if (item_end
< inode
->i_size
) {
571 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
572 found_type
= BTRFS_INODE_ITEM_KEY
;
573 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
574 found_type
= BTRFS_CSUM_ITEM_KEY
;
575 } else if (found_type
) {
580 btrfs_set_key_type(&key
, found_type
- 1);
583 if (btrfs_disk_key_offset(found_key
) >= inode
->i_size
)
589 /* FIXME, shrink the extent if the ref count is only 1 */
590 if (found_type
== BTRFS_EXTENT_DATA_KEY
&&
591 btrfs_file_extent_type(fi
) !=
592 BTRFS_FILE_EXTENT_INLINE
) {
595 u64 orig_num_blocks
=
596 btrfs_file_extent_num_blocks(fi
);
597 extent_num_blocks
= inode
->i_size
-
598 btrfs_disk_key_offset(found_key
) +
600 extent_num_blocks
>>= inode
->i_blkbits
;
601 btrfs_set_file_extent_num_blocks(fi
,
603 inode
->i_blocks
-= (orig_num_blocks
-
604 extent_num_blocks
) << 3;
605 btrfs_mark_buffer_dirty(path
->nodes
[0]);
608 btrfs_file_extent_disk_blocknr(fi
);
610 btrfs_file_extent_disk_num_blocks(fi
);
611 /* FIXME blocksize != 4096 */
612 num_dec
= btrfs_file_extent_num_blocks(fi
) << 3;
613 if (extent_start
!= 0) {
615 inode
->i_blocks
-= num_dec
;
620 ret
= btrfs_del_item(trans
, root
, path
);
626 btrfs_release_path(root
, path
);
628 ret
= btrfs_free_extent(trans
, root
, extent_start
,
629 extent_num_blocks
, 0);
635 btrfs_release_path(root
, path
);
636 btrfs_free_path(path
);
637 inode
->i_sb
->s_dirt
= 1;
641 static int btrfs_cow_one_page(struct inode
*inode
, struct page
*page
,
646 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
647 u64 page_start
= page
->index
<< PAGE_CACHE_SHIFT
;
648 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
650 if (!PagePrivate(page
)) {
651 SetPagePrivate(page
);
652 set_page_private(page
, 1);
653 WARN_ON(!page
->mapping
->a_ops
->invalidatepage
);
654 page_cache_get(page
);
657 lock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
658 set_extent_delalloc(&BTRFS_I(inode
)->extent_tree
, page_start
,
660 if (zero_start
!= PAGE_CACHE_SIZE
) {
662 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
663 flush_dcache_page(page
);
666 set_page_dirty(page
);
667 unlock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
673 * taken from block_truncate_page, but does cow as it zeros out
674 * any bytes left in the last page in the file.
676 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
678 struct inode
*inode
= mapping
->host
;
679 unsigned blocksize
= 1 << inode
->i_blkbits
;
680 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
681 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
686 if ((offset
& (blocksize
- 1)) == 0)
690 page
= grab_cache_page(mapping
, index
);
693 if (!PageUptodate(page
)) {
694 ret
= btrfs_readpage(NULL
, page
);
696 if (!PageUptodate(page
)) {
701 page_start
= page
->index
<< PAGE_CACHE_SHIFT
;
703 ret
= btrfs_cow_one_page(inode
, page
, offset
);
706 page_cache_release(page
);
711 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
713 struct inode
*inode
= dentry
->d_inode
;
716 err
= inode_change_ok(inode
, attr
);
720 if (S_ISREG(inode
->i_mode
) &&
721 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
722 struct btrfs_trans_handle
*trans
;
723 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
724 u64 mask
= root
->blocksize
- 1;
725 u64 pos
= (inode
->i_size
+ mask
) & ~mask
;
728 if (attr
->ia_size
<= pos
)
731 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
733 hole_size
= (attr
->ia_size
- pos
+ mask
) & ~mask
;
734 hole_size
>>= inode
->i_blkbits
;
736 mutex_lock(&root
->fs_info
->fs_mutex
);
737 trans
= btrfs_start_transaction(root
, 1);
738 btrfs_set_trans_block_group(trans
, inode
);
739 err
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
740 pos
, 0, 0, hole_size
);
741 btrfs_end_transaction(trans
, root
);
742 mutex_unlock(&root
->fs_info
->fs_mutex
);
747 err
= inode_setattr(inode
, attr
);
751 void btrfs_delete_inode(struct inode
*inode
)
753 struct btrfs_trans_handle
*trans
;
754 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
757 truncate_inode_pages(&inode
->i_data
, 0);
758 if (is_bad_inode(inode
)) {
762 mutex_lock(&root
->fs_info
->fs_mutex
);
763 trans
= btrfs_start_transaction(root
, 1);
764 btrfs_set_trans_block_group(trans
, inode
);
765 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
768 ret
= btrfs_free_inode(trans
, root
, inode
);
771 btrfs_end_transaction(trans
, root
);
772 mutex_unlock(&root
->fs_info
->fs_mutex
);
773 btrfs_btree_balance_dirty(root
);
777 btrfs_end_transaction(trans
, root
);
778 mutex_unlock(&root
->fs_info
->fs_mutex
);
779 btrfs_btree_balance_dirty(root
);
785 * this returns the key found in the dir entry in the location pointer.
786 * If no dir entries were found, location->objectid is 0.
788 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
789 struct btrfs_key
*location
)
791 const char *name
= dentry
->d_name
.name
;
792 int namelen
= dentry
->d_name
.len
;
793 struct btrfs_dir_item
*di
;
794 struct btrfs_path
*path
;
795 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
798 path
= btrfs_alloc_path();
800 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
802 if (!di
|| IS_ERR(di
)) {
803 location
->objectid
= 0;
807 btrfs_disk_key_to_cpu(location
, &di
->location
);
809 btrfs_release_path(root
, path
);
810 btrfs_free_path(path
);
815 * when we hit a tree root in a directory, the btrfs part of the inode
816 * needs to be changed to reflect the root directory of the tree root. This
817 * is kind of like crossing a mount point.
819 static int fixup_tree_root_location(struct btrfs_root
*root
,
820 struct btrfs_key
*location
,
821 struct btrfs_root
**sub_root
,
822 struct dentry
*dentry
)
824 struct btrfs_path
*path
;
825 struct btrfs_root_item
*ri
;
827 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
829 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
832 path
= btrfs_alloc_path();
834 mutex_lock(&root
->fs_info
->fs_mutex
);
836 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
839 if (IS_ERR(*sub_root
))
840 return PTR_ERR(*sub_root
);
842 ri
= &(*sub_root
)->root_item
;
843 location
->objectid
= btrfs_root_dirid(ri
);
845 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
846 location
->offset
= 0;
848 btrfs_free_path(path
);
849 mutex_unlock(&root
->fs_info
->fs_mutex
);
853 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
855 struct btrfs_iget_args
*args
= p
;
856 inode
->i_ino
= args
->ino
;
857 BTRFS_I(inode
)->root
= args
->root
;
858 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
859 inode
->i_mapping
, GFP_NOFS
);
863 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
865 struct btrfs_iget_args
*args
= opaque
;
866 return (args
->ino
== inode
->i_ino
&&
867 args
->root
== BTRFS_I(inode
)->root
);
870 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
871 struct btrfs_root
*root
)
874 struct btrfs_iget_args args
;
878 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
879 btrfs_init_locked_inode
,
884 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
885 struct nameidata
*nd
)
887 struct inode
* inode
;
888 struct btrfs_inode
*bi
= BTRFS_I(dir
);
889 struct btrfs_root
*root
= bi
->root
;
890 struct btrfs_root
*sub_root
= root
;
891 struct btrfs_key location
;
894 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
895 return ERR_PTR(-ENAMETOOLONG
);
896 mutex_lock(&root
->fs_info
->fs_mutex
);
897 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
898 mutex_unlock(&root
->fs_info
->fs_mutex
);
902 if (location
.objectid
) {
903 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
908 return ERR_PTR(-ENOENT
);
909 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
912 return ERR_PTR(-EACCES
);
913 if (inode
->i_state
& I_NEW
) {
914 /* the inode and parent dir are two different roots */
915 if (sub_root
!= root
) {
917 sub_root
->inode
= inode
;
919 BTRFS_I(inode
)->root
= sub_root
;
920 memcpy(&BTRFS_I(inode
)->location
, &location
,
922 btrfs_read_locked_inode(inode
);
923 unlock_new_inode(inode
);
926 return d_splice_alias(inode
, dentry
);
929 static unsigned char btrfs_filetype_table
[] = {
930 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
933 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
935 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
936 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
937 struct btrfs_item
*item
;
938 struct btrfs_dir_item
*di
;
939 struct btrfs_key key
;
940 struct btrfs_path
*path
;
943 struct btrfs_leaf
*leaf
;
946 unsigned char d_type
;
951 int key_type
= BTRFS_DIR_INDEX_KEY
;
953 /* FIXME, use a real flag for deciding about the key type */
954 if (root
->fs_info
->tree_root
== root
)
955 key_type
= BTRFS_DIR_ITEM_KEY
;
956 mutex_lock(&root
->fs_info
->fs_mutex
);
957 key
.objectid
= inode
->i_ino
;
959 btrfs_set_key_type(&key
, key_type
);
960 key
.offset
= filp
->f_pos
;
961 path
= btrfs_alloc_path();
963 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
968 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
969 nritems
= btrfs_header_nritems(&leaf
->header
);
970 slot
= path
->slots
[0];
971 if (advance
|| slot
>= nritems
) {
972 if (slot
>= nritems
-1) {
973 ret
= btrfs_next_leaf(root
, path
);
976 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
977 nritems
= btrfs_header_nritems(&leaf
->header
);
978 slot
= path
->slots
[0];
985 item
= leaf
->items
+ slot
;
986 if (btrfs_disk_key_objectid(&item
->key
) != key
.objectid
)
988 if (btrfs_disk_key_type(&item
->key
) != key_type
)
990 if (btrfs_disk_key_offset(&item
->key
) < filp
->f_pos
)
992 filp
->f_pos
= btrfs_disk_key_offset(&item
->key
);
994 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
996 di_total
= btrfs_item_size(leaf
->items
+ slot
);
997 while(di_cur
< di_total
) {
998 d_type
= btrfs_filetype_table
[btrfs_dir_type(di
)];
999 over
= filldir(dirent
, (const char *)(di
+ 1),
1000 btrfs_dir_name_len(di
),
1001 btrfs_disk_key_offset(&item
->key
),
1002 btrfs_disk_key_objectid(&di
->location
),
1006 di_len
= btrfs_dir_name_len(di
) + sizeof(*di
);
1008 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
1015 btrfs_release_path(root
, path
);
1016 btrfs_free_path(path
);
1017 mutex_unlock(&root
->fs_info
->fs_mutex
);
1021 int btrfs_write_inode(struct inode
*inode
, int wait
)
1023 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1024 struct btrfs_trans_handle
*trans
;
1028 mutex_lock(&root
->fs_info
->fs_mutex
);
1029 trans
= btrfs_start_transaction(root
, 1);
1030 btrfs_set_trans_block_group(trans
, inode
);
1031 ret
= btrfs_commit_transaction(trans
, root
);
1032 mutex_unlock(&root
->fs_info
->fs_mutex
);
1038 * This is somewhat expensive, updating the tree every time the
1039 * inode changes. But, it is most likely to find the inode in cache.
1040 * FIXME, needs more benchmarking...there are no reasons other than performance
1041 * to keep or drop this code.
1043 void btrfs_dirty_inode(struct inode
*inode
)
1045 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1046 struct btrfs_trans_handle
*trans
;
1048 mutex_lock(&root
->fs_info
->fs_mutex
);
1049 trans
= btrfs_start_transaction(root
, 1);
1050 btrfs_set_trans_block_group(trans
, inode
);
1051 btrfs_update_inode(trans
, root
, inode
);
1052 btrfs_end_transaction(trans
, root
);
1053 mutex_unlock(&root
->fs_info
->fs_mutex
);
1056 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
1057 struct btrfs_root
*root
,
1059 struct btrfs_block_group_cache
*group
,
1062 struct inode
*inode
;
1063 struct btrfs_inode_item inode_item
;
1064 struct btrfs_key
*location
;
1068 inode
= new_inode(root
->fs_info
->sb
);
1070 return ERR_PTR(-ENOMEM
);
1072 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1073 inode
->i_mapping
, GFP_NOFS
);
1074 BTRFS_I(inode
)->root
= root
;
1080 group
= btrfs_find_block_group(root
, group
, 0, 0, owner
);
1081 BTRFS_I(inode
)->block_group
= group
;
1083 inode
->i_uid
= current
->fsuid
;
1084 inode
->i_gid
= current
->fsgid
;
1085 inode
->i_mode
= mode
;
1086 inode
->i_ino
= objectid
;
1087 inode
->i_blocks
= 0;
1088 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1089 fill_inode_item(&inode_item
, inode
);
1090 location
= &BTRFS_I(inode
)->location
;
1091 location
->objectid
= objectid
;
1092 location
->flags
= 0;
1093 location
->offset
= 0;
1094 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1096 ret
= btrfs_insert_inode(trans
, root
, objectid
, &inode_item
);
1098 return ERR_PTR(ret
);
1099 insert_inode_hash(inode
);
1103 static inline u8
btrfs_inode_type(struct inode
*inode
)
1105 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
1108 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
1109 struct dentry
*dentry
, struct inode
*inode
)
1112 struct btrfs_key key
;
1113 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
1114 struct inode
*parent_inode
;
1115 key
.objectid
= inode
->i_ino
;
1117 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1120 ret
= btrfs_insert_dir_item(trans
, root
,
1121 dentry
->d_name
.name
, dentry
->d_name
.len
,
1122 dentry
->d_parent
->d_inode
->i_ino
,
1123 &key
, btrfs_inode_type(inode
));
1125 parent_inode
= dentry
->d_parent
->d_inode
;
1126 parent_inode
->i_size
+= dentry
->d_name
.len
* 2;
1127 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
1128 ret
= btrfs_update_inode(trans
, root
,
1129 dentry
->d_parent
->d_inode
);
1134 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
1135 struct dentry
*dentry
, struct inode
*inode
)
1137 int err
= btrfs_add_link(trans
, dentry
, inode
);
1139 d_instantiate(dentry
, inode
);
1147 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1148 int mode
, dev_t rdev
)
1150 struct btrfs_trans_handle
*trans
;
1151 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1152 struct inode
*inode
;
1157 if (!new_valid_dev(rdev
))
1160 mutex_lock(&root
->fs_info
->fs_mutex
);
1161 trans
= btrfs_start_transaction(root
, 1);
1162 btrfs_set_trans_block_group(trans
, dir
);
1164 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1170 inode
= btrfs_new_inode(trans
, root
, objectid
,
1171 BTRFS_I(dir
)->block_group
, mode
);
1172 err
= PTR_ERR(inode
);
1176 btrfs_set_trans_block_group(trans
, inode
);
1177 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1181 inode
->i_op
= &btrfs_special_inode_operations
;
1182 init_special_inode(inode
, inode
->i_mode
, rdev
);
1183 btrfs_update_inode(trans
, root
, inode
);
1185 dir
->i_sb
->s_dirt
= 1;
1186 btrfs_update_inode_block_group(trans
, inode
);
1187 btrfs_update_inode_block_group(trans
, dir
);
1189 btrfs_end_transaction(trans
, root
);
1190 mutex_unlock(&root
->fs_info
->fs_mutex
);
1193 inode_dec_link_count(inode
);
1196 btrfs_btree_balance_dirty(root
);
1200 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
1201 int mode
, struct nameidata
*nd
)
1203 struct btrfs_trans_handle
*trans
;
1204 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1205 struct inode
*inode
;
1210 mutex_lock(&root
->fs_info
->fs_mutex
);
1211 trans
= btrfs_start_transaction(root
, 1);
1212 btrfs_set_trans_block_group(trans
, dir
);
1214 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1220 inode
= btrfs_new_inode(trans
, root
, objectid
,
1221 BTRFS_I(dir
)->block_group
, mode
);
1222 err
= PTR_ERR(inode
);
1226 btrfs_set_trans_block_group(trans
, inode
);
1227 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1231 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1232 inode
->i_fop
= &btrfs_file_operations
;
1233 inode
->i_op
= &btrfs_file_inode_operations
;
1234 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1235 inode
->i_mapping
, GFP_NOFS
);
1236 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
1238 dir
->i_sb
->s_dirt
= 1;
1239 btrfs_update_inode_block_group(trans
, inode
);
1240 btrfs_update_inode_block_group(trans
, dir
);
1242 btrfs_end_transaction(trans
, root
);
1243 mutex_unlock(&root
->fs_info
->fs_mutex
);
1246 inode_dec_link_count(inode
);
1249 btrfs_btree_balance_dirty(root
);
1253 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
1254 struct dentry
*dentry
)
1256 struct btrfs_trans_handle
*trans
;
1257 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1258 struct inode
*inode
= old_dentry
->d_inode
;
1262 if (inode
->i_nlink
== 0)
1266 mutex_lock(&root
->fs_info
->fs_mutex
);
1267 trans
= btrfs_start_transaction(root
, 1);
1268 btrfs_set_trans_block_group(trans
, dir
);
1269 atomic_inc(&inode
->i_count
);
1270 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1273 dir
->i_sb
->s_dirt
= 1;
1274 btrfs_update_inode_block_group(trans
, dir
);
1275 err
= btrfs_update_inode(trans
, root
, inode
);
1279 btrfs_end_transaction(trans
, root
);
1280 mutex_unlock(&root
->fs_info
->fs_mutex
);
1283 inode_dec_link_count(inode
);
1286 btrfs_btree_balance_dirty(root
);
1290 static int btrfs_make_empty_dir(struct btrfs_trans_handle
*trans
,
1291 struct btrfs_root
*root
,
1292 u64 objectid
, u64 dirid
)
1296 struct btrfs_key key
;
1301 key
.objectid
= objectid
;
1304 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1306 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 1, objectid
,
1307 &key
, BTRFS_FT_DIR
);
1310 key
.objectid
= dirid
;
1311 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 2, objectid
,
1312 &key
, BTRFS_FT_DIR
);
1319 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1321 struct inode
*inode
;
1322 struct btrfs_trans_handle
*trans
;
1323 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1325 int drop_on_err
= 0;
1328 mutex_lock(&root
->fs_info
->fs_mutex
);
1329 trans
= btrfs_start_transaction(root
, 1);
1330 btrfs_set_trans_block_group(trans
, dir
);
1331 if (IS_ERR(trans
)) {
1332 err
= PTR_ERR(trans
);
1336 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1342 inode
= btrfs_new_inode(trans
, root
, objectid
,
1343 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
1344 if (IS_ERR(inode
)) {
1345 err
= PTR_ERR(inode
);
1349 inode
->i_op
= &btrfs_dir_inode_operations
;
1350 inode
->i_fop
= &btrfs_dir_file_operations
;
1351 btrfs_set_trans_block_group(trans
, inode
);
1353 err
= btrfs_make_empty_dir(trans
, root
, inode
->i_ino
, dir
->i_ino
);
1358 err
= btrfs_update_inode(trans
, root
, inode
);
1361 err
= btrfs_add_link(trans
, dentry
, inode
);
1364 d_instantiate(dentry
, inode
);
1366 dir
->i_sb
->s_dirt
= 1;
1367 btrfs_update_inode_block_group(trans
, inode
);
1368 btrfs_update_inode_block_group(trans
, dir
);
1371 btrfs_end_transaction(trans
, root
);
1373 mutex_unlock(&root
->fs_info
->fs_mutex
);
1376 btrfs_btree_balance_dirty(root
);
1380 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
1381 size_t page_offset
, u64 start
, u64 end
,
1387 u64 extent_start
= 0;
1389 u64 objectid
= inode
->i_ino
;
1391 int failed_insert
= 0;
1392 struct btrfs_path
*path
;
1393 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1394 struct btrfs_file_extent_item
*item
;
1395 struct btrfs_leaf
*leaf
;
1396 struct btrfs_disk_key
*found_key
;
1397 struct extent_map
*em
= NULL
;
1398 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1399 struct btrfs_trans_handle
*trans
= NULL
;
1401 path
= btrfs_alloc_path();
1403 mutex_lock(&root
->fs_info
->fs_mutex
);
1406 em
= lookup_extent_mapping(em_tree
, start
, end
);
1411 em
= alloc_extent_map(GFP_NOFS
);
1419 em
->bdev
= inode
->i_sb
->s_bdev
;
1420 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
1421 objectid
, start
, 0);
1428 if (path
->slots
[0] == 0)
1433 item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]), path
->slots
[0],
1434 struct btrfs_file_extent_item
);
1435 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
1436 blocknr
= btrfs_file_extent_disk_blocknr(item
);
1437 blocknr
+= btrfs_file_extent_offset(item
);
1439 /* are we inside the extent that was found? */
1440 found_key
= &leaf
->items
[path
->slots
[0]].key
;
1441 found_type
= btrfs_disk_key_type(found_key
);
1442 if (btrfs_disk_key_objectid(found_key
) != objectid
||
1443 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
1447 found_type
= btrfs_file_extent_type(item
);
1448 extent_start
= btrfs_disk_key_offset(&leaf
->items
[path
->slots
[0]].key
);
1449 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1450 extent_end
= extent_start
+
1451 (btrfs_file_extent_num_blocks(item
) << inode
->i_blkbits
);
1453 if (start
< extent_start
|| start
>= extent_end
) {
1455 if (start
< extent_start
) {
1456 if (end
< extent_start
)
1458 em
->end
= extent_end
- 1;
1464 if (btrfs_file_extent_disk_blocknr(item
) == 0) {
1465 em
->start
= extent_start
;
1466 em
->end
= extent_end
- 1;
1467 em
->block_start
= 0;
1471 em
->block_start
= blocknr
<< inode
->i_blkbits
;
1472 em
->block_end
= em
->block_start
+
1473 (btrfs_file_extent_num_blocks(item
) <<
1474 inode
->i_blkbits
) - 1;
1475 em
->start
= extent_start
;
1476 em
->end
= extent_end
- 1;
1478 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1483 size
= btrfs_file_extent_inline_len(leaf
->items
+
1485 extent_end
= extent_start
+ size
;
1486 if (start
< extent_start
|| start
>= extent_end
) {
1488 if (start
< extent_start
) {
1489 if (end
< extent_start
)
1491 em
->end
= extent_end
- 1;
1497 em
->block_start
= EXTENT_MAP_INLINE
;
1498 em
->block_end
= EXTENT_MAP_INLINE
;
1499 em
->start
= extent_start
;
1500 em
->end
= extent_end
- 1;
1504 ptr
= btrfs_file_extent_inline_start(item
);
1506 memcpy(map
+ page_offset
, ptr
, size
);
1507 flush_dcache_page(result
->b_page
);
1509 set_extent_uptodate(em_tree
, extent_start
,
1510 extent_end
, GFP_NOFS
);
1513 printk("unkknown found_type %d\n", found_type
);
1520 em
->block_start
= 0;
1523 btrfs_release_path(root
, path
);
1524 if (em
->start
> start
|| em
->end
< start
) {
1525 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->end
, start
, end
);
1529 ret
= add_extent_mapping(em_tree
, em
);
1530 if (ret
== -EEXIST
) {
1531 free_extent_map(em
);
1533 if (failed_insert
> 5) {
1534 printk("failing to insert %Lu %Lu\n", start
, end
);
1543 btrfs_free_path(path
);
1545 ret
= btrfs_end_transaction(trans
, root
);
1549 mutex_unlock(&root
->fs_info
->fs_mutex
);
1551 free_extent_map(em
);
1553 return ERR_PTR(err
);
1560 * FIBMAP and others want to pass in a fake buffer head. They need to
1561 * use BTRFS_GET_BLOCK_NO_DIRECT to make sure we don't try to memcpy
1562 * any packed file data into the fake bh
1564 #define BTRFS_GET_BLOCK_NO_CREATE 0
1565 #define BTRFS_GET_BLOCK_CREATE 1
1566 #define BTRFS_GET_BLOCK_NO_DIRECT 2
1569 * FIXME create==1 doe not work.
1571 static int btrfs_get_block_lock(struct inode
*inode
, sector_t iblock
,
1572 struct buffer_head
*result
, int create
)
1577 u64 extent_start
= 0;
1579 u64 objectid
= inode
->i_ino
;
1582 struct btrfs_path
*path
;
1583 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1584 struct btrfs_file_extent_item
*item
;
1585 struct btrfs_leaf
*leaf
;
1586 struct btrfs_disk_key
*found_key
;
1587 struct btrfs_trans_handle
*trans
= NULL
;
1589 path
= btrfs_alloc_path();
1591 if (create
& BTRFS_GET_BLOCK_CREATE
) {
1593 * danger!, this only works if the page is properly up
1596 trans
= btrfs_start_transaction(root
, 1);
1601 ret
= btrfs_drop_extents(trans
, root
, inode
,
1602 iblock
<< inode
->i_blkbits
,
1603 (iblock
+ 1) << inode
->i_blkbits
,
1608 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
1610 iblock
<< inode
->i_blkbits
, 0);
1617 if (path
->slots
[0] == 0) {
1618 btrfs_release_path(root
, path
);
1624 item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]), path
->slots
[0],
1625 struct btrfs_file_extent_item
);
1626 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
1627 blocknr
= btrfs_file_extent_disk_blocknr(item
);
1628 blocknr
+= btrfs_file_extent_offset(item
);
1630 /* are we inside the extent that was found? */
1631 found_key
= &leaf
->items
[path
->slots
[0]].key
;
1632 found_type
= btrfs_disk_key_type(found_key
);
1633 if (btrfs_disk_key_objectid(found_key
) != objectid
||
1634 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
1639 found_type
= btrfs_file_extent_type(item
);
1640 extent_start
= btrfs_disk_key_offset(&leaf
->items
[path
->slots
[0]].key
);
1641 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1642 extent_start
= extent_start
>> inode
->i_blkbits
;
1643 extent_end
= extent_start
+ btrfs_file_extent_num_blocks(item
);
1645 if (btrfs_file_extent_disk_blocknr(item
) == 0)
1647 if (iblock
>= extent_start
&& iblock
< extent_end
) {
1648 btrfs_map_bh_to_logical(root
, result
, blocknr
+
1649 iblock
- extent_start
);
1652 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1657 if (create
& BTRFS_GET_BLOCK_NO_DIRECT
) {
1661 size
= btrfs_file_extent_inline_len(leaf
->items
+
1663 extent_end
= (extent_start
+ size
) >> inode
->i_blkbits
;
1664 extent_start
>>= inode
->i_blkbits
;
1665 if (iblock
< extent_start
|| iblock
> extent_end
) {
1668 ptr
= btrfs_file_extent_inline_start(item
);
1669 map
= kmap(result
->b_page
);
1670 memcpy(map
, ptr
, size
);
1671 memset(map
+ size
, 0, PAGE_CACHE_SIZE
- size
);
1672 flush_dcache_page(result
->b_page
);
1673 kunmap(result
->b_page
);
1674 set_buffer_uptodate(result
);
1675 SetPageChecked(result
->b_page
);
1676 btrfs_map_bh_to_logical(root
, result
, 0);
1679 if (create
& BTRFS_GET_BLOCK_CREATE
) {
1680 struct btrfs_key ins
;
1681 ret
= btrfs_alloc_extent(trans
, root
, inode
->i_ino
,
1682 1, 0, alloc_hint
, (u64
)-1,
1688 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
1689 iblock
<< inode
->i_blkbits
,
1690 ins
.objectid
, ins
.offset
,
1696 btrfs_map_bh_to_logical(root
, result
, ins
.objectid
);
1700 ret
= btrfs_end_transaction(trans
, root
);
1704 btrfs_free_path(path
);
1708 int btrfs_get_block(struct inode
*inode
, sector_t iblock
,
1709 struct buffer_head
*result
, int create
)
1712 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1713 mutex_lock(&root
->fs_info
->fs_mutex
);
1714 err
= btrfs_get_block_lock(inode
, iblock
, result
, create
);
1715 mutex_unlock(&root
->fs_info
->fs_mutex
);
1719 static int btrfs_get_block_bmap(struct inode
*inode
, sector_t iblock
,
1720 struct buffer_head
*result
, int create
)
1722 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1723 u64 start
= iblock
<< inode
->i_blkbits
;
1724 u64 end
= start
+ root
->blocksize
-1;
1725 struct extent_map
*em
;
1727 em
= btrfs_get_extent(inode
, NULL
, 0, start
, end
, 0);
1728 if (em
&& !IS_ERR(em
) && em
->block_start
!= EXTENT_MAP_INLINE
&&
1729 em
->block_start
!= 0) {
1731 offset
= start
- em
->start
;
1732 start
= (em
->block_start
+ offset
) >> inode
->i_blkbits
;
1733 btrfs_map_bh_to_logical(root
, result
, start
);
1738 static sector_t
btrfs_bmap(struct address_space
*as
, sector_t block
)
1740 return generic_block_bmap(as
, block
, btrfs_get_block_bmap
);
1743 static int btrfs_prepare_write(struct file
*file
, struct page
*page
,
1744 unsigned from
, unsigned to
)
1746 return extent_prepare_write(&BTRFS_I(page
->mapping
->host
)->extent_tree
,
1747 page
->mapping
->host
, page
, from
, to
,
1751 int btrfs_readpage(struct file
*file
, struct page
*page
)
1753 struct extent_map_tree
*tree
;
1754 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1755 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
1757 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
1759 struct extent_map_tree
*tree
;
1762 if (current
->flags
& PF_MEMALLOC
) {
1763 redirty_page_for_writepage(wbc
, page
);
1767 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1768 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
1771 static int btrfs_releasepage(struct page
*page
, gfp_t unused_gfp_flags
)
1773 struct extent_map_tree
*tree
;
1776 if (page
->private != 1) {
1778 return try_to_free_buffers(page
);
1780 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1781 ret
= try_release_extent_mapping(tree
, page
);
1783 ClearPagePrivate(page
);
1784 set_page_private(page
, 0);
1785 page_cache_release(page
);
1790 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
1792 struct extent_map_tree
*tree
;
1794 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1795 extent_invalidatepage(tree
, page
, offset
);
1796 btrfs_releasepage(page
, GFP_NOFS
);
1800 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1801 * called from a page fault handler when a page is first dirtied. Hence we must
1802 * be careful to check for EOF conditions here. We set the page up correctly
1803 * for a written page which means we get ENOSPC checking when writing into
1804 * holes and correct delalloc and unwritten extent mapping on filesystems that
1805 * support these features.
1807 * We are not allowed to take the i_mutex here so we have to play games to
1808 * protect against truncate races as the page could now be beyond EOF. Because
1809 * vmtruncate() writes the inode size before removing pages, once we have the
1810 * page lock we can determine safely if the page is beyond EOF. If it is not
1811 * beyond EOF, then the page is guaranteed safe against truncation until we
1814 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
1816 struct inode
*inode
= vma
->vm_file
->f_path
.dentry
->d_inode
;
1823 wait_on_page_writeback(page
);
1824 size
= i_size_read(inode
);
1825 page_start
= page
->index
<< PAGE_CACHE_SHIFT
;
1827 if ((page
->mapping
!= inode
->i_mapping
) ||
1828 (page_start
> size
)) {
1829 /* page got truncated out from underneath us */
1833 /* page is wholly or partially inside EOF */
1834 if (page_start
+ PAGE_CACHE_SIZE
> size
)
1835 end
= size
& ~PAGE_CACHE_MASK
;
1837 end
= PAGE_CACHE_SIZE
;
1839 ret
= btrfs_cow_one_page(inode
, page
, end
);
1846 static void btrfs_truncate(struct inode
*inode
)
1848 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1850 struct btrfs_trans_handle
*trans
;
1852 if (!S_ISREG(inode
->i_mode
))
1854 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1857 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1859 mutex_lock(&root
->fs_info
->fs_mutex
);
1860 trans
= btrfs_start_transaction(root
, 1);
1861 btrfs_set_trans_block_group(trans
, inode
);
1863 /* FIXME, add redo link to tree so we don't leak on crash */
1864 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
1865 btrfs_update_inode(trans
, root
, inode
);
1866 ret
= btrfs_end_transaction(trans
, root
);
1868 mutex_unlock(&root
->fs_info
->fs_mutex
);
1869 btrfs_btree_balance_dirty(root
);
1872 int btrfs_commit_write(struct file
*file
, struct page
*page
,
1873 unsigned from
, unsigned to
)
1875 return extent_commit_write(&BTRFS_I(page
->mapping
->host
)->extent_tree
,
1876 page
->mapping
->host
, page
, from
, to
);
1879 static int create_subvol(struct btrfs_root
*root
, char *name
, int namelen
)
1881 struct btrfs_trans_handle
*trans
;
1882 struct btrfs_key key
;
1883 struct btrfs_root_item root_item
;
1884 struct btrfs_inode_item
*inode_item
;
1885 struct buffer_head
*subvol
;
1886 struct btrfs_leaf
*leaf
;
1887 struct btrfs_root
*new_root
;
1888 struct inode
*inode
;
1893 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
1895 mutex_lock(&root
->fs_info
->fs_mutex
);
1896 trans
= btrfs_start_transaction(root
, 1);
1899 subvol
= btrfs_alloc_free_block(trans
, root
, 0, 0);
1901 return PTR_ERR(subvol
);
1902 leaf
= btrfs_buffer_leaf(subvol
);
1903 btrfs_set_header_nritems(&leaf
->header
, 0);
1904 btrfs_set_header_level(&leaf
->header
, 0);
1905 btrfs_set_header_blocknr(&leaf
->header
, bh_blocknr(subvol
));
1906 btrfs_set_header_generation(&leaf
->header
, trans
->transid
);
1907 btrfs_set_header_owner(&leaf
->header
, root
->root_key
.objectid
);
1908 memcpy(leaf
->header
.fsid
, root
->fs_info
->disk_super
->fsid
,
1909 sizeof(leaf
->header
.fsid
));
1910 btrfs_mark_buffer_dirty(subvol
);
1912 inode_item
= &root_item
.inode
;
1913 memset(inode_item
, 0, sizeof(*inode_item
));
1914 btrfs_set_inode_generation(inode_item
, 1);
1915 btrfs_set_inode_size(inode_item
, 3);
1916 btrfs_set_inode_nlink(inode_item
, 1);
1917 btrfs_set_inode_nblocks(inode_item
, 1);
1918 btrfs_set_inode_mode(inode_item
, S_IFDIR
| 0755);
1920 btrfs_set_root_blocknr(&root_item
, bh_blocknr(subvol
));
1921 btrfs_set_root_refs(&root_item
, 1);
1922 btrfs_set_root_blocks_used(&root_item
, 0);
1923 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
1924 root_item
.drop_level
= 0;
1928 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
1933 btrfs_set_root_dirid(&root_item
, new_dirid
);
1935 key
.objectid
= objectid
;
1938 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
1939 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
1945 * insert the directory item
1947 key
.offset
= (u64
)-1;
1948 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
1949 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
1950 name
, namelen
, dir
->i_ino
, &key
,
1955 ret
= btrfs_commit_transaction(trans
, root
);
1959 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
, name
, namelen
);
1962 trans
= btrfs_start_transaction(new_root
, 1);
1965 inode
= btrfs_new_inode(trans
, new_root
, new_dirid
,
1966 BTRFS_I(dir
)->block_group
, S_IFDIR
| 0700);
1969 inode
->i_op
= &btrfs_dir_inode_operations
;
1970 inode
->i_fop
= &btrfs_dir_file_operations
;
1971 new_root
->inode
= inode
;
1973 ret
= btrfs_make_empty_dir(trans
, new_root
, new_dirid
, new_dirid
);
1979 ret
= btrfs_update_inode(trans
, new_root
, inode
);
1983 err
= btrfs_commit_transaction(trans
, root
);
1987 mutex_unlock(&root
->fs_info
->fs_mutex
);
1988 btrfs_btree_balance_dirty(root
);
1992 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
1994 struct btrfs_trans_handle
*trans
;
1995 struct btrfs_key key
;
1996 struct btrfs_root_item new_root_item
;
1997 struct buffer_head
*tmp
;
2002 if (!root
->ref_cows
)
2005 mutex_lock(&root
->fs_info
->fs_mutex
);
2006 trans
= btrfs_start_transaction(root
, 1);
2009 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
2013 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2018 memcpy(&new_root_item
, &root
->root_item
,
2019 sizeof(new_root_item
));
2021 key
.objectid
= objectid
;
2024 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2025 btrfs_cow_block(trans
, root
, root
->node
, NULL
, 0, &tmp
);
2026 btrfs_set_root_blocknr(&new_root_item
, bh_blocknr(root
->node
));
2028 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2034 * insert the directory item
2036 key
.offset
= (u64
)-1;
2037 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2039 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2040 &key
, BTRFS_FT_DIR
);
2045 ret
= btrfs_inc_root_ref(trans
, root
);
2050 err
= btrfs_commit_transaction(trans
, root
);
2053 mutex_unlock(&root
->fs_info
->fs_mutex
);
2054 btrfs_btree_balance_dirty(root
);
2058 int btrfs_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int
2059 cmd
, unsigned long arg
)
2061 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2062 struct btrfs_ioctl_vol_args vol_args
;
2064 struct btrfs_dir_item
*di
;
2066 struct btrfs_path
*path
;
2070 case BTRFS_IOC_SNAP_CREATE
:
2071 if (copy_from_user(&vol_args
,
2072 (struct btrfs_ioctl_vol_args __user
*)arg
,
2075 namelen
= strlen(vol_args
.name
);
2076 if (namelen
> BTRFS_VOL_NAME_MAX
)
2078 if (strchr(vol_args
.name
, '/'))
2080 path
= btrfs_alloc_path();
2083 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2084 mutex_lock(&root
->fs_info
->fs_mutex
);
2085 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
2087 vol_args
.name
, namelen
, 0);
2088 mutex_unlock(&root
->fs_info
->fs_mutex
);
2089 btrfs_free_path(path
);
2090 if (di
&& !IS_ERR(di
))
2095 if (root
== root
->fs_info
->tree_root
)
2096 ret
= create_subvol(root
, vol_args
.name
, namelen
);
2098 ret
= create_snapshot(root
, vol_args
.name
, namelen
);
2101 case BTRFS_IOC_DEFRAG
:
2102 mutex_lock(&root
->fs_info
->fs_mutex
);
2103 btrfs_defrag_root(root
, 0);
2104 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
2105 mutex_unlock(&root
->fs_info
->fs_mutex
);
2114 #ifdef CONFIG_COMPAT
2115 long btrfs_compat_ioctl(struct file
*file
, unsigned int cmd
,
2118 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
2121 ret
= btrfs_ioctl(inode
, file
, cmd
, (unsigned long) compat_ptr(arg
));
2129 * Called inside transaction, so use GFP_NOFS
2131 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2133 struct btrfs_inode
*ei
;
2135 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2139 return &ei
->vfs_inode
;
2142 void btrfs_destroy_inode(struct inode
*inode
)
2144 WARN_ON(!list_empty(&inode
->i_dentry
));
2145 WARN_ON(inode
->i_data
.nrpages
);
2147 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2150 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2151 unsigned long flags
)
2153 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2155 inode_init_once(&ei
->vfs_inode
);
2158 void btrfs_destroy_cachep(void)
2160 if (btrfs_inode_cachep
)
2161 kmem_cache_destroy(btrfs_inode_cachep
);
2162 if (btrfs_trans_handle_cachep
)
2163 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2164 if (btrfs_transaction_cachep
)
2165 kmem_cache_destroy(btrfs_transaction_cachep
);
2166 if (btrfs_bit_radix_cachep
)
2167 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2168 if (btrfs_path_cachep
)
2169 kmem_cache_destroy(btrfs_path_cachep
);
2172 static struct kmem_cache
*cache_create(const char *name
, size_t size
,
2173 unsigned long extra_flags
,
2174 void (*ctor
)(void *, struct kmem_cache
*,
2177 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
2178 SLAB_MEM_SPREAD
| extra_flags
), ctor
2179 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2185 int btrfs_init_cachep(void)
2187 btrfs_inode_cachep
= cache_create("btrfs_inode_cache",
2188 sizeof(struct btrfs_inode
),
2190 if (!btrfs_inode_cachep
)
2192 btrfs_trans_handle_cachep
= cache_create("btrfs_trans_handle_cache",
2193 sizeof(struct btrfs_trans_handle
),
2195 if (!btrfs_trans_handle_cachep
)
2197 btrfs_transaction_cachep
= cache_create("btrfs_transaction_cache",
2198 sizeof(struct btrfs_transaction
),
2200 if (!btrfs_transaction_cachep
)
2202 btrfs_path_cachep
= cache_create("btrfs_path_cache",
2203 sizeof(struct btrfs_transaction
),
2205 if (!btrfs_path_cachep
)
2207 btrfs_bit_radix_cachep
= cache_create("btrfs_radix", 256,
2208 SLAB_DESTROY_BY_RCU
, NULL
);
2209 if (!btrfs_bit_radix_cachep
)
2213 btrfs_destroy_cachep();
2217 static int btrfs_getattr(struct vfsmount
*mnt
,
2218 struct dentry
*dentry
, struct kstat
*stat
)
2220 struct inode
*inode
= dentry
->d_inode
;
2221 generic_fillattr(inode
, stat
);
2222 stat
->blksize
= 256 * 1024;
2226 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
2227 struct inode
* new_dir
,struct dentry
*new_dentry
)
2229 struct btrfs_trans_handle
*trans
;
2230 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
2231 struct inode
*new_inode
= new_dentry
->d_inode
;
2232 struct inode
*old_inode
= old_dentry
->d_inode
;
2233 struct timespec ctime
= CURRENT_TIME
;
2234 struct btrfs_path
*path
;
2235 struct btrfs_dir_item
*di
;
2238 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
2239 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
2242 mutex_lock(&root
->fs_info
->fs_mutex
);
2243 trans
= btrfs_start_transaction(root
, 1);
2244 btrfs_set_trans_block_group(trans
, new_dir
);
2245 path
= btrfs_alloc_path();
2251 old_dentry
->d_inode
->i_nlink
++;
2252 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
2253 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
2254 old_inode
->i_ctime
= ctime
;
2255 if (S_ISDIR(old_inode
->i_mode
) && old_dir
!= new_dir
) {
2256 struct btrfs_key
*location
= &BTRFS_I(new_dir
)->location
;
2258 di
= btrfs_lookup_dir_item(trans
, root
, path
, old_inode
->i_ino
,
2268 old_parent_oid
= btrfs_disk_key_objectid(&di
->location
);
2269 ret
= btrfs_del_item(trans
, root
, path
);
2273 btrfs_release_path(root
, path
);
2275 di
= btrfs_lookup_dir_index_item(trans
, root
, path
,
2287 ret
= btrfs_del_item(trans
, root
, path
);
2291 btrfs_release_path(root
, path
);
2293 ret
= btrfs_insert_dir_item(trans
, root
, "..", 2,
2294 old_inode
->i_ino
, location
,
2301 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
2306 new_inode
->i_ctime
= CURRENT_TIME
;
2307 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
2310 if (S_ISDIR(new_inode
->i_mode
))
2311 clear_nlink(new_inode
);
2313 drop_nlink(new_inode
);
2314 ret
= btrfs_update_inode(trans
, root
, new_inode
);
2318 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
);
2323 btrfs_free_path(path
);
2324 btrfs_end_transaction(trans
, root
);
2325 mutex_unlock(&root
->fs_info
->fs_mutex
);
2329 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
2330 const char *symname
)
2332 struct btrfs_trans_handle
*trans
;
2333 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2334 struct btrfs_path
*path
;
2335 struct btrfs_key key
;
2336 struct inode
*inode
;
2343 struct btrfs_file_extent_item
*ei
;
2345 name_len
= strlen(symname
) + 1;
2346 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
2347 return -ENAMETOOLONG
;
2348 mutex_lock(&root
->fs_info
->fs_mutex
);
2349 trans
= btrfs_start_transaction(root
, 1);
2350 btrfs_set_trans_block_group(trans
, dir
);
2352 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2358 inode
= btrfs_new_inode(trans
, root
, objectid
,
2359 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
2360 err
= PTR_ERR(inode
);
2364 btrfs_set_trans_block_group(trans
, inode
);
2365 err
= btrfs_add_nondir(trans
, dentry
, inode
);
2369 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2370 inode
->i_fop
= &btrfs_file_operations
;
2371 inode
->i_op
= &btrfs_file_inode_operations
;
2372 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
2373 inode
->i_mapping
, GFP_NOFS
);
2374 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
2376 dir
->i_sb
->s_dirt
= 1;
2377 btrfs_update_inode_block_group(trans
, inode
);
2378 btrfs_update_inode_block_group(trans
, dir
);
2382 path
= btrfs_alloc_path();
2384 key
.objectid
= inode
->i_ino
;
2387 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
2388 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
2389 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2395 ei
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
2396 path
->slots
[0], struct btrfs_file_extent_item
);
2397 btrfs_set_file_extent_generation(ei
, trans
->transid
);
2398 btrfs_set_file_extent_type(ei
,
2399 BTRFS_FILE_EXTENT_INLINE
);
2400 ptr
= btrfs_file_extent_inline_start(ei
);
2401 btrfs_memcpy(root
, path
->nodes
[0]->b_data
,
2402 ptr
, symname
, name_len
);
2403 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2404 btrfs_free_path(path
);
2405 inode
->i_op
= &btrfs_symlink_inode_operations
;
2406 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2407 inode
->i_size
= name_len
- 1;
2408 err
= btrfs_update_inode(trans
, root
, inode
);
2413 btrfs_end_transaction(trans
, root
);
2414 mutex_unlock(&root
->fs_info
->fs_mutex
);
2416 inode_dec_link_count(inode
);
2419 btrfs_btree_balance_dirty(root
);
2423 static struct inode_operations btrfs_dir_inode_operations
= {
2424 .lookup
= btrfs_lookup
,
2425 .create
= btrfs_create
,
2426 .unlink
= btrfs_unlink
,
2428 .mkdir
= btrfs_mkdir
,
2429 .rmdir
= btrfs_rmdir
,
2430 .rename
= btrfs_rename
,
2431 .symlink
= btrfs_symlink
,
2432 .setattr
= btrfs_setattr
,
2433 .mknod
= btrfs_mknod
,
2436 static struct inode_operations btrfs_dir_ro_inode_operations
= {
2437 .lookup
= btrfs_lookup
,
2440 static struct file_operations btrfs_dir_file_operations
= {
2441 .llseek
= generic_file_llseek
,
2442 .read
= generic_read_dir
,
2443 .readdir
= btrfs_readdir
,
2444 .ioctl
= btrfs_ioctl
,
2445 #ifdef CONFIG_COMPAT
2446 .compat_ioctl
= btrfs_compat_ioctl
,
2450 static struct extent_map_ops btrfs_extent_map_ops
= {
2451 .fill_delalloc
= run_delalloc_range
,
2452 .writepage_io_hook
= btrfs_writepage_io_hook
,
2453 .readpage_io_hook
= btrfs_readpage_io_hook
,
2454 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
2457 static struct address_space_operations btrfs_aops
= {
2458 .readpage
= btrfs_readpage
,
2459 .writepage
= btrfs_writepage
,
2460 .sync_page
= block_sync_page
,
2461 .prepare_write
= btrfs_prepare_write
,
2462 .commit_write
= btrfs_commit_write
,
2464 .invalidatepage
= btrfs_invalidatepage
,
2465 .releasepage
= btrfs_releasepage
,
2466 .set_page_dirty
= __set_page_dirty_nobuffers
,
2469 static struct address_space_operations btrfs_symlink_aops
= {
2470 .readpage
= btrfs_readpage
,
2471 .writepage
= btrfs_writepage
,
2474 static struct inode_operations btrfs_file_inode_operations
= {
2475 .truncate
= btrfs_truncate
,
2476 .getattr
= btrfs_getattr
,
2477 .setattr
= btrfs_setattr
,
2480 static struct inode_operations btrfs_special_inode_operations
= {
2481 .getattr
= btrfs_getattr
,
2482 .setattr
= btrfs_setattr
,
2485 static struct inode_operations btrfs_symlink_inode_operations
= {
2486 .readlink
= generic_readlink
,
2487 .follow_link
= page_follow_link_light
,
2488 .put_link
= page_put_link
,