2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/buffer_head.h>
21 #include <linux/pagemap.h>
22 #include <linux/highmem.h>
23 #include <linux/time.h>
24 #include <linux/init.h>
25 #include <linux/string.h>
26 #include <linux/smp_lock.h>
27 #include <linux/backing-dev.h>
28 #include <linux/mpage.h>
29 #include <linux/swap.h>
30 #include <linux/writeback.h>
31 #include <linux/statfs.h>
32 #include <linux/compat.h>
33 #include <linux/bit_spinlock.h>
34 #include <linux/version.h>
37 #include "transaction.h"
38 #include "btrfs_inode.h"
40 #include "print-tree.h"
42 struct btrfs_iget_args
{
44 struct btrfs_root
*root
;
47 static struct inode_operations btrfs_dir_inode_operations
;
48 static struct inode_operations btrfs_symlink_inode_operations
;
49 static struct inode_operations btrfs_dir_ro_inode_operations
;
50 static struct inode_operations btrfs_special_inode_operations
;
51 static struct inode_operations btrfs_file_inode_operations
;
52 static struct address_space_operations btrfs_aops
;
53 static struct address_space_operations btrfs_symlink_aops
;
54 static struct file_operations btrfs_dir_file_operations
;
55 static struct extent_map_ops btrfs_extent_map_ops
;
57 static struct kmem_cache
*btrfs_inode_cachep
;
58 struct kmem_cache
*btrfs_trans_handle_cachep
;
59 struct kmem_cache
*btrfs_transaction_cachep
;
60 struct kmem_cache
*btrfs_bit_radix_cachep
;
61 struct kmem_cache
*btrfs_path_cachep
;
64 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
65 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
66 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
67 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
68 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
69 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
70 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
71 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
74 static int run_delalloc_range(struct inode
*inode
, u64 start
, u64 end
)
76 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
77 struct btrfs_trans_handle
*trans
;
82 u64 blocksize
= root
->sectorsize
;
84 mutex_lock(&root
->fs_info
->fs_mutex
);
85 trans
= btrfs_start_transaction(root
, 1);
86 btrfs_set_trans_block_group(trans
, inode
);
88 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
89 ret
= btrfs_drop_extents(trans
, root
, inode
,
90 start
, start
+ num_bytes
, start
, &alloc_hint
);
92 ret
= btrfs_alloc_extent(trans
, root
, inode
->i_ino
, num_bytes
, 0,
93 alloc_hint
, (u64
)-1, &ins
, 1);
98 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
99 start
, ins
.objectid
, ins
.offset
,
102 btrfs_end_transaction(trans
, root
);
103 mutex_unlock(&root
->fs_info
->fs_mutex
);
107 int btrfs_writepage_io_hook(struct page
*page
, u64 start
, u64 end
)
109 struct inode
*inode
= page
->mapping
->host
;
110 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
111 struct btrfs_trans_handle
*trans
;
114 u64 page_start
= page
->index
<< PAGE_CACHE_SHIFT
;
115 size_t offset
= start
- page_start
;
117 mutex_lock(&root
->fs_info
->fs_mutex
);
118 trans
= btrfs_start_transaction(root
, 1);
119 btrfs_set_trans_block_group(trans
, inode
);
121 btrfs_csum_file_block(trans
, root
, inode
, inode
->i_ino
,
122 start
, kaddr
+ offset
, end
- start
+ 1);
124 ret
= btrfs_end_transaction(trans
, root
);
126 mutex_unlock(&root
->fs_info
->fs_mutex
);
130 int btrfs_readpage_io_hook(struct page
*page
, u64 start
, u64 end
)
133 struct inode
*inode
= page
->mapping
->host
;
134 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
135 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
136 struct btrfs_csum_item
*item
;
137 struct btrfs_path
*path
= NULL
;
140 mutex_lock(&root
->fs_info
->fs_mutex
);
141 path
= btrfs_alloc_path();
142 item
= btrfs_lookup_csum(NULL
, root
, path
, inode
->i_ino
, start
, 0);
145 /* a csum that isn't present is a preallocated region. */
146 if (ret
== -ENOENT
|| ret
== -EFBIG
)
151 read_extent_buffer(path
->nodes
[0], &csum
, (unsigned long)item
,
153 set_state_private(em_tree
, start
, csum
);
156 btrfs_free_path(path
);
157 mutex_unlock(&root
->fs_info
->fs_mutex
);
161 int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
)
163 size_t offset
= start
- (page
->index
<< PAGE_CACHE_SHIFT
);
164 struct inode
*inode
= page
->mapping
->host
;
165 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
169 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
173 ret
= get_state_private(em_tree
, start
, &private);
174 local_irq_save(flags
);
175 kaddr
= kmap_atomic(page
, KM_IRQ0
);
179 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
180 btrfs_csum_final(csum
, (char *)&csum
);
181 if (csum
!= private) {
184 kunmap_atomic(kaddr
, KM_IRQ0
);
185 local_irq_restore(flags
);
189 printk("btrfs csum failed ino %lu off %llu\n",
190 page
->mapping
->host
->i_ino
, (unsigned long long)start
);
191 memset(kaddr
+ offset
, 1, end
- start
+ 1);
192 flush_dcache_page(page
);
193 kunmap_atomic(kaddr
, KM_IRQ0
);
194 local_irq_restore(flags
);
198 void btrfs_read_locked_inode(struct inode
*inode
)
200 struct btrfs_path
*path
;
201 struct extent_buffer
*leaf
;
202 struct btrfs_inode_item
*inode_item
;
203 struct btrfs_inode_timespec
*tspec
;
204 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
205 struct btrfs_key location
;
206 u64 alloc_group_block
;
210 path
= btrfs_alloc_path();
212 mutex_lock(&root
->fs_info
->fs_mutex
);
214 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
215 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
219 leaf
= path
->nodes
[0];
220 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
221 struct btrfs_inode_item
);
223 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
224 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
225 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
226 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
227 inode
->i_size
= btrfs_inode_size(leaf
, inode_item
);
229 tspec
= btrfs_inode_atime(inode_item
);
230 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
231 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
233 tspec
= btrfs_inode_mtime(inode_item
);
234 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
235 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
237 tspec
= btrfs_inode_ctime(inode_item
);
238 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
239 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
241 inode
->i_blocks
= btrfs_inode_nblocks(leaf
, inode_item
);
242 inode
->i_generation
= btrfs_inode_generation(leaf
, inode_item
);
244 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
246 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
247 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
250 btrfs_free_path(path
);
253 mutex_unlock(&root
->fs_info
->fs_mutex
);
255 switch (inode
->i_mode
& S_IFMT
) {
257 inode
->i_mapping
->a_ops
= &btrfs_aops
;
258 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
259 inode
->i_fop
= &btrfs_file_operations
;
260 inode
->i_op
= &btrfs_file_inode_operations
;
263 inode
->i_fop
= &btrfs_dir_file_operations
;
264 if (root
== root
->fs_info
->tree_root
)
265 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
267 inode
->i_op
= &btrfs_dir_inode_operations
;
270 inode
->i_op
= &btrfs_symlink_inode_operations
;
271 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
274 init_special_inode(inode
, inode
->i_mode
, rdev
);
280 btrfs_release_path(root
, path
);
281 btrfs_free_path(path
);
282 mutex_unlock(&root
->fs_info
->fs_mutex
);
283 make_bad_inode(inode
);
286 static void fill_inode_item(struct extent_buffer
*leaf
,
287 struct btrfs_inode_item
*item
,
290 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
291 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
292 btrfs_set_inode_size(leaf
, item
, inode
->i_size
);
293 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
294 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
296 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
297 inode
->i_atime
.tv_sec
);
298 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
299 inode
->i_atime
.tv_nsec
);
301 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
302 inode
->i_mtime
.tv_sec
);
303 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
304 inode
->i_mtime
.tv_nsec
);
306 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
307 inode
->i_ctime
.tv_sec
);
308 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
309 inode
->i_ctime
.tv_nsec
);
311 btrfs_set_inode_nblocks(leaf
, item
, inode
->i_blocks
);
312 btrfs_set_inode_generation(leaf
, item
, inode
->i_generation
);
313 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
314 btrfs_set_inode_block_group(leaf
, item
,
315 BTRFS_I(inode
)->block_group
->key
.objectid
);
318 int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
319 struct btrfs_root
*root
,
322 struct btrfs_inode_item
*inode_item
;
323 struct btrfs_path
*path
;
324 struct extent_buffer
*leaf
;
327 path
= btrfs_alloc_path();
329 ret
= btrfs_lookup_inode(trans
, root
, path
,
330 &BTRFS_I(inode
)->location
, 1);
337 leaf
= path
->nodes
[0];
338 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
339 struct btrfs_inode_item
);
341 fill_inode_item(leaf
, inode_item
, inode
);
342 btrfs_mark_buffer_dirty(leaf
);
343 btrfs_set_inode_last_trans(trans
, inode
);
346 btrfs_release_path(root
, path
);
347 btrfs_free_path(path
);
352 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
353 struct btrfs_root
*root
,
355 struct dentry
*dentry
)
357 struct btrfs_path
*path
;
358 const char *name
= dentry
->d_name
.name
;
359 int name_len
= dentry
->d_name
.len
;
361 struct extent_buffer
*leaf
;
362 struct btrfs_dir_item
*di
;
363 struct btrfs_key key
;
365 path
= btrfs_alloc_path();
371 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
381 leaf
= path
->nodes
[0];
382 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
383 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
386 btrfs_release_path(root
, path
);
388 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
389 key
.objectid
, name
, name_len
, -1);
398 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
400 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
402 btrfs_free_path(path
);
404 dir
->i_size
-= name_len
* 2;
405 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
406 btrfs_update_inode(trans
, root
, dir
);
407 drop_nlink(dentry
->d_inode
);
408 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
409 dir
->i_sb
->s_dirt
= 1;
414 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
416 struct btrfs_root
*root
;
417 struct btrfs_trans_handle
*trans
;
421 root
= BTRFS_I(dir
)->root
;
422 mutex_lock(&root
->fs_info
->fs_mutex
);
423 trans
= btrfs_start_transaction(root
, 1);
425 btrfs_set_trans_block_group(trans
, dir
);
426 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
427 nr
= trans
->blocks_used
;
429 btrfs_end_transaction(trans
, root
);
430 mutex_unlock(&root
->fs_info
->fs_mutex
);
431 btrfs_btree_balance_dirty(root
, nr
);
436 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
438 struct inode
*inode
= dentry
->d_inode
;
441 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
442 struct btrfs_path
*path
;
443 struct btrfs_key key
;
444 struct btrfs_trans_handle
*trans
;
445 struct btrfs_key found_key
;
447 struct extent_buffer
*leaf
;
448 char *goodnames
= "..";
451 path
= btrfs_alloc_path();
453 mutex_lock(&root
->fs_info
->fs_mutex
);
454 trans
= btrfs_start_transaction(root
, 1);
456 btrfs_set_trans_block_group(trans
, dir
);
457 key
.objectid
= inode
->i_ino
;
458 key
.offset
= (u64
)-1;
461 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
467 if (path
->slots
[0] == 0) {
472 leaf
= path
->nodes
[0];
473 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
474 found_type
= btrfs_key_type(&found_key
);
475 if (found_key
.objectid
!= inode
->i_ino
) {
479 if ((found_type
!= BTRFS_DIR_ITEM_KEY
&&
480 found_type
!= BTRFS_DIR_INDEX_KEY
) ||
481 (!btrfs_match_dir_item_name(root
, path
, goodnames
, 2) &&
482 !btrfs_match_dir_item_name(root
, path
, goodnames
, 1))) {
486 ret
= btrfs_del_item(trans
, root
, path
);
489 if (found_type
== BTRFS_DIR_ITEM_KEY
&& found_key
.offset
== 1)
491 btrfs_release_path(root
, path
);
494 btrfs_release_path(root
, path
);
496 /* now the directory is empty */
497 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
502 btrfs_release_path(root
, path
);
503 btrfs_free_path(path
);
504 mutex_unlock(&root
->fs_info
->fs_mutex
);
505 nr
= trans
->blocks_used
;
506 ret
= btrfs_end_transaction(trans
, root
);
507 btrfs_btree_balance_dirty(root
, nr
);
513 static int btrfs_free_inode(struct btrfs_trans_handle
*trans
,
514 struct btrfs_root
*root
,
517 struct btrfs_path
*path
;
522 path
= btrfs_alloc_path();
524 ret
= btrfs_lookup_inode(trans
, root
, path
,
525 &BTRFS_I(inode
)->location
, -1);
529 ret
= btrfs_del_item(trans
, root
, path
);
530 btrfs_free_path(path
);
535 * this can truncate away extent items, csum items and directory items.
536 * It starts at a high offset and removes keys until it can't find
537 * any higher than i_size.
539 * csum items that cross the new i_size are truncated to the new size
542 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
543 struct btrfs_root
*root
,
547 struct btrfs_path
*path
;
548 struct btrfs_key key
;
549 struct btrfs_key found_key
;
551 struct extent_buffer
*leaf
;
552 struct btrfs_file_extent_item
*fi
;
553 u64 extent_start
= 0;
554 u64 extent_num_bytes
= 0;
559 btrfs_drop_extent_cache(inode
, inode
->i_size
, (u64
)-1);
560 path
= btrfs_alloc_path();
564 /* FIXME, add redo link to tree so we don't leak on crash */
565 key
.objectid
= inode
->i_ino
;
566 key
.offset
= (u64
)-1;
570 btrfs_init_path(path
);
572 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
577 BUG_ON(path
->slots
[0] == 0);
580 leaf
= path
->nodes
[0];
581 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
582 found_type
= btrfs_key_type(&found_key
);
584 if (found_key
.objectid
!= inode
->i_ino
)
587 if (found_type
!= BTRFS_CSUM_ITEM_KEY
&&
588 found_type
!= BTRFS_DIR_ITEM_KEY
&&
589 found_type
!= BTRFS_DIR_INDEX_KEY
&&
590 found_type
!= BTRFS_EXTENT_DATA_KEY
)
593 item_end
= found_key
.offset
;
594 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
595 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
596 struct btrfs_file_extent_item
);
597 if (btrfs_file_extent_type(leaf
, fi
) !=
598 BTRFS_FILE_EXTENT_INLINE
) {
600 btrfs_file_extent_num_bytes(leaf
, fi
);
603 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
604 ret
= btrfs_csum_truncate(trans
, root
, path
,
608 if (item_end
< inode
->i_size
) {
609 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
610 found_type
= BTRFS_INODE_ITEM_KEY
;
611 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
612 found_type
= BTRFS_CSUM_ITEM_KEY
;
613 } else if (found_type
) {
618 btrfs_set_key_type(&key
, found_type
);
621 if (found_key
.offset
>= inode
->i_size
)
627 /* FIXME, shrink the extent if the ref count is only 1 */
628 if (found_type
== BTRFS_EXTENT_DATA_KEY
&&
629 btrfs_file_extent_type(leaf
, fi
) !=
630 BTRFS_FILE_EXTENT_INLINE
) {
632 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
635 btrfs_file_extent_num_bytes(leaf
, fi
);
636 extent_num_bytes
= inode
->i_size
-
637 found_key
.offset
+ root
->sectorsize
- 1;
638 btrfs_set_file_extent_num_bytes(leaf
, fi
,
640 num_dec
= (orig_num_bytes
-
641 extent_num_bytes
) >> 9;
642 if (extent_start
!= 0) {
643 inode
->i_blocks
-= num_dec
;
645 btrfs_mark_buffer_dirty(leaf
);
648 btrfs_file_extent_disk_num_bytes(leaf
,
650 /* FIXME blocksize != 4096 */
651 num_dec
= btrfs_file_extent_num_bytes(leaf
,
653 if (extent_start
!= 0) {
655 inode
->i_blocks
-= num_dec
;
660 ret
= btrfs_del_item(trans
, root
, path
);
666 btrfs_release_path(root
, path
);
668 ret
= btrfs_free_extent(trans
, root
, extent_start
,
669 extent_num_bytes
, 0);
675 btrfs_release_path(root
, path
);
676 btrfs_free_path(path
);
677 inode
->i_sb
->s_dirt
= 1;
681 static int btrfs_cow_one_page(struct inode
*inode
, struct page
*page
,
686 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
687 u64 page_start
= page
->index
<< PAGE_CACHE_SHIFT
;
688 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
690 set_page_extent_mapped(page
);
692 lock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
693 set_extent_delalloc(&BTRFS_I(inode
)->extent_tree
, page_start
,
695 if (zero_start
!= PAGE_CACHE_SIZE
) {
697 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
698 flush_dcache_page(page
);
701 set_page_dirty(page
);
702 unlock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
708 * taken from block_truncate_page, but does cow as it zeros out
709 * any bytes left in the last page in the file.
711 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
713 struct inode
*inode
= mapping
->host
;
714 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
715 u32 blocksize
= root
->sectorsize
;
716 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
717 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
722 if ((offset
& (blocksize
- 1)) == 0)
725 down_read(&root
->snap_sem
);
727 page
= grab_cache_page(mapping
, index
);
730 if (!PageUptodate(page
)) {
731 ret
= btrfs_readpage(NULL
, page
);
733 if (!PageUptodate(page
)) {
738 page_start
= page
->index
<< PAGE_CACHE_SHIFT
;
740 ret
= btrfs_cow_one_page(inode
, page
, offset
);
743 page_cache_release(page
);
744 up_read(&BTRFS_I(inode
)->root
->snap_sem
);
749 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
751 struct inode
*inode
= dentry
->d_inode
;
754 err
= inode_change_ok(inode
, attr
);
758 if (S_ISREG(inode
->i_mode
) &&
759 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
760 struct btrfs_trans_handle
*trans
;
761 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
762 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
764 u64 mask
= root
->sectorsize
- 1;
765 u64 pos
= (inode
->i_size
+ mask
) & ~mask
;
766 u64 block_end
= attr
->ia_size
| mask
;
770 if (attr
->ia_size
<= pos
)
773 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
775 lock_extent(em_tree
, pos
, block_end
, GFP_NOFS
);
776 hole_size
= (attr
->ia_size
- pos
+ mask
) & ~mask
;
778 mutex_lock(&root
->fs_info
->fs_mutex
);
779 trans
= btrfs_start_transaction(root
, 1);
780 btrfs_set_trans_block_group(trans
, inode
);
781 err
= btrfs_drop_extents(trans
, root
, inode
,
782 pos
, pos
+ hole_size
, pos
,
785 err
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
786 pos
, 0, 0, hole_size
);
787 btrfs_end_transaction(trans
, root
);
788 mutex_unlock(&root
->fs_info
->fs_mutex
);
789 unlock_extent(em_tree
, pos
, block_end
, GFP_NOFS
);
794 err
= inode_setattr(inode
, attr
);
798 void btrfs_delete_inode(struct inode
*inode
)
800 struct btrfs_trans_handle
*trans
;
801 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
805 truncate_inode_pages(&inode
->i_data
, 0);
806 if (is_bad_inode(inode
)) {
811 mutex_lock(&root
->fs_info
->fs_mutex
);
812 trans
= btrfs_start_transaction(root
, 1);
814 btrfs_set_trans_block_group(trans
, inode
);
815 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
818 ret
= btrfs_free_inode(trans
, root
, inode
);
821 nr
= trans
->blocks_used
;
823 btrfs_end_transaction(trans
, root
);
824 mutex_unlock(&root
->fs_info
->fs_mutex
);
825 btrfs_btree_balance_dirty(root
, nr
);
829 nr
= trans
->blocks_used
;
830 btrfs_end_transaction(trans
, root
);
831 mutex_unlock(&root
->fs_info
->fs_mutex
);
832 btrfs_btree_balance_dirty(root
, nr
);
838 * this returns the key found in the dir entry in the location pointer.
839 * If no dir entries were found, location->objectid is 0.
841 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
842 struct btrfs_key
*location
)
844 const char *name
= dentry
->d_name
.name
;
845 int namelen
= dentry
->d_name
.len
;
846 struct btrfs_dir_item
*di
;
847 struct btrfs_path
*path
;
848 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
851 path
= btrfs_alloc_path();
853 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
855 if (!di
|| IS_ERR(di
)) {
856 location
->objectid
= 0;
860 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
862 btrfs_release_path(root
, path
);
863 btrfs_free_path(path
);
868 * when we hit a tree root in a directory, the btrfs part of the inode
869 * needs to be changed to reflect the root directory of the tree root. This
870 * is kind of like crossing a mount point.
872 static int fixup_tree_root_location(struct btrfs_root
*root
,
873 struct btrfs_key
*location
,
874 struct btrfs_root
**sub_root
,
875 struct dentry
*dentry
)
877 struct btrfs_path
*path
;
878 struct btrfs_root_item
*ri
;
880 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
882 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
885 path
= btrfs_alloc_path();
887 mutex_lock(&root
->fs_info
->fs_mutex
);
889 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
892 if (IS_ERR(*sub_root
))
893 return PTR_ERR(*sub_root
);
895 ri
= &(*sub_root
)->root_item
;
896 location
->objectid
= btrfs_root_dirid(ri
);
897 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
898 location
->offset
= 0;
900 btrfs_free_path(path
);
901 mutex_unlock(&root
->fs_info
->fs_mutex
);
905 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
907 struct btrfs_iget_args
*args
= p
;
908 inode
->i_ino
= args
->ino
;
909 BTRFS_I(inode
)->root
= args
->root
;
910 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
911 inode
->i_mapping
, GFP_NOFS
);
915 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
917 struct btrfs_iget_args
*args
= opaque
;
918 return (args
->ino
== inode
->i_ino
&&
919 args
->root
== BTRFS_I(inode
)->root
);
922 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
923 struct btrfs_root
*root
)
926 struct btrfs_iget_args args
;
930 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
931 btrfs_init_locked_inode
,
936 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
937 struct nameidata
*nd
)
939 struct inode
* inode
;
940 struct btrfs_inode
*bi
= BTRFS_I(dir
);
941 struct btrfs_root
*root
= bi
->root
;
942 struct btrfs_root
*sub_root
= root
;
943 struct btrfs_key location
;
946 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
947 return ERR_PTR(-ENAMETOOLONG
);
949 mutex_lock(&root
->fs_info
->fs_mutex
);
950 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
951 mutex_unlock(&root
->fs_info
->fs_mutex
);
957 if (location
.objectid
) {
958 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
963 return ERR_PTR(-ENOENT
);
964 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
967 return ERR_PTR(-EACCES
);
968 if (inode
->i_state
& I_NEW
) {
969 /* the inode and parent dir are two different roots */
970 if (sub_root
!= root
) {
972 sub_root
->inode
= inode
;
974 BTRFS_I(inode
)->root
= sub_root
;
975 memcpy(&BTRFS_I(inode
)->location
, &location
,
977 btrfs_read_locked_inode(inode
);
978 unlock_new_inode(inode
);
981 return d_splice_alias(inode
, dentry
);
984 static unsigned char btrfs_filetype_table
[] = {
985 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
988 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
990 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
991 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
992 struct btrfs_item
*item
;
993 struct btrfs_dir_item
*di
;
994 struct btrfs_key key
;
995 struct btrfs_key found_key
;
996 struct btrfs_path
*path
;
999 struct extent_buffer
*leaf
;
1002 unsigned char d_type
;
1007 int key_type
= BTRFS_DIR_INDEX_KEY
;
1012 /* FIXME, use a real flag for deciding about the key type */
1013 if (root
->fs_info
->tree_root
== root
)
1014 key_type
= BTRFS_DIR_ITEM_KEY
;
1016 mutex_lock(&root
->fs_info
->fs_mutex
);
1017 key
.objectid
= inode
->i_ino
;
1018 btrfs_set_key_type(&key
, key_type
);
1019 key
.offset
= filp
->f_pos
;
1021 path
= btrfs_alloc_path();
1023 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1028 leaf
= path
->nodes
[0];
1029 nritems
= btrfs_header_nritems(leaf
);
1030 slot
= path
->slots
[0];
1031 if (advance
|| slot
>= nritems
) {
1032 if (slot
>= nritems
-1) {
1033 ret
= btrfs_next_leaf(root
, path
);
1036 leaf
= path
->nodes
[0];
1037 nritems
= btrfs_header_nritems(leaf
);
1038 slot
= path
->slots
[0];
1045 item
= btrfs_item_nr(leaf
, slot
);
1046 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1048 if (found_key
.objectid
!= key
.objectid
)
1050 if (btrfs_key_type(&found_key
) != key_type
)
1052 if (found_key
.offset
< filp
->f_pos
)
1055 filp
->f_pos
= found_key
.offset
;
1057 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
1059 di_total
= btrfs_item_size(leaf
, item
);
1060 while(di_cur
< di_total
) {
1061 struct btrfs_key location
;
1063 name_len
= btrfs_dir_name_len(leaf
, di
);
1064 if (name_len
< 32) {
1065 name_ptr
= tmp_name
;
1067 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
1070 read_extent_buffer(leaf
, name_ptr
,
1071 (unsigned long)(di
+ 1), name_len
);
1073 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
1074 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
1076 over
= filldir(dirent
, name_ptr
, name_len
,
1081 if (name_ptr
!= tmp_name
)
1086 di_len
= btrfs_dir_name_len(leaf
, di
) + sizeof(*di
);
1088 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
1095 btrfs_release_path(root
, path
);
1096 btrfs_free_path(path
);
1097 mutex_unlock(&root
->fs_info
->fs_mutex
);
1101 int btrfs_write_inode(struct inode
*inode
, int wait
)
1103 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1104 struct btrfs_trans_handle
*trans
;
1108 mutex_lock(&root
->fs_info
->fs_mutex
);
1109 trans
= btrfs_start_transaction(root
, 1);
1110 btrfs_set_trans_block_group(trans
, inode
);
1111 ret
= btrfs_commit_transaction(trans
, root
);
1112 mutex_unlock(&root
->fs_info
->fs_mutex
);
1118 * This is somewhat expensive, updating the tree every time the
1119 * inode changes. But, it is most likely to find the inode in cache.
1120 * FIXME, needs more benchmarking...there are no reasons other than performance
1121 * to keep or drop this code.
1123 void btrfs_dirty_inode(struct inode
*inode
)
1125 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1126 struct btrfs_trans_handle
*trans
;
1128 mutex_lock(&root
->fs_info
->fs_mutex
);
1129 trans
= btrfs_start_transaction(root
, 1);
1130 btrfs_set_trans_block_group(trans
, inode
);
1131 btrfs_update_inode(trans
, root
, inode
);
1132 btrfs_end_transaction(trans
, root
);
1133 mutex_unlock(&root
->fs_info
->fs_mutex
);
1136 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
1137 struct btrfs_root
*root
,
1139 struct btrfs_block_group_cache
*group
,
1142 struct inode
*inode
;
1143 struct btrfs_inode_item
*inode_item
;
1144 struct btrfs_key
*location
;
1145 struct btrfs_path
*path
;
1149 path
= btrfs_alloc_path();
1152 inode
= new_inode(root
->fs_info
->sb
);
1154 return ERR_PTR(-ENOMEM
);
1156 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1157 inode
->i_mapping
, GFP_NOFS
);
1158 BTRFS_I(inode
)->root
= root
;
1164 group
= btrfs_find_block_group(root
, group
, 0, 0, owner
);
1165 BTRFS_I(inode
)->block_group
= group
;
1167 ret
= btrfs_insert_empty_inode(trans
, root
, path
, objectid
);
1171 inode
->i_uid
= current
->fsuid
;
1172 inode
->i_gid
= current
->fsgid
;
1173 inode
->i_mode
= mode
;
1174 inode
->i_ino
= objectid
;
1175 inode
->i_blocks
= 0;
1176 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1177 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1178 struct btrfs_inode_item
);
1179 fill_inode_item(path
->nodes
[0], inode_item
, inode
);
1180 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1181 btrfs_free_path(path
);
1183 location
= &BTRFS_I(inode
)->location
;
1184 location
->objectid
= objectid
;
1185 location
->offset
= 0;
1186 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1188 insert_inode_hash(inode
);
1191 btrfs_free_path(path
);
1192 return ERR_PTR(ret
);
1195 static inline u8
btrfs_inode_type(struct inode
*inode
)
1197 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
1200 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
1201 struct dentry
*dentry
, struct inode
*inode
)
1204 struct btrfs_key key
;
1205 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
1206 struct inode
*parent_inode
;
1208 key
.objectid
= inode
->i_ino
;
1209 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1212 ret
= btrfs_insert_dir_item(trans
, root
,
1213 dentry
->d_name
.name
, dentry
->d_name
.len
,
1214 dentry
->d_parent
->d_inode
->i_ino
,
1215 &key
, btrfs_inode_type(inode
));
1217 parent_inode
= dentry
->d_parent
->d_inode
;
1218 parent_inode
->i_size
+= dentry
->d_name
.len
* 2;
1219 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
1220 ret
= btrfs_update_inode(trans
, root
,
1221 dentry
->d_parent
->d_inode
);
1226 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
1227 struct dentry
*dentry
, struct inode
*inode
)
1229 int err
= btrfs_add_link(trans
, dentry
, inode
);
1231 d_instantiate(dentry
, inode
);
1239 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1240 int mode
, dev_t rdev
)
1242 struct btrfs_trans_handle
*trans
;
1243 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1244 struct inode
*inode
;
1250 if (!new_valid_dev(rdev
))
1253 mutex_lock(&root
->fs_info
->fs_mutex
);
1254 trans
= btrfs_start_transaction(root
, 1);
1255 btrfs_set_trans_block_group(trans
, dir
);
1257 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1263 inode
= btrfs_new_inode(trans
, root
, objectid
,
1264 BTRFS_I(dir
)->block_group
, mode
);
1265 err
= PTR_ERR(inode
);
1269 btrfs_set_trans_block_group(trans
, inode
);
1270 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1274 inode
->i_op
= &btrfs_special_inode_operations
;
1275 init_special_inode(inode
, inode
->i_mode
, rdev
);
1276 btrfs_update_inode(trans
, root
, inode
);
1278 dir
->i_sb
->s_dirt
= 1;
1279 btrfs_update_inode_block_group(trans
, inode
);
1280 btrfs_update_inode_block_group(trans
, dir
);
1282 nr
= trans
->blocks_used
;
1283 btrfs_end_transaction(trans
, root
);
1284 mutex_unlock(&root
->fs_info
->fs_mutex
);
1287 inode_dec_link_count(inode
);
1290 btrfs_btree_balance_dirty(root
, nr
);
1294 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
1295 int mode
, struct nameidata
*nd
)
1297 struct btrfs_trans_handle
*trans
;
1298 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1299 struct inode
*inode
;
1305 mutex_lock(&root
->fs_info
->fs_mutex
);
1306 trans
= btrfs_start_transaction(root
, 1);
1307 btrfs_set_trans_block_group(trans
, dir
);
1309 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1315 inode
= btrfs_new_inode(trans
, root
, objectid
,
1316 BTRFS_I(dir
)->block_group
, mode
);
1317 err
= PTR_ERR(inode
);
1321 btrfs_set_trans_block_group(trans
, inode
);
1322 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1326 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1327 inode
->i_fop
= &btrfs_file_operations
;
1328 inode
->i_op
= &btrfs_file_inode_operations
;
1329 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1330 inode
->i_mapping
, GFP_NOFS
);
1331 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
1333 dir
->i_sb
->s_dirt
= 1;
1334 btrfs_update_inode_block_group(trans
, inode
);
1335 btrfs_update_inode_block_group(trans
, dir
);
1337 nr
= trans
->blocks_used
;
1338 btrfs_end_transaction(trans
, root
);
1339 mutex_unlock(&root
->fs_info
->fs_mutex
);
1342 inode_dec_link_count(inode
);
1345 btrfs_btree_balance_dirty(root
, nr
);
1349 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
1350 struct dentry
*dentry
)
1352 struct btrfs_trans_handle
*trans
;
1353 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1354 struct inode
*inode
= old_dentry
->d_inode
;
1359 if (inode
->i_nlink
== 0)
1363 mutex_lock(&root
->fs_info
->fs_mutex
);
1364 trans
= btrfs_start_transaction(root
, 1);
1366 btrfs_set_trans_block_group(trans
, dir
);
1367 atomic_inc(&inode
->i_count
);
1368 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1373 dir
->i_sb
->s_dirt
= 1;
1374 btrfs_update_inode_block_group(trans
, dir
);
1375 err
= btrfs_update_inode(trans
, root
, inode
);
1380 nr
= trans
->blocks_used
;
1381 btrfs_end_transaction(trans
, root
);
1382 mutex_unlock(&root
->fs_info
->fs_mutex
);
1385 inode_dec_link_count(inode
);
1388 btrfs_btree_balance_dirty(root
, nr
);
1392 static int btrfs_make_empty_dir(struct btrfs_trans_handle
*trans
,
1393 struct btrfs_root
*root
,
1394 u64 objectid
, u64 dirid
)
1398 struct btrfs_key key
;
1403 key
.objectid
= objectid
;
1405 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1407 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 1, objectid
,
1408 &key
, BTRFS_FT_DIR
);
1412 key
.objectid
= dirid
;
1413 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 2, objectid
,
1414 &key
, BTRFS_FT_DIR
);
1421 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1423 struct inode
*inode
;
1424 struct btrfs_trans_handle
*trans
;
1425 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1427 int drop_on_err
= 0;
1429 unsigned long nr
= 1;
1431 mutex_lock(&root
->fs_info
->fs_mutex
);
1432 trans
= btrfs_start_transaction(root
, 1);
1433 btrfs_set_trans_block_group(trans
, dir
);
1435 if (IS_ERR(trans
)) {
1436 err
= PTR_ERR(trans
);
1440 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1446 inode
= btrfs_new_inode(trans
, root
, objectid
,
1447 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
1448 if (IS_ERR(inode
)) {
1449 err
= PTR_ERR(inode
);
1454 inode
->i_op
= &btrfs_dir_inode_operations
;
1455 inode
->i_fop
= &btrfs_dir_file_operations
;
1456 btrfs_set_trans_block_group(trans
, inode
);
1458 err
= btrfs_make_empty_dir(trans
, root
, inode
->i_ino
, dir
->i_ino
);
1463 err
= btrfs_update_inode(trans
, root
, inode
);
1467 err
= btrfs_add_link(trans
, dentry
, inode
);
1471 d_instantiate(dentry
, inode
);
1473 dir
->i_sb
->s_dirt
= 1;
1474 btrfs_update_inode_block_group(trans
, inode
);
1475 btrfs_update_inode_block_group(trans
, dir
);
1478 nr
= trans
->blocks_used
;
1479 btrfs_end_transaction(trans
, root
);
1482 mutex_unlock(&root
->fs_info
->fs_mutex
);
1485 btrfs_btree_balance_dirty(root
, nr
);
1489 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
1490 size_t page_offset
, u64 start
, u64 end
,
1496 u64 extent_start
= 0;
1498 u64 objectid
= inode
->i_ino
;
1500 int failed_insert
= 0;
1501 struct btrfs_path
*path
;
1502 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1503 struct btrfs_file_extent_item
*item
;
1504 struct extent_buffer
*leaf
;
1505 struct btrfs_key found_key
;
1506 struct extent_map
*em
= NULL
;
1507 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1508 struct btrfs_trans_handle
*trans
= NULL
;
1510 path
= btrfs_alloc_path();
1512 mutex_lock(&root
->fs_info
->fs_mutex
);
1515 em
= lookup_extent_mapping(em_tree
, start
, end
);
1520 em
= alloc_extent_map(GFP_NOFS
);
1525 em
->start
= EXTENT_MAP_HOLE
;
1526 em
->end
= EXTENT_MAP_HOLE
;
1528 em
->bdev
= inode
->i_sb
->s_bdev
;
1529 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
1530 objectid
, start
, 0);
1537 if (path
->slots
[0] == 0)
1542 leaf
= path
->nodes
[0];
1543 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1544 struct btrfs_file_extent_item
);
1545 /* are we inside the extent that was found? */
1546 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1547 found_type
= btrfs_key_type(&found_key
);
1548 if (found_key
.objectid
!= objectid
||
1549 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
1553 found_type
= btrfs_file_extent_type(leaf
, item
);
1554 extent_start
= found_key
.offset
;
1555 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1556 extent_end
= extent_start
+
1557 btrfs_file_extent_num_bytes(leaf
, item
);
1559 if (start
< extent_start
|| start
>= extent_end
) {
1561 if (start
< extent_start
) {
1562 if (end
< extent_start
)
1564 em
->end
= extent_end
- 1;
1570 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
1572 em
->start
= extent_start
;
1573 em
->end
= extent_end
- 1;
1574 em
->block_start
= EXTENT_MAP_HOLE
;
1575 em
->block_end
= EXTENT_MAP_HOLE
;
1578 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
1579 em
->block_start
= bytenr
;
1580 em
->block_end
= em
->block_start
+
1581 btrfs_file_extent_num_bytes(leaf
, item
) - 1;
1582 em
->start
= extent_start
;
1583 em
->end
= extent_end
- 1;
1585 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1589 size_t extent_offset
;
1592 size
= btrfs_file_extent_inline_len(leaf
, btrfs_item_nr(leaf
,
1595 extent_end
= (extent_start
+ size
) |
1596 ((u64
)root
->sectorsize
- 1);
1597 if (start
< extent_start
|| start
>= extent_end
) {
1599 if (start
< extent_start
) {
1600 if (end
< extent_start
)
1602 em
->end
= extent_end
;
1609 extent_offset
= (page
->index
<< PAGE_CACHE_SHIFT
) -
1611 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
1613 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- page_offset
,
1614 size
- extent_offset
);
1616 em
->block_start
= EXTENT_MAP_INLINE
;
1617 em
->block_end
= EXTENT_MAP_INLINE
;
1618 em
->start
= extent_start
+ extent_offset
;
1619 em
->end
= (em
->start
+ copy_size
-1) |
1620 ((u64
)root
->sectorsize
-1);
1626 read_extent_buffer(leaf
, map
+ page_offset
, ptr
, copy_size
);
1628 memset(map + page_offset + copy_size, 0,
1629 PAGE_CACHE_SIZE - copy_size - page_offset);
1631 flush_dcache_page(page
);
1633 set_extent_uptodate(em_tree
, em
->start
, em
->end
, GFP_NOFS
);
1636 printk("unkknown found_type %d\n", found_type
);
1643 em
->block_start
= EXTENT_MAP_HOLE
;
1644 em
->block_end
= EXTENT_MAP_HOLE
;
1646 btrfs_release_path(root
, path
);
1647 if (em
->start
> start
|| em
->end
< start
) {
1648 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->end
, start
, end
);
1652 ret
= add_extent_mapping(em_tree
, em
);
1653 if (ret
== -EEXIST
) {
1654 free_extent_map(em
);
1657 if (failed_insert
> 5) {
1658 printk("failing to insert %Lu %Lu\n", start
, end
);
1666 btrfs_free_path(path
);
1668 ret
= btrfs_end_transaction(trans
, root
);
1672 mutex_unlock(&root
->fs_info
->fs_mutex
);
1674 free_extent_map(em
);
1676 return ERR_PTR(err
);
1681 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
1683 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
1686 static int btrfs_prepare_write(struct file
*file
, struct page
*page
,
1687 unsigned from
, unsigned to
)
1689 return extent_prepare_write(&BTRFS_I(page
->mapping
->host
)->extent_tree
,
1690 page
->mapping
->host
, page
, from
, to
,
1694 int btrfs_readpage(struct file
*file
, struct page
*page
)
1696 struct extent_map_tree
*tree
;
1697 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1698 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
1700 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
1702 struct extent_map_tree
*tree
;
1705 if (current
->flags
& PF_MEMALLOC
) {
1706 redirty_page_for_writepage(wbc
, page
);
1710 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1711 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
1714 static int btrfs_releasepage(struct page
*page
, gfp_t unused_gfp_flags
)
1716 struct extent_map_tree
*tree
;
1719 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1720 ret
= try_release_extent_mapping(tree
, page
);
1722 ClearPagePrivate(page
);
1723 set_page_private(page
, 0);
1724 page_cache_release(page
);
1729 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
1731 struct extent_map_tree
*tree
;
1733 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1734 extent_invalidatepage(tree
, page
, offset
);
1735 btrfs_releasepage(page
, GFP_NOFS
);
1739 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1740 * called from a page fault handler when a page is first dirtied. Hence we must
1741 * be careful to check for EOF conditions here. We set the page up correctly
1742 * for a written page which means we get ENOSPC checking when writing into
1743 * holes and correct delalloc and unwritten extent mapping on filesystems that
1744 * support these features.
1746 * We are not allowed to take the i_mutex here so we have to play games to
1747 * protect against truncate races as the page could now be beyond EOF. Because
1748 * vmtruncate() writes the inode size before removing pages, once we have the
1749 * page lock we can determine safely if the page is beyond EOF. If it is not
1750 * beyond EOF, then the page is guaranteed safe against truncation until we
1753 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
1755 struct inode
*inode
= vma
->vm_file
->f_path
.dentry
->d_inode
;
1761 down_read(&BTRFS_I(inode
)->root
->snap_sem
);
1763 wait_on_page_writeback(page
);
1764 size
= i_size_read(inode
);
1765 page_start
= page
->index
<< PAGE_CACHE_SHIFT
;
1767 if ((page
->mapping
!= inode
->i_mapping
) ||
1768 (page_start
> size
)) {
1769 /* page got truncated out from underneath us */
1773 /* page is wholly or partially inside EOF */
1774 if (page_start
+ PAGE_CACHE_SIZE
> size
)
1775 end
= size
& ~PAGE_CACHE_MASK
;
1777 end
= PAGE_CACHE_SIZE
;
1779 ret
= btrfs_cow_one_page(inode
, page
, end
);
1782 up_read(&BTRFS_I(inode
)->root
->snap_sem
);
1787 static void btrfs_truncate(struct inode
*inode
)
1789 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1791 struct btrfs_trans_handle
*trans
;
1794 if (!S_ISREG(inode
->i_mode
))
1796 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1799 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1801 mutex_lock(&root
->fs_info
->fs_mutex
);
1802 trans
= btrfs_start_transaction(root
, 1);
1803 btrfs_set_trans_block_group(trans
, inode
);
1805 /* FIXME, add redo link to tree so we don't leak on crash */
1806 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
1807 btrfs_update_inode(trans
, root
, inode
);
1808 nr
= trans
->blocks_used
;
1810 ret
= btrfs_end_transaction(trans
, root
);
1812 mutex_unlock(&root
->fs_info
->fs_mutex
);
1813 btrfs_btree_balance_dirty(root
, nr
);
1816 int btrfs_commit_write(struct file
*file
, struct page
*page
,
1817 unsigned from
, unsigned to
)
1819 return extent_commit_write(&BTRFS_I(page
->mapping
->host
)->extent_tree
,
1820 page
->mapping
->host
, page
, from
, to
);
1823 static int create_subvol(struct btrfs_root
*root
, char *name
, int namelen
)
1825 struct btrfs_trans_handle
*trans
;
1826 struct btrfs_key key
;
1827 struct btrfs_root_item root_item
;
1828 struct btrfs_inode_item
*inode_item
;
1829 struct extent_buffer
*leaf
;
1830 struct btrfs_root
*new_root
;
1831 struct inode
*inode
;
1836 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
1837 unsigned long nr
= 1;
1839 mutex_lock(&root
->fs_info
->fs_mutex
);
1840 trans
= btrfs_start_transaction(root
, 1);
1843 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
, 0, 0);
1845 return PTR_ERR(leaf
);
1847 btrfs_set_header_nritems(leaf
, 0);
1848 btrfs_set_header_level(leaf
, 0);
1849 btrfs_set_header_bytenr(leaf
, leaf
->start
);
1850 btrfs_set_header_generation(leaf
, trans
->transid
);
1851 btrfs_set_header_owner(leaf
, root
->root_key
.objectid
);
1852 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
1853 (unsigned long)btrfs_header_fsid(leaf
),
1855 btrfs_mark_buffer_dirty(leaf
);
1857 inode_item
= &root_item
.inode
;
1858 memset(inode_item
, 0, sizeof(*inode_item
));
1859 inode_item
->generation
= cpu_to_le64(1);
1860 inode_item
->size
= cpu_to_le64(3);
1861 inode_item
->nlink
= cpu_to_le32(1);
1862 inode_item
->nblocks
= cpu_to_le64(1);
1863 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
1865 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
1866 btrfs_set_root_level(&root_item
, 0);
1867 btrfs_set_root_refs(&root_item
, 1);
1868 btrfs_set_root_used(&root_item
, 0);
1870 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
1871 root_item
.drop_level
= 0;
1873 free_extent_buffer(leaf
);
1876 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
1881 btrfs_set_root_dirid(&root_item
, new_dirid
);
1883 key
.objectid
= objectid
;
1885 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
1886 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
1892 * insert the directory item
1894 key
.offset
= (u64
)-1;
1895 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
1896 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
1897 name
, namelen
, dir
->i_ino
, &key
,
1902 ret
= btrfs_commit_transaction(trans
, root
);
1906 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
, name
, namelen
);
1909 trans
= btrfs_start_transaction(new_root
, 1);
1912 inode
= btrfs_new_inode(trans
, new_root
, new_dirid
,
1913 BTRFS_I(dir
)->block_group
, S_IFDIR
| 0700);
1916 inode
->i_op
= &btrfs_dir_inode_operations
;
1917 inode
->i_fop
= &btrfs_dir_file_operations
;
1918 new_root
->inode
= inode
;
1920 ret
= btrfs_make_empty_dir(trans
, new_root
, new_dirid
, new_dirid
);
1926 ret
= btrfs_update_inode(trans
, new_root
, inode
);
1930 nr
= trans
->blocks_used
;
1931 err
= btrfs_commit_transaction(trans
, root
);
1935 mutex_unlock(&root
->fs_info
->fs_mutex
);
1936 btrfs_btree_balance_dirty(root
, nr
);
1940 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
1942 struct btrfs_trans_handle
*trans
;
1943 struct btrfs_key key
;
1944 struct btrfs_root_item new_root_item
;
1945 struct extent_buffer
*tmp
;
1951 if (!root
->ref_cows
)
1954 down_write(&root
->snap_sem
);
1955 freeze_bdev(root
->fs_info
->sb
->s_bdev
);
1956 thaw_bdev(root
->fs_info
->sb
->s_bdev
, root
->fs_info
->sb
);
1958 mutex_lock(&root
->fs_info
->fs_mutex
);
1959 trans
= btrfs_start_transaction(root
, 1);
1962 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
1966 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
1971 memcpy(&new_root_item
, &root
->root_item
,
1972 sizeof(new_root_item
));
1974 key
.objectid
= objectid
;
1976 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
1978 btrfs_cow_block(trans
, root
, root
->node
, NULL
, 0, &tmp
);
1979 btrfs_set_root_bytenr(&new_root_item
, root
->node
->start
);
1980 btrfs_set_root_level(&new_root_item
, btrfs_header_level(root
->node
));
1982 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
1988 * insert the directory item
1990 key
.offset
= (u64
)-1;
1991 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
1993 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
1994 &key
, BTRFS_FT_DIR
);
1999 ret
= btrfs_inc_root_ref(trans
, root
);
2003 nr
= trans
->blocks_used
;
2004 err
= btrfs_commit_transaction(trans
, root
);
2009 mutex_unlock(&root
->fs_info
->fs_mutex
);
2010 up_write(&root
->snap_sem
);
2011 btrfs_btree_balance_dirty(root
, nr
);
2015 static unsigned long force_ra(struct address_space
*mapping
,
2016 struct file_ra_state
*ra
, struct file
*file
,
2017 pgoff_t offset
, pgoff_t last_index
)
2021 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2022 req_size
= last_index
- offset
+ 1;
2023 offset
= page_cache_readahead(mapping
, ra
, file
, offset
, req_size
);
2026 req_size
= min(last_index
- offset
+ 1, (pgoff_t
)128);
2027 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
2028 return offset
+ req_size
;
2032 int btrfs_defrag_file(struct file
*file
) {
2033 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
2034 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2036 unsigned long last_index
;
2037 unsigned long ra_index
= 0;
2042 mutex_lock(&inode
->i_mutex
);
2043 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
2044 for (i
= 0; i
<= last_index
; i
++) {
2045 if (i
== ra_index
) {
2046 ra_index
= force_ra(inode
->i_mapping
, &file
->f_ra
,
2047 file
, ra_index
, last_index
);
2049 page
= grab_cache_page(inode
->i_mapping
, i
);
2052 if (!PageUptodate(page
)) {
2053 btrfs_readpage(NULL
, page
);
2055 if (!PageUptodate(page
)) {
2057 page_cache_release(page
);
2061 page_start
= page
->index
<< PAGE_CACHE_SHIFT
;
2062 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2064 lock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
2065 set_extent_delalloc(em_tree
, page_start
,
2066 page_end
, GFP_NOFS
);
2067 unlock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
2068 set_page_dirty(page
);
2070 page_cache_release(page
);
2071 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
2075 mutex_unlock(&inode
->i_mutex
);
2079 static int btrfs_ioctl_snap_create(struct btrfs_root
*root
, void __user
*arg
)
2081 struct btrfs_ioctl_vol_args vol_args
;
2082 struct btrfs_dir_item
*di
;
2083 struct btrfs_path
*path
;
2087 if (copy_from_user(&vol_args
, arg
, sizeof(vol_args
)))
2090 namelen
= strlen(vol_args
.name
);
2091 if (namelen
> BTRFS_VOL_NAME_MAX
)
2093 if (strchr(vol_args
.name
, '/'))
2096 path
= btrfs_alloc_path();
2100 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2101 mutex_lock(&root
->fs_info
->fs_mutex
);
2102 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
2104 vol_args
.name
, namelen
, 0);
2105 mutex_unlock(&root
->fs_info
->fs_mutex
);
2106 btrfs_free_path(path
);
2107 if (di
&& !IS_ERR(di
))
2112 if (root
== root
->fs_info
->tree_root
)
2113 return create_subvol(root
, vol_args
.name
, namelen
);
2114 return create_snapshot(root
, vol_args
.name
, namelen
);
2117 static int btrfs_ioctl_defrag(struct file
*file
)
2119 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
2120 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2122 switch (inode
->i_mode
& S_IFMT
) {
2124 mutex_lock(&root
->fs_info
->fs_mutex
);
2125 btrfs_defrag_root(root
, 0);
2126 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
2127 mutex_unlock(&root
->fs_info
->fs_mutex
);
2130 btrfs_defrag_file(file
);
2137 long btrfs_ioctl(struct file
*file
, unsigned int
2138 cmd
, unsigned long arg
)
2140 struct btrfs_root
*root
= BTRFS_I(file
->f_path
.dentry
->d_inode
)->root
;
2143 case BTRFS_IOC_SNAP_CREATE
:
2144 return btrfs_ioctl_snap_create(root
, (void __user
*)arg
);
2145 case BTRFS_IOC_DEFRAG
:
2146 return btrfs_ioctl_defrag(file
);
2153 * Called inside transaction, so use GFP_NOFS
2155 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2157 struct btrfs_inode
*ei
;
2159 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2163 return &ei
->vfs_inode
;
2166 void btrfs_destroy_inode(struct inode
*inode
)
2168 WARN_ON(!list_empty(&inode
->i_dentry
));
2169 WARN_ON(inode
->i_data
.nrpages
);
2171 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2174 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2175 unsigned long flags
)
2177 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2179 inode_init_once(&ei
->vfs_inode
);
2182 void btrfs_destroy_cachep(void)
2184 if (btrfs_inode_cachep
)
2185 kmem_cache_destroy(btrfs_inode_cachep
);
2186 if (btrfs_trans_handle_cachep
)
2187 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2188 if (btrfs_transaction_cachep
)
2189 kmem_cache_destroy(btrfs_transaction_cachep
);
2190 if (btrfs_bit_radix_cachep
)
2191 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2192 if (btrfs_path_cachep
)
2193 kmem_cache_destroy(btrfs_path_cachep
);
2196 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
2197 unsigned long extra_flags
,
2198 void (*ctor
)(void *, struct kmem_cache
*,
2201 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
2202 SLAB_MEM_SPREAD
| extra_flags
), ctor
2203 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2209 int btrfs_init_cachep(void)
2211 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
2212 sizeof(struct btrfs_inode
),
2214 if (!btrfs_inode_cachep
)
2216 btrfs_trans_handle_cachep
=
2217 btrfs_cache_create("btrfs_trans_handle_cache",
2218 sizeof(struct btrfs_trans_handle
),
2220 if (!btrfs_trans_handle_cachep
)
2222 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
2223 sizeof(struct btrfs_transaction
),
2225 if (!btrfs_transaction_cachep
)
2227 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
2228 sizeof(struct btrfs_path
),
2230 if (!btrfs_path_cachep
)
2232 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
2233 SLAB_DESTROY_BY_RCU
, NULL
);
2234 if (!btrfs_bit_radix_cachep
)
2238 btrfs_destroy_cachep();
2242 static int btrfs_getattr(struct vfsmount
*mnt
,
2243 struct dentry
*dentry
, struct kstat
*stat
)
2245 struct inode
*inode
= dentry
->d_inode
;
2246 generic_fillattr(inode
, stat
);
2247 stat
->blksize
= 256 * 1024;
2251 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
2252 struct inode
* new_dir
,struct dentry
*new_dentry
)
2254 struct btrfs_trans_handle
*trans
;
2255 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
2256 struct inode
*new_inode
= new_dentry
->d_inode
;
2257 struct inode
*old_inode
= old_dentry
->d_inode
;
2258 struct timespec ctime
= CURRENT_TIME
;
2259 struct btrfs_path
*path
;
2260 struct btrfs_dir_item
*di
;
2263 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
2264 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
2268 mutex_lock(&root
->fs_info
->fs_mutex
);
2269 trans
= btrfs_start_transaction(root
, 1);
2271 btrfs_set_trans_block_group(trans
, new_dir
);
2272 path
= btrfs_alloc_path();
2278 old_dentry
->d_inode
->i_nlink
++;
2279 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
2280 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
2281 old_inode
->i_ctime
= ctime
;
2283 if (S_ISDIR(old_inode
->i_mode
) && old_dir
!= new_dir
) {
2284 struct btrfs_key
*location
= &BTRFS_I(new_dir
)->location
;
2285 struct btrfs_key old_parent_key
;
2286 di
= btrfs_lookup_dir_item(trans
, root
, path
, old_inode
->i_ino
,
2296 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &old_parent_key
);
2297 ret
= btrfs_del_item(trans
, root
, path
);
2301 btrfs_release_path(root
, path
);
2303 di
= btrfs_lookup_dir_index_item(trans
, root
, path
,
2305 old_parent_key
.objectid
,
2315 ret
= btrfs_del_item(trans
, root
, path
);
2319 btrfs_release_path(root
, path
);
2321 ret
= btrfs_insert_dir_item(trans
, root
, "..", 2,
2322 old_inode
->i_ino
, location
,
2329 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
2334 new_inode
->i_ctime
= CURRENT_TIME
;
2335 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
2339 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
);
2344 btrfs_free_path(path
);
2345 btrfs_end_transaction(trans
, root
);
2346 mutex_unlock(&root
->fs_info
->fs_mutex
);
2350 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
2351 const char *symname
)
2353 struct btrfs_trans_handle
*trans
;
2354 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2355 struct btrfs_path
*path
;
2356 struct btrfs_key key
;
2357 struct inode
*inode
;
2364 struct btrfs_file_extent_item
*ei
;
2365 struct extent_buffer
*leaf
;
2368 name_len
= strlen(symname
) + 1;
2369 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
2370 return -ENAMETOOLONG
;
2371 mutex_lock(&root
->fs_info
->fs_mutex
);
2372 trans
= btrfs_start_transaction(root
, 1);
2373 btrfs_set_trans_block_group(trans
, dir
);
2375 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2381 inode
= btrfs_new_inode(trans
, root
, objectid
,
2382 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
2383 err
= PTR_ERR(inode
);
2387 btrfs_set_trans_block_group(trans
, inode
);
2388 err
= btrfs_add_nondir(trans
, dentry
, inode
);
2392 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2393 inode
->i_fop
= &btrfs_file_operations
;
2394 inode
->i_op
= &btrfs_file_inode_operations
;
2395 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
2396 inode
->i_mapping
, GFP_NOFS
);
2397 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
2399 dir
->i_sb
->s_dirt
= 1;
2400 btrfs_update_inode_block_group(trans
, inode
);
2401 btrfs_update_inode_block_group(trans
, dir
);
2405 path
= btrfs_alloc_path();
2407 key
.objectid
= inode
->i_ino
;
2409 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
2410 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
2411 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2417 leaf
= path
->nodes
[0];
2418 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
2419 struct btrfs_file_extent_item
);
2420 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
2421 btrfs_set_file_extent_type(leaf
, ei
,
2422 BTRFS_FILE_EXTENT_INLINE
);
2423 ptr
= btrfs_file_extent_inline_start(ei
);
2424 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
2425 btrfs_mark_buffer_dirty(leaf
);
2426 btrfs_free_path(path
);
2428 inode
->i_op
= &btrfs_symlink_inode_operations
;
2429 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2430 inode
->i_size
= name_len
- 1;
2431 err
= btrfs_update_inode(trans
, root
, inode
);
2436 nr
= trans
->blocks_used
;
2437 btrfs_end_transaction(trans
, root
);
2438 mutex_unlock(&root
->fs_info
->fs_mutex
);
2440 inode_dec_link_count(inode
);
2443 btrfs_btree_balance_dirty(root
, nr
);
2447 static struct inode_operations btrfs_dir_inode_operations
= {
2448 .lookup
= btrfs_lookup
,
2449 .create
= btrfs_create
,
2450 .unlink
= btrfs_unlink
,
2452 .mkdir
= btrfs_mkdir
,
2453 .rmdir
= btrfs_rmdir
,
2454 .rename
= btrfs_rename
,
2455 .symlink
= btrfs_symlink
,
2456 .setattr
= btrfs_setattr
,
2457 .mknod
= btrfs_mknod
,
2460 static struct inode_operations btrfs_dir_ro_inode_operations
= {
2461 .lookup
= btrfs_lookup
,
2464 static struct file_operations btrfs_dir_file_operations
= {
2465 .llseek
= generic_file_llseek
,
2466 .read
= generic_read_dir
,
2467 .readdir
= btrfs_readdir
,
2468 .unlocked_ioctl
= btrfs_ioctl
,
2469 #ifdef CONFIG_COMPAT
2470 .compat_ioctl
= btrfs_ioctl
,
2474 static struct extent_map_ops btrfs_extent_map_ops
= {
2475 .fill_delalloc
= run_delalloc_range
,
2476 .writepage_io_hook
= btrfs_writepage_io_hook
,
2477 .readpage_io_hook
= btrfs_readpage_io_hook
,
2478 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
2481 static struct address_space_operations btrfs_aops
= {
2482 .readpage
= btrfs_readpage
,
2483 .writepage
= btrfs_writepage
,
2484 .sync_page
= block_sync_page
,
2485 .prepare_write
= btrfs_prepare_write
,
2486 .commit_write
= btrfs_commit_write
,
2488 .invalidatepage
= btrfs_invalidatepage
,
2489 .releasepage
= btrfs_releasepage
,
2490 .set_page_dirty
= __set_page_dirty_nobuffers
,
2493 static struct address_space_operations btrfs_symlink_aops
= {
2494 .readpage
= btrfs_readpage
,
2495 .writepage
= btrfs_writepage
,
2496 .invalidatepage
= btrfs_invalidatepage
,
2497 .releasepage
= btrfs_releasepage
,
2500 static struct inode_operations btrfs_file_inode_operations
= {
2501 .truncate
= btrfs_truncate
,
2502 .getattr
= btrfs_getattr
,
2503 .setattr
= btrfs_setattr
,
2506 static struct inode_operations btrfs_special_inode_operations
= {
2507 .getattr
= btrfs_getattr
,
2508 .setattr
= btrfs_setattr
,
2511 static struct inode_operations btrfs_symlink_inode_operations
= {
2512 .readlink
= generic_readlink
,
2513 .follow_link
= page_follow_link_light
,
2514 .put_link
= page_put_link
,