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/bio.h>
20 #include <linux/buffer_head.h>
22 #include <linux/pagemap.h>
23 #include <linux/highmem.h>
24 #include <linux/time.h>
25 #include <linux/init.h>
26 #include <linux/string.h>
27 #include <linux/smp_lock.h>
28 #include <linux/backing-dev.h>
29 #include <linux/mpage.h>
30 #include <linux/swap.h>
31 #include <linux/writeback.h>
32 #include <linux/statfs.h>
33 #include <linux/compat.h>
34 #include <linux/bit_spinlock.h>
35 #include <linux/version.h>
36 #include <linux/xattr.h>
39 #include "transaction.h"
40 #include "btrfs_inode.h"
42 #include "print-tree.h"
44 struct btrfs_iget_args
{
46 struct btrfs_root
*root
;
49 static struct inode_operations btrfs_dir_inode_operations
;
50 static struct inode_operations btrfs_symlink_inode_operations
;
51 static struct inode_operations btrfs_dir_ro_inode_operations
;
52 static struct inode_operations btrfs_special_inode_operations
;
53 static struct inode_operations btrfs_file_inode_operations
;
54 static struct address_space_operations btrfs_aops
;
55 static struct address_space_operations btrfs_symlink_aops
;
56 static struct file_operations btrfs_dir_file_operations
;
57 static struct extent_io_ops btrfs_extent_io_ops
;
59 static struct kmem_cache
*btrfs_inode_cachep
;
60 struct kmem_cache
*btrfs_trans_handle_cachep
;
61 struct kmem_cache
*btrfs_transaction_cachep
;
62 struct kmem_cache
*btrfs_bit_radix_cachep
;
63 struct kmem_cache
*btrfs_path_cachep
;
66 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
67 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
68 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
69 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
70 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
71 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
72 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
73 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
76 int btrfs_check_free_space(struct btrfs_root
*root
, u64 num_required
,
79 u64 total
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
80 u64 used
= btrfs_super_bytes_used(&root
->fs_info
->super_copy
);
91 spin_lock(&root
->fs_info
->delalloc_lock
);
92 if (used
+ root
->fs_info
->delalloc_bytes
+ num_required
> thresh
)
94 spin_unlock(&root
->fs_info
->delalloc_lock
);
98 static int cow_file_range(struct inode
*inode
, u64 start
, u64 end
)
100 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
101 struct btrfs_trans_handle
*trans
;
105 u64 blocksize
= root
->sectorsize
;
106 u64 orig_start
= start
;
108 struct btrfs_key ins
;
111 trans
= btrfs_start_transaction(root
, 1);
113 btrfs_set_trans_block_group(trans
, inode
);
115 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
116 num_bytes
= max(blocksize
, num_bytes
);
117 ret
= btrfs_drop_extents(trans
, root
, inode
,
118 start
, start
+ num_bytes
, start
, &alloc_hint
);
119 orig_num_bytes
= num_bytes
;
121 if (alloc_hint
== EXTENT_MAP_INLINE
)
124 while(num_bytes
> 0) {
125 cur_alloc_size
= min(num_bytes
, root
->fs_info
->max_extent
);
126 ret
= btrfs_alloc_extent(trans
, root
, cur_alloc_size
,
127 root
->root_key
.objectid
,
129 inode
->i_ino
, start
, 0,
130 alloc_hint
, (u64
)-1, &ins
, 1);
135 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
136 start
, ins
.objectid
, ins
.offset
,
138 inode
->i_blocks
+= ins
.offset
>> 9;
139 btrfs_check_file(root
, inode
);
140 num_bytes
-= cur_alloc_size
;
141 alloc_hint
= ins
.objectid
+ ins
.offset
;
142 start
+= cur_alloc_size
;
144 btrfs_drop_extent_cache(inode
, orig_start
,
145 orig_start
+ orig_num_bytes
- 1);
146 btrfs_add_ordered_inode(inode
);
147 btrfs_update_inode(trans
, root
, inode
);
149 btrfs_end_transaction(trans
, root
);
153 static int run_delalloc_nocow(struct inode
*inode
, u64 start
, u64 end
)
161 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
162 struct extent_buffer
*leaf
;
164 struct btrfs_path
*path
;
165 struct btrfs_file_extent_item
*item
;
168 struct btrfs_key found_key
;
170 total_fs_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
171 path
= btrfs_alloc_path();
174 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
175 inode
->i_ino
, start
, 0);
177 btrfs_free_path(path
);
183 if (path
->slots
[0] == 0)
188 leaf
= path
->nodes
[0];
189 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
190 struct btrfs_file_extent_item
);
192 /* are we inside the extent that was found? */
193 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
194 found_type
= btrfs_key_type(&found_key
);
195 if (found_key
.objectid
!= inode
->i_ino
||
196 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
200 found_type
= btrfs_file_extent_type(leaf
, item
);
201 extent_start
= found_key
.offset
;
202 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
203 u64 extent_num_bytes
;
205 extent_num_bytes
= btrfs_file_extent_num_bytes(leaf
, item
);
206 extent_end
= extent_start
+ extent_num_bytes
;
209 if (loops
&& start
!= extent_start
)
212 if (start
< extent_start
|| start
>= extent_end
)
215 cow_end
= min(end
, extent_end
- 1);
216 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
221 * we may be called by the resizer, make sure we're inside
222 * the limits of the FS
224 if (bytenr
+ extent_num_bytes
> total_fs_bytes
)
227 if (btrfs_count_snapshots_in_path(root
, path
, bytenr
) != 1) {
237 btrfs_free_path(path
);
240 btrfs_release_path(root
, path
);
245 cow_file_range(inode
, start
, cow_end
);
250 static int run_delalloc_range(struct inode
*inode
, u64 start
, u64 end
)
252 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
254 mutex_lock(&root
->fs_info
->fs_mutex
);
255 if (btrfs_test_opt(root
, NODATACOW
) ||
256 btrfs_test_flag(inode
, NODATACOW
))
257 ret
= run_delalloc_nocow(inode
, start
, end
);
259 ret
= cow_file_range(inode
, start
, end
);
261 mutex_unlock(&root
->fs_info
->fs_mutex
);
265 int btrfs_set_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
266 unsigned long old
, unsigned long bits
)
268 if (!(old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
269 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
270 spin_lock(&root
->fs_info
->delalloc_lock
);
271 BTRFS_I(inode
)->delalloc_bytes
+= end
- start
+ 1;
272 root
->fs_info
->delalloc_bytes
+= end
- start
+ 1;
273 spin_unlock(&root
->fs_info
->delalloc_lock
);
278 int btrfs_clear_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
279 unsigned long old
, unsigned long bits
)
281 if ((old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
282 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
283 spin_lock(&root
->fs_info
->delalloc_lock
);
284 if (end
- start
+ 1 > root
->fs_info
->delalloc_bytes
) {
285 printk("warning: delalloc account %Lu %Lu\n",
286 end
- start
+ 1, root
->fs_info
->delalloc_bytes
);
287 root
->fs_info
->delalloc_bytes
= 0;
288 BTRFS_I(inode
)->delalloc_bytes
= 0;
290 root
->fs_info
->delalloc_bytes
-= end
- start
+ 1;
291 BTRFS_I(inode
)->delalloc_bytes
-= end
- start
+ 1;
293 spin_unlock(&root
->fs_info
->delalloc_lock
);
298 int btrfs_submit_bio_hook(int rw
, struct bio
*bio
)
300 struct bio_vec
*bvec
= bio
->bi_io_vec
;
301 struct inode
*inode
= bvec
->bv_page
->mapping
->host
;
302 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
303 struct btrfs_trans_handle
*trans
;
309 if (btrfs_test_opt(root
, NODATASUM
) ||
310 btrfs_test_flag(inode
, NODATASUM
))
313 mutex_lock(&root
->fs_info
->fs_mutex
);
314 trans
= btrfs_start_transaction(root
, 1);
315 btrfs_set_trans_block_group(trans
, inode
);
316 btrfs_csum_file_blocks(trans
, root
, inode
, bio
);
317 ret
= btrfs_end_transaction(trans
, root
);
319 mutex_unlock(&root
->fs_info
->fs_mutex
);
323 int btrfs_readpage_io_hook(struct page
*page
, u64 start
, u64 end
)
326 struct inode
*inode
= page
->mapping
->host
;
327 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
328 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
329 struct btrfs_csum_item
*item
;
330 struct btrfs_path
*path
= NULL
;
332 if (btrfs_test_opt(root
, NODATASUM
) ||
333 btrfs_test_flag(inode
, NODATASUM
))
335 mutex_lock(&root
->fs_info
->fs_mutex
);
336 path
= btrfs_alloc_path();
337 item
= btrfs_lookup_csum(NULL
, root
, path
, inode
->i_ino
, start
, 0);
340 /* a csum that isn't present is a preallocated region. */
341 if (ret
== -ENOENT
|| ret
== -EFBIG
)
344 printk("no csum found for inode %lu start %Lu\n", inode
->i_ino
, start
);
347 read_extent_buffer(path
->nodes
[0], &csum
, (unsigned long)item
,
349 set_state_private(io_tree
, start
, csum
);
352 btrfs_free_path(path
);
353 mutex_unlock(&root
->fs_info
->fs_mutex
);
357 int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
358 struct extent_state
*state
)
360 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
361 struct inode
*inode
= page
->mapping
->host
;
362 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
364 u64
private = ~(u32
)0;
366 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
370 if (btrfs_test_opt(root
, NODATASUM
) ||
371 btrfs_test_flag(inode
, NODATASUM
))
373 if (state
&& state
->start
== start
) {
374 private = state
->private;
377 ret
= get_state_private(io_tree
, start
, &private);
379 local_irq_save(flags
);
380 kaddr
= kmap_atomic(page
, KM_IRQ0
);
384 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
385 btrfs_csum_final(csum
, (char *)&csum
);
386 if (csum
!= private) {
389 kunmap_atomic(kaddr
, KM_IRQ0
);
390 local_irq_restore(flags
);
394 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
395 page
->mapping
->host
->i_ino
, (unsigned long long)start
, csum
,
397 memset(kaddr
+ offset
, 1, end
- start
+ 1);
398 flush_dcache_page(page
);
399 kunmap_atomic(kaddr
, KM_IRQ0
);
400 local_irq_restore(flags
);
404 void btrfs_read_locked_inode(struct inode
*inode
)
406 struct btrfs_path
*path
;
407 struct extent_buffer
*leaf
;
408 struct btrfs_inode_item
*inode_item
;
409 struct btrfs_inode_timespec
*tspec
;
410 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
411 struct btrfs_key location
;
412 u64 alloc_group_block
;
416 path
= btrfs_alloc_path();
418 mutex_lock(&root
->fs_info
->fs_mutex
);
419 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
421 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
425 leaf
= path
->nodes
[0];
426 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
427 struct btrfs_inode_item
);
429 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
430 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
431 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
432 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
433 inode
->i_size
= btrfs_inode_size(leaf
, inode_item
);
435 tspec
= btrfs_inode_atime(inode_item
);
436 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
437 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
439 tspec
= btrfs_inode_mtime(inode_item
);
440 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
441 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
443 tspec
= btrfs_inode_ctime(inode_item
);
444 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
445 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
447 inode
->i_blocks
= btrfs_inode_nblocks(leaf
, inode_item
);
448 inode
->i_generation
= btrfs_inode_generation(leaf
, inode_item
);
450 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
452 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
453 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
455 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
456 if (!BTRFS_I(inode
)->block_group
) {
457 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
,
460 btrfs_free_path(path
);
463 mutex_unlock(&root
->fs_info
->fs_mutex
);
465 switch (inode
->i_mode
& S_IFMT
) {
467 inode
->i_mapping
->a_ops
= &btrfs_aops
;
468 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
469 inode
->i_fop
= &btrfs_file_operations
;
470 inode
->i_op
= &btrfs_file_inode_operations
;
473 inode
->i_fop
= &btrfs_dir_file_operations
;
474 if (root
== root
->fs_info
->tree_root
)
475 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
477 inode
->i_op
= &btrfs_dir_inode_operations
;
480 inode
->i_op
= &btrfs_symlink_inode_operations
;
481 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
484 init_special_inode(inode
, inode
->i_mode
, rdev
);
490 btrfs_release_path(root
, path
);
491 btrfs_free_path(path
);
492 mutex_unlock(&root
->fs_info
->fs_mutex
);
493 make_bad_inode(inode
);
496 static void fill_inode_item(struct extent_buffer
*leaf
,
497 struct btrfs_inode_item
*item
,
500 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
501 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
502 btrfs_set_inode_size(leaf
, item
, inode
->i_size
);
503 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
504 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
506 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
507 inode
->i_atime
.tv_sec
);
508 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
509 inode
->i_atime
.tv_nsec
);
511 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
512 inode
->i_mtime
.tv_sec
);
513 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
514 inode
->i_mtime
.tv_nsec
);
516 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
517 inode
->i_ctime
.tv_sec
);
518 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
519 inode
->i_ctime
.tv_nsec
);
521 btrfs_set_inode_nblocks(leaf
, item
, inode
->i_blocks
);
522 btrfs_set_inode_generation(leaf
, item
, inode
->i_generation
);
523 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
524 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
525 btrfs_set_inode_block_group(leaf
, item
,
526 BTRFS_I(inode
)->block_group
->key
.objectid
);
529 int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
530 struct btrfs_root
*root
,
533 struct btrfs_inode_item
*inode_item
;
534 struct btrfs_path
*path
;
535 struct extent_buffer
*leaf
;
538 path
= btrfs_alloc_path();
540 ret
= btrfs_lookup_inode(trans
, root
, path
,
541 &BTRFS_I(inode
)->location
, 1);
548 leaf
= path
->nodes
[0];
549 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
550 struct btrfs_inode_item
);
552 fill_inode_item(leaf
, inode_item
, inode
);
553 btrfs_mark_buffer_dirty(leaf
);
554 btrfs_set_inode_last_trans(trans
, inode
);
557 btrfs_release_path(root
, path
);
558 btrfs_free_path(path
);
563 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
564 struct btrfs_root
*root
,
566 struct dentry
*dentry
)
568 struct btrfs_path
*path
;
569 const char *name
= dentry
->d_name
.name
;
570 int name_len
= dentry
->d_name
.len
;
572 struct extent_buffer
*leaf
;
573 struct btrfs_dir_item
*di
;
574 struct btrfs_key key
;
576 path
= btrfs_alloc_path();
582 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
592 leaf
= path
->nodes
[0];
593 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
594 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
597 btrfs_release_path(root
, path
);
599 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
600 key
.objectid
, name
, name_len
, -1);
609 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
611 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
612 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
613 dentry
->d_inode
->i_ino
,
614 dentry
->d_parent
->d_inode
->i_ino
);
616 printk("failed to delete reference to %.*s, "
617 "inode %lu parent %lu\n", name_len
, name
,
618 dentry
->d_inode
->i_ino
,
619 dentry
->d_parent
->d_inode
->i_ino
);
622 btrfs_free_path(path
);
624 dir
->i_size
-= name_len
* 2;
625 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
626 btrfs_update_inode(trans
, root
, dir
);
627 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
628 dentry
->d_inode
->i_nlink
--;
630 drop_nlink(dentry
->d_inode
);
632 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
633 dir
->i_sb
->s_dirt
= 1;
638 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
640 struct btrfs_root
*root
;
641 struct btrfs_trans_handle
*trans
;
642 struct inode
*inode
= dentry
->d_inode
;
644 unsigned long nr
= 0;
646 root
= BTRFS_I(dir
)->root
;
647 mutex_lock(&root
->fs_info
->fs_mutex
);
649 ret
= btrfs_check_free_space(root
, 1, 1);
653 trans
= btrfs_start_transaction(root
, 1);
655 btrfs_set_trans_block_group(trans
, dir
);
656 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
657 nr
= trans
->blocks_used
;
659 if (inode
->i_nlink
== 0) {
661 /* if the inode isn't linked anywhere,
662 * we don't need to worry about
665 found
= btrfs_del_ordered_inode(inode
);
667 atomic_dec(&inode
->i_count
);
671 btrfs_end_transaction(trans
, root
);
673 mutex_unlock(&root
->fs_info
->fs_mutex
);
674 btrfs_btree_balance_dirty(root
, nr
);
675 btrfs_throttle(root
);
679 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
681 struct inode
*inode
= dentry
->d_inode
;
684 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
685 struct btrfs_trans_handle
*trans
;
686 unsigned long nr
= 0;
688 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
691 mutex_lock(&root
->fs_info
->fs_mutex
);
692 ret
= btrfs_check_free_space(root
, 1, 1);
696 trans
= btrfs_start_transaction(root
, 1);
697 btrfs_set_trans_block_group(trans
, dir
);
699 /* now the directory is empty */
700 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
705 nr
= trans
->blocks_used
;
706 ret
= btrfs_end_transaction(trans
, root
);
708 mutex_unlock(&root
->fs_info
->fs_mutex
);
709 btrfs_btree_balance_dirty(root
, nr
);
710 btrfs_throttle(root
);
718 * this can truncate away extent items, csum items and directory items.
719 * It starts at a high offset and removes keys until it can't find
720 * any higher than i_size.
722 * csum items that cross the new i_size are truncated to the new size
725 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
726 struct btrfs_root
*root
,
731 struct btrfs_path
*path
;
732 struct btrfs_key key
;
733 struct btrfs_key found_key
;
735 struct extent_buffer
*leaf
;
736 struct btrfs_file_extent_item
*fi
;
737 u64 extent_start
= 0;
738 u64 extent_num_bytes
= 0;
744 int pending_del_nr
= 0;
745 int pending_del_slot
= 0;
746 int extent_type
= -1;
748 btrfs_drop_extent_cache(inode
, inode
->i_size
, (u64
)-1);
749 path
= btrfs_alloc_path();
753 /* FIXME, add redo link to tree so we don't leak on crash */
754 key
.objectid
= inode
->i_ino
;
755 key
.offset
= (u64
)-1;
758 btrfs_init_path(path
);
760 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
765 BUG_ON(path
->slots
[0] == 0);
771 leaf
= path
->nodes
[0];
772 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
773 found_type
= btrfs_key_type(&found_key
);
775 if (found_key
.objectid
!= inode
->i_ino
)
778 if (found_type
< min_type
)
781 item_end
= found_key
.offset
;
782 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
783 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
784 struct btrfs_file_extent_item
);
785 extent_type
= btrfs_file_extent_type(leaf
, fi
);
786 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
788 btrfs_file_extent_num_bytes(leaf
, fi
);
789 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
790 struct btrfs_item
*item
= btrfs_item_nr(leaf
,
792 item_end
+= btrfs_file_extent_inline_len(leaf
,
797 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
798 ret
= btrfs_csum_truncate(trans
, root
, path
,
802 if (item_end
< inode
->i_size
) {
803 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
804 found_type
= BTRFS_INODE_ITEM_KEY
;
805 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
806 found_type
= BTRFS_CSUM_ITEM_KEY
;
807 } else if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
808 found_type
= BTRFS_XATTR_ITEM_KEY
;
809 } else if (found_type
== BTRFS_XATTR_ITEM_KEY
) {
810 found_type
= BTRFS_INODE_REF_KEY
;
811 } else if (found_type
) {
816 btrfs_set_key_type(&key
, found_type
);
819 if (found_key
.offset
>= inode
->i_size
)
825 /* FIXME, shrink the extent if the ref count is only 1 */
826 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
829 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
831 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
834 btrfs_file_extent_num_bytes(leaf
, fi
);
835 extent_num_bytes
= inode
->i_size
-
836 found_key
.offset
+ root
->sectorsize
- 1;
837 extent_num_bytes
= extent_num_bytes
&
838 ~((u64
)root
->sectorsize
- 1);
839 btrfs_set_file_extent_num_bytes(leaf
, fi
,
841 num_dec
= (orig_num_bytes
-
843 if (extent_start
!= 0)
844 dec_i_blocks(inode
, num_dec
);
845 btrfs_mark_buffer_dirty(leaf
);
848 btrfs_file_extent_disk_num_bytes(leaf
,
850 /* FIXME blocksize != 4096 */
851 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
852 if (extent_start
!= 0) {
854 dec_i_blocks(inode
, num_dec
);
856 root_gen
= btrfs_header_generation(leaf
);
857 root_owner
= btrfs_header_owner(leaf
);
859 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
861 u32 newsize
= inode
->i_size
- found_key
.offset
;
862 dec_i_blocks(inode
, item_end
+ 1 -
863 found_key
.offset
- newsize
);
865 btrfs_file_extent_calc_inline_size(newsize
);
866 ret
= btrfs_truncate_item(trans
, root
, path
,
870 dec_i_blocks(inode
, item_end
+ 1 -
876 if (!pending_del_nr
) {
877 /* no pending yet, add ourselves */
878 pending_del_slot
= path
->slots
[0];
880 } else if (pending_del_nr
&&
881 path
->slots
[0] + 1 == pending_del_slot
) {
882 /* hop on the pending chunk */
884 pending_del_slot
= path
->slots
[0];
886 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path
->slots
[0], pending_del_nr
, pending_del_slot
);
892 ret
= btrfs_free_extent(trans
, root
, extent_start
,
895 root_gen
, inode
->i_ino
,
896 found_key
.offset
, 0);
900 if (path
->slots
[0] == 0) {
903 btrfs_release_path(root
, path
);
908 if (pending_del_nr
&&
909 path
->slots
[0] + 1 != pending_del_slot
) {
910 struct btrfs_key debug
;
912 btrfs_item_key_to_cpu(path
->nodes
[0], &debug
,
914 ret
= btrfs_del_items(trans
, root
, path
,
919 btrfs_release_path(root
, path
);
925 if (pending_del_nr
) {
926 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
929 btrfs_release_path(root
, path
);
930 btrfs_free_path(path
);
931 inode
->i_sb
->s_dirt
= 1;
935 static int btrfs_cow_one_page(struct inode
*inode
, struct page
*page
,
939 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
940 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
941 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
944 WARN_ON(!PageLocked(page
));
945 set_page_extent_mapped(page
);
947 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
948 set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, page_start
,
951 if (zero_start
!= PAGE_CACHE_SIZE
) {
953 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
954 flush_dcache_page(page
);
957 set_page_dirty(page
);
958 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
964 * taken from block_truncate_page, but does cow as it zeros out
965 * any bytes left in the last page in the file.
967 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
969 struct inode
*inode
= mapping
->host
;
970 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
971 u32 blocksize
= root
->sectorsize
;
972 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
973 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
978 if ((offset
& (blocksize
- 1)) == 0)
982 page
= grab_cache_page(mapping
, index
);
985 if (!PageUptodate(page
)) {
986 ret
= btrfs_readpage(NULL
, page
);
988 if (!PageUptodate(page
)) {
993 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
995 ret
= btrfs_cow_one_page(inode
, page
, offset
);
998 page_cache_release(page
);
1003 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1005 struct inode
*inode
= dentry
->d_inode
;
1008 err
= inode_change_ok(inode
, attr
);
1012 if (S_ISREG(inode
->i_mode
) &&
1013 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
1014 struct btrfs_trans_handle
*trans
;
1015 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1016 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1018 u64 mask
= root
->sectorsize
- 1;
1019 u64 hole_start
= (inode
->i_size
+ mask
) & ~mask
;
1020 u64 block_end
= (attr
->ia_size
+ mask
) & ~mask
;
1024 if (attr
->ia_size
<= hole_start
)
1027 mutex_lock(&root
->fs_info
->fs_mutex
);
1028 err
= btrfs_check_free_space(root
, 1, 0);
1029 mutex_unlock(&root
->fs_info
->fs_mutex
);
1033 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1035 lock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1036 hole_size
= block_end
- hole_start
;
1038 mutex_lock(&root
->fs_info
->fs_mutex
);
1039 trans
= btrfs_start_transaction(root
, 1);
1040 btrfs_set_trans_block_group(trans
, inode
);
1041 err
= btrfs_drop_extents(trans
, root
, inode
,
1042 hole_start
, block_end
, hole_start
,
1045 if (alloc_hint
!= EXTENT_MAP_INLINE
) {
1046 err
= btrfs_insert_file_extent(trans
, root
,
1050 btrfs_drop_extent_cache(inode
, hole_start
,
1052 btrfs_check_file(root
, inode
);
1054 btrfs_end_transaction(trans
, root
);
1055 mutex_unlock(&root
->fs_info
->fs_mutex
);
1056 unlock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1061 err
= inode_setattr(inode
, attr
);
1066 void btrfs_put_inode(struct inode
*inode
)
1070 if (!BTRFS_I(inode
)->ordered_trans
) {
1074 if (mapping_tagged(inode
->i_mapping
, PAGECACHE_TAG_DIRTY
) ||
1075 mapping_tagged(inode
->i_mapping
, PAGECACHE_TAG_WRITEBACK
))
1078 ret
= btrfs_del_ordered_inode(inode
);
1080 atomic_dec(&inode
->i_count
);
1084 void btrfs_delete_inode(struct inode
*inode
)
1086 struct btrfs_trans_handle
*trans
;
1087 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1091 truncate_inode_pages(&inode
->i_data
, 0);
1092 if (is_bad_inode(inode
)) {
1097 mutex_lock(&root
->fs_info
->fs_mutex
);
1098 trans
= btrfs_start_transaction(root
, 1);
1100 btrfs_set_trans_block_group(trans
, inode
);
1101 ret
= btrfs_truncate_in_trans(trans
, root
, inode
, 0);
1103 goto no_delete_lock
;
1105 nr
= trans
->blocks_used
;
1108 btrfs_end_transaction(trans
, root
);
1109 mutex_unlock(&root
->fs_info
->fs_mutex
);
1110 btrfs_btree_balance_dirty(root
, nr
);
1111 btrfs_throttle(root
);
1115 nr
= trans
->blocks_used
;
1116 btrfs_end_transaction(trans
, root
);
1117 mutex_unlock(&root
->fs_info
->fs_mutex
);
1118 btrfs_btree_balance_dirty(root
, nr
);
1119 btrfs_throttle(root
);
1125 * this returns the key found in the dir entry in the location pointer.
1126 * If no dir entries were found, location->objectid is 0.
1128 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
1129 struct btrfs_key
*location
)
1131 const char *name
= dentry
->d_name
.name
;
1132 int namelen
= dentry
->d_name
.len
;
1133 struct btrfs_dir_item
*di
;
1134 struct btrfs_path
*path
;
1135 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1138 if (namelen
== 1 && strcmp(name
, ".") == 0) {
1139 location
->objectid
= dir
->i_ino
;
1140 location
->type
= BTRFS_INODE_ITEM_KEY
;
1141 location
->offset
= 0;
1144 path
= btrfs_alloc_path();
1147 if (namelen
== 2 && strcmp(name
, "..") == 0) {
1148 struct btrfs_key key
;
1149 struct extent_buffer
*leaf
;
1153 key
.objectid
= dir
->i_ino
;
1154 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1156 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1160 leaf
= path
->nodes
[0];
1161 slot
= path
->slots
[0];
1162 nritems
= btrfs_header_nritems(leaf
);
1163 if (slot
>= nritems
)
1166 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1167 if (key
.objectid
!= dir
->i_ino
||
1168 key
.type
!= BTRFS_INODE_REF_KEY
) {
1171 location
->objectid
= key
.offset
;
1172 location
->type
= BTRFS_INODE_ITEM_KEY
;
1173 location
->offset
= 0;
1177 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
1181 if (!di
|| IS_ERR(di
)) {
1184 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
1186 btrfs_free_path(path
);
1189 location
->objectid
= 0;
1194 * when we hit a tree root in a directory, the btrfs part of the inode
1195 * needs to be changed to reflect the root directory of the tree root. This
1196 * is kind of like crossing a mount point.
1198 static int fixup_tree_root_location(struct btrfs_root
*root
,
1199 struct btrfs_key
*location
,
1200 struct btrfs_root
**sub_root
,
1201 struct dentry
*dentry
)
1203 struct btrfs_path
*path
;
1204 struct btrfs_root_item
*ri
;
1206 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
1208 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
1211 path
= btrfs_alloc_path();
1213 mutex_lock(&root
->fs_info
->fs_mutex
);
1215 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
1216 dentry
->d_name
.name
,
1217 dentry
->d_name
.len
);
1218 if (IS_ERR(*sub_root
))
1219 return PTR_ERR(*sub_root
);
1221 ri
= &(*sub_root
)->root_item
;
1222 location
->objectid
= btrfs_root_dirid(ri
);
1223 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1224 location
->offset
= 0;
1226 btrfs_free_path(path
);
1227 mutex_unlock(&root
->fs_info
->fs_mutex
);
1231 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
1233 struct btrfs_iget_args
*args
= p
;
1234 inode
->i_ino
= args
->ino
;
1235 BTRFS_I(inode
)->root
= args
->root
;
1236 BTRFS_I(inode
)->delalloc_bytes
= 0;
1237 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1238 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1239 inode
->i_mapping
, GFP_NOFS
);
1243 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
1245 struct btrfs_iget_args
*args
= opaque
;
1246 return (args
->ino
== inode
->i_ino
&&
1247 args
->root
== BTRFS_I(inode
)->root
);
1250 struct inode
*btrfs_ilookup(struct super_block
*s
, u64 objectid
,
1253 struct btrfs_iget_args args
;
1254 args
.ino
= objectid
;
1255 args
.root
= btrfs_lookup_fs_root(btrfs_sb(s
)->fs_info
, root_objectid
);
1260 return ilookup5(s
, objectid
, btrfs_find_actor
, (void *)&args
);
1263 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
1264 struct btrfs_root
*root
)
1266 struct inode
*inode
;
1267 struct btrfs_iget_args args
;
1268 args
.ino
= objectid
;
1271 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
1272 btrfs_init_locked_inode
,
1277 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
1278 struct nameidata
*nd
)
1280 struct inode
* inode
;
1281 struct btrfs_inode
*bi
= BTRFS_I(dir
);
1282 struct btrfs_root
*root
= bi
->root
;
1283 struct btrfs_root
*sub_root
= root
;
1284 struct btrfs_key location
;
1287 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
1288 return ERR_PTR(-ENAMETOOLONG
);
1290 mutex_lock(&root
->fs_info
->fs_mutex
);
1291 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
1292 mutex_unlock(&root
->fs_info
->fs_mutex
);
1295 return ERR_PTR(ret
);
1298 if (location
.objectid
) {
1299 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
1302 return ERR_PTR(ret
);
1304 return ERR_PTR(-ENOENT
);
1305 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
1308 return ERR_PTR(-EACCES
);
1309 if (inode
->i_state
& I_NEW
) {
1310 /* the inode and parent dir are two different roots */
1311 if (sub_root
!= root
) {
1313 sub_root
->inode
= inode
;
1315 BTRFS_I(inode
)->root
= sub_root
;
1316 memcpy(&BTRFS_I(inode
)->location
, &location
,
1318 btrfs_read_locked_inode(inode
);
1319 unlock_new_inode(inode
);
1322 return d_splice_alias(inode
, dentry
);
1325 static unsigned char btrfs_filetype_table
[] = {
1326 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
1329 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1331 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1332 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1333 struct btrfs_item
*item
;
1334 struct btrfs_dir_item
*di
;
1335 struct btrfs_key key
;
1336 struct btrfs_key found_key
;
1337 struct btrfs_path
*path
;
1340 struct extent_buffer
*leaf
;
1343 unsigned char d_type
;
1348 int key_type
= BTRFS_DIR_INDEX_KEY
;
1353 /* FIXME, use a real flag for deciding about the key type */
1354 if (root
->fs_info
->tree_root
== root
)
1355 key_type
= BTRFS_DIR_ITEM_KEY
;
1357 /* special case for "." */
1358 if (filp
->f_pos
== 0) {
1359 over
= filldir(dirent
, ".", 1,
1367 mutex_lock(&root
->fs_info
->fs_mutex
);
1368 key
.objectid
= inode
->i_ino
;
1369 path
= btrfs_alloc_path();
1372 /* special case for .., just use the back ref */
1373 if (filp
->f_pos
== 1) {
1374 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1376 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1378 leaf
= path
->nodes
[0];
1379 slot
= path
->slots
[0];
1380 nritems
= btrfs_header_nritems(leaf
);
1381 if (slot
>= nritems
) {
1382 btrfs_release_path(root
, path
);
1383 goto read_dir_items
;
1385 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1386 btrfs_release_path(root
, path
);
1387 if (found_key
.objectid
!= key
.objectid
||
1388 found_key
.type
!= BTRFS_INODE_REF_KEY
)
1389 goto read_dir_items
;
1390 over
= filldir(dirent
, "..", 2,
1391 2, found_key
.offset
, DT_DIR
);
1398 btrfs_set_key_type(&key
, key_type
);
1399 key
.offset
= filp
->f_pos
;
1401 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1406 leaf
= path
->nodes
[0];
1407 nritems
= btrfs_header_nritems(leaf
);
1408 slot
= path
->slots
[0];
1409 if (advance
|| slot
>= nritems
) {
1410 if (slot
>= nritems
-1) {
1411 ret
= btrfs_next_leaf(root
, path
);
1414 leaf
= path
->nodes
[0];
1415 nritems
= btrfs_header_nritems(leaf
);
1416 slot
= path
->slots
[0];
1423 item
= btrfs_item_nr(leaf
, slot
);
1424 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1426 if (found_key
.objectid
!= key
.objectid
)
1428 if (btrfs_key_type(&found_key
) != key_type
)
1430 if (found_key
.offset
< filp
->f_pos
)
1433 filp
->f_pos
= found_key
.offset
;
1435 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
1437 di_total
= btrfs_item_size(leaf
, item
);
1438 while(di_cur
< di_total
) {
1439 struct btrfs_key location
;
1441 name_len
= btrfs_dir_name_len(leaf
, di
);
1442 if (name_len
< 32) {
1443 name_ptr
= tmp_name
;
1445 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
1448 read_extent_buffer(leaf
, name_ptr
,
1449 (unsigned long)(di
+ 1), name_len
);
1451 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
1452 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
1453 over
= filldir(dirent
, name_ptr
, name_len
,
1458 if (name_ptr
!= tmp_name
)
1463 di_len
= btrfs_dir_name_len(leaf
, di
) +
1464 btrfs_dir_data_len(leaf
, di
) +sizeof(*di
);
1466 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
1469 if (key_type
== BTRFS_DIR_INDEX_KEY
)
1470 filp
->f_pos
= INT_LIMIT(typeof(filp
->f_pos
));
1476 btrfs_release_path(root
, path
);
1477 btrfs_free_path(path
);
1478 mutex_unlock(&root
->fs_info
->fs_mutex
);
1482 int btrfs_write_inode(struct inode
*inode
, int wait
)
1484 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1485 struct btrfs_trans_handle
*trans
;
1489 mutex_lock(&root
->fs_info
->fs_mutex
);
1490 trans
= btrfs_start_transaction(root
, 1);
1491 btrfs_set_trans_block_group(trans
, inode
);
1492 ret
= btrfs_commit_transaction(trans
, root
);
1493 mutex_unlock(&root
->fs_info
->fs_mutex
);
1499 * This is somewhat expensive, updating the tree every time the
1500 * inode changes. But, it is most likely to find the inode in cache.
1501 * FIXME, needs more benchmarking...there are no reasons other than performance
1502 * to keep or drop this code.
1504 void btrfs_dirty_inode(struct inode
*inode
)
1506 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1507 struct btrfs_trans_handle
*trans
;
1509 mutex_lock(&root
->fs_info
->fs_mutex
);
1510 trans
= btrfs_start_transaction(root
, 1);
1511 btrfs_set_trans_block_group(trans
, inode
);
1512 btrfs_update_inode(trans
, root
, inode
);
1513 btrfs_end_transaction(trans
, root
);
1514 mutex_unlock(&root
->fs_info
->fs_mutex
);
1517 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
1518 struct btrfs_root
*root
,
1519 const char *name
, int name_len
,
1522 struct btrfs_block_group_cache
*group
,
1525 struct inode
*inode
;
1526 struct btrfs_inode_item
*inode_item
;
1527 struct btrfs_key
*location
;
1528 struct btrfs_path
*path
;
1529 struct btrfs_inode_ref
*ref
;
1530 struct btrfs_key key
[2];
1536 path
= btrfs_alloc_path();
1539 inode
= new_inode(root
->fs_info
->sb
);
1541 return ERR_PTR(-ENOMEM
);
1543 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1544 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1545 inode
->i_mapping
, GFP_NOFS
);
1546 BTRFS_I(inode
)->delalloc_bytes
= 0;
1547 BTRFS_I(inode
)->root
= root
;
1553 group
= btrfs_find_block_group(root
, group
, 0, 0, owner
);
1554 BTRFS_I(inode
)->block_group
= group
;
1555 BTRFS_I(inode
)->flags
= 0;
1557 key
[0].objectid
= objectid
;
1558 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
1561 key
[1].objectid
= objectid
;
1562 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
1563 key
[1].offset
= ref_objectid
;
1565 sizes
[0] = sizeof(struct btrfs_inode_item
);
1566 sizes
[1] = name_len
+ sizeof(*ref
);
1568 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
1572 if (objectid
> root
->highest_inode
)
1573 root
->highest_inode
= objectid
;
1575 inode
->i_uid
= current
->fsuid
;
1576 inode
->i_gid
= current
->fsgid
;
1577 inode
->i_mode
= mode
;
1578 inode
->i_ino
= objectid
;
1579 inode
->i_blocks
= 0;
1580 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1581 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1582 struct btrfs_inode_item
);
1583 fill_inode_item(path
->nodes
[0], inode_item
, inode
);
1585 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
1586 struct btrfs_inode_ref
);
1587 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
1588 ptr
= (unsigned long)(ref
+ 1);
1589 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
1591 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1592 btrfs_free_path(path
);
1594 location
= &BTRFS_I(inode
)->location
;
1595 location
->objectid
= objectid
;
1596 location
->offset
= 0;
1597 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1599 insert_inode_hash(inode
);
1602 btrfs_free_path(path
);
1603 return ERR_PTR(ret
);
1606 static inline u8
btrfs_inode_type(struct inode
*inode
)
1608 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
1611 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
1612 struct dentry
*dentry
, struct inode
*inode
,
1616 struct btrfs_key key
;
1617 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
1618 struct inode
*parent_inode
;
1620 key
.objectid
= inode
->i_ino
;
1621 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1624 ret
= btrfs_insert_dir_item(trans
, root
,
1625 dentry
->d_name
.name
, dentry
->d_name
.len
,
1626 dentry
->d_parent
->d_inode
->i_ino
,
1627 &key
, btrfs_inode_type(inode
));
1630 ret
= btrfs_insert_inode_ref(trans
, root
,
1631 dentry
->d_name
.name
,
1634 dentry
->d_parent
->d_inode
->i_ino
);
1636 parent_inode
= dentry
->d_parent
->d_inode
;
1637 parent_inode
->i_size
+= dentry
->d_name
.len
* 2;
1638 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
1639 ret
= btrfs_update_inode(trans
, root
,
1640 dentry
->d_parent
->d_inode
);
1645 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
1646 struct dentry
*dentry
, struct inode
*inode
,
1649 int err
= btrfs_add_link(trans
, dentry
, inode
, backref
);
1651 d_instantiate(dentry
, inode
);
1659 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1660 int mode
, dev_t rdev
)
1662 struct btrfs_trans_handle
*trans
;
1663 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1664 struct inode
*inode
= NULL
;
1668 unsigned long nr
= 0;
1670 if (!new_valid_dev(rdev
))
1673 mutex_lock(&root
->fs_info
->fs_mutex
);
1674 err
= btrfs_check_free_space(root
, 1, 0);
1678 trans
= btrfs_start_transaction(root
, 1);
1679 btrfs_set_trans_block_group(trans
, dir
);
1681 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1687 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
1689 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
1690 BTRFS_I(dir
)->block_group
, mode
);
1691 err
= PTR_ERR(inode
);
1695 btrfs_set_trans_block_group(trans
, inode
);
1696 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
1700 inode
->i_op
= &btrfs_special_inode_operations
;
1701 init_special_inode(inode
, inode
->i_mode
, rdev
);
1702 btrfs_update_inode(trans
, root
, inode
);
1704 dir
->i_sb
->s_dirt
= 1;
1705 btrfs_update_inode_block_group(trans
, inode
);
1706 btrfs_update_inode_block_group(trans
, dir
);
1708 nr
= trans
->blocks_used
;
1709 btrfs_end_transaction(trans
, root
);
1711 mutex_unlock(&root
->fs_info
->fs_mutex
);
1714 inode_dec_link_count(inode
);
1717 btrfs_btree_balance_dirty(root
, nr
);
1718 btrfs_throttle(root
);
1722 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
1723 int mode
, struct nameidata
*nd
)
1725 struct btrfs_trans_handle
*trans
;
1726 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1727 struct inode
*inode
= NULL
;
1730 unsigned long nr
= 0;
1733 mutex_lock(&root
->fs_info
->fs_mutex
);
1734 err
= btrfs_check_free_space(root
, 1, 0);
1737 trans
= btrfs_start_transaction(root
, 1);
1738 btrfs_set_trans_block_group(trans
, dir
);
1740 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1746 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
1748 dentry
->d_parent
->d_inode
->i_ino
,
1749 objectid
, BTRFS_I(dir
)->block_group
, mode
);
1750 err
= PTR_ERR(inode
);
1754 btrfs_set_trans_block_group(trans
, inode
);
1755 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
1759 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1760 inode
->i_fop
= &btrfs_file_operations
;
1761 inode
->i_op
= &btrfs_file_inode_operations
;
1762 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1763 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1764 inode
->i_mapping
, GFP_NOFS
);
1765 BTRFS_I(inode
)->delalloc_bytes
= 0;
1766 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
1768 dir
->i_sb
->s_dirt
= 1;
1769 btrfs_update_inode_block_group(trans
, inode
);
1770 btrfs_update_inode_block_group(trans
, dir
);
1772 nr
= trans
->blocks_used
;
1773 btrfs_end_transaction(trans
, root
);
1775 mutex_unlock(&root
->fs_info
->fs_mutex
);
1778 inode_dec_link_count(inode
);
1781 btrfs_btree_balance_dirty(root
, nr
);
1782 btrfs_throttle(root
);
1786 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
1787 struct dentry
*dentry
)
1789 struct btrfs_trans_handle
*trans
;
1790 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1791 struct inode
*inode
= old_dentry
->d_inode
;
1792 unsigned long nr
= 0;
1796 if (inode
->i_nlink
== 0)
1799 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1804 mutex_lock(&root
->fs_info
->fs_mutex
);
1805 err
= btrfs_check_free_space(root
, 1, 0);
1808 trans
= btrfs_start_transaction(root
, 1);
1810 btrfs_set_trans_block_group(trans
, dir
);
1811 atomic_inc(&inode
->i_count
);
1812 err
= btrfs_add_nondir(trans
, dentry
, inode
, 1);
1817 dir
->i_sb
->s_dirt
= 1;
1818 btrfs_update_inode_block_group(trans
, dir
);
1819 err
= btrfs_update_inode(trans
, root
, inode
);
1824 nr
= trans
->blocks_used
;
1825 btrfs_end_transaction(trans
, root
);
1827 mutex_unlock(&root
->fs_info
->fs_mutex
);
1830 inode_dec_link_count(inode
);
1833 btrfs_btree_balance_dirty(root
, nr
);
1834 btrfs_throttle(root
);
1838 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1840 struct inode
*inode
;
1841 struct btrfs_trans_handle
*trans
;
1842 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1844 int drop_on_err
= 0;
1846 unsigned long nr
= 1;
1848 mutex_lock(&root
->fs_info
->fs_mutex
);
1849 err
= btrfs_check_free_space(root
, 1, 0);
1853 trans
= btrfs_start_transaction(root
, 1);
1854 btrfs_set_trans_block_group(trans
, dir
);
1856 if (IS_ERR(trans
)) {
1857 err
= PTR_ERR(trans
);
1861 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1867 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
1869 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
1870 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
1871 if (IS_ERR(inode
)) {
1872 err
= PTR_ERR(inode
);
1877 inode
->i_op
= &btrfs_dir_inode_operations
;
1878 inode
->i_fop
= &btrfs_dir_file_operations
;
1879 btrfs_set_trans_block_group(trans
, inode
);
1882 err
= btrfs_update_inode(trans
, root
, inode
);
1886 err
= btrfs_add_link(trans
, dentry
, inode
, 0);
1890 d_instantiate(dentry
, inode
);
1892 dir
->i_sb
->s_dirt
= 1;
1893 btrfs_update_inode_block_group(trans
, inode
);
1894 btrfs_update_inode_block_group(trans
, dir
);
1897 nr
= trans
->blocks_used
;
1898 btrfs_end_transaction(trans
, root
);
1901 mutex_unlock(&root
->fs_info
->fs_mutex
);
1904 btrfs_btree_balance_dirty(root
, nr
);
1905 btrfs_throttle(root
);
1909 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
1910 size_t pg_offset
, u64 start
, u64 len
,
1916 u64 extent_start
= 0;
1918 u64 objectid
= inode
->i_ino
;
1920 struct btrfs_path
*path
;
1921 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1922 struct btrfs_file_extent_item
*item
;
1923 struct extent_buffer
*leaf
;
1924 struct btrfs_key found_key
;
1925 struct extent_map
*em
= NULL
;
1926 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1927 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1928 struct btrfs_trans_handle
*trans
= NULL
;
1930 path
= btrfs_alloc_path();
1932 mutex_lock(&root
->fs_info
->fs_mutex
);
1935 spin_lock(&em_tree
->lock
);
1936 em
= lookup_extent_mapping(em_tree
, start
, len
);
1937 spin_unlock(&em_tree
->lock
);
1940 if (em
->start
> start
) {
1941 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
1942 start
, len
, em
->start
, em
->len
);
1945 if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
1946 free_extent_map(em
);
1950 em
= alloc_extent_map(GFP_NOFS
);
1956 em
->start
= EXTENT_MAP_HOLE
;
1958 em
->bdev
= inode
->i_sb
->s_bdev
;
1959 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
1960 objectid
, start
, trans
!= NULL
);
1967 if (path
->slots
[0] == 0)
1972 leaf
= path
->nodes
[0];
1973 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1974 struct btrfs_file_extent_item
);
1975 /* are we inside the extent that was found? */
1976 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1977 found_type
= btrfs_key_type(&found_key
);
1978 if (found_key
.objectid
!= objectid
||
1979 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
1983 found_type
= btrfs_file_extent_type(leaf
, item
);
1984 extent_start
= found_key
.offset
;
1985 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1986 extent_end
= extent_start
+
1987 btrfs_file_extent_num_bytes(leaf
, item
);
1989 if (start
< extent_start
|| start
>= extent_end
) {
1991 if (start
< extent_start
) {
1992 if (start
+ len
<= extent_start
)
1994 em
->len
= extent_end
- extent_start
;
2000 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
2002 em
->start
= extent_start
;
2003 em
->len
= extent_end
- extent_start
;
2004 em
->block_start
= EXTENT_MAP_HOLE
;
2007 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
2008 em
->block_start
= bytenr
;
2009 em
->start
= extent_start
;
2010 em
->len
= extent_end
- extent_start
;
2012 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
2017 size_t extent_offset
;
2020 size
= btrfs_file_extent_inline_len(leaf
, btrfs_item_nr(leaf
,
2022 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
2023 ~((u64
)root
->sectorsize
- 1);
2024 if (start
< extent_start
|| start
>= extent_end
) {
2026 if (start
< extent_start
) {
2027 if (start
+ len
<= extent_start
)
2029 em
->len
= extent_end
- extent_start
;
2035 em
->block_start
= EXTENT_MAP_INLINE
;
2038 em
->start
= extent_start
;
2043 page_start
= page_offset(page
) + pg_offset
;
2044 extent_offset
= page_start
- extent_start
;
2045 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
2046 size
- extent_offset
);
2047 em
->start
= extent_start
+ extent_offset
;
2048 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
2049 ~((u64
)root
->sectorsize
- 1);
2051 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
2052 if (create
== 0 && !PageUptodate(page
)) {
2053 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2055 flush_dcache_page(page
);
2056 } else if (create
&& PageUptodate(page
)) {
2059 free_extent_map(em
);
2061 btrfs_release_path(root
, path
);
2062 trans
= btrfs_start_transaction(root
, 1);
2065 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2067 btrfs_mark_buffer_dirty(leaf
);
2070 set_extent_uptodate(io_tree
, em
->start
,
2071 extent_map_end(em
) - 1, GFP_NOFS
);
2074 printk("unkknown found_type %d\n", found_type
);
2081 em
->block_start
= EXTENT_MAP_HOLE
;
2083 btrfs_release_path(root
, path
);
2084 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
2085 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->len
, start
, len
);
2091 spin_lock(&em_tree
->lock
);
2092 ret
= add_extent_mapping(em_tree
, em
);
2093 if (ret
== -EEXIST
) {
2094 free_extent_map(em
);
2095 em
= lookup_extent_mapping(em_tree
, start
, len
);
2098 printk("failing to insert %Lu %Lu\n", start
, len
);
2101 spin_unlock(&em_tree
->lock
);
2103 btrfs_free_path(path
);
2105 ret
= btrfs_end_transaction(trans
, root
);
2109 mutex_unlock(&root
->fs_info
->fs_mutex
);
2111 free_extent_map(em
);
2113 return ERR_PTR(err
);
2118 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
2120 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
2123 int btrfs_readpage(struct file
*file
, struct page
*page
)
2125 struct extent_io_tree
*tree
;
2126 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2127 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
2130 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
2132 struct extent_io_tree
*tree
;
2135 if (current
->flags
& PF_MEMALLOC
) {
2136 redirty_page_for_writepage(wbc
, page
);
2140 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2141 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
2144 static int btrfs_writepages(struct address_space
*mapping
,
2145 struct writeback_control
*wbc
)
2147 struct extent_io_tree
*tree
;
2148 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
2149 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
2153 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
2154 struct list_head
*pages
, unsigned nr_pages
)
2156 struct extent_io_tree
*tree
;
2157 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
2158 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
2162 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
2164 struct extent_io_tree
*tree
;
2165 struct extent_map_tree
*map
;
2168 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2169 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
2170 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
2172 ClearPagePrivate(page
);
2173 set_page_private(page
, 0);
2174 page_cache_release(page
);
2179 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
2181 struct extent_io_tree
*tree
;
2183 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2184 extent_invalidatepage(tree
, page
, offset
);
2185 btrfs_releasepage(page
, GFP_NOFS
);
2189 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2190 * called from a page fault handler when a page is first dirtied. Hence we must
2191 * be careful to check for EOF conditions here. We set the page up correctly
2192 * for a written page which means we get ENOSPC checking when writing into
2193 * holes and correct delalloc and unwritten extent mapping on filesystems that
2194 * support these features.
2196 * We are not allowed to take the i_mutex here so we have to play games to
2197 * protect against truncate races as the page could now be beyond EOF. Because
2198 * vmtruncate() writes the inode size before removing pages, once we have the
2199 * page lock we can determine safely if the page is beyond EOF. If it is not
2200 * beyond EOF, then the page is guaranteed safe against truncation until we
2203 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
2205 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
2206 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2212 mutex_lock(&root
->fs_info
->fs_mutex
);
2213 ret
= btrfs_check_free_space(root
, PAGE_CACHE_SIZE
, 0);
2214 mutex_unlock(&root
->fs_info
->fs_mutex
);
2221 wait_on_page_writeback(page
);
2222 size
= i_size_read(inode
);
2223 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2225 if ((page
->mapping
!= inode
->i_mapping
) ||
2226 (page_start
> size
)) {
2227 /* page got truncated out from underneath us */
2231 /* page is wholly or partially inside EOF */
2232 if (page_start
+ PAGE_CACHE_SIZE
> size
)
2233 end
= size
& ~PAGE_CACHE_MASK
;
2235 end
= PAGE_CACHE_SIZE
;
2237 ret
= btrfs_cow_one_page(inode
, page
, end
);
2245 static void btrfs_truncate(struct inode
*inode
)
2247 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2249 struct btrfs_trans_handle
*trans
;
2252 if (!S_ISREG(inode
->i_mode
))
2254 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
2257 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
2259 mutex_lock(&root
->fs_info
->fs_mutex
);
2260 trans
= btrfs_start_transaction(root
, 1);
2261 btrfs_set_trans_block_group(trans
, inode
);
2263 /* FIXME, add redo link to tree so we don't leak on crash */
2264 ret
= btrfs_truncate_in_trans(trans
, root
, inode
,
2265 BTRFS_EXTENT_DATA_KEY
);
2266 btrfs_update_inode(trans
, root
, inode
);
2267 nr
= trans
->blocks_used
;
2269 ret
= btrfs_end_transaction(trans
, root
);
2271 mutex_unlock(&root
->fs_info
->fs_mutex
);
2272 btrfs_btree_balance_dirty(root
, nr
);
2273 btrfs_throttle(root
);
2276 static int noinline
create_subvol(struct btrfs_root
*root
, char *name
,
2279 struct btrfs_trans_handle
*trans
;
2280 struct btrfs_key key
;
2281 struct btrfs_root_item root_item
;
2282 struct btrfs_inode_item
*inode_item
;
2283 struct extent_buffer
*leaf
;
2284 struct btrfs_root
*new_root
= root
;
2285 struct inode
*inode
;
2290 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
2291 unsigned long nr
= 1;
2293 mutex_lock(&root
->fs_info
->fs_mutex
);
2294 ret
= btrfs_check_free_space(root
, 1, 0);
2298 trans
= btrfs_start_transaction(root
, 1);
2301 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2306 leaf
= __btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
2307 objectid
, trans
->transid
, 0, 0,
2310 return PTR_ERR(leaf
);
2312 btrfs_set_header_nritems(leaf
, 0);
2313 btrfs_set_header_level(leaf
, 0);
2314 btrfs_set_header_bytenr(leaf
, leaf
->start
);
2315 btrfs_set_header_generation(leaf
, trans
->transid
);
2316 btrfs_set_header_owner(leaf
, objectid
);
2318 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
2319 (unsigned long)btrfs_header_fsid(leaf
),
2321 btrfs_mark_buffer_dirty(leaf
);
2323 inode_item
= &root_item
.inode
;
2324 memset(inode_item
, 0, sizeof(*inode_item
));
2325 inode_item
->generation
= cpu_to_le64(1);
2326 inode_item
->size
= cpu_to_le64(3);
2327 inode_item
->nlink
= cpu_to_le32(1);
2328 inode_item
->nblocks
= cpu_to_le64(1);
2329 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
2331 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
2332 btrfs_set_root_level(&root_item
, 0);
2333 btrfs_set_root_refs(&root_item
, 1);
2334 btrfs_set_root_used(&root_item
, 0);
2336 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
2337 root_item
.drop_level
= 0;
2339 free_extent_buffer(leaf
);
2342 btrfs_set_root_dirid(&root_item
, new_dirid
);
2344 key
.objectid
= objectid
;
2346 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2347 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2353 * insert the directory item
2355 key
.offset
= (u64
)-1;
2356 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
2357 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2358 name
, namelen
, dir
->i_ino
, &key
,
2363 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
2364 name
, namelen
, objectid
,
2365 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
);
2369 ret
= btrfs_commit_transaction(trans
, root
);
2373 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
, name
, namelen
);
2376 trans
= btrfs_start_transaction(new_root
, 1);
2379 inode
= btrfs_new_inode(trans
, new_root
, "..", 2, new_dirid
,
2381 BTRFS_I(dir
)->block_group
, S_IFDIR
| 0700);
2384 inode
->i_op
= &btrfs_dir_inode_operations
;
2385 inode
->i_fop
= &btrfs_dir_file_operations
;
2386 new_root
->inode
= inode
;
2388 ret
= btrfs_insert_inode_ref(trans
, new_root
, "..", 2, new_dirid
,
2392 ret
= btrfs_update_inode(trans
, new_root
, inode
);
2396 nr
= trans
->blocks_used
;
2397 err
= btrfs_commit_transaction(trans
, new_root
);
2401 mutex_unlock(&root
->fs_info
->fs_mutex
);
2402 btrfs_btree_balance_dirty(root
, nr
);
2403 btrfs_throttle(root
);
2407 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
2409 struct btrfs_pending_snapshot
*pending_snapshot
;
2410 struct btrfs_trans_handle
*trans
;
2413 unsigned long nr
= 0;
2415 if (!root
->ref_cows
)
2418 mutex_lock(&root
->fs_info
->fs_mutex
);
2419 ret
= btrfs_check_free_space(root
, 1, 0);
2423 pending_snapshot
= kmalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
2424 if (!pending_snapshot
) {
2428 pending_snapshot
->name
= kmalloc(namelen
+ 1, GFP_NOFS
);
2429 if (!pending_snapshot
->name
) {
2431 kfree(pending_snapshot
);
2434 memcpy(pending_snapshot
->name
, name
, namelen
);
2435 pending_snapshot
->name
[namelen
] = '\0';
2436 trans
= btrfs_start_transaction(root
, 1);
2438 pending_snapshot
->root
= root
;
2439 list_add(&pending_snapshot
->list
,
2440 &trans
->transaction
->pending_snapshots
);
2441 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
2442 err
= btrfs_commit_transaction(trans
, root
);
2445 mutex_unlock(&root
->fs_info
->fs_mutex
);
2446 btrfs_btree_balance_dirty(root
, nr
);
2447 btrfs_throttle(root
);
2451 unsigned long btrfs_force_ra(struct address_space
*mapping
,
2452 struct file_ra_state
*ra
, struct file
*file
,
2453 pgoff_t offset
, pgoff_t last_index
)
2457 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2458 req_size
= last_index
- offset
+ 1;
2459 offset
= page_cache_readahead(mapping
, ra
, file
, offset
, req_size
);
2462 req_size
= min(last_index
- offset
+ 1, (pgoff_t
)128);
2463 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
2464 return offset
+ req_size
;
2468 int btrfs_defrag_file(struct file
*file
) {
2469 struct inode
*inode
= fdentry(file
)->d_inode
;
2470 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2471 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2473 unsigned long last_index
;
2474 unsigned long ra_index
= 0;
2480 mutex_lock(&root
->fs_info
->fs_mutex
);
2481 ret
= btrfs_check_free_space(root
, inode
->i_size
, 0);
2482 mutex_unlock(&root
->fs_info
->fs_mutex
);
2486 mutex_lock(&inode
->i_mutex
);
2487 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
2488 for (i
= 0; i
<= last_index
; i
++) {
2489 if (i
== ra_index
) {
2490 ra_index
= btrfs_force_ra(inode
->i_mapping
,
2492 file
, ra_index
, last_index
);
2494 page
= grab_cache_page(inode
->i_mapping
, i
);
2497 if (!PageUptodate(page
)) {
2498 btrfs_readpage(NULL
, page
);
2500 if (!PageUptodate(page
)) {
2502 page_cache_release(page
);
2506 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2507 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2509 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2510 set_extent_delalloc(io_tree
, page_start
,
2511 page_end
, GFP_NOFS
);
2513 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2514 set_page_dirty(page
);
2516 page_cache_release(page
);
2517 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
2521 mutex_unlock(&inode
->i_mutex
);
2525 static int btrfs_ioctl_resize(struct btrfs_root
*root
, void __user
*arg
)
2529 struct btrfs_ioctl_vol_args
*vol_args
;
2530 struct btrfs_trans_handle
*trans
;
2536 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
2541 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
2545 namelen
= strlen(vol_args
->name
);
2546 if (namelen
> BTRFS_VOL_NAME_MAX
) {
2551 sizestr
= vol_args
->name
;
2552 if (!strcmp(sizestr
, "max"))
2553 new_size
= root
->fs_info
->sb
->s_bdev
->bd_inode
->i_size
;
2555 if (sizestr
[0] == '-') {
2558 } else if (sizestr
[0] == '+') {
2562 new_size
= btrfs_parse_size(sizestr
);
2563 if (new_size
== 0) {
2569 mutex_lock(&root
->fs_info
->fs_mutex
);
2570 old_size
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
2573 if (new_size
> old_size
) {
2577 new_size
= old_size
- new_size
;
2578 } else if (mod
> 0) {
2579 new_size
= old_size
+ new_size
;
2582 if (new_size
< 256 * 1024 * 1024) {
2586 if (new_size
> root
->fs_info
->sb
->s_bdev
->bd_inode
->i_size
) {
2591 do_div(new_size
, root
->sectorsize
);
2592 new_size
*= root
->sectorsize
;
2594 printk("new size is %Lu\n", new_size
);
2595 if (new_size
> old_size
) {
2596 trans
= btrfs_start_transaction(root
, 1);
2597 ret
= btrfs_grow_extent_tree(trans
, root
, new_size
);
2598 btrfs_commit_transaction(trans
, root
);
2600 ret
= btrfs_shrink_extent_tree(root
, new_size
);
2604 mutex_unlock(&root
->fs_info
->fs_mutex
);
2610 static int noinline
btrfs_ioctl_snap_create(struct btrfs_root
*root
,
2613 struct btrfs_ioctl_vol_args
*vol_args
;
2614 struct btrfs_dir_item
*di
;
2615 struct btrfs_path
*path
;
2620 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
2625 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
2630 namelen
= strlen(vol_args
->name
);
2631 if (namelen
> BTRFS_VOL_NAME_MAX
) {
2635 if (strchr(vol_args
->name
, '/')) {
2640 path
= btrfs_alloc_path();
2646 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2647 mutex_lock(&root
->fs_info
->fs_mutex
);
2648 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
2650 vol_args
->name
, namelen
, 0);
2651 mutex_unlock(&root
->fs_info
->fs_mutex
);
2652 btrfs_free_path(path
);
2654 if (di
&& !IS_ERR(di
)) {
2664 if (root
== root
->fs_info
->tree_root
)
2665 ret
= create_subvol(root
, vol_args
->name
, namelen
);
2667 ret
= create_snapshot(root
, vol_args
->name
, namelen
);
2673 static int btrfs_ioctl_defrag(struct file
*file
)
2675 struct inode
*inode
= fdentry(file
)->d_inode
;
2676 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2678 switch (inode
->i_mode
& S_IFMT
) {
2680 mutex_lock(&root
->fs_info
->fs_mutex
);
2681 btrfs_defrag_root(root
, 0);
2682 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
2683 mutex_unlock(&root
->fs_info
->fs_mutex
);
2686 btrfs_defrag_file(file
);
2693 long btrfs_ioctl(struct file
*file
, unsigned int
2694 cmd
, unsigned long arg
)
2696 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
2699 case BTRFS_IOC_SNAP_CREATE
:
2700 return btrfs_ioctl_snap_create(root
, (void __user
*)arg
);
2701 case BTRFS_IOC_DEFRAG
:
2702 return btrfs_ioctl_defrag(file
);
2703 case BTRFS_IOC_RESIZE
:
2704 return btrfs_ioctl_resize(root
, (void __user
*)arg
);
2711 * Called inside transaction, so use GFP_NOFS
2713 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2715 struct btrfs_inode
*ei
;
2717 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2721 ei
->ordered_trans
= 0;
2722 return &ei
->vfs_inode
;
2725 void btrfs_destroy_inode(struct inode
*inode
)
2727 WARN_ON(!list_empty(&inode
->i_dentry
));
2728 WARN_ON(mapping_nrpages(&inode
->i_data
));
2730 btrfs_drop_extent_cache(inode
, 0, (u64
)-1);
2731 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2734 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2735 static void init_once(struct kmem_cache
* cachep
, void *foo
)
2737 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2738 unsigned long flags
)
2741 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2743 inode_init_once(&ei
->vfs_inode
);
2746 void btrfs_destroy_cachep(void)
2748 if (btrfs_inode_cachep
)
2749 kmem_cache_destroy(btrfs_inode_cachep
);
2750 if (btrfs_trans_handle_cachep
)
2751 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2752 if (btrfs_transaction_cachep
)
2753 kmem_cache_destroy(btrfs_transaction_cachep
);
2754 if (btrfs_bit_radix_cachep
)
2755 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2756 if (btrfs_path_cachep
)
2757 kmem_cache_destroy(btrfs_path_cachep
);
2760 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
2761 unsigned long extra_flags
,
2762 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2763 void (*ctor
)(struct kmem_cache
*, void *)
2765 void (*ctor
)(void *, struct kmem_cache
*,
2770 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
2771 SLAB_MEM_SPREAD
| extra_flags
), ctor
2772 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2778 int btrfs_init_cachep(void)
2780 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
2781 sizeof(struct btrfs_inode
),
2783 if (!btrfs_inode_cachep
)
2785 btrfs_trans_handle_cachep
=
2786 btrfs_cache_create("btrfs_trans_handle_cache",
2787 sizeof(struct btrfs_trans_handle
),
2789 if (!btrfs_trans_handle_cachep
)
2791 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
2792 sizeof(struct btrfs_transaction
),
2794 if (!btrfs_transaction_cachep
)
2796 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
2797 sizeof(struct btrfs_path
),
2799 if (!btrfs_path_cachep
)
2801 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
2802 SLAB_DESTROY_BY_RCU
, NULL
);
2803 if (!btrfs_bit_radix_cachep
)
2807 btrfs_destroy_cachep();
2811 static int btrfs_getattr(struct vfsmount
*mnt
,
2812 struct dentry
*dentry
, struct kstat
*stat
)
2814 struct inode
*inode
= dentry
->d_inode
;
2815 generic_fillattr(inode
, stat
);
2816 stat
->blksize
= PAGE_CACHE_SIZE
;
2817 stat
->blocks
= inode
->i_blocks
+ (BTRFS_I(inode
)->delalloc_bytes
>> 9);
2821 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
2822 struct inode
* new_dir
,struct dentry
*new_dentry
)
2824 struct btrfs_trans_handle
*trans
;
2825 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
2826 struct inode
*new_inode
= new_dentry
->d_inode
;
2827 struct inode
*old_inode
= old_dentry
->d_inode
;
2828 struct timespec ctime
= CURRENT_TIME
;
2829 struct btrfs_path
*path
;
2832 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
2833 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
2837 mutex_lock(&root
->fs_info
->fs_mutex
);
2838 ret
= btrfs_check_free_space(root
, 1, 0);
2842 trans
= btrfs_start_transaction(root
, 1);
2844 btrfs_set_trans_block_group(trans
, new_dir
);
2845 path
= btrfs_alloc_path();
2851 old_dentry
->d_inode
->i_nlink
++;
2852 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
2853 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
2854 old_inode
->i_ctime
= ctime
;
2856 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
2861 new_inode
->i_ctime
= CURRENT_TIME
;
2862 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
2866 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
, 1);
2871 btrfs_free_path(path
);
2872 btrfs_end_transaction(trans
, root
);
2874 mutex_unlock(&root
->fs_info
->fs_mutex
);
2878 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
2879 const char *symname
)
2881 struct btrfs_trans_handle
*trans
;
2882 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2883 struct btrfs_path
*path
;
2884 struct btrfs_key key
;
2885 struct inode
*inode
= NULL
;
2892 struct btrfs_file_extent_item
*ei
;
2893 struct extent_buffer
*leaf
;
2894 unsigned long nr
= 0;
2896 name_len
= strlen(symname
) + 1;
2897 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
2898 return -ENAMETOOLONG
;
2900 mutex_lock(&root
->fs_info
->fs_mutex
);
2901 err
= btrfs_check_free_space(root
, 1, 0);
2905 trans
= btrfs_start_transaction(root
, 1);
2906 btrfs_set_trans_block_group(trans
, dir
);
2908 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2914 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
2916 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
2917 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
2918 err
= PTR_ERR(inode
);
2922 btrfs_set_trans_block_group(trans
, inode
);
2923 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
2927 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2928 inode
->i_fop
= &btrfs_file_operations
;
2929 inode
->i_op
= &btrfs_file_inode_operations
;
2930 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
2931 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
2932 inode
->i_mapping
, GFP_NOFS
);
2933 BTRFS_I(inode
)->delalloc_bytes
= 0;
2934 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2936 dir
->i_sb
->s_dirt
= 1;
2937 btrfs_update_inode_block_group(trans
, inode
);
2938 btrfs_update_inode_block_group(trans
, dir
);
2942 path
= btrfs_alloc_path();
2944 key
.objectid
= inode
->i_ino
;
2946 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
2947 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
2948 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2954 leaf
= path
->nodes
[0];
2955 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
2956 struct btrfs_file_extent_item
);
2957 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
2958 btrfs_set_file_extent_type(leaf
, ei
,
2959 BTRFS_FILE_EXTENT_INLINE
);
2960 ptr
= btrfs_file_extent_inline_start(ei
);
2961 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
2962 btrfs_mark_buffer_dirty(leaf
);
2963 btrfs_free_path(path
);
2965 inode
->i_op
= &btrfs_symlink_inode_operations
;
2966 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2967 inode
->i_size
= name_len
- 1;
2968 err
= btrfs_update_inode(trans
, root
, inode
);
2973 nr
= trans
->blocks_used
;
2974 btrfs_end_transaction(trans
, root
);
2976 mutex_unlock(&root
->fs_info
->fs_mutex
);
2978 inode_dec_link_count(inode
);
2981 btrfs_btree_balance_dirty(root
, nr
);
2982 btrfs_throttle(root
);
2985 static int btrfs_permission(struct inode
*inode
, int mask
,
2986 struct nameidata
*nd
)
2988 if (btrfs_test_flag(inode
, READONLY
) && (mask
& MAY_WRITE
))
2990 return generic_permission(inode
, mask
, NULL
);
2993 static struct inode_operations btrfs_dir_inode_operations
= {
2994 .lookup
= btrfs_lookup
,
2995 .create
= btrfs_create
,
2996 .unlink
= btrfs_unlink
,
2998 .mkdir
= btrfs_mkdir
,
2999 .rmdir
= btrfs_rmdir
,
3000 .rename
= btrfs_rename
,
3001 .symlink
= btrfs_symlink
,
3002 .setattr
= btrfs_setattr
,
3003 .mknod
= btrfs_mknod
,
3004 .setxattr
= generic_setxattr
,
3005 .getxattr
= generic_getxattr
,
3006 .listxattr
= btrfs_listxattr
,
3007 .removexattr
= generic_removexattr
,
3008 .permission
= btrfs_permission
,
3010 static struct inode_operations btrfs_dir_ro_inode_operations
= {
3011 .lookup
= btrfs_lookup
,
3012 .permission
= btrfs_permission
,
3014 static struct file_operations btrfs_dir_file_operations
= {
3015 .llseek
= generic_file_llseek
,
3016 .read
= generic_read_dir
,
3017 .readdir
= btrfs_readdir
,
3018 .unlocked_ioctl
= btrfs_ioctl
,
3019 #ifdef CONFIG_COMPAT
3020 .compat_ioctl
= btrfs_ioctl
,
3024 static struct extent_io_ops btrfs_extent_io_ops
= {
3025 .fill_delalloc
= run_delalloc_range
,
3026 .submit_bio_hook
= btrfs_submit_bio_hook
,
3027 .readpage_io_hook
= btrfs_readpage_io_hook
,
3028 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
3029 .set_bit_hook
= btrfs_set_bit_hook
,
3030 .clear_bit_hook
= btrfs_clear_bit_hook
,
3033 static struct address_space_operations btrfs_aops
= {
3034 .readpage
= btrfs_readpage
,
3035 .writepage
= btrfs_writepage
,
3036 .writepages
= btrfs_writepages
,
3037 .readpages
= btrfs_readpages
,
3038 .sync_page
= block_sync_page
,
3040 .invalidatepage
= btrfs_invalidatepage
,
3041 .releasepage
= btrfs_releasepage
,
3042 .set_page_dirty
= __set_page_dirty_nobuffers
,
3045 static struct address_space_operations btrfs_symlink_aops
= {
3046 .readpage
= btrfs_readpage
,
3047 .writepage
= btrfs_writepage
,
3048 .invalidatepage
= btrfs_invalidatepage
,
3049 .releasepage
= btrfs_releasepage
,
3052 static struct inode_operations btrfs_file_inode_operations
= {
3053 .truncate
= btrfs_truncate
,
3054 .getattr
= btrfs_getattr
,
3055 .setattr
= btrfs_setattr
,
3056 .setxattr
= generic_setxattr
,
3057 .getxattr
= generic_getxattr
,
3058 .listxattr
= btrfs_listxattr
,
3059 .removexattr
= generic_removexattr
,
3060 .permission
= btrfs_permission
,
3062 static struct inode_operations btrfs_special_inode_operations
= {
3063 .getattr
= btrfs_getattr
,
3064 .setattr
= btrfs_setattr
,
3065 .permission
= btrfs_permission
,
3067 static struct inode_operations btrfs_symlink_inode_operations
= {
3068 .readlink
= generic_readlink
,
3069 .follow_link
= page_follow_link_light
,
3070 .put_link
= page_put_link
,
3071 .permission
= btrfs_permission
,