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>
35 #include <linux/xattr.h>
38 #include "transaction.h"
39 #include "btrfs_inode.h"
41 #include "print-tree.h"
43 struct btrfs_iget_args
{
45 struct btrfs_root
*root
;
48 static struct inode_operations btrfs_dir_inode_operations
;
49 static struct inode_operations btrfs_symlink_inode_operations
;
50 static struct inode_operations btrfs_dir_ro_inode_operations
;
51 static struct inode_operations btrfs_special_inode_operations
;
52 static struct inode_operations btrfs_file_inode_operations
;
53 static struct address_space_operations btrfs_aops
;
54 static struct address_space_operations btrfs_symlink_aops
;
55 static struct file_operations btrfs_dir_file_operations
;
56 static struct extent_map_ops btrfs_extent_map_ops
;
58 static struct kmem_cache
*btrfs_inode_cachep
;
59 struct kmem_cache
*btrfs_trans_handle_cachep
;
60 struct kmem_cache
*btrfs_transaction_cachep
;
61 struct kmem_cache
*btrfs_bit_radix_cachep
;
62 struct kmem_cache
*btrfs_path_cachep
;
65 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
66 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
67 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
68 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
69 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
70 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
71 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
72 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
75 static int run_delalloc_range(struct inode
*inode
, u64 start
, u64 end
)
77 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
78 struct btrfs_trans_handle
*trans
;
83 u64 blocksize
= root
->sectorsize
;
85 mutex_lock(&root
->fs_info
->fs_mutex
);
86 trans
= btrfs_start_transaction(root
, 1);
87 btrfs_set_trans_block_group(trans
, inode
);
89 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
90 ret
= btrfs_drop_extents(trans
, root
, inode
,
91 start
, start
+ num_bytes
, start
, &alloc_hint
);
93 if (alloc_hint
== EXTENT_MAP_INLINE
)
96 ret
= btrfs_alloc_extent(trans
, root
, inode
->i_ino
, num_bytes
, 0,
97 alloc_hint
, (u64
)-1, &ins
, 1);
102 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
103 start
, ins
.objectid
, ins
.offset
,
106 btrfs_end_transaction(trans
, root
);
107 mutex_unlock(&root
->fs_info
->fs_mutex
);
111 int btrfs_writepage_io_hook(struct page
*page
, u64 start
, u64 end
)
113 struct inode
*inode
= page
->mapping
->host
;
114 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
115 struct btrfs_trans_handle
*trans
;
118 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
119 size_t offset
= start
- page_start
;
121 mutex_lock(&root
->fs_info
->fs_mutex
);
122 trans
= btrfs_start_transaction(root
, 1);
123 btrfs_set_trans_block_group(trans
, inode
);
125 btrfs_csum_file_block(trans
, root
, inode
, inode
->i_ino
,
126 start
, kaddr
+ offset
, end
- start
+ 1);
128 ret
= btrfs_end_transaction(trans
, root
);
130 mutex_unlock(&root
->fs_info
->fs_mutex
);
134 int btrfs_readpage_io_hook(struct page
*page
, u64 start
, u64 end
)
137 struct inode
*inode
= page
->mapping
->host
;
138 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
139 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
140 struct btrfs_csum_item
*item
;
141 struct btrfs_path
*path
= NULL
;
144 mutex_lock(&root
->fs_info
->fs_mutex
);
145 path
= btrfs_alloc_path();
146 item
= btrfs_lookup_csum(NULL
, root
, path
, inode
->i_ino
, start
, 0);
149 /* a csum that isn't present is a preallocated region. */
150 if (ret
== -ENOENT
|| ret
== -EFBIG
)
155 read_extent_buffer(path
->nodes
[0], &csum
, (unsigned long)item
,
157 set_state_private(em_tree
, start
, csum
);
160 btrfs_free_path(path
);
161 mutex_unlock(&root
->fs_info
->fs_mutex
);
165 int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
)
167 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
168 struct inode
*inode
= page
->mapping
->host
;
169 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
173 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
177 ret
= get_state_private(em_tree
, start
, &private);
178 local_irq_save(flags
);
179 kaddr
= kmap_atomic(page
, KM_IRQ0
);
183 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
184 btrfs_csum_final(csum
, (char *)&csum
);
185 if (csum
!= private) {
188 kunmap_atomic(kaddr
, KM_IRQ0
);
189 local_irq_restore(flags
);
193 printk("btrfs csum failed ino %lu off %llu\n",
194 page
->mapping
->host
->i_ino
, (unsigned long long)start
);
195 memset(kaddr
+ offset
, 1, end
- start
+ 1);
196 flush_dcache_page(page
);
197 kunmap_atomic(kaddr
, KM_IRQ0
);
198 local_irq_restore(flags
);
202 void btrfs_read_locked_inode(struct inode
*inode
)
204 struct btrfs_path
*path
;
205 struct extent_buffer
*leaf
;
206 struct btrfs_inode_item
*inode_item
;
207 struct btrfs_inode_timespec
*tspec
;
208 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
209 struct btrfs_key location
;
210 u64 alloc_group_block
;
214 path
= btrfs_alloc_path();
216 mutex_lock(&root
->fs_info
->fs_mutex
);
218 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
219 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
223 leaf
= path
->nodes
[0];
224 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
225 struct btrfs_inode_item
);
227 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
228 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
229 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
230 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
231 inode
->i_size
= btrfs_inode_size(leaf
, inode_item
);
233 tspec
= btrfs_inode_atime(inode_item
);
234 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
235 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
237 tspec
= btrfs_inode_mtime(inode_item
);
238 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
239 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
241 tspec
= btrfs_inode_ctime(inode_item
);
242 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
243 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
245 inode
->i_blocks
= btrfs_inode_nblocks(leaf
, inode_item
);
246 inode
->i_generation
= btrfs_inode_generation(leaf
, inode_item
);
248 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
250 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
251 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
254 btrfs_free_path(path
);
257 mutex_unlock(&root
->fs_info
->fs_mutex
);
259 switch (inode
->i_mode
& S_IFMT
) {
261 inode
->i_mapping
->a_ops
= &btrfs_aops
;
262 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
263 inode
->i_fop
= &btrfs_file_operations
;
264 inode
->i_op
= &btrfs_file_inode_operations
;
267 inode
->i_fop
= &btrfs_dir_file_operations
;
268 if (root
== root
->fs_info
->tree_root
)
269 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
271 inode
->i_op
= &btrfs_dir_inode_operations
;
274 inode
->i_op
= &btrfs_symlink_inode_operations
;
275 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
278 init_special_inode(inode
, inode
->i_mode
, rdev
);
284 btrfs_release_path(root
, path
);
285 btrfs_free_path(path
);
286 mutex_unlock(&root
->fs_info
->fs_mutex
);
287 make_bad_inode(inode
);
290 static void fill_inode_item(struct extent_buffer
*leaf
,
291 struct btrfs_inode_item
*item
,
294 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
295 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
296 btrfs_set_inode_size(leaf
, item
, inode
->i_size
);
297 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
298 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
300 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
301 inode
->i_atime
.tv_sec
);
302 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
303 inode
->i_atime
.tv_nsec
);
305 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
306 inode
->i_mtime
.tv_sec
);
307 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
308 inode
->i_mtime
.tv_nsec
);
310 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
311 inode
->i_ctime
.tv_sec
);
312 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
313 inode
->i_ctime
.tv_nsec
);
315 btrfs_set_inode_nblocks(leaf
, item
, inode
->i_blocks
);
316 btrfs_set_inode_generation(leaf
, item
, inode
->i_generation
);
317 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
318 btrfs_set_inode_block_group(leaf
, item
,
319 BTRFS_I(inode
)->block_group
->key
.objectid
);
322 int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
323 struct btrfs_root
*root
,
326 struct btrfs_inode_item
*inode_item
;
327 struct btrfs_path
*path
;
328 struct extent_buffer
*leaf
;
331 path
= btrfs_alloc_path();
333 ret
= btrfs_lookup_inode(trans
, root
, path
,
334 &BTRFS_I(inode
)->location
, 1);
341 leaf
= path
->nodes
[0];
342 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
343 struct btrfs_inode_item
);
345 fill_inode_item(leaf
, inode_item
, inode
);
346 btrfs_mark_buffer_dirty(leaf
);
347 btrfs_set_inode_last_trans(trans
, inode
);
350 btrfs_release_path(root
, path
);
351 btrfs_free_path(path
);
356 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
357 struct btrfs_root
*root
,
359 struct dentry
*dentry
)
361 struct btrfs_path
*path
;
362 const char *name
= dentry
->d_name
.name
;
363 int name_len
= dentry
->d_name
.len
;
365 struct extent_buffer
*leaf
;
366 struct btrfs_dir_item
*di
;
367 struct btrfs_key key
;
369 path
= btrfs_alloc_path();
375 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
385 leaf
= path
->nodes
[0];
386 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
387 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
390 btrfs_release_path(root
, path
);
392 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
393 key
.objectid
, name
, name_len
, -1);
402 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
404 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
406 btrfs_free_path(path
);
408 dir
->i_size
-= name_len
* 2;
409 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
410 btrfs_update_inode(trans
, root
, dir
);
411 drop_nlink(dentry
->d_inode
);
412 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
413 dir
->i_sb
->s_dirt
= 1;
418 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
420 struct btrfs_root
*root
;
421 struct btrfs_trans_handle
*trans
;
425 root
= BTRFS_I(dir
)->root
;
426 mutex_lock(&root
->fs_info
->fs_mutex
);
427 trans
= btrfs_start_transaction(root
, 1);
429 btrfs_set_trans_block_group(trans
, dir
);
430 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
431 nr
= trans
->blocks_used
;
433 btrfs_end_transaction(trans
, root
);
434 mutex_unlock(&root
->fs_info
->fs_mutex
);
435 btrfs_btree_balance_dirty(root
, nr
);
440 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
442 struct inode
*inode
= dentry
->d_inode
;
445 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
446 struct btrfs_path
*path
;
447 struct btrfs_key key
;
448 struct btrfs_trans_handle
*trans
;
449 struct btrfs_key found_key
;
451 struct extent_buffer
*leaf
;
452 char *goodnames
= "..";
455 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
458 path
= btrfs_alloc_path();
460 mutex_lock(&root
->fs_info
->fs_mutex
);
461 trans
= btrfs_start_transaction(root
, 1);
463 btrfs_set_trans_block_group(trans
, dir
);
464 key
.objectid
= inode
->i_ino
;
465 key
.offset
= (u64
)-1;
468 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
474 if (path
->slots
[0] == 0) {
479 leaf
= path
->nodes
[0];
480 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
481 found_type
= btrfs_key_type(&found_key
);
482 if (found_key
.objectid
!= inode
->i_ino
) {
486 if ((found_type
!= BTRFS_DIR_ITEM_KEY
&&
487 found_type
!= BTRFS_DIR_INDEX_KEY
) ||
488 (!btrfs_match_dir_item_name(root
, path
, goodnames
, 2) &&
489 !btrfs_match_dir_item_name(root
, path
, goodnames
, 1))) {
493 ret
= btrfs_del_item(trans
, root
, path
);
496 if (found_type
== BTRFS_DIR_ITEM_KEY
&& found_key
.offset
== 1)
498 btrfs_release_path(root
, path
);
501 btrfs_release_path(root
, path
);
503 /* now the directory is empty */
504 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
509 btrfs_release_path(root
, path
);
510 btrfs_free_path(path
);
511 nr
= trans
->blocks_used
;
512 ret
= btrfs_end_transaction(trans
, root
);
513 mutex_unlock(&root
->fs_info
->fs_mutex
);
514 btrfs_btree_balance_dirty(root
, nr
);
520 static int btrfs_free_inode(struct btrfs_trans_handle
*trans
,
521 struct btrfs_root
*root
,
524 struct btrfs_path
*path
;
529 path
= btrfs_alloc_path();
531 ret
= btrfs_lookup_inode(trans
, root
, path
,
532 &BTRFS_I(inode
)->location
, -1);
536 ret
= btrfs_del_item(trans
, root
, path
);
537 btrfs_free_path(path
);
542 * this can truncate away extent items, csum items and directory items.
543 * It starts at a high offset and removes keys until it can't find
544 * any higher than i_size.
546 * csum items that cross the new i_size are truncated to the new size
549 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
550 struct btrfs_root
*root
,
554 struct btrfs_path
*path
;
555 struct btrfs_key key
;
556 struct btrfs_key found_key
;
558 struct extent_buffer
*leaf
;
559 struct btrfs_file_extent_item
*fi
;
560 u64 extent_start
= 0;
561 u64 extent_num_bytes
= 0;
565 int extent_type
= -1;
567 btrfs_drop_extent_cache(inode
, inode
->i_size
, (u64
)-1);
568 path
= btrfs_alloc_path();
572 /* FIXME, add redo link to tree so we don't leak on crash */
573 key
.objectid
= inode
->i_ino
;
574 key
.offset
= (u64
)-1;
578 btrfs_init_path(path
);
580 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
585 BUG_ON(path
->slots
[0] == 0);
588 leaf
= path
->nodes
[0];
589 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
590 found_type
= btrfs_key_type(&found_key
);
592 if (found_key
.objectid
!= inode
->i_ino
)
595 if (found_type
!= BTRFS_CSUM_ITEM_KEY
&&
596 found_type
!= BTRFS_DIR_ITEM_KEY
&&
597 found_type
!= BTRFS_DIR_INDEX_KEY
&&
598 found_type
!= BTRFS_EXTENT_DATA_KEY
)
601 item_end
= found_key
.offset
;
602 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
603 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
604 struct btrfs_file_extent_item
);
605 extent_type
= btrfs_file_extent_type(leaf
, fi
);
606 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
608 btrfs_file_extent_num_bytes(leaf
, fi
);
609 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
610 struct btrfs_item
*item
= btrfs_item_nr(leaf
,
612 item_end
+= btrfs_file_extent_inline_len(leaf
,
617 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
618 ret
= btrfs_csum_truncate(trans
, root
, path
,
622 if (item_end
< inode
->i_size
) {
623 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
624 found_type
= BTRFS_INODE_ITEM_KEY
;
625 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
626 found_type
= BTRFS_CSUM_ITEM_KEY
;
627 } else if (found_type
) {
632 btrfs_set_key_type(&key
, found_type
);
633 btrfs_release_path(root
, path
);
636 if (found_key
.offset
>= inode
->i_size
)
642 /* FIXME, shrink the extent if the ref count is only 1 */
643 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
646 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
648 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
651 btrfs_file_extent_num_bytes(leaf
, fi
);
652 extent_num_bytes
= inode
->i_size
-
653 found_key
.offset
+ root
->sectorsize
- 1;
654 btrfs_set_file_extent_num_bytes(leaf
, fi
,
656 num_dec
= (orig_num_bytes
-
657 extent_num_bytes
) >> 9;
658 if (extent_start
!= 0) {
659 inode
->i_blocks
-= num_dec
;
661 btrfs_mark_buffer_dirty(leaf
);
664 btrfs_file_extent_disk_num_bytes(leaf
,
666 /* FIXME blocksize != 4096 */
667 num_dec
= btrfs_file_extent_num_bytes(leaf
,
669 if (extent_start
!= 0) {
671 inode
->i_blocks
-= num_dec
;
674 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
&&
676 u32 newsize
= inode
->i_size
- found_key
.offset
;
677 newsize
= btrfs_file_extent_calc_inline_size(newsize
);
678 ret
= btrfs_truncate_item(trans
, root
, path
,
684 ret
= btrfs_del_item(trans
, root
, path
);
690 btrfs_release_path(root
, path
);
692 ret
= btrfs_free_extent(trans
, root
, extent_start
,
693 extent_num_bytes
, 0);
699 btrfs_release_path(root
, path
);
700 btrfs_free_path(path
);
701 inode
->i_sb
->s_dirt
= 1;
705 static int btrfs_cow_one_page(struct inode
*inode
, struct page
*page
,
710 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
711 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
712 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
714 set_page_extent_mapped(page
);
716 lock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
717 set_extent_delalloc(&BTRFS_I(inode
)->extent_tree
, page_start
,
719 if (zero_start
!= PAGE_CACHE_SIZE
) {
721 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
722 flush_dcache_page(page
);
725 set_page_dirty(page
);
726 unlock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
732 * taken from block_truncate_page, but does cow as it zeros out
733 * any bytes left in the last page in the file.
735 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
737 struct inode
*inode
= mapping
->host
;
738 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
739 u32 blocksize
= root
->sectorsize
;
740 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
741 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
746 if ((offset
& (blocksize
- 1)) == 0)
749 down_read(&root
->snap_sem
);
751 page
= grab_cache_page(mapping
, index
);
754 if (!PageUptodate(page
)) {
755 ret
= btrfs_readpage(NULL
, page
);
757 if (!PageUptodate(page
)) {
762 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
764 ret
= btrfs_cow_one_page(inode
, page
, offset
);
767 page_cache_release(page
);
768 up_read(&BTRFS_I(inode
)->root
->snap_sem
);
773 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
775 struct inode
*inode
= dentry
->d_inode
;
778 err
= inode_change_ok(inode
, attr
);
782 if (S_ISREG(inode
->i_mode
) &&
783 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
784 struct btrfs_trans_handle
*trans
;
785 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
786 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
788 u64 mask
= root
->sectorsize
- 1;
789 u64 pos
= (inode
->i_size
+ mask
) & ~mask
;
790 u64 block_end
= attr
->ia_size
| mask
;
794 if (attr
->ia_size
<= pos
)
797 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
799 lock_extent(em_tree
, pos
, block_end
, GFP_NOFS
);
800 hole_size
= (attr
->ia_size
- pos
+ mask
) & ~mask
;
802 mutex_lock(&root
->fs_info
->fs_mutex
);
803 trans
= btrfs_start_transaction(root
, 1);
804 btrfs_set_trans_block_group(trans
, inode
);
805 err
= btrfs_drop_extents(trans
, root
, inode
,
806 pos
, pos
+ hole_size
, pos
,
809 if (alloc_hint
!= EXTENT_MAP_INLINE
) {
810 err
= btrfs_insert_file_extent(trans
, root
,
812 pos
, 0, 0, hole_size
);
814 btrfs_end_transaction(trans
, root
);
815 mutex_unlock(&root
->fs_info
->fs_mutex
);
816 unlock_extent(em_tree
, pos
, block_end
, GFP_NOFS
);
821 err
= inode_setattr(inode
, attr
);
825 void btrfs_delete_inode(struct inode
*inode
)
827 struct btrfs_trans_handle
*trans
;
828 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
832 truncate_inode_pages(&inode
->i_data
, 0);
833 if (is_bad_inode(inode
)) {
838 mutex_lock(&root
->fs_info
->fs_mutex
);
839 trans
= btrfs_start_transaction(root
, 1);
841 btrfs_set_trans_block_group(trans
, inode
);
842 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
845 ret
= btrfs_delete_xattrs(trans
, root
, inode
);
848 ret
= btrfs_free_inode(trans
, root
, inode
);
851 nr
= trans
->blocks_used
;
853 btrfs_end_transaction(trans
, root
);
854 mutex_unlock(&root
->fs_info
->fs_mutex
);
855 btrfs_btree_balance_dirty(root
, nr
);
859 nr
= trans
->blocks_used
;
860 btrfs_end_transaction(trans
, root
);
861 mutex_unlock(&root
->fs_info
->fs_mutex
);
862 btrfs_btree_balance_dirty(root
, nr
);
868 * this returns the key found in the dir entry in the location pointer.
869 * If no dir entries were found, location->objectid is 0.
871 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
872 struct btrfs_key
*location
)
874 const char *name
= dentry
->d_name
.name
;
875 int namelen
= dentry
->d_name
.len
;
876 struct btrfs_dir_item
*di
;
877 struct btrfs_path
*path
;
878 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
881 path
= btrfs_alloc_path();
883 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
887 if (!di
|| IS_ERR(di
)) {
888 location
->objectid
= 0;
891 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
893 btrfs_release_path(root
, path
);
894 btrfs_free_path(path
);
899 * when we hit a tree root in a directory, the btrfs part of the inode
900 * needs to be changed to reflect the root directory of the tree root. This
901 * is kind of like crossing a mount point.
903 static int fixup_tree_root_location(struct btrfs_root
*root
,
904 struct btrfs_key
*location
,
905 struct btrfs_root
**sub_root
,
906 struct dentry
*dentry
)
908 struct btrfs_path
*path
;
909 struct btrfs_root_item
*ri
;
911 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
913 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
916 path
= btrfs_alloc_path();
918 mutex_lock(&root
->fs_info
->fs_mutex
);
920 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
923 if (IS_ERR(*sub_root
))
924 return PTR_ERR(*sub_root
);
926 ri
= &(*sub_root
)->root_item
;
927 location
->objectid
= btrfs_root_dirid(ri
);
928 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
929 location
->offset
= 0;
931 btrfs_free_path(path
);
932 mutex_unlock(&root
->fs_info
->fs_mutex
);
936 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
938 struct btrfs_iget_args
*args
= p
;
939 inode
->i_ino
= args
->ino
;
940 BTRFS_I(inode
)->root
= args
->root
;
941 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
942 inode
->i_mapping
, GFP_NOFS
);
946 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
948 struct btrfs_iget_args
*args
= opaque
;
949 return (args
->ino
== inode
->i_ino
&&
950 args
->root
== BTRFS_I(inode
)->root
);
953 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
954 struct btrfs_root
*root
)
957 struct btrfs_iget_args args
;
961 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
962 btrfs_init_locked_inode
,
967 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
968 struct nameidata
*nd
)
970 struct inode
* inode
;
971 struct btrfs_inode
*bi
= BTRFS_I(dir
);
972 struct btrfs_root
*root
= bi
->root
;
973 struct btrfs_root
*sub_root
= root
;
974 struct btrfs_key location
;
977 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
978 return ERR_PTR(-ENAMETOOLONG
);
980 mutex_lock(&root
->fs_info
->fs_mutex
);
981 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
982 mutex_unlock(&root
->fs_info
->fs_mutex
);
988 if (location
.objectid
) {
989 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
994 return ERR_PTR(-ENOENT
);
995 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
998 return ERR_PTR(-EACCES
);
999 if (inode
->i_state
& I_NEW
) {
1000 /* the inode and parent dir are two different roots */
1001 if (sub_root
!= root
) {
1003 sub_root
->inode
= inode
;
1005 BTRFS_I(inode
)->root
= sub_root
;
1006 memcpy(&BTRFS_I(inode
)->location
, &location
,
1008 btrfs_read_locked_inode(inode
);
1009 unlock_new_inode(inode
);
1012 return d_splice_alias(inode
, dentry
);
1015 static unsigned char btrfs_filetype_table
[] = {
1016 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
1019 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1021 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1022 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1023 struct btrfs_item
*item
;
1024 struct btrfs_dir_item
*di
;
1025 struct btrfs_key key
;
1026 struct btrfs_key found_key
;
1027 struct btrfs_path
*path
;
1030 struct extent_buffer
*leaf
;
1033 unsigned char d_type
;
1038 int key_type
= BTRFS_DIR_INDEX_KEY
;
1043 /* FIXME, use a real flag for deciding about the key type */
1044 if (root
->fs_info
->tree_root
== root
)
1045 key_type
= BTRFS_DIR_ITEM_KEY
;
1047 mutex_lock(&root
->fs_info
->fs_mutex
);
1048 key
.objectid
= inode
->i_ino
;
1049 btrfs_set_key_type(&key
, key_type
);
1050 key
.offset
= filp
->f_pos
;
1052 path
= btrfs_alloc_path();
1054 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1059 leaf
= path
->nodes
[0];
1060 nritems
= btrfs_header_nritems(leaf
);
1061 slot
= path
->slots
[0];
1062 if (advance
|| slot
>= nritems
) {
1063 if (slot
>= nritems
-1) {
1064 ret
= btrfs_next_leaf(root
, path
);
1067 leaf
= path
->nodes
[0];
1068 nritems
= btrfs_header_nritems(leaf
);
1069 slot
= path
->slots
[0];
1076 item
= btrfs_item_nr(leaf
, slot
);
1077 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1079 if (found_key
.objectid
!= key
.objectid
)
1081 if (btrfs_key_type(&found_key
) != key_type
)
1083 if (found_key
.offset
< filp
->f_pos
)
1086 filp
->f_pos
= found_key
.offset
;
1088 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
1090 di_total
= btrfs_item_size(leaf
, item
);
1091 while(di_cur
< di_total
) {
1092 struct btrfs_key location
;
1094 name_len
= btrfs_dir_name_len(leaf
, di
);
1095 if (name_len
< 32) {
1096 name_ptr
= tmp_name
;
1098 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
1101 read_extent_buffer(leaf
, name_ptr
,
1102 (unsigned long)(di
+ 1), name_len
);
1104 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
1105 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
1107 over
= filldir(dirent
, name_ptr
, name_len
,
1112 if (name_ptr
!= tmp_name
)
1117 di_len
= btrfs_dir_name_len(leaf
, di
) +
1118 btrfs_dir_data_len(leaf
, di
) +sizeof(*di
);
1120 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
1127 btrfs_release_path(root
, path
);
1128 btrfs_free_path(path
);
1129 mutex_unlock(&root
->fs_info
->fs_mutex
);
1133 int btrfs_write_inode(struct inode
*inode
, int wait
)
1135 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1136 struct btrfs_trans_handle
*trans
;
1140 mutex_lock(&root
->fs_info
->fs_mutex
);
1141 trans
= btrfs_start_transaction(root
, 1);
1142 btrfs_set_trans_block_group(trans
, inode
);
1143 ret
= btrfs_commit_transaction(trans
, root
);
1144 mutex_unlock(&root
->fs_info
->fs_mutex
);
1150 * This is somewhat expensive, updating the tree every time the
1151 * inode changes. But, it is most likely to find the inode in cache.
1152 * FIXME, needs more benchmarking...there are no reasons other than performance
1153 * to keep or drop this code.
1155 void btrfs_dirty_inode(struct inode
*inode
)
1157 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1158 struct btrfs_trans_handle
*trans
;
1160 mutex_lock(&root
->fs_info
->fs_mutex
);
1161 trans
= btrfs_start_transaction(root
, 1);
1162 btrfs_set_trans_block_group(trans
, inode
);
1163 btrfs_update_inode(trans
, root
, inode
);
1164 btrfs_end_transaction(trans
, root
);
1165 mutex_unlock(&root
->fs_info
->fs_mutex
);
1168 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
1169 struct btrfs_root
*root
,
1171 struct btrfs_block_group_cache
*group
,
1174 struct inode
*inode
;
1175 struct btrfs_inode_item
*inode_item
;
1176 struct btrfs_key
*location
;
1177 struct btrfs_path
*path
;
1181 path
= btrfs_alloc_path();
1184 inode
= new_inode(root
->fs_info
->sb
);
1186 return ERR_PTR(-ENOMEM
);
1188 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1189 inode
->i_mapping
, GFP_NOFS
);
1190 BTRFS_I(inode
)->root
= root
;
1196 group
= btrfs_find_block_group(root
, group
, 0, 0, owner
);
1197 BTRFS_I(inode
)->block_group
= group
;
1199 ret
= btrfs_insert_empty_inode(trans
, root
, path
, objectid
);
1203 inode
->i_uid
= current
->fsuid
;
1204 inode
->i_gid
= current
->fsgid
;
1205 inode
->i_mode
= mode
;
1206 inode
->i_ino
= objectid
;
1207 inode
->i_blocks
= 0;
1208 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1209 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1210 struct btrfs_inode_item
);
1211 fill_inode_item(path
->nodes
[0], inode_item
, inode
);
1212 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1213 btrfs_free_path(path
);
1215 location
= &BTRFS_I(inode
)->location
;
1216 location
->objectid
= objectid
;
1217 location
->offset
= 0;
1218 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1220 insert_inode_hash(inode
);
1223 btrfs_free_path(path
);
1224 return ERR_PTR(ret
);
1227 static inline u8
btrfs_inode_type(struct inode
*inode
)
1229 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
1232 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
1233 struct dentry
*dentry
, struct inode
*inode
)
1236 struct btrfs_key key
;
1237 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
1238 struct inode
*parent_inode
;
1240 key
.objectid
= inode
->i_ino
;
1241 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1244 ret
= btrfs_insert_dir_item(trans
, root
,
1245 dentry
->d_name
.name
, dentry
->d_name
.len
,
1246 dentry
->d_parent
->d_inode
->i_ino
,
1247 &key
, btrfs_inode_type(inode
));
1249 parent_inode
= dentry
->d_parent
->d_inode
;
1250 parent_inode
->i_size
+= dentry
->d_name
.len
* 2;
1251 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
1252 ret
= btrfs_update_inode(trans
, root
,
1253 dentry
->d_parent
->d_inode
);
1258 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
1259 struct dentry
*dentry
, struct inode
*inode
)
1261 int err
= btrfs_add_link(trans
, dentry
, inode
);
1263 d_instantiate(dentry
, inode
);
1271 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1272 int mode
, dev_t rdev
)
1274 struct btrfs_trans_handle
*trans
;
1275 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1276 struct inode
*inode
;
1282 if (!new_valid_dev(rdev
))
1285 mutex_lock(&root
->fs_info
->fs_mutex
);
1286 trans
= btrfs_start_transaction(root
, 1);
1287 btrfs_set_trans_block_group(trans
, dir
);
1289 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1295 inode
= btrfs_new_inode(trans
, root
, objectid
,
1296 BTRFS_I(dir
)->block_group
, mode
);
1297 err
= PTR_ERR(inode
);
1301 btrfs_set_trans_block_group(trans
, inode
);
1302 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1306 inode
->i_op
= &btrfs_special_inode_operations
;
1307 init_special_inode(inode
, inode
->i_mode
, rdev
);
1308 btrfs_update_inode(trans
, root
, inode
);
1310 dir
->i_sb
->s_dirt
= 1;
1311 btrfs_update_inode_block_group(trans
, inode
);
1312 btrfs_update_inode_block_group(trans
, dir
);
1314 nr
= trans
->blocks_used
;
1315 btrfs_end_transaction(trans
, root
);
1316 mutex_unlock(&root
->fs_info
->fs_mutex
);
1319 inode_dec_link_count(inode
);
1322 btrfs_btree_balance_dirty(root
, nr
);
1326 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
1327 int mode
, struct nameidata
*nd
)
1329 struct btrfs_trans_handle
*trans
;
1330 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1331 struct inode
*inode
;
1337 mutex_lock(&root
->fs_info
->fs_mutex
);
1338 trans
= btrfs_start_transaction(root
, 1);
1339 btrfs_set_trans_block_group(trans
, dir
);
1341 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1347 inode
= btrfs_new_inode(trans
, root
, objectid
,
1348 BTRFS_I(dir
)->block_group
, mode
);
1349 err
= PTR_ERR(inode
);
1353 btrfs_set_trans_block_group(trans
, inode
);
1354 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1358 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1359 inode
->i_fop
= &btrfs_file_operations
;
1360 inode
->i_op
= &btrfs_file_inode_operations
;
1361 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1362 inode
->i_mapping
, GFP_NOFS
);
1363 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
1365 dir
->i_sb
->s_dirt
= 1;
1366 btrfs_update_inode_block_group(trans
, inode
);
1367 btrfs_update_inode_block_group(trans
, dir
);
1369 nr
= trans
->blocks_used
;
1370 btrfs_end_transaction(trans
, root
);
1371 mutex_unlock(&root
->fs_info
->fs_mutex
);
1374 inode_dec_link_count(inode
);
1377 btrfs_btree_balance_dirty(root
, nr
);
1381 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
1382 struct dentry
*dentry
)
1384 struct btrfs_trans_handle
*trans
;
1385 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1386 struct inode
*inode
= old_dentry
->d_inode
;
1391 if (inode
->i_nlink
== 0)
1395 mutex_lock(&root
->fs_info
->fs_mutex
);
1396 trans
= btrfs_start_transaction(root
, 1);
1398 btrfs_set_trans_block_group(trans
, dir
);
1399 atomic_inc(&inode
->i_count
);
1400 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1405 dir
->i_sb
->s_dirt
= 1;
1406 btrfs_update_inode_block_group(trans
, dir
);
1407 err
= btrfs_update_inode(trans
, root
, inode
);
1412 nr
= trans
->blocks_used
;
1413 btrfs_end_transaction(trans
, root
);
1414 mutex_unlock(&root
->fs_info
->fs_mutex
);
1417 inode_dec_link_count(inode
);
1420 btrfs_btree_balance_dirty(root
, nr
);
1424 static int btrfs_make_empty_dir(struct btrfs_trans_handle
*trans
,
1425 struct btrfs_root
*root
,
1426 u64 objectid
, u64 dirid
)
1430 struct btrfs_key key
;
1435 key
.objectid
= objectid
;
1437 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1439 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 1, objectid
,
1440 &key
, BTRFS_FT_DIR
);
1444 key
.objectid
= dirid
;
1445 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 2, objectid
,
1446 &key
, BTRFS_FT_DIR
);
1453 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1455 struct inode
*inode
;
1456 struct btrfs_trans_handle
*trans
;
1457 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1459 int drop_on_err
= 0;
1461 unsigned long nr
= 1;
1463 mutex_lock(&root
->fs_info
->fs_mutex
);
1464 trans
= btrfs_start_transaction(root
, 1);
1465 btrfs_set_trans_block_group(trans
, dir
);
1467 if (IS_ERR(trans
)) {
1468 err
= PTR_ERR(trans
);
1472 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1478 inode
= btrfs_new_inode(trans
, root
, objectid
,
1479 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
1480 if (IS_ERR(inode
)) {
1481 err
= PTR_ERR(inode
);
1486 inode
->i_op
= &btrfs_dir_inode_operations
;
1487 inode
->i_fop
= &btrfs_dir_file_operations
;
1488 btrfs_set_trans_block_group(trans
, inode
);
1490 err
= btrfs_make_empty_dir(trans
, root
, inode
->i_ino
, dir
->i_ino
);
1495 err
= btrfs_update_inode(trans
, root
, inode
);
1499 err
= btrfs_add_link(trans
, dentry
, inode
);
1503 d_instantiate(dentry
, inode
);
1505 dir
->i_sb
->s_dirt
= 1;
1506 btrfs_update_inode_block_group(trans
, inode
);
1507 btrfs_update_inode_block_group(trans
, dir
);
1510 nr
= trans
->blocks_used
;
1511 btrfs_end_transaction(trans
, root
);
1514 mutex_unlock(&root
->fs_info
->fs_mutex
);
1517 btrfs_btree_balance_dirty(root
, nr
);
1521 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
1522 size_t page_offset
, u64 start
, u64 end
,
1528 u64 extent_start
= 0;
1530 u64 objectid
= inode
->i_ino
;
1532 int failed_insert
= 0;
1533 struct btrfs_path
*path
;
1534 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1535 struct btrfs_file_extent_item
*item
;
1536 struct extent_buffer
*leaf
;
1537 struct btrfs_key found_key
;
1538 struct extent_map
*em
= NULL
;
1539 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1540 struct btrfs_trans_handle
*trans
= NULL
;
1542 path
= btrfs_alloc_path();
1544 mutex_lock(&root
->fs_info
->fs_mutex
);
1547 em
= lookup_extent_mapping(em_tree
, start
, end
);
1552 em
= alloc_extent_map(GFP_NOFS
);
1557 em
->start
= EXTENT_MAP_HOLE
;
1558 em
->end
= EXTENT_MAP_HOLE
;
1560 em
->bdev
= inode
->i_sb
->s_bdev
;
1561 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
1562 objectid
, start
, trans
!= NULL
);
1569 if (path
->slots
[0] == 0)
1574 leaf
= path
->nodes
[0];
1575 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1576 struct btrfs_file_extent_item
);
1577 /* are we inside the extent that was found? */
1578 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1579 found_type
= btrfs_key_type(&found_key
);
1580 if (found_key
.objectid
!= objectid
||
1581 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
1585 found_type
= btrfs_file_extent_type(leaf
, item
);
1586 extent_start
= found_key
.offset
;
1587 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1588 extent_end
= extent_start
+
1589 btrfs_file_extent_num_bytes(leaf
, item
);
1591 if (start
< extent_start
|| start
>= extent_end
) {
1593 if (start
< extent_start
) {
1594 if (end
< extent_start
)
1596 em
->end
= extent_end
- 1;
1602 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
1604 em
->start
= extent_start
;
1605 em
->end
= extent_end
- 1;
1606 em
->block_start
= EXTENT_MAP_HOLE
;
1607 em
->block_end
= EXTENT_MAP_HOLE
;
1610 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
1611 em
->block_start
= bytenr
;
1612 em
->block_end
= em
->block_start
+
1613 btrfs_file_extent_num_bytes(leaf
, item
) - 1;
1614 em
->start
= extent_start
;
1615 em
->end
= extent_end
- 1;
1617 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1621 size_t extent_offset
;
1624 size
= btrfs_file_extent_inline_len(leaf
, btrfs_item_nr(leaf
,
1626 extent_end
= (extent_start
+ size
- 1) |
1627 ((u64
)root
->sectorsize
- 1);
1628 if (start
< extent_start
|| start
>= extent_end
) {
1630 if (start
< extent_start
) {
1631 if (end
< extent_start
)
1633 em
->end
= extent_end
;
1639 em
->block_start
= EXTENT_MAP_INLINE
;
1640 em
->block_end
= EXTENT_MAP_INLINE
;
1643 em
->start
= extent_start
;
1644 em
->end
= extent_start
+ size
- 1;
1648 extent_offset
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) -
1649 extent_start
+ page_offset
;
1650 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- page_offset
,
1651 size
- extent_offset
);
1652 em
->start
= extent_start
+ extent_offset
;
1653 em
->end
= (em
->start
+ copy_size
-1) |
1654 ((u64
)root
->sectorsize
-1);
1656 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
1657 if (create
== 0 && !PageUptodate(page
)) {
1658 read_extent_buffer(leaf
, map
+ page_offset
, ptr
,
1660 flush_dcache_page(page
);
1661 } else if (create
&& PageUptodate(page
)) {
1664 free_extent_map(em
);
1666 btrfs_release_path(root
, path
);
1667 trans
= btrfs_start_transaction(root
, 1);
1670 write_extent_buffer(leaf
, map
+ page_offset
, ptr
,
1672 btrfs_mark_buffer_dirty(leaf
);
1675 set_extent_uptodate(em_tree
, em
->start
, em
->end
, GFP_NOFS
);
1678 printk("unkknown found_type %d\n", found_type
);
1685 em
->block_start
= EXTENT_MAP_HOLE
;
1686 em
->block_end
= EXTENT_MAP_HOLE
;
1688 btrfs_release_path(root
, path
);
1689 if (em
->start
> start
|| em
->end
< start
) {
1690 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->end
, start
, end
);
1694 ret
= add_extent_mapping(em_tree
, em
);
1695 if (ret
== -EEXIST
) {
1696 free_extent_map(em
);
1699 if (failed_insert
> 5) {
1700 printk("failing to insert %Lu %Lu\n", start
, end
);
1708 btrfs_free_path(path
);
1710 ret
= btrfs_end_transaction(trans
, root
);
1714 mutex_unlock(&root
->fs_info
->fs_mutex
);
1716 free_extent_map(em
);
1718 return ERR_PTR(err
);
1723 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
1725 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
1728 static int btrfs_prepare_write(struct file
*file
, struct page
*page
,
1729 unsigned from
, unsigned to
)
1731 return extent_prepare_write(&BTRFS_I(page
->mapping
->host
)->extent_tree
,
1732 page
->mapping
->host
, page
, from
, to
,
1736 int btrfs_readpage(struct file
*file
, struct page
*page
)
1738 struct extent_map_tree
*tree
;
1739 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1740 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
1742 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
1744 struct extent_map_tree
*tree
;
1747 if (current
->flags
& PF_MEMALLOC
) {
1748 redirty_page_for_writepage(wbc
, page
);
1752 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1753 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
1756 static int btrfs_writepages(struct address_space
*mapping
,
1757 struct writeback_control
*wbc
)
1759 struct extent_map_tree
*tree
;
1760 tree
= &BTRFS_I(mapping
->host
)->extent_tree
;
1761 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
1765 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
1766 struct list_head
*pages
, unsigned nr_pages
)
1768 struct extent_map_tree
*tree
;
1769 tree
= &BTRFS_I(mapping
->host
)->extent_tree
;
1770 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
1774 static int btrfs_releasepage(struct page
*page
, gfp_t unused_gfp_flags
)
1776 struct extent_map_tree
*tree
;
1779 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1780 ret
= try_release_extent_mapping(tree
, page
);
1782 ClearPagePrivate(page
);
1783 set_page_private(page
, 0);
1784 page_cache_release(page
);
1789 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
1791 struct extent_map_tree
*tree
;
1793 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1794 extent_invalidatepage(tree
, page
, offset
);
1795 btrfs_releasepage(page
, GFP_NOFS
);
1799 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1800 * called from a page fault handler when a page is first dirtied. Hence we must
1801 * be careful to check for EOF conditions here. We set the page up correctly
1802 * for a written page which means we get ENOSPC checking when writing into
1803 * holes and correct delalloc and unwritten extent mapping on filesystems that
1804 * support these features.
1806 * We are not allowed to take the i_mutex here so we have to play games to
1807 * protect against truncate races as the page could now be beyond EOF. Because
1808 * vmtruncate() writes the inode size before removing pages, once we have the
1809 * page lock we can determine safely if the page is beyond EOF. If it is not
1810 * beyond EOF, then the page is guaranteed safe against truncation until we
1813 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
1815 struct inode
*inode
= vma
->vm_file
->f_path
.dentry
->d_inode
;
1821 down_read(&BTRFS_I(inode
)->root
->snap_sem
);
1823 wait_on_page_writeback(page
);
1824 size
= i_size_read(inode
);
1825 page_start
= (u64
)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
);
1842 up_read(&BTRFS_I(inode
)->root
->snap_sem
);
1847 static void btrfs_truncate(struct inode
*inode
)
1849 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1851 struct btrfs_trans_handle
*trans
;
1854 if (!S_ISREG(inode
->i_mode
))
1856 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1859 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1861 mutex_lock(&root
->fs_info
->fs_mutex
);
1862 trans
= btrfs_start_transaction(root
, 1);
1863 btrfs_set_trans_block_group(trans
, inode
);
1865 /* FIXME, add redo link to tree so we don't leak on crash */
1866 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
1867 btrfs_update_inode(trans
, root
, inode
);
1868 nr
= trans
->blocks_used
;
1870 ret
= btrfs_end_transaction(trans
, root
);
1872 mutex_unlock(&root
->fs_info
->fs_mutex
);
1873 btrfs_btree_balance_dirty(root
, nr
);
1876 int btrfs_commit_write(struct file
*file
, struct page
*page
,
1877 unsigned from
, unsigned to
)
1879 return extent_commit_write(&BTRFS_I(page
->mapping
->host
)->extent_tree
,
1880 page
->mapping
->host
, page
, from
, to
);
1883 static int create_subvol(struct btrfs_root
*root
, char *name
, int namelen
)
1885 struct btrfs_trans_handle
*trans
;
1886 struct btrfs_key key
;
1887 struct btrfs_root_item root_item
;
1888 struct btrfs_inode_item
*inode_item
;
1889 struct extent_buffer
*leaf
;
1890 struct btrfs_root
*new_root
;
1891 struct inode
*inode
;
1896 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
1897 unsigned long nr
= 1;
1899 mutex_lock(&root
->fs_info
->fs_mutex
);
1900 trans
= btrfs_start_transaction(root
, 1);
1903 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
, 0, 0);
1905 return PTR_ERR(leaf
);
1907 btrfs_set_header_nritems(leaf
, 0);
1908 btrfs_set_header_level(leaf
, 0);
1909 btrfs_set_header_bytenr(leaf
, leaf
->start
);
1910 btrfs_set_header_generation(leaf
, trans
->transid
);
1911 btrfs_set_header_owner(leaf
, root
->root_key
.objectid
);
1912 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
1913 (unsigned long)btrfs_header_fsid(leaf
),
1915 btrfs_mark_buffer_dirty(leaf
);
1917 inode_item
= &root_item
.inode
;
1918 memset(inode_item
, 0, sizeof(*inode_item
));
1919 inode_item
->generation
= cpu_to_le64(1);
1920 inode_item
->size
= cpu_to_le64(3);
1921 inode_item
->nlink
= cpu_to_le32(1);
1922 inode_item
->nblocks
= cpu_to_le64(1);
1923 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
1925 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
1926 btrfs_set_root_level(&root_item
, 0);
1927 btrfs_set_root_refs(&root_item
, 1);
1928 btrfs_set_root_used(&root_item
, 0);
1930 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
1931 root_item
.drop_level
= 0;
1933 free_extent_buffer(leaf
);
1936 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
1941 btrfs_set_root_dirid(&root_item
, new_dirid
);
1943 key
.objectid
= objectid
;
1945 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
1946 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
1952 * insert the directory item
1954 key
.offset
= (u64
)-1;
1955 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
1956 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
1957 name
, namelen
, dir
->i_ino
, &key
,
1962 ret
= btrfs_commit_transaction(trans
, root
);
1966 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
, name
, namelen
);
1969 trans
= btrfs_start_transaction(new_root
, 1);
1972 inode
= btrfs_new_inode(trans
, new_root
, new_dirid
,
1973 BTRFS_I(dir
)->block_group
, S_IFDIR
| 0700);
1976 inode
->i_op
= &btrfs_dir_inode_operations
;
1977 inode
->i_fop
= &btrfs_dir_file_operations
;
1978 new_root
->inode
= inode
;
1980 ret
= btrfs_make_empty_dir(trans
, new_root
, new_dirid
, new_dirid
);
1986 ret
= btrfs_update_inode(trans
, new_root
, inode
);
1990 nr
= trans
->blocks_used
;
1991 err
= btrfs_commit_transaction(trans
, root
);
1995 mutex_unlock(&root
->fs_info
->fs_mutex
);
1996 btrfs_btree_balance_dirty(root
, nr
);
2000 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
2002 struct btrfs_trans_handle
*trans
;
2003 struct btrfs_key key
;
2004 struct btrfs_root_item new_root_item
;
2005 struct extent_buffer
*tmp
;
2011 if (!root
->ref_cows
)
2014 down_write(&root
->snap_sem
);
2015 freeze_bdev(root
->fs_info
->sb
->s_bdev
);
2016 thaw_bdev(root
->fs_info
->sb
->s_bdev
, root
->fs_info
->sb
);
2018 mutex_lock(&root
->fs_info
->fs_mutex
);
2019 trans
= btrfs_start_transaction(root
, 1);
2022 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
2026 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2031 memcpy(&new_root_item
, &root
->root_item
,
2032 sizeof(new_root_item
));
2034 key
.objectid
= objectid
;
2036 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2037 extent_buffer_get(root
->node
);
2038 btrfs_cow_block(trans
, root
, root
->node
, NULL
, 0, &tmp
);
2039 free_extent_buffer(tmp
);
2040 btrfs_set_root_bytenr(&new_root_item
, root
->node
->start
);
2041 btrfs_set_root_level(&new_root_item
, btrfs_header_level(root
->node
));
2042 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2048 * insert the directory item
2050 key
.offset
= (u64
)-1;
2051 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2053 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2054 &key
, BTRFS_FT_DIR
);
2059 ret
= btrfs_inc_root_ref(trans
, root
);
2063 nr
= trans
->blocks_used
;
2064 err
= btrfs_commit_transaction(trans
, root
);
2069 mutex_unlock(&root
->fs_info
->fs_mutex
);
2070 up_write(&root
->snap_sem
);
2071 btrfs_btree_balance_dirty(root
, nr
);
2075 static unsigned long force_ra(struct address_space
*mapping
,
2076 struct file_ra_state
*ra
, struct file
*file
,
2077 pgoff_t offset
, pgoff_t last_index
)
2081 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2082 req_size
= last_index
- offset
+ 1;
2083 offset
= page_cache_readahead(mapping
, ra
, file
, offset
, req_size
);
2086 req_size
= min(last_index
- offset
+ 1, (pgoff_t
)128);
2087 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
2088 return offset
+ req_size
;
2092 int btrfs_defrag_file(struct file
*file
) {
2093 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
2094 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2096 unsigned long last_index
;
2097 unsigned long ra_index
= 0;
2102 mutex_lock(&inode
->i_mutex
);
2103 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
2104 for (i
= 0; i
<= last_index
; i
++) {
2105 if (i
== ra_index
) {
2106 ra_index
= force_ra(inode
->i_mapping
, &file
->f_ra
,
2107 file
, ra_index
, last_index
);
2109 page
= grab_cache_page(inode
->i_mapping
, i
);
2112 if (!PageUptodate(page
)) {
2113 btrfs_readpage(NULL
, page
);
2115 if (!PageUptodate(page
)) {
2117 page_cache_release(page
);
2121 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2122 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2124 lock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
2125 set_extent_delalloc(em_tree
, page_start
,
2126 page_end
, GFP_NOFS
);
2127 unlock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
2128 set_page_dirty(page
);
2130 page_cache_release(page
);
2131 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
2135 mutex_unlock(&inode
->i_mutex
);
2139 static int btrfs_ioctl_snap_create(struct btrfs_root
*root
, void __user
*arg
)
2141 struct btrfs_ioctl_vol_args vol_args
;
2142 struct btrfs_dir_item
*di
;
2143 struct btrfs_path
*path
;
2147 if (copy_from_user(&vol_args
, arg
, sizeof(vol_args
)))
2150 namelen
= strlen(vol_args
.name
);
2151 if (namelen
> BTRFS_VOL_NAME_MAX
)
2153 if (strchr(vol_args
.name
, '/'))
2156 path
= btrfs_alloc_path();
2160 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2161 mutex_lock(&root
->fs_info
->fs_mutex
);
2162 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
2164 vol_args
.name
, namelen
, 0);
2165 mutex_unlock(&root
->fs_info
->fs_mutex
);
2166 btrfs_free_path(path
);
2167 if (di
&& !IS_ERR(di
))
2172 if (root
== root
->fs_info
->tree_root
)
2173 return create_subvol(root
, vol_args
.name
, namelen
);
2174 return create_snapshot(root
, vol_args
.name
, namelen
);
2177 static int btrfs_ioctl_defrag(struct file
*file
)
2179 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
2180 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2182 switch (inode
->i_mode
& S_IFMT
) {
2184 mutex_lock(&root
->fs_info
->fs_mutex
);
2185 btrfs_defrag_root(root
, 0);
2186 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
2187 mutex_unlock(&root
->fs_info
->fs_mutex
);
2190 btrfs_defrag_file(file
);
2197 long btrfs_ioctl(struct file
*file
, unsigned int
2198 cmd
, unsigned long arg
)
2200 struct btrfs_root
*root
= BTRFS_I(file
->f_path
.dentry
->d_inode
)->root
;
2203 case BTRFS_IOC_SNAP_CREATE
:
2204 return btrfs_ioctl_snap_create(root
, (void __user
*)arg
);
2205 case BTRFS_IOC_DEFRAG
:
2206 return btrfs_ioctl_defrag(file
);
2213 * Called inside transaction, so use GFP_NOFS
2215 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2217 struct btrfs_inode
*ei
;
2219 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2223 return &ei
->vfs_inode
;
2226 void btrfs_destroy_inode(struct inode
*inode
)
2228 WARN_ON(!list_empty(&inode
->i_dentry
));
2229 WARN_ON(inode
->i_data
.nrpages
);
2231 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2234 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2235 static void init_once(struct kmem_cache
* cachep
, void *foo
)
2237 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2238 unsigned long flags
)
2241 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2243 inode_init_once(&ei
->vfs_inode
);
2246 void btrfs_destroy_cachep(void)
2248 if (btrfs_inode_cachep
)
2249 kmem_cache_destroy(btrfs_inode_cachep
);
2250 if (btrfs_trans_handle_cachep
)
2251 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2252 if (btrfs_transaction_cachep
)
2253 kmem_cache_destroy(btrfs_transaction_cachep
);
2254 if (btrfs_bit_radix_cachep
)
2255 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2256 if (btrfs_path_cachep
)
2257 kmem_cache_destroy(btrfs_path_cachep
);
2260 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
2261 unsigned long extra_flags
,
2262 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2263 void (*ctor
)(struct kmem_cache
*, void *)
2265 void (*ctor
)(void *, struct kmem_cache
*,
2270 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
2271 SLAB_MEM_SPREAD
| extra_flags
), ctor
2272 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2278 int btrfs_init_cachep(void)
2280 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
2281 sizeof(struct btrfs_inode
),
2283 if (!btrfs_inode_cachep
)
2285 btrfs_trans_handle_cachep
=
2286 btrfs_cache_create("btrfs_trans_handle_cache",
2287 sizeof(struct btrfs_trans_handle
),
2289 if (!btrfs_trans_handle_cachep
)
2291 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
2292 sizeof(struct btrfs_transaction
),
2294 if (!btrfs_transaction_cachep
)
2296 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
2297 sizeof(struct btrfs_path
),
2299 if (!btrfs_path_cachep
)
2301 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
2302 SLAB_DESTROY_BY_RCU
, NULL
);
2303 if (!btrfs_bit_radix_cachep
)
2307 btrfs_destroy_cachep();
2311 static int btrfs_getattr(struct vfsmount
*mnt
,
2312 struct dentry
*dentry
, struct kstat
*stat
)
2314 struct inode
*inode
= dentry
->d_inode
;
2315 generic_fillattr(inode
, stat
);
2316 stat
->blksize
= 256 * 1024;
2320 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
2321 struct inode
* new_dir
,struct dentry
*new_dentry
)
2323 struct btrfs_trans_handle
*trans
;
2324 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
2325 struct inode
*new_inode
= new_dentry
->d_inode
;
2326 struct inode
*old_inode
= old_dentry
->d_inode
;
2327 struct timespec ctime
= CURRENT_TIME
;
2328 struct btrfs_path
*path
;
2329 struct btrfs_dir_item
*di
;
2332 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
2333 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
2337 mutex_lock(&root
->fs_info
->fs_mutex
);
2338 trans
= btrfs_start_transaction(root
, 1);
2340 btrfs_set_trans_block_group(trans
, new_dir
);
2341 path
= btrfs_alloc_path();
2347 old_dentry
->d_inode
->i_nlink
++;
2348 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
2349 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
2350 old_inode
->i_ctime
= ctime
;
2352 if (S_ISDIR(old_inode
->i_mode
) && old_dir
!= new_dir
) {
2353 struct btrfs_key
*location
= &BTRFS_I(new_dir
)->location
;
2354 struct btrfs_key old_parent_key
;
2355 di
= btrfs_lookup_dir_item(trans
, root
, path
, old_inode
->i_ino
,
2365 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &old_parent_key
);
2366 ret
= btrfs_del_item(trans
, root
, path
);
2370 btrfs_release_path(root
, path
);
2372 di
= btrfs_lookup_dir_index_item(trans
, root
, path
,
2374 old_parent_key
.objectid
,
2384 ret
= btrfs_del_item(trans
, root
, path
);
2388 btrfs_release_path(root
, path
);
2390 ret
= btrfs_insert_dir_item(trans
, root
, "..", 2,
2391 old_inode
->i_ino
, location
,
2398 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
2403 new_inode
->i_ctime
= CURRENT_TIME
;
2404 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
2408 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
);
2413 btrfs_free_path(path
);
2414 btrfs_end_transaction(trans
, root
);
2415 mutex_unlock(&root
->fs_info
->fs_mutex
);
2419 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
2420 const char *symname
)
2422 struct btrfs_trans_handle
*trans
;
2423 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2424 struct btrfs_path
*path
;
2425 struct btrfs_key key
;
2426 struct inode
*inode
;
2433 struct btrfs_file_extent_item
*ei
;
2434 struct extent_buffer
*leaf
;
2437 name_len
= strlen(symname
) + 1;
2438 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
2439 return -ENAMETOOLONG
;
2440 mutex_lock(&root
->fs_info
->fs_mutex
);
2441 trans
= btrfs_start_transaction(root
, 1);
2442 btrfs_set_trans_block_group(trans
, dir
);
2444 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2450 inode
= btrfs_new_inode(trans
, root
, objectid
,
2451 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
2452 err
= PTR_ERR(inode
);
2456 btrfs_set_trans_block_group(trans
, inode
);
2457 err
= btrfs_add_nondir(trans
, dentry
, inode
);
2461 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2462 inode
->i_fop
= &btrfs_file_operations
;
2463 inode
->i_op
= &btrfs_file_inode_operations
;
2464 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
2465 inode
->i_mapping
, GFP_NOFS
);
2466 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
2468 dir
->i_sb
->s_dirt
= 1;
2469 btrfs_update_inode_block_group(trans
, inode
);
2470 btrfs_update_inode_block_group(trans
, dir
);
2474 path
= btrfs_alloc_path();
2476 key
.objectid
= inode
->i_ino
;
2478 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
2479 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
2480 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2486 leaf
= path
->nodes
[0];
2487 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
2488 struct btrfs_file_extent_item
);
2489 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
2490 btrfs_set_file_extent_type(leaf
, ei
,
2491 BTRFS_FILE_EXTENT_INLINE
);
2492 ptr
= btrfs_file_extent_inline_start(ei
);
2493 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
2494 btrfs_mark_buffer_dirty(leaf
);
2495 btrfs_free_path(path
);
2497 inode
->i_op
= &btrfs_symlink_inode_operations
;
2498 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2499 inode
->i_size
= name_len
- 1;
2500 err
= btrfs_update_inode(trans
, root
, inode
);
2505 nr
= trans
->blocks_used
;
2506 btrfs_end_transaction(trans
, root
);
2507 mutex_unlock(&root
->fs_info
->fs_mutex
);
2509 inode_dec_link_count(inode
);
2512 btrfs_btree_balance_dirty(root
, nr
);
2516 static struct inode_operations btrfs_dir_inode_operations
= {
2517 .lookup
= btrfs_lookup
,
2518 .create
= btrfs_create
,
2519 .unlink
= btrfs_unlink
,
2521 .mkdir
= btrfs_mkdir
,
2522 .rmdir
= btrfs_rmdir
,
2523 .rename
= btrfs_rename
,
2524 .symlink
= btrfs_symlink
,
2525 .setattr
= btrfs_setattr
,
2526 .mknod
= btrfs_mknod
,
2527 .setxattr
= generic_setxattr
,
2528 .getxattr
= generic_getxattr
,
2529 .listxattr
= btrfs_listxattr
,
2530 .removexattr
= generic_removexattr
,
2533 static struct inode_operations btrfs_dir_ro_inode_operations
= {
2534 .lookup
= btrfs_lookup
,
2537 static struct file_operations btrfs_dir_file_operations
= {
2538 .llseek
= generic_file_llseek
,
2539 .read
= generic_read_dir
,
2540 .readdir
= btrfs_readdir
,
2541 .unlocked_ioctl
= btrfs_ioctl
,
2542 #ifdef CONFIG_COMPAT
2543 .compat_ioctl
= btrfs_ioctl
,
2547 static struct extent_map_ops btrfs_extent_map_ops
= {
2548 .fill_delalloc
= run_delalloc_range
,
2549 .writepage_io_hook
= btrfs_writepage_io_hook
,
2550 .readpage_io_hook
= btrfs_readpage_io_hook
,
2551 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
2554 static struct address_space_operations btrfs_aops
= {
2555 .readpage
= btrfs_readpage
,
2556 .writepage
= btrfs_writepage
,
2557 .writepages
= btrfs_writepages
,
2558 .readpages
= btrfs_readpages
,
2559 .sync_page
= block_sync_page
,
2560 .prepare_write
= btrfs_prepare_write
,
2561 .commit_write
= btrfs_commit_write
,
2563 .invalidatepage
= btrfs_invalidatepage
,
2564 .releasepage
= btrfs_releasepage
,
2565 .set_page_dirty
= __set_page_dirty_nobuffers
,
2568 static struct address_space_operations btrfs_symlink_aops
= {
2569 .readpage
= btrfs_readpage
,
2570 .writepage
= btrfs_writepage
,
2571 .invalidatepage
= btrfs_invalidatepage
,
2572 .releasepage
= btrfs_releasepage
,
2575 static struct inode_operations btrfs_file_inode_operations
= {
2576 .truncate
= btrfs_truncate
,
2577 .getattr
= btrfs_getattr
,
2578 .setattr
= btrfs_setattr
,
2579 .setxattr
= generic_setxattr
,
2580 .getxattr
= generic_getxattr
,
2581 .listxattr
= btrfs_listxattr
,
2582 .removexattr
= generic_removexattr
,
2585 static struct inode_operations btrfs_special_inode_operations
= {
2586 .getattr
= btrfs_getattr
,
2587 .setattr
= btrfs_setattr
,
2590 static struct inode_operations btrfs_symlink_inode_operations
= {
2591 .readlink
= generic_readlink
,
2592 .follow_link
= page_follow_link_light
,
2593 .put_link
= page_put_link
,