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)
689 down_read(&BTRFS_I(inode
)->root
->snap_sem
);
691 page
= grab_cache_page(mapping
, index
);
694 if (!PageUptodate(page
)) {
695 ret
= btrfs_readpage(NULL
, page
);
697 if (!PageUptodate(page
)) {
702 page_start
= page
->index
<< PAGE_CACHE_SHIFT
;
704 ret
= btrfs_cow_one_page(inode
, page
, offset
);
707 page_cache_release(page
);
708 up_read(&BTRFS_I(inode
)->root
->snap_sem
);
713 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
715 struct inode
*inode
= dentry
->d_inode
;
718 err
= inode_change_ok(inode
, attr
);
722 if (S_ISREG(inode
->i_mode
) &&
723 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
724 struct btrfs_trans_handle
*trans
;
725 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
726 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
728 u64 mask
= root
->blocksize
- 1;
729 u64 pos
= (inode
->i_size
+ mask
) & ~mask
;
730 u64 block_end
= attr
->ia_size
| mask
;
734 if (attr
->ia_size
<= pos
)
737 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
739 lock_extent(em_tree
, pos
, block_end
, GFP_NOFS
);
740 hole_size
= (attr
->ia_size
- pos
+ mask
) & ~mask
;
742 mutex_lock(&root
->fs_info
->fs_mutex
);
743 trans
= btrfs_start_transaction(root
, 1);
744 btrfs_set_trans_block_group(trans
, inode
);
745 err
= btrfs_drop_extents(trans
, root
, inode
,
746 pos
, pos
+ hole_size
, &alloc_hint
);
748 hole_size
>>= inode
->i_blkbits
;
750 err
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
751 pos
, 0, 0, hole_size
);
752 btrfs_end_transaction(trans
, root
);
753 mutex_unlock(&root
->fs_info
->fs_mutex
);
754 unlock_extent(em_tree
, pos
, block_end
, GFP_NOFS
);
759 err
= inode_setattr(inode
, attr
);
763 void btrfs_delete_inode(struct inode
*inode
)
765 struct btrfs_trans_handle
*trans
;
766 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
769 truncate_inode_pages(&inode
->i_data
, 0);
770 if (is_bad_inode(inode
)) {
774 mutex_lock(&root
->fs_info
->fs_mutex
);
775 trans
= btrfs_start_transaction(root
, 1);
776 btrfs_set_trans_block_group(trans
, inode
);
777 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
780 ret
= btrfs_free_inode(trans
, root
, inode
);
783 btrfs_end_transaction(trans
, root
);
784 mutex_unlock(&root
->fs_info
->fs_mutex
);
785 btrfs_btree_balance_dirty(root
);
789 btrfs_end_transaction(trans
, root
);
790 mutex_unlock(&root
->fs_info
->fs_mutex
);
791 btrfs_btree_balance_dirty(root
);
797 * this returns the key found in the dir entry in the location pointer.
798 * If no dir entries were found, location->objectid is 0.
800 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
801 struct btrfs_key
*location
)
803 const char *name
= dentry
->d_name
.name
;
804 int namelen
= dentry
->d_name
.len
;
805 struct btrfs_dir_item
*di
;
806 struct btrfs_path
*path
;
807 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
810 path
= btrfs_alloc_path();
812 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
814 if (!di
|| IS_ERR(di
)) {
815 location
->objectid
= 0;
819 btrfs_disk_key_to_cpu(location
, &di
->location
);
821 btrfs_release_path(root
, path
);
822 btrfs_free_path(path
);
827 * when we hit a tree root in a directory, the btrfs part of the inode
828 * needs to be changed to reflect the root directory of the tree root. This
829 * is kind of like crossing a mount point.
831 static int fixup_tree_root_location(struct btrfs_root
*root
,
832 struct btrfs_key
*location
,
833 struct btrfs_root
**sub_root
,
834 struct dentry
*dentry
)
836 struct btrfs_path
*path
;
837 struct btrfs_root_item
*ri
;
839 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
841 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
844 path
= btrfs_alloc_path();
846 mutex_lock(&root
->fs_info
->fs_mutex
);
848 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
851 if (IS_ERR(*sub_root
))
852 return PTR_ERR(*sub_root
);
854 ri
= &(*sub_root
)->root_item
;
855 location
->objectid
= btrfs_root_dirid(ri
);
857 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
858 location
->offset
= 0;
860 btrfs_free_path(path
);
861 mutex_unlock(&root
->fs_info
->fs_mutex
);
865 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
867 struct btrfs_iget_args
*args
= p
;
868 inode
->i_ino
= args
->ino
;
869 BTRFS_I(inode
)->root
= args
->root
;
870 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
871 inode
->i_mapping
, GFP_NOFS
);
875 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
877 struct btrfs_iget_args
*args
= opaque
;
878 return (args
->ino
== inode
->i_ino
&&
879 args
->root
== BTRFS_I(inode
)->root
);
882 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
883 struct btrfs_root
*root
)
886 struct btrfs_iget_args args
;
890 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
891 btrfs_init_locked_inode
,
896 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
897 struct nameidata
*nd
)
899 struct inode
* inode
;
900 struct btrfs_inode
*bi
= BTRFS_I(dir
);
901 struct btrfs_root
*root
= bi
->root
;
902 struct btrfs_root
*sub_root
= root
;
903 struct btrfs_key location
;
906 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
907 return ERR_PTR(-ENAMETOOLONG
);
908 mutex_lock(&root
->fs_info
->fs_mutex
);
909 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
910 mutex_unlock(&root
->fs_info
->fs_mutex
);
914 if (location
.objectid
) {
915 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
920 return ERR_PTR(-ENOENT
);
921 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
924 return ERR_PTR(-EACCES
);
925 if (inode
->i_state
& I_NEW
) {
926 /* the inode and parent dir are two different roots */
927 if (sub_root
!= root
) {
929 sub_root
->inode
= inode
;
931 BTRFS_I(inode
)->root
= sub_root
;
932 memcpy(&BTRFS_I(inode
)->location
, &location
,
934 btrfs_read_locked_inode(inode
);
935 unlock_new_inode(inode
);
938 return d_splice_alias(inode
, dentry
);
941 static unsigned char btrfs_filetype_table
[] = {
942 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
945 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
947 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
948 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
949 struct btrfs_item
*item
;
950 struct btrfs_dir_item
*di
;
951 struct btrfs_key key
;
952 struct btrfs_path
*path
;
955 struct btrfs_leaf
*leaf
;
958 unsigned char d_type
;
963 int key_type
= BTRFS_DIR_INDEX_KEY
;
965 /* FIXME, use a real flag for deciding about the key type */
966 if (root
->fs_info
->tree_root
== root
)
967 key_type
= BTRFS_DIR_ITEM_KEY
;
968 mutex_lock(&root
->fs_info
->fs_mutex
);
969 key
.objectid
= inode
->i_ino
;
971 btrfs_set_key_type(&key
, key_type
);
972 key
.offset
= filp
->f_pos
;
973 path
= btrfs_alloc_path();
975 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
980 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
981 nritems
= btrfs_header_nritems(&leaf
->header
);
982 slot
= path
->slots
[0];
983 if (advance
|| slot
>= nritems
) {
984 if (slot
>= nritems
-1) {
985 ret
= btrfs_next_leaf(root
, path
);
988 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
989 nritems
= btrfs_header_nritems(&leaf
->header
);
990 slot
= path
->slots
[0];
997 item
= leaf
->items
+ slot
;
998 if (btrfs_disk_key_objectid(&item
->key
) != key
.objectid
)
1000 if (btrfs_disk_key_type(&item
->key
) != key_type
)
1002 if (btrfs_disk_key_offset(&item
->key
) < filp
->f_pos
)
1004 filp
->f_pos
= btrfs_disk_key_offset(&item
->key
);
1006 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
1008 di_total
= btrfs_item_size(leaf
->items
+ slot
);
1009 while(di_cur
< di_total
) {
1010 d_type
= btrfs_filetype_table
[btrfs_dir_type(di
)];
1011 over
= filldir(dirent
, (const char *)(di
+ 1),
1012 btrfs_dir_name_len(di
),
1013 btrfs_disk_key_offset(&item
->key
),
1014 btrfs_disk_key_objectid(&di
->location
),
1018 di_len
= btrfs_dir_name_len(di
) + sizeof(*di
);
1020 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
1027 btrfs_release_path(root
, path
);
1028 btrfs_free_path(path
);
1029 mutex_unlock(&root
->fs_info
->fs_mutex
);
1033 int btrfs_write_inode(struct inode
*inode
, int wait
)
1035 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1036 struct btrfs_trans_handle
*trans
;
1040 mutex_lock(&root
->fs_info
->fs_mutex
);
1041 trans
= btrfs_start_transaction(root
, 1);
1042 btrfs_set_trans_block_group(trans
, inode
);
1043 ret
= btrfs_commit_transaction(trans
, root
);
1044 mutex_unlock(&root
->fs_info
->fs_mutex
);
1050 * This is somewhat expensive, updating the tree every time the
1051 * inode changes. But, it is most likely to find the inode in cache.
1052 * FIXME, needs more benchmarking...there are no reasons other than performance
1053 * to keep or drop this code.
1055 void btrfs_dirty_inode(struct inode
*inode
)
1057 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1058 struct btrfs_trans_handle
*trans
;
1060 mutex_lock(&root
->fs_info
->fs_mutex
);
1061 trans
= btrfs_start_transaction(root
, 1);
1062 btrfs_set_trans_block_group(trans
, inode
);
1063 btrfs_update_inode(trans
, root
, inode
);
1064 btrfs_end_transaction(trans
, root
);
1065 mutex_unlock(&root
->fs_info
->fs_mutex
);
1068 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
1069 struct btrfs_root
*root
,
1071 struct btrfs_block_group_cache
*group
,
1074 struct inode
*inode
;
1075 struct btrfs_inode_item inode_item
;
1076 struct btrfs_key
*location
;
1080 inode
= new_inode(root
->fs_info
->sb
);
1082 return ERR_PTR(-ENOMEM
);
1084 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1085 inode
->i_mapping
, GFP_NOFS
);
1086 BTRFS_I(inode
)->root
= root
;
1092 group
= btrfs_find_block_group(root
, group
, 0, 0, owner
);
1093 BTRFS_I(inode
)->block_group
= group
;
1095 inode
->i_uid
= current
->fsuid
;
1096 inode
->i_gid
= current
->fsgid
;
1097 inode
->i_mode
= mode
;
1098 inode
->i_ino
= objectid
;
1099 inode
->i_blocks
= 0;
1100 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1101 fill_inode_item(&inode_item
, inode
);
1102 location
= &BTRFS_I(inode
)->location
;
1103 location
->objectid
= objectid
;
1104 location
->flags
= 0;
1105 location
->offset
= 0;
1106 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1108 ret
= btrfs_insert_inode(trans
, root
, objectid
, &inode_item
);
1110 return ERR_PTR(ret
);
1111 insert_inode_hash(inode
);
1115 static inline u8
btrfs_inode_type(struct inode
*inode
)
1117 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
1120 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
1121 struct dentry
*dentry
, struct inode
*inode
)
1124 struct btrfs_key key
;
1125 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
1126 struct inode
*parent_inode
;
1127 key
.objectid
= inode
->i_ino
;
1129 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1132 ret
= btrfs_insert_dir_item(trans
, root
,
1133 dentry
->d_name
.name
, dentry
->d_name
.len
,
1134 dentry
->d_parent
->d_inode
->i_ino
,
1135 &key
, btrfs_inode_type(inode
));
1137 parent_inode
= dentry
->d_parent
->d_inode
;
1138 parent_inode
->i_size
+= dentry
->d_name
.len
* 2;
1139 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
1140 ret
= btrfs_update_inode(trans
, root
,
1141 dentry
->d_parent
->d_inode
);
1146 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
1147 struct dentry
*dentry
, struct inode
*inode
)
1149 int err
= btrfs_add_link(trans
, dentry
, inode
);
1151 d_instantiate(dentry
, inode
);
1159 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1160 int mode
, dev_t rdev
)
1162 struct btrfs_trans_handle
*trans
;
1163 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1164 struct inode
*inode
;
1169 if (!new_valid_dev(rdev
))
1172 mutex_lock(&root
->fs_info
->fs_mutex
);
1173 trans
= btrfs_start_transaction(root
, 1);
1174 btrfs_set_trans_block_group(trans
, dir
);
1176 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1182 inode
= btrfs_new_inode(trans
, root
, objectid
,
1183 BTRFS_I(dir
)->block_group
, mode
);
1184 err
= PTR_ERR(inode
);
1188 btrfs_set_trans_block_group(trans
, inode
);
1189 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1193 inode
->i_op
= &btrfs_special_inode_operations
;
1194 init_special_inode(inode
, inode
->i_mode
, rdev
);
1195 btrfs_update_inode(trans
, root
, inode
);
1197 dir
->i_sb
->s_dirt
= 1;
1198 btrfs_update_inode_block_group(trans
, inode
);
1199 btrfs_update_inode_block_group(trans
, dir
);
1201 btrfs_end_transaction(trans
, root
);
1202 mutex_unlock(&root
->fs_info
->fs_mutex
);
1205 inode_dec_link_count(inode
);
1208 btrfs_btree_balance_dirty(root
);
1212 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
1213 int mode
, struct nameidata
*nd
)
1215 struct btrfs_trans_handle
*trans
;
1216 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1217 struct inode
*inode
;
1222 mutex_lock(&root
->fs_info
->fs_mutex
);
1223 trans
= btrfs_start_transaction(root
, 1);
1224 btrfs_set_trans_block_group(trans
, dir
);
1226 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1232 inode
= btrfs_new_inode(trans
, root
, objectid
,
1233 BTRFS_I(dir
)->block_group
, mode
);
1234 err
= PTR_ERR(inode
);
1238 btrfs_set_trans_block_group(trans
, inode
);
1239 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1243 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1244 inode
->i_fop
= &btrfs_file_operations
;
1245 inode
->i_op
= &btrfs_file_inode_operations
;
1246 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1247 inode
->i_mapping
, GFP_NOFS
);
1248 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
1250 dir
->i_sb
->s_dirt
= 1;
1251 btrfs_update_inode_block_group(trans
, inode
);
1252 btrfs_update_inode_block_group(trans
, dir
);
1254 btrfs_end_transaction(trans
, root
);
1255 mutex_unlock(&root
->fs_info
->fs_mutex
);
1258 inode_dec_link_count(inode
);
1261 btrfs_btree_balance_dirty(root
);
1265 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
1266 struct dentry
*dentry
)
1268 struct btrfs_trans_handle
*trans
;
1269 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1270 struct inode
*inode
= old_dentry
->d_inode
;
1274 if (inode
->i_nlink
== 0)
1278 mutex_lock(&root
->fs_info
->fs_mutex
);
1279 trans
= btrfs_start_transaction(root
, 1);
1280 btrfs_set_trans_block_group(trans
, dir
);
1281 atomic_inc(&inode
->i_count
);
1282 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1285 dir
->i_sb
->s_dirt
= 1;
1286 btrfs_update_inode_block_group(trans
, dir
);
1287 err
= btrfs_update_inode(trans
, root
, inode
);
1291 btrfs_end_transaction(trans
, root
);
1292 mutex_unlock(&root
->fs_info
->fs_mutex
);
1295 inode_dec_link_count(inode
);
1298 btrfs_btree_balance_dirty(root
);
1302 static int btrfs_make_empty_dir(struct btrfs_trans_handle
*trans
,
1303 struct btrfs_root
*root
,
1304 u64 objectid
, u64 dirid
)
1308 struct btrfs_key key
;
1313 key
.objectid
= objectid
;
1316 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1318 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 1, objectid
,
1319 &key
, BTRFS_FT_DIR
);
1322 key
.objectid
= dirid
;
1323 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 2, objectid
,
1324 &key
, BTRFS_FT_DIR
);
1331 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1333 struct inode
*inode
;
1334 struct btrfs_trans_handle
*trans
;
1335 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1337 int drop_on_err
= 0;
1340 mutex_lock(&root
->fs_info
->fs_mutex
);
1341 trans
= btrfs_start_transaction(root
, 1);
1342 btrfs_set_trans_block_group(trans
, dir
);
1343 if (IS_ERR(trans
)) {
1344 err
= PTR_ERR(trans
);
1348 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1354 inode
= btrfs_new_inode(trans
, root
, objectid
,
1355 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
1356 if (IS_ERR(inode
)) {
1357 err
= PTR_ERR(inode
);
1361 inode
->i_op
= &btrfs_dir_inode_operations
;
1362 inode
->i_fop
= &btrfs_dir_file_operations
;
1363 btrfs_set_trans_block_group(trans
, inode
);
1365 err
= btrfs_make_empty_dir(trans
, root
, inode
->i_ino
, dir
->i_ino
);
1370 err
= btrfs_update_inode(trans
, root
, inode
);
1373 err
= btrfs_add_link(trans
, dentry
, inode
);
1376 d_instantiate(dentry
, inode
);
1378 dir
->i_sb
->s_dirt
= 1;
1379 btrfs_update_inode_block_group(trans
, inode
);
1380 btrfs_update_inode_block_group(trans
, dir
);
1383 btrfs_end_transaction(trans
, root
);
1385 mutex_unlock(&root
->fs_info
->fs_mutex
);
1388 btrfs_btree_balance_dirty(root
);
1392 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
1393 size_t page_offset
, u64 start
, u64 end
,
1399 u64 extent_start
= 0;
1401 u64 objectid
= inode
->i_ino
;
1403 int failed_insert
= 0;
1404 struct btrfs_path
*path
;
1405 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1406 struct btrfs_file_extent_item
*item
;
1407 struct btrfs_leaf
*leaf
;
1408 struct btrfs_disk_key
*found_key
;
1409 struct extent_map
*em
= NULL
;
1410 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1411 struct btrfs_trans_handle
*trans
= NULL
;
1413 path
= btrfs_alloc_path();
1415 mutex_lock(&root
->fs_info
->fs_mutex
);
1418 em
= lookup_extent_mapping(em_tree
, start
, end
);
1423 em
= alloc_extent_map(GFP_NOFS
);
1431 em
->bdev
= inode
->i_sb
->s_bdev
;
1432 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
1433 objectid
, start
, 0);
1440 if (path
->slots
[0] == 0)
1445 item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]), path
->slots
[0],
1446 struct btrfs_file_extent_item
);
1447 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
1448 blocknr
= btrfs_file_extent_disk_blocknr(item
);
1449 blocknr
+= btrfs_file_extent_offset(item
);
1451 /* are we inside the extent that was found? */
1452 found_key
= &leaf
->items
[path
->slots
[0]].key
;
1453 found_type
= btrfs_disk_key_type(found_key
);
1454 if (btrfs_disk_key_objectid(found_key
) != objectid
||
1455 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
1459 found_type
= btrfs_file_extent_type(item
);
1460 extent_start
= btrfs_disk_key_offset(&leaf
->items
[path
->slots
[0]].key
);
1461 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1462 extent_end
= extent_start
+
1463 (btrfs_file_extent_num_blocks(item
) << inode
->i_blkbits
);
1465 if (start
< extent_start
|| start
>= extent_end
) {
1467 if (start
< extent_start
) {
1468 if (end
< extent_start
)
1470 em
->end
= extent_end
- 1;
1476 if (btrfs_file_extent_disk_blocknr(item
) == 0) {
1477 em
->start
= extent_start
;
1478 em
->end
= extent_end
- 1;
1479 em
->block_start
= 0;
1483 em
->block_start
= blocknr
<< inode
->i_blkbits
;
1484 em
->block_end
= em
->block_start
+
1485 (btrfs_file_extent_num_blocks(item
) <<
1486 inode
->i_blkbits
) - 1;
1487 em
->start
= extent_start
;
1488 em
->end
= extent_end
- 1;
1490 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1495 size
= btrfs_file_extent_inline_len(leaf
->items
+
1497 extent_end
= extent_start
+ size
;
1498 if (start
< extent_start
|| start
>= extent_end
) {
1500 if (start
< extent_start
) {
1501 if (end
< extent_start
)
1503 em
->end
= extent_end
- 1;
1509 em
->block_start
= EXTENT_MAP_INLINE
;
1510 em
->block_end
= EXTENT_MAP_INLINE
;
1511 em
->start
= extent_start
;
1512 em
->end
= extent_end
- 1;
1516 ptr
= btrfs_file_extent_inline_start(item
);
1518 memcpy(map
+ page_offset
, ptr
, size
);
1519 flush_dcache_page(result
->b_page
);
1521 set_extent_uptodate(em_tree
, extent_start
,
1522 extent_end
, GFP_NOFS
);
1525 printk("unkknown found_type %d\n", found_type
);
1532 em
->block_start
= 0;
1535 btrfs_release_path(root
, path
);
1536 if (em
->start
> start
|| em
->end
< start
) {
1537 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->end
, start
, end
);
1541 ret
= add_extent_mapping(em_tree
, em
);
1542 if (ret
== -EEXIST
) {
1543 free_extent_map(em
);
1546 if (failed_insert
> 5) {
1547 printk("failing to insert %Lu %Lu\n", start
, end
);
1555 btrfs_free_path(path
);
1557 ret
= btrfs_end_transaction(trans
, root
);
1561 mutex_unlock(&root
->fs_info
->fs_mutex
);
1563 free_extent_map(em
);
1565 return ERR_PTR(err
);
1570 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
1572 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
1575 static int btrfs_prepare_write(struct file
*file
, struct page
*page
,
1576 unsigned from
, unsigned to
)
1578 return extent_prepare_write(&BTRFS_I(page
->mapping
->host
)->extent_tree
,
1579 page
->mapping
->host
, page
, from
, to
,
1583 int btrfs_readpage(struct file
*file
, struct page
*page
)
1585 struct extent_map_tree
*tree
;
1586 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1587 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
1589 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
1591 struct extent_map_tree
*tree
;
1594 if (current
->flags
& PF_MEMALLOC
) {
1595 redirty_page_for_writepage(wbc
, page
);
1599 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1600 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
1603 static int btrfs_releasepage(struct page
*page
, gfp_t unused_gfp_flags
)
1605 struct extent_map_tree
*tree
;
1608 if (page
->private != 1) {
1610 return try_to_free_buffers(page
);
1612 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1613 ret
= try_release_extent_mapping(tree
, page
);
1615 ClearPagePrivate(page
);
1616 set_page_private(page
, 0);
1617 page_cache_release(page
);
1622 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
1624 struct extent_map_tree
*tree
;
1626 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1627 extent_invalidatepage(tree
, page
, offset
);
1628 btrfs_releasepage(page
, GFP_NOFS
);
1632 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1633 * called from a page fault handler when a page is first dirtied. Hence we must
1634 * be careful to check for EOF conditions here. We set the page up correctly
1635 * for a written page which means we get ENOSPC checking when writing into
1636 * holes and correct delalloc and unwritten extent mapping on filesystems that
1637 * support these features.
1639 * We are not allowed to take the i_mutex here so we have to play games to
1640 * protect against truncate races as the page could now be beyond EOF. Because
1641 * vmtruncate() writes the inode size before removing pages, once we have the
1642 * page lock we can determine safely if the page is beyond EOF. If it is not
1643 * beyond EOF, then the page is guaranteed safe against truncation until we
1646 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
1648 struct inode
*inode
= vma
->vm_file
->f_path
.dentry
->d_inode
;
1654 down_read(&BTRFS_I(inode
)->root
->snap_sem
);
1656 wait_on_page_writeback(page
);
1657 size
= i_size_read(inode
);
1658 page_start
= page
->index
<< PAGE_CACHE_SHIFT
;
1660 if ((page
->mapping
!= inode
->i_mapping
) ||
1661 (page_start
> size
)) {
1662 /* page got truncated out from underneath us */
1666 /* page is wholly or partially inside EOF */
1667 if (page_start
+ PAGE_CACHE_SIZE
> size
)
1668 end
= size
& ~PAGE_CACHE_MASK
;
1670 end
= PAGE_CACHE_SIZE
;
1672 ret
= btrfs_cow_one_page(inode
, page
, end
);
1675 up_read(&BTRFS_I(inode
)->root
->snap_sem
);
1680 static void btrfs_truncate(struct inode
*inode
)
1682 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1684 struct btrfs_trans_handle
*trans
;
1686 if (!S_ISREG(inode
->i_mode
))
1688 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1691 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1693 mutex_lock(&root
->fs_info
->fs_mutex
);
1694 trans
= btrfs_start_transaction(root
, 1);
1695 btrfs_set_trans_block_group(trans
, inode
);
1697 /* FIXME, add redo link to tree so we don't leak on crash */
1698 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
1699 btrfs_update_inode(trans
, root
, inode
);
1700 ret
= btrfs_end_transaction(trans
, root
);
1702 mutex_unlock(&root
->fs_info
->fs_mutex
);
1703 btrfs_btree_balance_dirty(root
);
1706 int btrfs_commit_write(struct file
*file
, struct page
*page
,
1707 unsigned from
, unsigned to
)
1709 return extent_commit_write(&BTRFS_I(page
->mapping
->host
)->extent_tree
,
1710 page
->mapping
->host
, page
, from
, to
);
1713 static int create_subvol(struct btrfs_root
*root
, char *name
, int namelen
)
1715 struct btrfs_trans_handle
*trans
;
1716 struct btrfs_key key
;
1717 struct btrfs_root_item root_item
;
1718 struct btrfs_inode_item
*inode_item
;
1719 struct buffer_head
*subvol
;
1720 struct btrfs_leaf
*leaf
;
1721 struct btrfs_root
*new_root
;
1722 struct inode
*inode
;
1727 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
1729 mutex_lock(&root
->fs_info
->fs_mutex
);
1730 trans
= btrfs_start_transaction(root
, 1);
1733 subvol
= btrfs_alloc_free_block(trans
, root
, 0, 0);
1735 return PTR_ERR(subvol
);
1736 leaf
= btrfs_buffer_leaf(subvol
);
1737 btrfs_set_header_nritems(&leaf
->header
, 0);
1738 btrfs_set_header_level(&leaf
->header
, 0);
1739 btrfs_set_header_blocknr(&leaf
->header
, bh_blocknr(subvol
));
1740 btrfs_set_header_generation(&leaf
->header
, trans
->transid
);
1741 btrfs_set_header_owner(&leaf
->header
, root
->root_key
.objectid
);
1742 memcpy(leaf
->header
.fsid
, root
->fs_info
->disk_super
->fsid
,
1743 sizeof(leaf
->header
.fsid
));
1744 btrfs_mark_buffer_dirty(subvol
);
1746 inode_item
= &root_item
.inode
;
1747 memset(inode_item
, 0, sizeof(*inode_item
));
1748 btrfs_set_inode_generation(inode_item
, 1);
1749 btrfs_set_inode_size(inode_item
, 3);
1750 btrfs_set_inode_nlink(inode_item
, 1);
1751 btrfs_set_inode_nblocks(inode_item
, 1);
1752 btrfs_set_inode_mode(inode_item
, S_IFDIR
| 0755);
1754 btrfs_set_root_blocknr(&root_item
, bh_blocknr(subvol
));
1755 btrfs_set_root_refs(&root_item
, 1);
1756 btrfs_set_root_blocks_used(&root_item
, 0);
1757 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
1758 root_item
.drop_level
= 0;
1762 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
1767 btrfs_set_root_dirid(&root_item
, new_dirid
);
1769 key
.objectid
= objectid
;
1772 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
1773 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
1779 * insert the directory item
1781 key
.offset
= (u64
)-1;
1782 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
1783 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
1784 name
, namelen
, dir
->i_ino
, &key
,
1789 ret
= btrfs_commit_transaction(trans
, root
);
1793 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
, name
, namelen
);
1796 trans
= btrfs_start_transaction(new_root
, 1);
1799 inode
= btrfs_new_inode(trans
, new_root
, new_dirid
,
1800 BTRFS_I(dir
)->block_group
, S_IFDIR
| 0700);
1803 inode
->i_op
= &btrfs_dir_inode_operations
;
1804 inode
->i_fop
= &btrfs_dir_file_operations
;
1805 new_root
->inode
= inode
;
1807 ret
= btrfs_make_empty_dir(trans
, new_root
, new_dirid
, new_dirid
);
1813 ret
= btrfs_update_inode(trans
, new_root
, inode
);
1817 err
= btrfs_commit_transaction(trans
, root
);
1821 mutex_unlock(&root
->fs_info
->fs_mutex
);
1822 btrfs_btree_balance_dirty(root
);
1826 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
1828 struct btrfs_trans_handle
*trans
;
1829 struct btrfs_key key
;
1830 struct btrfs_root_item new_root_item
;
1831 struct buffer_head
*tmp
;
1836 if (!root
->ref_cows
)
1839 down_write(&root
->snap_sem
);
1840 freeze_bdev(root
->fs_info
->sb
->s_bdev
);
1841 thaw_bdev(root
->fs_info
->sb
->s_bdev
, root
->fs_info
->sb
);
1843 mutex_lock(&root
->fs_info
->fs_mutex
);
1844 trans
= btrfs_start_transaction(root
, 1);
1847 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
1851 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
1856 memcpy(&new_root_item
, &root
->root_item
,
1857 sizeof(new_root_item
));
1859 key
.objectid
= objectid
;
1862 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
1863 btrfs_cow_block(trans
, root
, root
->node
, NULL
, 0, &tmp
);
1864 btrfs_set_root_blocknr(&new_root_item
, bh_blocknr(root
->node
));
1866 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
1872 * insert the directory item
1874 key
.offset
= (u64
)-1;
1875 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
1877 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
1878 &key
, BTRFS_FT_DIR
);
1883 ret
= btrfs_inc_root_ref(trans
, root
);
1887 err
= btrfs_commit_transaction(trans
, root
);
1890 mutex_unlock(&root
->fs_info
->fs_mutex
);
1891 up_write(&root
->snap_sem
);
1892 btrfs_btree_balance_dirty(root
);
1896 static unsigned long force_ra(struct address_space
*mapping
,
1897 struct file_ra_state
*ra
, struct file
*file
,
1898 pgoff_t offset
, pgoff_t last_index
)
1902 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
1903 req_size
= last_index
- offset
+ 1;
1904 offset
= page_cache_readahead(mapping
, ra
, file
, offset
, req_size
);
1907 req_size
= min(last_index
- offset
+ 1, (pgoff_t
)128);
1908 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
1909 return offset
+ req_size
;
1913 int btrfs_defrag_file(struct file
*file
) {
1914 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1915 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1917 unsigned long last_index
;
1918 unsigned long ra_index
= 0;
1923 mutex_lock(&inode
->i_mutex
);
1924 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
1925 for (i
= 0; i
<= last_index
; i
++) {
1926 if (i
== ra_index
) {
1927 ra_index
= force_ra(inode
->i_mapping
, &file
->f_ra
,
1928 file
, ra_index
, last_index
);
1930 page
= grab_cache_page(inode
->i_mapping
, i
);
1933 if (!PageUptodate(page
)) {
1934 btrfs_readpage(NULL
, page
);
1936 if (!PageUptodate(page
)) {
1938 page_cache_release(page
);
1942 page_start
= page
->index
<< PAGE_CACHE_SHIFT
;
1943 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
1945 lock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
1946 set_extent_delalloc(em_tree
, page_start
,
1947 page_end
, GFP_NOFS
);
1948 unlock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
1949 set_page_dirty(page
);
1951 page_cache_release(page
);
1952 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
1956 mutex_unlock(&inode
->i_mutex
);
1960 int btrfs_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int
1961 cmd
, unsigned long arg
)
1963 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1964 struct btrfs_ioctl_vol_args vol_args
;
1966 struct btrfs_dir_item
*di
;
1968 struct btrfs_path
*path
;
1972 case BTRFS_IOC_SNAP_CREATE
:
1973 if (copy_from_user(&vol_args
,
1974 (struct btrfs_ioctl_vol_args __user
*)arg
,
1977 namelen
= strlen(vol_args
.name
);
1978 if (namelen
> BTRFS_VOL_NAME_MAX
)
1980 if (strchr(vol_args
.name
, '/'))
1982 path
= btrfs_alloc_path();
1985 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
1986 mutex_lock(&root
->fs_info
->fs_mutex
);
1987 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
1989 vol_args
.name
, namelen
, 0);
1990 mutex_unlock(&root
->fs_info
->fs_mutex
);
1991 btrfs_free_path(path
);
1992 if (di
&& !IS_ERR(di
))
1997 if (root
== root
->fs_info
->tree_root
)
1998 ret
= create_subvol(root
, vol_args
.name
, namelen
);
2000 ret
= create_snapshot(root
, vol_args
.name
, namelen
);
2003 case BTRFS_IOC_DEFRAG
:
2004 if (S_ISDIR(inode
->i_mode
)) {
2005 mutex_lock(&root
->fs_info
->fs_mutex
);
2006 btrfs_defrag_root(root
, 0);
2007 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
2008 mutex_unlock(&root
->fs_info
->fs_mutex
);
2009 } else if (S_ISREG(inode
->i_mode
)) {
2010 btrfs_defrag_file(filp
);
2020 #ifdef CONFIG_COMPAT
2021 long btrfs_compat_ioctl(struct file
*file
, unsigned int cmd
,
2024 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
2027 ret
= btrfs_ioctl(inode
, file
, cmd
, (unsigned long) compat_ptr(arg
));
2035 * Called inside transaction, so use GFP_NOFS
2037 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2039 struct btrfs_inode
*ei
;
2041 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2045 return &ei
->vfs_inode
;
2048 void btrfs_destroy_inode(struct inode
*inode
)
2050 WARN_ON(!list_empty(&inode
->i_dentry
));
2051 WARN_ON(inode
->i_data
.nrpages
);
2053 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2056 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2057 unsigned long flags
)
2059 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2061 inode_init_once(&ei
->vfs_inode
);
2064 void btrfs_destroy_cachep(void)
2066 if (btrfs_inode_cachep
)
2067 kmem_cache_destroy(btrfs_inode_cachep
);
2068 if (btrfs_trans_handle_cachep
)
2069 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2070 if (btrfs_transaction_cachep
)
2071 kmem_cache_destroy(btrfs_transaction_cachep
);
2072 if (btrfs_bit_radix_cachep
)
2073 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2074 if (btrfs_path_cachep
)
2075 kmem_cache_destroy(btrfs_path_cachep
);
2078 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
2079 unsigned long extra_flags
,
2080 void (*ctor
)(void *, struct kmem_cache
*,
2083 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
2084 SLAB_MEM_SPREAD
| extra_flags
), ctor
2085 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2091 int btrfs_init_cachep(void)
2093 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
2094 sizeof(struct btrfs_inode
),
2096 if (!btrfs_inode_cachep
)
2098 btrfs_trans_handle_cachep
=
2099 btrfs_cache_create("btrfs_trans_handle_cache",
2100 sizeof(struct btrfs_trans_handle
),
2102 if (!btrfs_trans_handle_cachep
)
2104 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
2105 sizeof(struct btrfs_transaction
),
2107 if (!btrfs_transaction_cachep
)
2109 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
2110 sizeof(struct btrfs_transaction
),
2112 if (!btrfs_path_cachep
)
2114 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
2115 SLAB_DESTROY_BY_RCU
, NULL
);
2116 if (!btrfs_bit_radix_cachep
)
2120 btrfs_destroy_cachep();
2124 static int btrfs_getattr(struct vfsmount
*mnt
,
2125 struct dentry
*dentry
, struct kstat
*stat
)
2127 struct inode
*inode
= dentry
->d_inode
;
2128 generic_fillattr(inode
, stat
);
2129 stat
->blksize
= 256 * 1024;
2133 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
2134 struct inode
* new_dir
,struct dentry
*new_dentry
)
2136 struct btrfs_trans_handle
*trans
;
2137 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
2138 struct inode
*new_inode
= new_dentry
->d_inode
;
2139 struct inode
*old_inode
= old_dentry
->d_inode
;
2140 struct timespec ctime
= CURRENT_TIME
;
2141 struct btrfs_path
*path
;
2142 struct btrfs_dir_item
*di
;
2145 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
2146 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
2149 mutex_lock(&root
->fs_info
->fs_mutex
);
2150 trans
= btrfs_start_transaction(root
, 1);
2151 btrfs_set_trans_block_group(trans
, new_dir
);
2152 path
= btrfs_alloc_path();
2158 old_dentry
->d_inode
->i_nlink
++;
2159 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
2160 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
2161 old_inode
->i_ctime
= ctime
;
2162 if (S_ISDIR(old_inode
->i_mode
) && old_dir
!= new_dir
) {
2163 struct btrfs_key
*location
= &BTRFS_I(new_dir
)->location
;
2165 di
= btrfs_lookup_dir_item(trans
, root
, path
, old_inode
->i_ino
,
2175 old_parent_oid
= btrfs_disk_key_objectid(&di
->location
);
2176 ret
= btrfs_del_item(trans
, root
, path
);
2180 btrfs_release_path(root
, path
);
2182 di
= btrfs_lookup_dir_index_item(trans
, root
, path
,
2194 ret
= btrfs_del_item(trans
, root
, path
);
2198 btrfs_release_path(root
, path
);
2200 ret
= btrfs_insert_dir_item(trans
, root
, "..", 2,
2201 old_inode
->i_ino
, location
,
2208 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
2213 new_inode
->i_ctime
= CURRENT_TIME
;
2214 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
2218 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
);
2223 btrfs_free_path(path
);
2224 btrfs_end_transaction(trans
, root
);
2225 mutex_unlock(&root
->fs_info
->fs_mutex
);
2229 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
2230 const char *symname
)
2232 struct btrfs_trans_handle
*trans
;
2233 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2234 struct btrfs_path
*path
;
2235 struct btrfs_key key
;
2236 struct inode
*inode
;
2243 struct btrfs_file_extent_item
*ei
;
2245 name_len
= strlen(symname
) + 1;
2246 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
2247 return -ENAMETOOLONG
;
2248 mutex_lock(&root
->fs_info
->fs_mutex
);
2249 trans
= btrfs_start_transaction(root
, 1);
2250 btrfs_set_trans_block_group(trans
, dir
);
2252 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2258 inode
= btrfs_new_inode(trans
, root
, objectid
,
2259 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
2260 err
= PTR_ERR(inode
);
2264 btrfs_set_trans_block_group(trans
, inode
);
2265 err
= btrfs_add_nondir(trans
, dentry
, inode
);
2269 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2270 inode
->i_fop
= &btrfs_file_operations
;
2271 inode
->i_op
= &btrfs_file_inode_operations
;
2272 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
2273 inode
->i_mapping
, GFP_NOFS
);
2274 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
2276 dir
->i_sb
->s_dirt
= 1;
2277 btrfs_update_inode_block_group(trans
, inode
);
2278 btrfs_update_inode_block_group(trans
, dir
);
2282 path
= btrfs_alloc_path();
2284 key
.objectid
= inode
->i_ino
;
2287 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
2288 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
2289 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2295 ei
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
2296 path
->slots
[0], struct btrfs_file_extent_item
);
2297 btrfs_set_file_extent_generation(ei
, trans
->transid
);
2298 btrfs_set_file_extent_type(ei
,
2299 BTRFS_FILE_EXTENT_INLINE
);
2300 ptr
= btrfs_file_extent_inline_start(ei
);
2301 btrfs_memcpy(root
, path
->nodes
[0]->b_data
,
2302 ptr
, symname
, name_len
);
2303 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2304 btrfs_free_path(path
);
2305 inode
->i_op
= &btrfs_symlink_inode_operations
;
2306 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2307 inode
->i_size
= name_len
- 1;
2308 err
= btrfs_update_inode(trans
, root
, inode
);
2313 btrfs_end_transaction(trans
, root
);
2314 mutex_unlock(&root
->fs_info
->fs_mutex
);
2316 inode_dec_link_count(inode
);
2319 btrfs_btree_balance_dirty(root
);
2323 static struct inode_operations btrfs_dir_inode_operations
= {
2324 .lookup
= btrfs_lookup
,
2325 .create
= btrfs_create
,
2326 .unlink
= btrfs_unlink
,
2328 .mkdir
= btrfs_mkdir
,
2329 .rmdir
= btrfs_rmdir
,
2330 .rename
= btrfs_rename
,
2331 .symlink
= btrfs_symlink
,
2332 .setattr
= btrfs_setattr
,
2333 .mknod
= btrfs_mknod
,
2336 static struct inode_operations btrfs_dir_ro_inode_operations
= {
2337 .lookup
= btrfs_lookup
,
2340 static struct file_operations btrfs_dir_file_operations
= {
2341 .llseek
= generic_file_llseek
,
2342 .read
= generic_read_dir
,
2343 .readdir
= btrfs_readdir
,
2344 .ioctl
= btrfs_ioctl
,
2345 #ifdef CONFIG_COMPAT
2346 .compat_ioctl
= btrfs_compat_ioctl
,
2350 static struct extent_map_ops btrfs_extent_map_ops
= {
2351 .fill_delalloc
= run_delalloc_range
,
2352 .writepage_io_hook
= btrfs_writepage_io_hook
,
2353 .readpage_io_hook
= btrfs_readpage_io_hook
,
2354 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
2357 static struct address_space_operations btrfs_aops
= {
2358 .readpage
= btrfs_readpage
,
2359 .writepage
= btrfs_writepage
,
2360 .sync_page
= block_sync_page
,
2361 .prepare_write
= btrfs_prepare_write
,
2362 .commit_write
= btrfs_commit_write
,
2364 .invalidatepage
= btrfs_invalidatepage
,
2365 .releasepage
= btrfs_releasepage
,
2366 .set_page_dirty
= __set_page_dirty_nobuffers
,
2369 static struct address_space_operations btrfs_symlink_aops
= {
2370 .readpage
= btrfs_readpage
,
2371 .writepage
= btrfs_writepage
,
2372 .invalidatepage
= btrfs_invalidatepage
,
2373 .releasepage
= btrfs_releasepage
,
2376 static struct inode_operations btrfs_file_inode_operations
= {
2377 .truncate
= btrfs_truncate
,
2378 .getattr
= btrfs_getattr
,
2379 .setattr
= btrfs_setattr
,
2382 static struct inode_operations btrfs_special_inode_operations
= {
2383 .getattr
= btrfs_getattr
,
2384 .setattr
= btrfs_setattr
,
2387 static struct inode_operations btrfs_symlink_inode_operations
= {
2388 .readlink
= generic_readlink
,
2389 .follow_link
= page_follow_link_light
,
2390 .put_link
= page_put_link
,