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/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/statfs.h>
34 #include <linux/compat.h>
35 #include <linux/bit_spinlock.h>
36 #include <linux/xattr.h>
37 #include <linux/posix_acl.h>
38 #include <linux/falloc.h>
39 #include <linux/slab.h>
43 #include "transaction.h"
44 #include "btrfs_inode.h"
46 #include "print-tree.h"
48 #include "ordered-data.h"
51 #include "compression.h"
53 #include "free-space-cache.h"
54 #include "inode-map.h"
56 struct btrfs_iget_args
{
58 struct btrfs_root
*root
;
61 static const struct inode_operations btrfs_dir_inode_operations
;
62 static const struct inode_operations btrfs_symlink_inode_operations
;
63 static const struct inode_operations btrfs_dir_ro_inode_operations
;
64 static const struct inode_operations btrfs_special_inode_operations
;
65 static const struct inode_operations btrfs_file_inode_operations
;
66 static const struct address_space_operations btrfs_aops
;
67 static const struct address_space_operations btrfs_symlink_aops
;
68 static const struct file_operations btrfs_dir_file_operations
;
69 static struct extent_io_ops btrfs_extent_io_ops
;
71 static struct kmem_cache
*btrfs_inode_cachep
;
72 struct kmem_cache
*btrfs_trans_handle_cachep
;
73 struct kmem_cache
*btrfs_transaction_cachep
;
74 struct kmem_cache
*btrfs_path_cachep
;
75 struct kmem_cache
*btrfs_free_space_cachep
;
78 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
79 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
80 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
81 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
82 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
83 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
84 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
85 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
88 static int btrfs_setsize(struct inode
*inode
, loff_t newsize
);
89 static int btrfs_truncate(struct inode
*inode
);
90 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
);
91 static noinline
int cow_file_range(struct inode
*inode
,
92 struct page
*locked_page
,
93 u64 start
, u64 end
, int *page_started
,
94 unsigned long *nr_written
, int unlock
);
96 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
97 struct inode
*inode
, struct inode
*dir
,
98 const struct qstr
*qstr
)
102 err
= btrfs_init_acl(trans
, inode
, dir
);
104 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
109 * this does all the hard work for inserting an inline extent into
110 * the btree. The caller should have done a btrfs_drop_extents so that
111 * no overlapping inline items exist in the btree
113 static noinline
int insert_inline_extent(struct btrfs_trans_handle
*trans
,
114 struct btrfs_root
*root
, struct inode
*inode
,
115 u64 start
, size_t size
, size_t compressed_size
,
117 struct page
**compressed_pages
)
119 struct btrfs_key key
;
120 struct btrfs_path
*path
;
121 struct extent_buffer
*leaf
;
122 struct page
*page
= NULL
;
125 struct btrfs_file_extent_item
*ei
;
128 size_t cur_size
= size
;
130 unsigned long offset
;
132 if (compressed_size
&& compressed_pages
)
133 cur_size
= compressed_size
;
135 path
= btrfs_alloc_path();
139 path
->leave_spinning
= 1;
140 btrfs_set_trans_block_group(trans
, inode
);
142 key
.objectid
= btrfs_ino(inode
);
144 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
145 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
147 inode_add_bytes(inode
, size
);
148 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
155 leaf
= path
->nodes
[0];
156 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
157 struct btrfs_file_extent_item
);
158 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
159 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
160 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
161 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
162 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
163 ptr
= btrfs_file_extent_inline_start(ei
);
165 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
168 while (compressed_size
> 0) {
169 cpage
= compressed_pages
[i
];
170 cur_size
= min_t(unsigned long, compressed_size
,
173 kaddr
= kmap_atomic(cpage
, KM_USER0
);
174 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
175 kunmap_atomic(kaddr
, KM_USER0
);
179 compressed_size
-= cur_size
;
181 btrfs_set_file_extent_compression(leaf
, ei
,
184 page
= find_get_page(inode
->i_mapping
,
185 start
>> PAGE_CACHE_SHIFT
);
186 btrfs_set_file_extent_compression(leaf
, ei
, 0);
187 kaddr
= kmap_atomic(page
, KM_USER0
);
188 offset
= start
& (PAGE_CACHE_SIZE
- 1);
189 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
190 kunmap_atomic(kaddr
, KM_USER0
);
191 page_cache_release(page
);
193 btrfs_mark_buffer_dirty(leaf
);
194 btrfs_free_path(path
);
197 * we're an inline extent, so nobody can
198 * extend the file past i_size without locking
199 * a page we already have locked.
201 * We must do any isize and inode updates
202 * before we unlock the pages. Otherwise we
203 * could end up racing with unlink.
205 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
206 btrfs_update_inode(trans
, root
, inode
);
210 btrfs_free_path(path
);
216 * conditionally insert an inline extent into the file. This
217 * does the checks required to make sure the data is small enough
218 * to fit as an inline extent.
220 static noinline
int cow_file_range_inline(struct btrfs_trans_handle
*trans
,
221 struct btrfs_root
*root
,
222 struct inode
*inode
, u64 start
, u64 end
,
223 size_t compressed_size
, int compress_type
,
224 struct page
**compressed_pages
)
226 u64 isize
= i_size_read(inode
);
227 u64 actual_end
= min(end
+ 1, isize
);
228 u64 inline_len
= actual_end
- start
;
229 u64 aligned_end
= (end
+ root
->sectorsize
- 1) &
230 ~((u64
)root
->sectorsize
- 1);
232 u64 data_len
= inline_len
;
236 data_len
= compressed_size
;
239 actual_end
>= PAGE_CACHE_SIZE
||
240 data_len
>= BTRFS_MAX_INLINE_DATA_SIZE(root
) ||
242 (actual_end
& (root
->sectorsize
- 1)) == 0) ||
244 data_len
> root
->fs_info
->max_inline
) {
248 ret
= btrfs_drop_extents(trans
, inode
, start
, aligned_end
,
252 if (isize
> actual_end
)
253 inline_len
= min_t(u64
, isize
, actual_end
);
254 ret
= insert_inline_extent(trans
, root
, inode
, start
,
255 inline_len
, compressed_size
,
256 compress_type
, compressed_pages
);
258 btrfs_delalloc_release_metadata(inode
, end
+ 1 - start
);
259 btrfs_drop_extent_cache(inode
, start
, aligned_end
- 1, 0);
263 struct async_extent
{
268 unsigned long nr_pages
;
270 struct list_head list
;
275 struct btrfs_root
*root
;
276 struct page
*locked_page
;
279 struct list_head extents
;
280 struct btrfs_work work
;
283 static noinline
int add_async_extent(struct async_cow
*cow
,
284 u64 start
, u64 ram_size
,
287 unsigned long nr_pages
,
290 struct async_extent
*async_extent
;
292 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
293 BUG_ON(!async_extent
);
294 async_extent
->start
= start
;
295 async_extent
->ram_size
= ram_size
;
296 async_extent
->compressed_size
= compressed_size
;
297 async_extent
->pages
= pages
;
298 async_extent
->nr_pages
= nr_pages
;
299 async_extent
->compress_type
= compress_type
;
300 list_add_tail(&async_extent
->list
, &cow
->extents
);
305 * we create compressed extents in two phases. The first
306 * phase compresses a range of pages that have already been
307 * locked (both pages and state bits are locked).
309 * This is done inside an ordered work queue, and the compression
310 * is spread across many cpus. The actual IO submission is step
311 * two, and the ordered work queue takes care of making sure that
312 * happens in the same order things were put onto the queue by
313 * writepages and friends.
315 * If this code finds it can't get good compression, it puts an
316 * entry onto the work queue to write the uncompressed bytes. This
317 * makes sure that both compressed inodes and uncompressed inodes
318 * are written in the same order that pdflush sent them down.
320 static noinline
int compress_file_range(struct inode
*inode
,
321 struct page
*locked_page
,
323 struct async_cow
*async_cow
,
326 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
327 struct btrfs_trans_handle
*trans
;
329 u64 blocksize
= root
->sectorsize
;
331 u64 isize
= i_size_read(inode
);
333 struct page
**pages
= NULL
;
334 unsigned long nr_pages
;
335 unsigned long nr_pages_ret
= 0;
336 unsigned long total_compressed
= 0;
337 unsigned long total_in
= 0;
338 unsigned long max_compressed
= 128 * 1024;
339 unsigned long max_uncompressed
= 128 * 1024;
342 int compress_type
= root
->fs_info
->compress_type
;
344 actual_end
= min_t(u64
, isize
, end
+ 1);
347 nr_pages
= (end
>> PAGE_CACHE_SHIFT
) - (start
>> PAGE_CACHE_SHIFT
) + 1;
348 nr_pages
= min(nr_pages
, (128 * 1024UL) / PAGE_CACHE_SIZE
);
351 * we don't want to send crud past the end of i_size through
352 * compression, that's just a waste of CPU time. So, if the
353 * end of the file is before the start of our current
354 * requested range of bytes, we bail out to the uncompressed
355 * cleanup code that can deal with all of this.
357 * It isn't really the fastest way to fix things, but this is a
358 * very uncommon corner.
360 if (actual_end
<= start
)
361 goto cleanup_and_bail_uncompressed
;
363 total_compressed
= actual_end
- start
;
365 /* we want to make sure that amount of ram required to uncompress
366 * an extent is reasonable, so we limit the total size in ram
367 * of a compressed extent to 128k. This is a crucial number
368 * because it also controls how easily we can spread reads across
369 * cpus for decompression.
371 * We also want to make sure the amount of IO required to do
372 * a random read is reasonably small, so we limit the size of
373 * a compressed extent to 128k.
375 total_compressed
= min(total_compressed
, max_uncompressed
);
376 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
377 num_bytes
= max(blocksize
, num_bytes
);
382 * we do compression for mount -o compress and when the
383 * inode has not been flagged as nocompress. This flag can
384 * change at any time if we discover bad compression ratios.
386 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
) &&
387 (btrfs_test_opt(root
, COMPRESS
) ||
388 (BTRFS_I(inode
)->force_compress
) ||
389 (BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
))) {
391 pages
= kzalloc(sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
394 if (BTRFS_I(inode
)->force_compress
)
395 compress_type
= BTRFS_I(inode
)->force_compress
;
397 ret
= btrfs_compress_pages(compress_type
,
398 inode
->i_mapping
, start
,
399 total_compressed
, pages
,
400 nr_pages
, &nr_pages_ret
,
406 unsigned long offset
= total_compressed
&
407 (PAGE_CACHE_SIZE
- 1);
408 struct page
*page
= pages
[nr_pages_ret
- 1];
411 /* zero the tail end of the last page, we might be
412 * sending it down to disk
415 kaddr
= kmap_atomic(page
, KM_USER0
);
416 memset(kaddr
+ offset
, 0,
417 PAGE_CACHE_SIZE
- offset
);
418 kunmap_atomic(kaddr
, KM_USER0
);
424 trans
= btrfs_join_transaction(root
, 1);
425 BUG_ON(IS_ERR(trans
));
426 btrfs_set_trans_block_group(trans
, inode
);
427 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
429 /* lets try to make an inline extent */
430 if (ret
|| total_in
< (actual_end
- start
)) {
431 /* we didn't compress the entire range, try
432 * to make an uncompressed inline extent.
434 ret
= cow_file_range_inline(trans
, root
, inode
,
435 start
, end
, 0, 0, NULL
);
437 /* try making a compressed inline extent */
438 ret
= cow_file_range_inline(trans
, root
, inode
,
441 compress_type
, pages
);
445 * inline extent creation worked, we don't need
446 * to create any more async work items. Unlock
447 * and free up our temp pages.
449 extent_clear_unlock_delalloc(inode
,
450 &BTRFS_I(inode
)->io_tree
,
452 EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
453 EXTENT_CLEAR_DELALLOC
|
454 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
);
456 btrfs_end_transaction(trans
, root
);
459 btrfs_end_transaction(trans
, root
);
464 * we aren't doing an inline extent round the compressed size
465 * up to a block size boundary so the allocator does sane
468 total_compressed
= (total_compressed
+ blocksize
- 1) &
472 * one last check to make sure the compression is really a
473 * win, compare the page count read with the blocks on disk
475 total_in
= (total_in
+ PAGE_CACHE_SIZE
- 1) &
476 ~(PAGE_CACHE_SIZE
- 1);
477 if (total_compressed
>= total_in
) {
480 num_bytes
= total_in
;
483 if (!will_compress
&& pages
) {
485 * the compression code ran but failed to make things smaller,
486 * free any pages it allocated and our page pointer array
488 for (i
= 0; i
< nr_pages_ret
; i
++) {
489 WARN_ON(pages
[i
]->mapping
);
490 page_cache_release(pages
[i
]);
494 total_compressed
= 0;
497 /* flag the file so we don't compress in the future */
498 if (!btrfs_test_opt(root
, FORCE_COMPRESS
) &&
499 !(BTRFS_I(inode
)->force_compress
)) {
500 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
506 /* the async work queues will take care of doing actual
507 * allocation on disk for these compressed pages,
508 * and will submit them to the elevator.
510 add_async_extent(async_cow
, start
, num_bytes
,
511 total_compressed
, pages
, nr_pages_ret
,
514 if (start
+ num_bytes
< end
) {
521 cleanup_and_bail_uncompressed
:
523 * No compression, but we still need to write the pages in
524 * the file we've been given so far. redirty the locked
525 * page if it corresponds to our extent and set things up
526 * for the async work queue to run cow_file_range to do
527 * the normal delalloc dance
529 if (page_offset(locked_page
) >= start
&&
530 page_offset(locked_page
) <= end
) {
531 __set_page_dirty_nobuffers(locked_page
);
532 /* unlocked later on in the async handlers */
534 add_async_extent(async_cow
, start
, end
- start
+ 1,
535 0, NULL
, 0, BTRFS_COMPRESS_NONE
);
543 for (i
= 0; i
< nr_pages_ret
; i
++) {
544 WARN_ON(pages
[i
]->mapping
);
545 page_cache_release(pages
[i
]);
553 * phase two of compressed writeback. This is the ordered portion
554 * of the code, which only gets called in the order the work was
555 * queued. We walk all the async extents created by compress_file_range
556 * and send them down to the disk.
558 static noinline
int submit_compressed_extents(struct inode
*inode
,
559 struct async_cow
*async_cow
)
561 struct async_extent
*async_extent
;
563 struct btrfs_trans_handle
*trans
;
564 struct btrfs_key ins
;
565 struct extent_map
*em
;
566 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
567 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
568 struct extent_io_tree
*io_tree
;
571 if (list_empty(&async_cow
->extents
))
575 while (!list_empty(&async_cow
->extents
)) {
576 async_extent
= list_entry(async_cow
->extents
.next
,
577 struct async_extent
, list
);
578 list_del(&async_extent
->list
);
580 io_tree
= &BTRFS_I(inode
)->io_tree
;
583 /* did the compression code fall back to uncompressed IO? */
584 if (!async_extent
->pages
) {
585 int page_started
= 0;
586 unsigned long nr_written
= 0;
588 lock_extent(io_tree
, async_extent
->start
,
589 async_extent
->start
+
590 async_extent
->ram_size
- 1, GFP_NOFS
);
592 /* allocate blocks */
593 ret
= cow_file_range(inode
, async_cow
->locked_page
,
595 async_extent
->start
+
596 async_extent
->ram_size
- 1,
597 &page_started
, &nr_written
, 0);
600 * if page_started, cow_file_range inserted an
601 * inline extent and took care of all the unlocking
602 * and IO for us. Otherwise, we need to submit
603 * all those pages down to the drive.
605 if (!page_started
&& !ret
)
606 extent_write_locked_range(io_tree
,
607 inode
, async_extent
->start
,
608 async_extent
->start
+
609 async_extent
->ram_size
- 1,
617 lock_extent(io_tree
, async_extent
->start
,
618 async_extent
->start
+ async_extent
->ram_size
- 1,
621 trans
= btrfs_join_transaction(root
, 1);
622 BUG_ON(IS_ERR(trans
));
623 ret
= btrfs_reserve_extent(trans
, root
,
624 async_extent
->compressed_size
,
625 async_extent
->compressed_size
,
628 btrfs_end_transaction(trans
, root
);
632 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
633 WARN_ON(async_extent
->pages
[i
]->mapping
);
634 page_cache_release(async_extent
->pages
[i
]);
636 kfree(async_extent
->pages
);
637 async_extent
->nr_pages
= 0;
638 async_extent
->pages
= NULL
;
639 unlock_extent(io_tree
, async_extent
->start
,
640 async_extent
->start
+
641 async_extent
->ram_size
- 1, GFP_NOFS
);
646 * here we're doing allocation and writeback of the
649 btrfs_drop_extent_cache(inode
, async_extent
->start
,
650 async_extent
->start
+
651 async_extent
->ram_size
- 1, 0);
653 em
= alloc_extent_map(GFP_NOFS
);
655 em
->start
= async_extent
->start
;
656 em
->len
= async_extent
->ram_size
;
657 em
->orig_start
= em
->start
;
659 em
->block_start
= ins
.objectid
;
660 em
->block_len
= ins
.offset
;
661 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
662 em
->compress_type
= async_extent
->compress_type
;
663 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
664 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
667 write_lock(&em_tree
->lock
);
668 ret
= add_extent_mapping(em_tree
, em
);
669 write_unlock(&em_tree
->lock
);
670 if (ret
!= -EEXIST
) {
674 btrfs_drop_extent_cache(inode
, async_extent
->start
,
675 async_extent
->start
+
676 async_extent
->ram_size
- 1, 0);
679 ret
= btrfs_add_ordered_extent_compress(inode
,
682 async_extent
->ram_size
,
684 BTRFS_ORDERED_COMPRESSED
,
685 async_extent
->compress_type
);
689 * clear dirty, set writeback and unlock the pages.
691 extent_clear_unlock_delalloc(inode
,
692 &BTRFS_I(inode
)->io_tree
,
694 async_extent
->start
+
695 async_extent
->ram_size
- 1,
696 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
697 EXTENT_CLEAR_UNLOCK
|
698 EXTENT_CLEAR_DELALLOC
|
699 EXTENT_CLEAR_DIRTY
| EXTENT_SET_WRITEBACK
);
701 ret
= btrfs_submit_compressed_write(inode
,
703 async_extent
->ram_size
,
705 ins
.offset
, async_extent
->pages
,
706 async_extent
->nr_pages
);
709 alloc_hint
= ins
.objectid
+ ins
.offset
;
717 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
720 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
721 struct extent_map
*em
;
724 read_lock(&em_tree
->lock
);
725 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
728 * if block start isn't an actual block number then find the
729 * first block in this inode and use that as a hint. If that
730 * block is also bogus then just don't worry about it.
732 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
734 em
= search_extent_mapping(em_tree
, 0, 0);
735 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
736 alloc_hint
= em
->block_start
;
740 alloc_hint
= em
->block_start
;
744 read_unlock(&em_tree
->lock
);
749 static inline bool is_free_space_inode(struct btrfs_root
*root
,
752 if (root
== root
->fs_info
->tree_root
||
753 BTRFS_I(inode
)->location
.objectid
== BTRFS_FREE_INO_OBJECTID
)
759 * when extent_io.c finds a delayed allocation range in the file,
760 * the call backs end up in this code. The basic idea is to
761 * allocate extents on disk for the range, and create ordered data structs
762 * in ram to track those extents.
764 * locked_page is the page that writepage had locked already. We use
765 * it to make sure we don't do extra locks or unlocks.
767 * *page_started is set to one if we unlock locked_page and do everything
768 * required to start IO on it. It may be clean and already done with
771 static noinline
int cow_file_range(struct inode
*inode
,
772 struct page
*locked_page
,
773 u64 start
, u64 end
, int *page_started
,
774 unsigned long *nr_written
,
777 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
778 struct btrfs_trans_handle
*trans
;
781 unsigned long ram_size
;
784 u64 blocksize
= root
->sectorsize
;
785 struct btrfs_key ins
;
786 struct extent_map
*em
;
787 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
790 BUG_ON(is_free_space_inode(root
, inode
));
791 trans
= btrfs_join_transaction(root
, 1);
792 BUG_ON(IS_ERR(trans
));
793 btrfs_set_trans_block_group(trans
, inode
);
794 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
796 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
797 num_bytes
= max(blocksize
, num_bytes
);
798 disk_num_bytes
= num_bytes
;
802 /* lets try to make an inline extent */
803 ret
= cow_file_range_inline(trans
, root
, inode
,
804 start
, end
, 0, 0, NULL
);
806 extent_clear_unlock_delalloc(inode
,
807 &BTRFS_I(inode
)->io_tree
,
809 EXTENT_CLEAR_UNLOCK_PAGE
|
810 EXTENT_CLEAR_UNLOCK
|
811 EXTENT_CLEAR_DELALLOC
|
813 EXTENT_SET_WRITEBACK
|
814 EXTENT_END_WRITEBACK
);
816 *nr_written
= *nr_written
+
817 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
824 BUG_ON(disk_num_bytes
>
825 btrfs_super_total_bytes(&root
->fs_info
->super_copy
));
827 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
828 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
830 while (disk_num_bytes
> 0) {
833 cur_alloc_size
= disk_num_bytes
;
834 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
835 root
->sectorsize
, 0, alloc_hint
,
839 em
= alloc_extent_map(GFP_NOFS
);
842 em
->orig_start
= em
->start
;
843 ram_size
= ins
.offset
;
844 em
->len
= ins
.offset
;
846 em
->block_start
= ins
.objectid
;
847 em
->block_len
= ins
.offset
;
848 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
849 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
852 write_lock(&em_tree
->lock
);
853 ret
= add_extent_mapping(em_tree
, em
);
854 write_unlock(&em_tree
->lock
);
855 if (ret
!= -EEXIST
) {
859 btrfs_drop_extent_cache(inode
, start
,
860 start
+ ram_size
- 1, 0);
863 cur_alloc_size
= ins
.offset
;
864 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
865 ram_size
, cur_alloc_size
, 0);
868 if (root
->root_key
.objectid
==
869 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
870 ret
= btrfs_reloc_clone_csums(inode
, start
,
875 if (disk_num_bytes
< cur_alloc_size
)
878 /* we're not doing compressed IO, don't unlock the first
879 * page (which the caller expects to stay locked), don't
880 * clear any dirty bits and don't set any writeback bits
882 * Do set the Private2 bit so we know this page was properly
883 * setup for writepage
885 op
= unlock
? EXTENT_CLEAR_UNLOCK_PAGE
: 0;
886 op
|= EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
889 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
890 start
, start
+ ram_size
- 1,
892 disk_num_bytes
-= cur_alloc_size
;
893 num_bytes
-= cur_alloc_size
;
894 alloc_hint
= ins
.objectid
+ ins
.offset
;
895 start
+= cur_alloc_size
;
899 btrfs_end_transaction(trans
, root
);
905 * work queue call back to started compression on a file and pages
907 static noinline
void async_cow_start(struct btrfs_work
*work
)
909 struct async_cow
*async_cow
;
911 async_cow
= container_of(work
, struct async_cow
, work
);
913 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
914 async_cow
->start
, async_cow
->end
, async_cow
,
917 async_cow
->inode
= NULL
;
921 * work queue call back to submit previously compressed pages
923 static noinline
void async_cow_submit(struct btrfs_work
*work
)
925 struct async_cow
*async_cow
;
926 struct btrfs_root
*root
;
927 unsigned long nr_pages
;
929 async_cow
= container_of(work
, struct async_cow
, work
);
931 root
= async_cow
->root
;
932 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
935 atomic_sub(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
937 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
939 waitqueue_active(&root
->fs_info
->async_submit_wait
))
940 wake_up(&root
->fs_info
->async_submit_wait
);
942 if (async_cow
->inode
)
943 submit_compressed_extents(async_cow
->inode
, async_cow
);
946 static noinline
void async_cow_free(struct btrfs_work
*work
)
948 struct async_cow
*async_cow
;
949 async_cow
= container_of(work
, struct async_cow
, work
);
953 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
954 u64 start
, u64 end
, int *page_started
,
955 unsigned long *nr_written
)
957 struct async_cow
*async_cow
;
958 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
959 unsigned long nr_pages
;
961 int limit
= 10 * 1024 * 1042;
963 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
964 1, 0, NULL
, GFP_NOFS
);
965 while (start
< end
) {
966 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
968 async_cow
->inode
= inode
;
969 async_cow
->root
= root
;
970 async_cow
->locked_page
= locked_page
;
971 async_cow
->start
= start
;
973 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
976 cur_end
= min(end
, start
+ 512 * 1024 - 1);
978 async_cow
->end
= cur_end
;
979 INIT_LIST_HEAD(&async_cow
->extents
);
981 async_cow
->work
.func
= async_cow_start
;
982 async_cow
->work
.ordered_func
= async_cow_submit
;
983 async_cow
->work
.ordered_free
= async_cow_free
;
984 async_cow
->work
.flags
= 0;
986 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
988 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
990 btrfs_queue_worker(&root
->fs_info
->delalloc_workers
,
993 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
994 wait_event(root
->fs_info
->async_submit_wait
,
995 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
999 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
1000 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1001 wait_event(root
->fs_info
->async_submit_wait
,
1002 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
1006 *nr_written
+= nr_pages
;
1007 start
= cur_end
+ 1;
1013 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
1014 u64 bytenr
, u64 num_bytes
)
1017 struct btrfs_ordered_sum
*sums
;
1020 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
1021 bytenr
+ num_bytes
- 1, &list
);
1022 if (ret
== 0 && list_empty(&list
))
1025 while (!list_empty(&list
)) {
1026 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1027 list_del(&sums
->list
);
1034 * when nowcow writeback call back. This checks for snapshots or COW copies
1035 * of the extents that exist in the file, and COWs the file as required.
1037 * If no cow copies or snapshots exist, we write directly to the existing
1040 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1041 struct page
*locked_page
,
1042 u64 start
, u64 end
, int *page_started
, int force
,
1043 unsigned long *nr_written
)
1045 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1046 struct btrfs_trans_handle
*trans
;
1047 struct extent_buffer
*leaf
;
1048 struct btrfs_path
*path
;
1049 struct btrfs_file_extent_item
*fi
;
1050 struct btrfs_key found_key
;
1063 u64 ino
= btrfs_ino(inode
);
1065 path
= btrfs_alloc_path();
1068 nolock
= is_free_space_inode(root
, inode
);
1071 trans
= btrfs_join_transaction_nolock(root
, 1);
1073 trans
= btrfs_join_transaction(root
, 1);
1074 BUG_ON(IS_ERR(trans
));
1076 cow_start
= (u64
)-1;
1079 ret
= btrfs_lookup_file_extent(trans
, root
, path
, ino
,
1082 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1083 leaf
= path
->nodes
[0];
1084 btrfs_item_key_to_cpu(leaf
, &found_key
,
1085 path
->slots
[0] - 1);
1086 if (found_key
.objectid
== ino
&&
1087 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1092 leaf
= path
->nodes
[0];
1093 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1094 ret
= btrfs_next_leaf(root
, path
);
1099 leaf
= path
->nodes
[0];
1105 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1107 if (found_key
.objectid
> ino
||
1108 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1109 found_key
.offset
> end
)
1112 if (found_key
.offset
> cur_offset
) {
1113 extent_end
= found_key
.offset
;
1118 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1119 struct btrfs_file_extent_item
);
1120 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1122 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1123 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1124 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1125 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1126 extent_end
= found_key
.offset
+
1127 btrfs_file_extent_num_bytes(leaf
, fi
);
1128 if (extent_end
<= start
) {
1132 if (disk_bytenr
== 0)
1134 if (btrfs_file_extent_compression(leaf
, fi
) ||
1135 btrfs_file_extent_encryption(leaf
, fi
) ||
1136 btrfs_file_extent_other_encoding(leaf
, fi
))
1138 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1140 if (btrfs_extent_readonly(root
, disk_bytenr
))
1142 if (btrfs_cross_ref_exist(trans
, root
, ino
,
1144 extent_offset
, disk_bytenr
))
1146 disk_bytenr
+= extent_offset
;
1147 disk_bytenr
+= cur_offset
- found_key
.offset
;
1148 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1150 * force cow if csum exists in the range.
1151 * this ensure that csum for a given extent are
1152 * either valid or do not exist.
1154 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1157 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1158 extent_end
= found_key
.offset
+
1159 btrfs_file_extent_inline_len(leaf
, fi
);
1160 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1165 if (extent_end
<= start
) {
1170 if (cow_start
== (u64
)-1)
1171 cow_start
= cur_offset
;
1172 cur_offset
= extent_end
;
1173 if (cur_offset
> end
)
1179 btrfs_release_path(root
, path
);
1180 if (cow_start
!= (u64
)-1) {
1181 ret
= cow_file_range(inode
, locked_page
, cow_start
,
1182 found_key
.offset
- 1, page_started
,
1185 cow_start
= (u64
)-1;
1188 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1189 struct extent_map
*em
;
1190 struct extent_map_tree
*em_tree
;
1191 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1192 em
= alloc_extent_map(GFP_NOFS
);
1194 em
->start
= cur_offset
;
1195 em
->orig_start
= em
->start
;
1196 em
->len
= num_bytes
;
1197 em
->block_len
= num_bytes
;
1198 em
->block_start
= disk_bytenr
;
1199 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1200 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1202 write_lock(&em_tree
->lock
);
1203 ret
= add_extent_mapping(em_tree
, em
);
1204 write_unlock(&em_tree
->lock
);
1205 if (ret
!= -EEXIST
) {
1206 free_extent_map(em
);
1209 btrfs_drop_extent_cache(inode
, em
->start
,
1210 em
->start
+ em
->len
- 1, 0);
1212 type
= BTRFS_ORDERED_PREALLOC
;
1214 type
= BTRFS_ORDERED_NOCOW
;
1217 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1218 num_bytes
, num_bytes
, type
);
1221 if (root
->root_key
.objectid
==
1222 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1223 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1228 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
1229 cur_offset
, cur_offset
+ num_bytes
- 1,
1230 locked_page
, EXTENT_CLEAR_UNLOCK_PAGE
|
1231 EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
1232 EXTENT_SET_PRIVATE2
);
1233 cur_offset
= extent_end
;
1234 if (cur_offset
> end
)
1237 btrfs_release_path(root
, path
);
1239 if (cur_offset
<= end
&& cow_start
== (u64
)-1)
1240 cow_start
= cur_offset
;
1241 if (cow_start
!= (u64
)-1) {
1242 ret
= cow_file_range(inode
, locked_page
, cow_start
, end
,
1243 page_started
, nr_written
, 1);
1248 ret
= btrfs_end_transaction_nolock(trans
, root
);
1251 ret
= btrfs_end_transaction(trans
, root
);
1254 btrfs_free_path(path
);
1259 * extent_io.c call back to do delayed allocation processing
1261 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1262 u64 start
, u64 end
, int *page_started
,
1263 unsigned long *nr_written
)
1266 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1268 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
)
1269 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1270 page_started
, 1, nr_written
);
1271 else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
)
1272 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1273 page_started
, 0, nr_written
);
1274 else if (!btrfs_test_opt(root
, COMPRESS
) &&
1275 !(BTRFS_I(inode
)->force_compress
) &&
1276 !(BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
))
1277 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1278 page_started
, nr_written
, 1);
1280 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1281 page_started
, nr_written
);
1285 static int btrfs_split_extent_hook(struct inode
*inode
,
1286 struct extent_state
*orig
, u64 split
)
1288 /* not delalloc, ignore it */
1289 if (!(orig
->state
& EXTENT_DELALLOC
))
1292 atomic_inc(&BTRFS_I(inode
)->outstanding_extents
);
1297 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1298 * extents so we can keep track of new extents that are just merged onto old
1299 * extents, such as when we are doing sequential writes, so we can properly
1300 * account for the metadata space we'll need.
1302 static int btrfs_merge_extent_hook(struct inode
*inode
,
1303 struct extent_state
*new,
1304 struct extent_state
*other
)
1306 /* not delalloc, ignore it */
1307 if (!(other
->state
& EXTENT_DELALLOC
))
1310 atomic_dec(&BTRFS_I(inode
)->outstanding_extents
);
1315 * extent_io.c set_bit_hook, used to track delayed allocation
1316 * bytes in this file, and to maintain the list of inodes that
1317 * have pending delalloc work to be done.
1319 static int btrfs_set_bit_hook(struct inode
*inode
,
1320 struct extent_state
*state
, int *bits
)
1324 * set_bit and clear bit hooks normally require _irqsave/restore
1325 * but in this case, we are only testeing for the DELALLOC
1326 * bit, which is only set or cleared with irqs on
1328 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1329 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1330 u64 len
= state
->end
+ 1 - state
->start
;
1331 bool do_list
= !is_free_space_inode(root
, inode
);
1333 if (*bits
& EXTENT_FIRST_DELALLOC
)
1334 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1336 atomic_inc(&BTRFS_I(inode
)->outstanding_extents
);
1338 spin_lock(&root
->fs_info
->delalloc_lock
);
1339 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1340 root
->fs_info
->delalloc_bytes
+= len
;
1341 if (do_list
&& list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1342 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1343 &root
->fs_info
->delalloc_inodes
);
1345 spin_unlock(&root
->fs_info
->delalloc_lock
);
1351 * extent_io.c clear_bit_hook, see set_bit_hook for why
1353 static int btrfs_clear_bit_hook(struct inode
*inode
,
1354 struct extent_state
*state
, int *bits
)
1357 * set_bit and clear bit hooks normally require _irqsave/restore
1358 * but in this case, we are only testeing for the DELALLOC
1359 * bit, which is only set or cleared with irqs on
1361 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1362 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1363 u64 len
= state
->end
+ 1 - state
->start
;
1364 bool do_list
= !is_free_space_inode(root
, inode
);
1366 if (*bits
& EXTENT_FIRST_DELALLOC
)
1367 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1368 else if (!(*bits
& EXTENT_DO_ACCOUNTING
))
1369 atomic_dec(&BTRFS_I(inode
)->outstanding_extents
);
1371 if (*bits
& EXTENT_DO_ACCOUNTING
)
1372 btrfs_delalloc_release_metadata(inode
, len
);
1374 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1376 btrfs_free_reserved_data_space(inode
, len
);
1378 spin_lock(&root
->fs_info
->delalloc_lock
);
1379 root
->fs_info
->delalloc_bytes
-= len
;
1380 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1382 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1383 !list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1384 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1386 spin_unlock(&root
->fs_info
->delalloc_lock
);
1392 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1393 * we don't create bios that span stripes or chunks
1395 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
1396 size_t size
, struct bio
*bio
,
1397 unsigned long bio_flags
)
1399 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1400 struct btrfs_mapping_tree
*map_tree
;
1401 u64 logical
= (u64
)bio
->bi_sector
<< 9;
1406 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1409 length
= bio
->bi_size
;
1410 map_tree
= &root
->fs_info
->mapping_tree
;
1411 map_length
= length
;
1412 ret
= btrfs_map_block(map_tree
, READ
, logical
,
1413 &map_length
, NULL
, 0);
1415 if (map_length
< length
+ size
)
1421 * in order to insert checksums into the metadata in large chunks,
1422 * we wait until bio submission time. All the pages in the bio are
1423 * checksummed and sums are attached onto the ordered extent record.
1425 * At IO completion time the cums attached on the ordered extent record
1426 * are inserted into the btree
1428 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1429 struct bio
*bio
, int mirror_num
,
1430 unsigned long bio_flags
,
1433 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1436 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1442 * in order to insert checksums into the metadata in large chunks,
1443 * we wait until bio submission time. All the pages in the bio are
1444 * checksummed and sums are attached onto the ordered extent record.
1446 * At IO completion time the cums attached on the ordered extent record
1447 * are inserted into the btree
1449 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1450 int mirror_num
, unsigned long bio_flags
,
1453 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1454 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1458 * extent_io.c submission hook. This does the right thing for csum calculation
1459 * on write, or reading the csums from the tree before a read
1461 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1462 int mirror_num
, unsigned long bio_flags
,
1465 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1469 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1471 if (is_free_space_inode(root
, inode
))
1472 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 2);
1474 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
1477 if (!(rw
& REQ_WRITE
)) {
1478 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1479 return btrfs_submit_compressed_read(inode
, bio
,
1480 mirror_num
, bio_flags
);
1481 } else if (!skip_sum
) {
1482 ret
= btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1487 } else if (!skip_sum
) {
1488 /* csum items have already been cloned */
1489 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1491 /* we're doing a write, do the async checksumming */
1492 return btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1493 inode
, rw
, bio
, mirror_num
,
1494 bio_flags
, bio_offset
,
1495 __btrfs_submit_bio_start
,
1496 __btrfs_submit_bio_done
);
1500 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1504 * given a list of ordered sums record them in the inode. This happens
1505 * at IO completion time based on sums calculated at bio submission time.
1507 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1508 struct inode
*inode
, u64 file_offset
,
1509 struct list_head
*list
)
1511 struct btrfs_ordered_sum
*sum
;
1513 btrfs_set_trans_block_group(trans
, inode
);
1515 list_for_each_entry(sum
, list
, list
) {
1516 btrfs_csum_file_blocks(trans
,
1517 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1522 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1523 struct extent_state
**cached_state
)
1525 if ((end
& (PAGE_CACHE_SIZE
- 1)) == 0)
1527 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1528 cached_state
, GFP_NOFS
);
1531 /* see btrfs_writepage_start_hook for details on why this is required */
1532 struct btrfs_writepage_fixup
{
1534 struct btrfs_work work
;
1537 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1539 struct btrfs_writepage_fixup
*fixup
;
1540 struct btrfs_ordered_extent
*ordered
;
1541 struct extent_state
*cached_state
= NULL
;
1543 struct inode
*inode
;
1547 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1551 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1552 ClearPageChecked(page
);
1556 inode
= page
->mapping
->host
;
1557 page_start
= page_offset(page
);
1558 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1560 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1561 &cached_state
, GFP_NOFS
);
1563 /* already ordered? We're done */
1564 if (PagePrivate2(page
))
1567 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1569 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1570 page_end
, &cached_state
, GFP_NOFS
);
1572 btrfs_start_ordered_extent(inode
, ordered
, 1);
1577 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1578 ClearPageChecked(page
);
1580 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1581 &cached_state
, GFP_NOFS
);
1584 page_cache_release(page
);
1589 * There are a few paths in the higher layers of the kernel that directly
1590 * set the page dirty bit without asking the filesystem if it is a
1591 * good idea. This causes problems because we want to make sure COW
1592 * properly happens and the data=ordered rules are followed.
1594 * In our case any range that doesn't have the ORDERED bit set
1595 * hasn't been properly setup for IO. We kick off an async process
1596 * to fix it up. The async helper will wait for ordered extents, set
1597 * the delalloc bit and make it safe to write the page.
1599 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1601 struct inode
*inode
= page
->mapping
->host
;
1602 struct btrfs_writepage_fixup
*fixup
;
1603 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1605 /* this page is properly in the ordered list */
1606 if (TestClearPagePrivate2(page
))
1609 if (PageChecked(page
))
1612 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1616 SetPageChecked(page
);
1617 page_cache_get(page
);
1618 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
1620 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
1624 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1625 struct inode
*inode
, u64 file_pos
,
1626 u64 disk_bytenr
, u64 disk_num_bytes
,
1627 u64 num_bytes
, u64 ram_bytes
,
1628 u8 compression
, u8 encryption
,
1629 u16 other_encoding
, int extent_type
)
1631 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1632 struct btrfs_file_extent_item
*fi
;
1633 struct btrfs_path
*path
;
1634 struct extent_buffer
*leaf
;
1635 struct btrfs_key ins
;
1639 path
= btrfs_alloc_path();
1642 path
->leave_spinning
= 1;
1645 * we may be replacing one extent in the tree with another.
1646 * The new extent is pinned in the extent map, and we don't want
1647 * to drop it from the cache until it is completely in the btree.
1649 * So, tell btrfs_drop_extents to leave this extent in the cache.
1650 * the caller is expected to unpin it and allow it to be merged
1653 ret
= btrfs_drop_extents(trans
, inode
, file_pos
, file_pos
+ num_bytes
,
1657 ins
.objectid
= btrfs_ino(inode
);
1658 ins
.offset
= file_pos
;
1659 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1660 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
, sizeof(*fi
));
1662 leaf
= path
->nodes
[0];
1663 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1664 struct btrfs_file_extent_item
);
1665 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1666 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1667 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1668 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1669 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1670 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1671 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1672 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1673 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1674 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1676 btrfs_unlock_up_safe(path
, 1);
1677 btrfs_set_lock_blocking(leaf
);
1679 btrfs_mark_buffer_dirty(leaf
);
1681 inode_add_bytes(inode
, num_bytes
);
1683 ins
.objectid
= disk_bytenr
;
1684 ins
.offset
= disk_num_bytes
;
1685 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1686 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
1687 root
->root_key
.objectid
,
1688 btrfs_ino(inode
), file_pos
, &ins
);
1690 btrfs_free_path(path
);
1696 * helper function for btrfs_finish_ordered_io, this
1697 * just reads in some of the csum leaves to prime them into ram
1698 * before we start the transaction. It limits the amount of btree
1699 * reads required while inside the transaction.
1701 /* as ordered data IO finishes, this gets called so we can finish
1702 * an ordered extent if the range of bytes in the file it covers are
1705 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
)
1707 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1708 struct btrfs_trans_handle
*trans
= NULL
;
1709 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
1710 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1711 struct extent_state
*cached_state
= NULL
;
1712 int compress_type
= 0;
1716 ret
= btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
1720 BUG_ON(!ordered_extent
);
1722 nolock
= is_free_space_inode(root
, inode
);
1724 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
1725 BUG_ON(!list_empty(&ordered_extent
->list
));
1726 ret
= btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
1729 trans
= btrfs_join_transaction_nolock(root
, 1);
1731 trans
= btrfs_join_transaction(root
, 1);
1732 BUG_ON(IS_ERR(trans
));
1733 btrfs_set_trans_block_group(trans
, inode
);
1734 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1735 ret
= btrfs_update_inode(trans
, root
, inode
);
1741 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
1742 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
1743 0, &cached_state
, GFP_NOFS
);
1746 trans
= btrfs_join_transaction_nolock(root
, 1);
1748 trans
= btrfs_join_transaction(root
, 1);
1749 BUG_ON(IS_ERR(trans
));
1750 btrfs_set_trans_block_group(trans
, inode
);
1751 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1753 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
1754 compress_type
= ordered_extent
->compress_type
;
1755 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
1756 BUG_ON(compress_type
);
1757 ret
= btrfs_mark_extent_written(trans
, inode
,
1758 ordered_extent
->file_offset
,
1759 ordered_extent
->file_offset
+
1760 ordered_extent
->len
);
1763 BUG_ON(root
== root
->fs_info
->tree_root
);
1764 ret
= insert_reserved_file_extent(trans
, inode
,
1765 ordered_extent
->file_offset
,
1766 ordered_extent
->start
,
1767 ordered_extent
->disk_len
,
1768 ordered_extent
->len
,
1769 ordered_extent
->len
,
1770 compress_type
, 0, 0,
1771 BTRFS_FILE_EXTENT_REG
);
1772 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
1773 ordered_extent
->file_offset
,
1774 ordered_extent
->len
);
1777 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
1778 ordered_extent
->file_offset
+
1779 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
1781 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
1782 &ordered_extent
->list
);
1784 ret
= btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
1786 ret
= btrfs_update_inode(trans
, root
, inode
);
1793 btrfs_end_transaction_nolock(trans
, root
);
1795 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
1797 btrfs_end_transaction(trans
, root
);
1801 btrfs_put_ordered_extent(ordered_extent
);
1802 /* once for the tree */
1803 btrfs_put_ordered_extent(ordered_extent
);
1808 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
1809 struct extent_state
*state
, int uptodate
)
1811 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
1813 ClearPagePrivate2(page
);
1814 return btrfs_finish_ordered_io(page
->mapping
->host
, start
, end
);
1818 * When IO fails, either with EIO or csum verification fails, we
1819 * try other mirrors that might have a good copy of the data. This
1820 * io_failure_record is used to record state as we go through all the
1821 * mirrors. If another mirror has good data, the page is set up to date
1822 * and things continue. If a good mirror can't be found, the original
1823 * bio end_io callback is called to indicate things have failed.
1825 struct io_failure_record
{
1830 unsigned long bio_flags
;
1834 static int btrfs_io_failed_hook(struct bio
*failed_bio
,
1835 struct page
*page
, u64 start
, u64 end
,
1836 struct extent_state
*state
)
1838 struct io_failure_record
*failrec
= NULL
;
1840 struct extent_map
*em
;
1841 struct inode
*inode
= page
->mapping
->host
;
1842 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
1843 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1850 ret
= get_state_private(failure_tree
, start
, &private);
1852 failrec
= kmalloc(sizeof(*failrec
), GFP_NOFS
);
1855 failrec
->start
= start
;
1856 failrec
->len
= end
- start
+ 1;
1857 failrec
->last_mirror
= 0;
1858 failrec
->bio_flags
= 0;
1860 read_lock(&em_tree
->lock
);
1861 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
1862 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
1863 free_extent_map(em
);
1866 read_unlock(&em_tree
->lock
);
1868 if (!em
|| IS_ERR(em
)) {
1872 logical
= start
- em
->start
;
1873 logical
= em
->block_start
+ logical
;
1874 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
1875 logical
= em
->block_start
;
1876 failrec
->bio_flags
= EXTENT_BIO_COMPRESSED
;
1877 extent_set_compress_type(&failrec
->bio_flags
,
1880 failrec
->logical
= logical
;
1881 free_extent_map(em
);
1882 set_extent_bits(failure_tree
, start
, end
, EXTENT_LOCKED
|
1883 EXTENT_DIRTY
, GFP_NOFS
);
1884 set_state_private(failure_tree
, start
,
1885 (u64
)(unsigned long)failrec
);
1887 failrec
= (struct io_failure_record
*)(unsigned long)private;
1889 num_copies
= btrfs_num_copies(
1890 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
1891 failrec
->logical
, failrec
->len
);
1892 failrec
->last_mirror
++;
1894 spin_lock(&BTRFS_I(inode
)->io_tree
.lock
);
1895 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
1898 if (state
&& state
->start
!= failrec
->start
)
1900 spin_unlock(&BTRFS_I(inode
)->io_tree
.lock
);
1902 if (!state
|| failrec
->last_mirror
> num_copies
) {
1903 set_state_private(failure_tree
, failrec
->start
, 0);
1904 clear_extent_bits(failure_tree
, failrec
->start
,
1905 failrec
->start
+ failrec
->len
- 1,
1906 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
1910 bio
= bio_alloc(GFP_NOFS
, 1);
1911 bio
->bi_private
= state
;
1912 bio
->bi_end_io
= failed_bio
->bi_end_io
;
1913 bio
->bi_sector
= failrec
->logical
>> 9;
1914 bio
->bi_bdev
= failed_bio
->bi_bdev
;
1917 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
1918 if (failed_bio
->bi_rw
& REQ_WRITE
)
1923 ret
= BTRFS_I(inode
)->io_tree
.ops
->submit_bio_hook(inode
, rw
, bio
,
1924 failrec
->last_mirror
,
1925 failrec
->bio_flags
, 0);
1930 * each time an IO finishes, we do a fast check in the IO failure tree
1931 * to see if we need to process or clean up an io_failure_record
1933 static int btrfs_clean_io_failures(struct inode
*inode
, u64 start
)
1936 u64 private_failure
;
1937 struct io_failure_record
*failure
;
1941 if (count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
1942 (u64
)-1, 1, EXTENT_DIRTY
, 0)) {
1943 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
,
1944 start
, &private_failure
);
1946 failure
= (struct io_failure_record
*)(unsigned long)
1948 set_state_private(&BTRFS_I(inode
)->io_failure_tree
,
1950 clear_extent_bits(&BTRFS_I(inode
)->io_failure_tree
,
1952 failure
->start
+ failure
->len
- 1,
1953 EXTENT_DIRTY
| EXTENT_LOCKED
,
1962 * when reads are done, we need to check csums to verify the data is correct
1963 * if there's a match, we allow the bio to finish. If not, we go through
1964 * the io_failure_record routines to find good copies
1966 static int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
1967 struct extent_state
*state
)
1969 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
1970 struct inode
*inode
= page
->mapping
->host
;
1971 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1973 u64
private = ~(u32
)0;
1975 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1978 if (PageChecked(page
)) {
1979 ClearPageChecked(page
);
1983 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
1986 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
1987 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
1988 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
1993 if (state
&& state
->start
== start
) {
1994 private = state
->private;
1997 ret
= get_state_private(io_tree
, start
, &private);
1999 kaddr
= kmap_atomic(page
, KM_USER0
);
2003 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
2004 btrfs_csum_final(csum
, (char *)&csum
);
2005 if (csum
!= private)
2008 kunmap_atomic(kaddr
, KM_USER0
);
2010 /* if the io failure tree for this inode is non-empty,
2011 * check to see if we've recovered from a failed IO
2013 btrfs_clean_io_failures(inode
, start
);
2017 if (printk_ratelimit()) {
2018 printk(KERN_INFO
"btrfs csum failed ino %llu off %llu csum %u "
2020 (unsigned long long)btrfs_ino(page
->mapping
->host
),
2021 (unsigned long long)start
, csum
,
2022 (unsigned long long)private);
2024 memset(kaddr
+ offset
, 1, end
- start
+ 1);
2025 flush_dcache_page(page
);
2026 kunmap_atomic(kaddr
, KM_USER0
);
2032 struct delayed_iput
{
2033 struct list_head list
;
2034 struct inode
*inode
;
2037 void btrfs_add_delayed_iput(struct inode
*inode
)
2039 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2040 struct delayed_iput
*delayed
;
2042 if (atomic_add_unless(&inode
->i_count
, -1, 1))
2045 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
2046 delayed
->inode
= inode
;
2048 spin_lock(&fs_info
->delayed_iput_lock
);
2049 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
2050 spin_unlock(&fs_info
->delayed_iput_lock
);
2053 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
2056 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2057 struct delayed_iput
*delayed
;
2060 spin_lock(&fs_info
->delayed_iput_lock
);
2061 empty
= list_empty(&fs_info
->delayed_iputs
);
2062 spin_unlock(&fs_info
->delayed_iput_lock
);
2066 down_read(&root
->fs_info
->cleanup_work_sem
);
2067 spin_lock(&fs_info
->delayed_iput_lock
);
2068 list_splice_init(&fs_info
->delayed_iputs
, &list
);
2069 spin_unlock(&fs_info
->delayed_iput_lock
);
2071 while (!list_empty(&list
)) {
2072 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
2073 list_del(&delayed
->list
);
2074 iput(delayed
->inode
);
2077 up_read(&root
->fs_info
->cleanup_work_sem
);
2081 * calculate extra metadata reservation when snapshotting a subvolume
2082 * contains orphan files.
2084 void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle
*trans
,
2085 struct btrfs_pending_snapshot
*pending
,
2086 u64
*bytes_to_reserve
)
2088 struct btrfs_root
*root
;
2089 struct btrfs_block_rsv
*block_rsv
;
2093 root
= pending
->root
;
2094 if (!root
->orphan_block_rsv
|| list_empty(&root
->orphan_list
))
2097 block_rsv
= root
->orphan_block_rsv
;
2099 /* orphan block reservation for the snapshot */
2100 num_bytes
= block_rsv
->size
;
2103 * after the snapshot is created, COWing tree blocks may use more
2104 * space than it frees. So we should make sure there is enough
2107 index
= trans
->transid
& 0x1;
2108 if (block_rsv
->reserved
+ block_rsv
->freed
[index
] < block_rsv
->size
) {
2109 num_bytes
+= block_rsv
->size
-
2110 (block_rsv
->reserved
+ block_rsv
->freed
[index
]);
2113 *bytes_to_reserve
+= num_bytes
;
2116 void btrfs_orphan_post_snapshot(struct btrfs_trans_handle
*trans
,
2117 struct btrfs_pending_snapshot
*pending
)
2119 struct btrfs_root
*root
= pending
->root
;
2120 struct btrfs_root
*snap
= pending
->snap
;
2121 struct btrfs_block_rsv
*block_rsv
;
2126 if (!root
->orphan_block_rsv
|| list_empty(&root
->orphan_list
))
2129 /* refill source subvolume's orphan block reservation */
2130 block_rsv
= root
->orphan_block_rsv
;
2131 index
= trans
->transid
& 0x1;
2132 if (block_rsv
->reserved
+ block_rsv
->freed
[index
] < block_rsv
->size
) {
2133 num_bytes
= block_rsv
->size
-
2134 (block_rsv
->reserved
+ block_rsv
->freed
[index
]);
2135 ret
= btrfs_block_rsv_migrate(&pending
->block_rsv
,
2136 root
->orphan_block_rsv
,
2141 /* setup orphan block reservation for the snapshot */
2142 block_rsv
= btrfs_alloc_block_rsv(snap
);
2145 btrfs_add_durable_block_rsv(root
->fs_info
, block_rsv
);
2146 snap
->orphan_block_rsv
= block_rsv
;
2148 num_bytes
= root
->orphan_block_rsv
->size
;
2149 ret
= btrfs_block_rsv_migrate(&pending
->block_rsv
,
2150 block_rsv
, num_bytes
);
2154 /* insert orphan item for the snapshot */
2155 WARN_ON(!root
->orphan_item_inserted
);
2156 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
2157 snap
->root_key
.objectid
);
2159 snap
->orphan_item_inserted
= 1;
2163 enum btrfs_orphan_cleanup_state
{
2164 ORPHAN_CLEANUP_STARTED
= 1,
2165 ORPHAN_CLEANUP_DONE
= 2,
2169 * This is called in transaction commmit time. If there are no orphan
2170 * files in the subvolume, it removes orphan item and frees block_rsv
2173 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
2174 struct btrfs_root
*root
)
2178 if (!list_empty(&root
->orphan_list
) ||
2179 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
2182 if (root
->orphan_item_inserted
&&
2183 btrfs_root_refs(&root
->root_item
) > 0) {
2184 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
2185 root
->root_key
.objectid
);
2187 root
->orphan_item_inserted
= 0;
2190 if (root
->orphan_block_rsv
) {
2191 WARN_ON(root
->orphan_block_rsv
->size
> 0);
2192 btrfs_free_block_rsv(root
, root
->orphan_block_rsv
);
2193 root
->orphan_block_rsv
= NULL
;
2198 * This creates an orphan entry for the given inode in case something goes
2199 * wrong in the middle of an unlink/truncate.
2201 * NOTE: caller of this function should reserve 5 units of metadata for
2204 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2206 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2207 struct btrfs_block_rsv
*block_rsv
= NULL
;
2212 if (!root
->orphan_block_rsv
) {
2213 block_rsv
= btrfs_alloc_block_rsv(root
);
2217 spin_lock(&root
->orphan_lock
);
2218 if (!root
->orphan_block_rsv
) {
2219 root
->orphan_block_rsv
= block_rsv
;
2220 } else if (block_rsv
) {
2221 btrfs_free_block_rsv(root
, block_rsv
);
2225 if (list_empty(&BTRFS_I(inode
)->i_orphan
)) {
2226 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
2229 * For proper ENOSPC handling, we should do orphan
2230 * cleanup when mounting. But this introduces backward
2231 * compatibility issue.
2233 if (!xchg(&root
->orphan_item_inserted
, 1))
2241 if (!BTRFS_I(inode
)->orphan_meta_reserved
) {
2242 BTRFS_I(inode
)->orphan_meta_reserved
= 1;
2245 spin_unlock(&root
->orphan_lock
);
2248 btrfs_add_durable_block_rsv(root
->fs_info
, block_rsv
);
2250 /* grab metadata reservation from transaction handle */
2252 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
2256 /* insert an orphan item to track this unlinked/truncated file */
2258 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
2262 /* insert an orphan item to track subvolume contains orphan files */
2264 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
2265 root
->root_key
.objectid
);
2272 * We have done the truncate/delete so we can go ahead and remove the orphan
2273 * item for this particular inode.
2275 int btrfs_orphan_del(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2277 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2278 int delete_item
= 0;
2279 int release_rsv
= 0;
2282 spin_lock(&root
->orphan_lock
);
2283 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
2284 list_del_init(&BTRFS_I(inode
)->i_orphan
);
2288 if (BTRFS_I(inode
)->orphan_meta_reserved
) {
2289 BTRFS_I(inode
)->orphan_meta_reserved
= 0;
2292 spin_unlock(&root
->orphan_lock
);
2294 if (trans
&& delete_item
) {
2295 ret
= btrfs_del_orphan_item(trans
, root
, btrfs_ino(inode
));
2300 btrfs_orphan_release_metadata(inode
);
2306 * this cleans up any orphans that may be left on the list from the last use
2309 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
2311 struct btrfs_path
*path
;
2312 struct extent_buffer
*leaf
;
2313 struct btrfs_key key
, found_key
;
2314 struct btrfs_trans_handle
*trans
;
2315 struct inode
*inode
;
2316 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
2318 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
2321 path
= btrfs_alloc_path();
2328 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
2329 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
2330 key
.offset
= (u64
)-1;
2333 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2338 * if ret == 0 means we found what we were searching for, which
2339 * is weird, but possible, so only screw with path if we didn't
2340 * find the key and see if we have stuff that matches
2344 if (path
->slots
[0] == 0)
2349 /* pull out the item */
2350 leaf
= path
->nodes
[0];
2351 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2353 /* make sure the item matches what we want */
2354 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
2356 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
2359 /* release the path since we're done with it */
2360 btrfs_release_path(root
, path
);
2363 * this is where we are basically btrfs_lookup, without the
2364 * crossing root thing. we store the inode number in the
2365 * offset of the orphan item.
2367 found_key
.objectid
= found_key
.offset
;
2368 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
2369 found_key
.offset
= 0;
2370 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
2371 if (IS_ERR(inode
)) {
2372 ret
= PTR_ERR(inode
);
2377 * add this inode to the orphan list so btrfs_orphan_del does
2378 * the proper thing when we hit it
2380 spin_lock(&root
->orphan_lock
);
2381 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
2382 spin_unlock(&root
->orphan_lock
);
2385 * if this is a bad inode, means we actually succeeded in
2386 * removing the inode, but not the orphan record, which means
2387 * we need to manually delete the orphan since iput will just
2388 * do a destroy_inode
2390 if (is_bad_inode(inode
)) {
2391 trans
= btrfs_start_transaction(root
, 0);
2392 if (IS_ERR(trans
)) {
2393 ret
= PTR_ERR(trans
);
2396 btrfs_orphan_del(trans
, inode
);
2397 btrfs_end_transaction(trans
, root
);
2402 /* if we have links, this was a truncate, lets do that */
2403 if (inode
->i_nlink
) {
2404 if (!S_ISREG(inode
->i_mode
)) {
2410 ret
= btrfs_truncate(inode
);
2415 /* this will do delete_inode and everything for us */
2420 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
2422 if (root
->orphan_block_rsv
)
2423 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
2426 if (root
->orphan_block_rsv
|| root
->orphan_item_inserted
) {
2427 trans
= btrfs_join_transaction(root
, 1);
2429 btrfs_end_transaction(trans
, root
);
2433 printk(KERN_INFO
"btrfs: unlinked %d orphans\n", nr_unlink
);
2435 printk(KERN_INFO
"btrfs: truncated %d orphans\n", nr_truncate
);
2439 printk(KERN_CRIT
"btrfs: could not do orphan cleanup %d\n", ret
);
2440 btrfs_free_path(path
);
2445 * very simple check to peek ahead in the leaf looking for xattrs. If we
2446 * don't find any xattrs, we know there can't be any acls.
2448 * slot is the slot the inode is in, objectid is the objectid of the inode
2450 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
2451 int slot
, u64 objectid
)
2453 u32 nritems
= btrfs_header_nritems(leaf
);
2454 struct btrfs_key found_key
;
2458 while (slot
< nritems
) {
2459 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2461 /* we found a different objectid, there must not be acls */
2462 if (found_key
.objectid
!= objectid
)
2465 /* we found an xattr, assume we've got an acl */
2466 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
)
2470 * we found a key greater than an xattr key, there can't
2471 * be any acls later on
2473 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
2480 * it goes inode, inode backrefs, xattrs, extents,
2481 * so if there are a ton of hard links to an inode there can
2482 * be a lot of backrefs. Don't waste time searching too hard,
2483 * this is just an optimization
2488 /* we hit the end of the leaf before we found an xattr or
2489 * something larger than an xattr. We have to assume the inode
2496 * read an inode from the btree into the in-memory inode
2498 static void btrfs_read_locked_inode(struct inode
*inode
)
2500 struct btrfs_path
*path
;
2501 struct extent_buffer
*leaf
;
2502 struct btrfs_inode_item
*inode_item
;
2503 struct btrfs_timespec
*tspec
;
2504 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2505 struct btrfs_key location
;
2507 u64 alloc_group_block
;
2511 path
= btrfs_alloc_path();
2513 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
2515 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
2519 leaf
= path
->nodes
[0];
2520 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2521 struct btrfs_inode_item
);
2523 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
2524 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
2525 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
2526 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
2527 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
2529 tspec
= btrfs_inode_atime(inode_item
);
2530 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2531 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2533 tspec
= btrfs_inode_mtime(inode_item
);
2534 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2535 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2537 tspec
= btrfs_inode_ctime(inode_item
);
2538 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2539 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2541 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
2542 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
2543 BTRFS_I(inode
)->sequence
= btrfs_inode_sequence(leaf
, inode_item
);
2544 inode
->i_generation
= BTRFS_I(inode
)->generation
;
2546 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
2548 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
2549 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
2551 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
2554 * try to precache a NULL acl entry for files that don't have
2555 * any xattrs or acls
2557 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
2560 cache_no_acl(inode
);
2562 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
, 0,
2563 alloc_group_block
, 0);
2564 btrfs_free_path(path
);
2567 switch (inode
->i_mode
& S_IFMT
) {
2569 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2570 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2571 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2572 inode
->i_fop
= &btrfs_file_operations
;
2573 inode
->i_op
= &btrfs_file_inode_operations
;
2576 inode
->i_fop
= &btrfs_dir_file_operations
;
2577 if (root
== root
->fs_info
->tree_root
)
2578 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
2580 inode
->i_op
= &btrfs_dir_inode_operations
;
2583 inode
->i_op
= &btrfs_symlink_inode_operations
;
2584 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2585 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2588 inode
->i_op
= &btrfs_special_inode_operations
;
2589 init_special_inode(inode
, inode
->i_mode
, rdev
);
2593 btrfs_update_iflags(inode
);
2597 btrfs_free_path(path
);
2598 make_bad_inode(inode
);
2602 * given a leaf and an inode, copy the inode fields into the leaf
2604 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
2605 struct extent_buffer
*leaf
,
2606 struct btrfs_inode_item
*item
,
2607 struct inode
*inode
)
2609 if (!leaf
->map_token
)
2610 map_private_extent_buffer(leaf
, (unsigned long)item
,
2611 sizeof(struct btrfs_inode_item
),
2612 &leaf
->map_token
, &leaf
->kaddr
,
2613 &leaf
->map_start
, &leaf
->map_len
,
2616 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
2617 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
2618 btrfs_set_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
);
2619 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
2620 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
2622 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
2623 inode
->i_atime
.tv_sec
);
2624 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
2625 inode
->i_atime
.tv_nsec
);
2627 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
2628 inode
->i_mtime
.tv_sec
);
2629 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
2630 inode
->i_mtime
.tv_nsec
);
2632 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
2633 inode
->i_ctime
.tv_sec
);
2634 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
2635 inode
->i_ctime
.tv_nsec
);
2637 btrfs_set_inode_nbytes(leaf
, item
, inode_get_bytes(inode
));
2638 btrfs_set_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
);
2639 btrfs_set_inode_sequence(leaf
, item
, BTRFS_I(inode
)->sequence
);
2640 btrfs_set_inode_transid(leaf
, item
, trans
->transid
);
2641 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
2642 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
2643 btrfs_set_inode_block_group(leaf
, item
, BTRFS_I(inode
)->block_group
);
2645 if (leaf
->map_token
) {
2646 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
2647 leaf
->map_token
= NULL
;
2652 * copy everything in the in-memory inode into the btree.
2654 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
2655 struct btrfs_root
*root
, struct inode
*inode
)
2657 struct btrfs_inode_item
*inode_item
;
2658 struct btrfs_path
*path
;
2659 struct extent_buffer
*leaf
;
2662 path
= btrfs_alloc_path();
2664 path
->leave_spinning
= 1;
2665 ret
= btrfs_lookup_inode(trans
, root
, path
,
2666 &BTRFS_I(inode
)->location
, 1);
2673 btrfs_unlock_up_safe(path
, 1);
2674 leaf
= path
->nodes
[0];
2675 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2676 struct btrfs_inode_item
);
2678 fill_inode_item(trans
, leaf
, inode_item
, inode
);
2679 btrfs_mark_buffer_dirty(leaf
);
2680 btrfs_set_inode_last_trans(trans
, inode
);
2683 btrfs_free_path(path
);
2689 * unlink helper that gets used here in inode.c and in the tree logging
2690 * recovery code. It remove a link in a directory with a given name, and
2691 * also drops the back refs in the inode to the directory
2693 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
2694 struct btrfs_root
*root
,
2695 struct inode
*dir
, struct inode
*inode
,
2696 const char *name
, int name_len
)
2698 struct btrfs_path
*path
;
2700 struct extent_buffer
*leaf
;
2701 struct btrfs_dir_item
*di
;
2702 struct btrfs_key key
;
2704 u64 ino
= btrfs_ino(inode
);
2705 u64 dir_ino
= btrfs_ino(dir
);
2707 path
= btrfs_alloc_path();
2713 path
->leave_spinning
= 1;
2714 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
2715 name
, name_len
, -1);
2724 leaf
= path
->nodes
[0];
2725 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2726 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2729 btrfs_release_path(root
, path
);
2731 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
2734 printk(KERN_INFO
"btrfs failed to delete reference to %.*s, "
2735 "inode %llu parent %llu\n", name_len
, name
,
2736 (unsigned long long)ino
, (unsigned long long)dir_ino
);
2740 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir_ino
,
2741 index
, name
, name_len
, -1);
2750 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2751 btrfs_release_path(root
, path
);
2753 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
2755 BUG_ON(ret
!= 0 && ret
!= -ENOENT
);
2757 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
2762 btrfs_free_path(path
);
2766 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
2767 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
2768 btrfs_update_inode(trans
, root
, dir
);
2773 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
2774 struct btrfs_root
*root
,
2775 struct inode
*dir
, struct inode
*inode
,
2776 const char *name
, int name_len
)
2779 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
2781 btrfs_drop_nlink(inode
);
2782 ret
= btrfs_update_inode(trans
, root
, inode
);
2788 /* helper to check if there is any shared block in the path */
2789 static int check_path_shared(struct btrfs_root
*root
,
2790 struct btrfs_path
*path
)
2792 struct extent_buffer
*eb
;
2796 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
2799 if (!path
->nodes
[level
])
2801 eb
= path
->nodes
[level
];
2802 if (!btrfs_block_can_be_shared(root
, eb
))
2804 ret
= btrfs_lookup_extent_info(NULL
, root
, eb
->start
, eb
->len
,
2813 * helper to start transaction for unlink and rmdir.
2815 * unlink and rmdir are special in btrfs, they do not always free space.
2816 * so in enospc case, we should make sure they will free space before
2817 * allowing them to use the global metadata reservation.
2819 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
,
2820 struct dentry
*dentry
)
2822 struct btrfs_trans_handle
*trans
;
2823 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2824 struct btrfs_path
*path
;
2825 struct btrfs_inode_ref
*ref
;
2826 struct btrfs_dir_item
*di
;
2827 struct inode
*inode
= dentry
->d_inode
;
2832 u64 ino
= btrfs_ino(inode
);
2833 u64 dir_ino
= btrfs_ino(dir
);
2835 trans
= btrfs_start_transaction(root
, 10);
2836 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
2839 if (ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
2840 return ERR_PTR(-ENOSPC
);
2842 /* check if there is someone else holds reference */
2843 if (S_ISDIR(inode
->i_mode
) && atomic_read(&inode
->i_count
) > 1)
2844 return ERR_PTR(-ENOSPC
);
2846 if (atomic_read(&inode
->i_count
) > 2)
2847 return ERR_PTR(-ENOSPC
);
2849 if (xchg(&root
->fs_info
->enospc_unlink
, 1))
2850 return ERR_PTR(-ENOSPC
);
2852 path
= btrfs_alloc_path();
2854 root
->fs_info
->enospc_unlink
= 0;
2855 return ERR_PTR(-ENOMEM
);
2858 trans
= btrfs_start_transaction(root
, 0);
2859 if (IS_ERR(trans
)) {
2860 btrfs_free_path(path
);
2861 root
->fs_info
->enospc_unlink
= 0;
2865 path
->skip_locking
= 1;
2866 path
->search_commit_root
= 1;
2868 ret
= btrfs_lookup_inode(trans
, root
, path
,
2869 &BTRFS_I(dir
)->location
, 0);
2875 if (check_path_shared(root
, path
))
2880 btrfs_release_path(root
, path
);
2882 ret
= btrfs_lookup_inode(trans
, root
, path
,
2883 &BTRFS_I(inode
)->location
, 0);
2889 if (check_path_shared(root
, path
))
2894 btrfs_release_path(root
, path
);
2896 if (ret
== 0 && S_ISREG(inode
->i_mode
)) {
2897 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
2904 if (check_path_shared(root
, path
))
2906 btrfs_release_path(root
, path
);
2914 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
2915 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
2921 if (check_path_shared(root
, path
))
2927 btrfs_release_path(root
, path
);
2929 ref
= btrfs_lookup_inode_ref(trans
, root
, path
,
2930 dentry
->d_name
.name
, dentry
->d_name
.len
,
2937 if (check_path_shared(root
, path
))
2939 index
= btrfs_inode_ref_index(path
->nodes
[0], ref
);
2940 btrfs_release_path(root
, path
);
2942 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir_ino
, index
,
2943 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
2948 BUG_ON(ret
== -ENOENT
);
2949 if (check_path_shared(root
, path
))
2954 btrfs_free_path(path
);
2956 btrfs_end_transaction(trans
, root
);
2957 root
->fs_info
->enospc_unlink
= 0;
2958 return ERR_PTR(err
);
2961 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
2965 static void __unlink_end_trans(struct btrfs_trans_handle
*trans
,
2966 struct btrfs_root
*root
)
2968 if (trans
->block_rsv
== &root
->fs_info
->global_block_rsv
) {
2969 BUG_ON(!root
->fs_info
->enospc_unlink
);
2970 root
->fs_info
->enospc_unlink
= 0;
2972 btrfs_end_transaction_throttle(trans
, root
);
2975 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
2977 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2978 struct btrfs_trans_handle
*trans
;
2979 struct inode
*inode
= dentry
->d_inode
;
2981 unsigned long nr
= 0;
2983 trans
= __unlink_start_trans(dir
, dentry
);
2985 return PTR_ERR(trans
);
2987 btrfs_set_trans_block_group(trans
, dir
);
2989 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
2991 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
2992 dentry
->d_name
.name
, dentry
->d_name
.len
);
2995 if (inode
->i_nlink
== 0) {
2996 ret
= btrfs_orphan_add(trans
, inode
);
3000 nr
= trans
->blocks_used
;
3001 __unlink_end_trans(trans
, root
);
3002 btrfs_btree_balance_dirty(root
, nr
);
3006 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
3007 struct btrfs_root
*root
,
3008 struct inode
*dir
, u64 objectid
,
3009 const char *name
, int name_len
)
3011 struct btrfs_path
*path
;
3012 struct extent_buffer
*leaf
;
3013 struct btrfs_dir_item
*di
;
3014 struct btrfs_key key
;
3017 u64 dir_ino
= btrfs_ino(dir
);
3019 path
= btrfs_alloc_path();
3023 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3024 name
, name_len
, -1);
3025 BUG_ON(!di
|| IS_ERR(di
));
3027 leaf
= path
->nodes
[0];
3028 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3029 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
3030 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3032 btrfs_release_path(root
, path
);
3034 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
3035 objectid
, root
->root_key
.objectid
,
3036 dir_ino
, &index
, name
, name_len
);
3038 BUG_ON(ret
!= -ENOENT
);
3039 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
3041 BUG_ON(!di
|| IS_ERR(di
));
3043 leaf
= path
->nodes
[0];
3044 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3045 btrfs_release_path(root
, path
);
3049 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir_ino
,
3050 index
, name
, name_len
, -1);
3051 BUG_ON(!di
|| IS_ERR(di
));
3053 leaf
= path
->nodes
[0];
3054 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3055 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
3056 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3058 btrfs_release_path(root
, path
);
3060 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3061 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3062 ret
= btrfs_update_inode(trans
, root
, dir
);
3065 btrfs_free_path(path
);
3069 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3071 struct inode
*inode
= dentry
->d_inode
;
3073 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3074 struct btrfs_trans_handle
*trans
;
3075 unsigned long nr
= 0;
3077 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
||
3078 btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
)
3081 trans
= __unlink_start_trans(dir
, dentry
);
3083 return PTR_ERR(trans
);
3085 btrfs_set_trans_block_group(trans
, dir
);
3087 if (unlikely(btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
3088 err
= btrfs_unlink_subvol(trans
, root
, dir
,
3089 BTRFS_I(inode
)->location
.objectid
,
3090 dentry
->d_name
.name
,
3091 dentry
->d_name
.len
);
3095 err
= btrfs_orphan_add(trans
, inode
);
3099 /* now the directory is empty */
3100 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3101 dentry
->d_name
.name
, dentry
->d_name
.len
);
3103 btrfs_i_size_write(inode
, 0);
3105 nr
= trans
->blocks_used
;
3106 __unlink_end_trans(trans
, root
);
3107 btrfs_btree_balance_dirty(root
, nr
);
3114 * when truncating bytes in a file, it is possible to avoid reading
3115 * the leaves that contain only checksum items. This can be the
3116 * majority of the IO required to delete a large file, but it must
3117 * be done carefully.
3119 * The keys in the level just above the leaves are checked to make sure
3120 * the lowest key in a given leaf is a csum key, and starts at an offset
3121 * after the new size.
3123 * Then the key for the next leaf is checked to make sure it also has
3124 * a checksum item for the same file. If it does, we know our target leaf
3125 * contains only checksum items, and it can be safely freed without reading
3128 * This is just an optimization targeted at large files. It may do
3129 * nothing. It will return 0 unless things went badly.
3131 static noinline
int drop_csum_leaves(struct btrfs_trans_handle
*trans
,
3132 struct btrfs_root
*root
,
3133 struct btrfs_path
*path
,
3134 struct inode
*inode
, u64 new_size
)
3136 struct btrfs_key key
;
3139 struct btrfs_key found_key
;
3140 struct btrfs_key other_key
;
3141 struct btrfs_leaf_ref
*ref
;
3145 path
->lowest_level
= 1;
3146 key
.objectid
= inode
->i_ino
;
3147 key
.type
= BTRFS_CSUM_ITEM_KEY
;
3148 key
.offset
= new_size
;
3150 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3154 if (path
->nodes
[1] == NULL
) {
3159 btrfs_node_key_to_cpu(path
->nodes
[1], &found_key
, path
->slots
[1]);
3160 nritems
= btrfs_header_nritems(path
->nodes
[1]);
3165 if (path
->slots
[1] >= nritems
)
3168 /* did we find a key greater than anything we want to delete? */
3169 if (found_key
.objectid
> inode
->i_ino
||
3170 (found_key
.objectid
== inode
->i_ino
&& found_key
.type
> key
.type
))
3173 /* we check the next key in the node to make sure the leave contains
3174 * only checksum items. This comparison doesn't work if our
3175 * leaf is the last one in the node
3177 if (path
->slots
[1] + 1 >= nritems
) {
3179 /* search forward from the last key in the node, this
3180 * will bring us into the next node in the tree
3182 btrfs_node_key_to_cpu(path
->nodes
[1], &found_key
, nritems
- 1);
3184 /* unlikely, but we inc below, so check to be safe */
3185 if (found_key
.offset
== (u64
)-1)
3188 /* search_forward needs a path with locks held, do the
3189 * search again for the original key. It is possible
3190 * this will race with a balance and return a path that
3191 * we could modify, but this drop is just an optimization
3192 * and is allowed to miss some leaves.
3194 btrfs_release_path(root
, path
);
3197 /* setup a max key for search_forward */
3198 other_key
.offset
= (u64
)-1;
3199 other_key
.type
= key
.type
;
3200 other_key
.objectid
= key
.objectid
;
3202 path
->keep_locks
= 1;
3203 ret
= btrfs_search_forward(root
, &found_key
, &other_key
,
3205 path
->keep_locks
= 0;
3206 if (ret
|| found_key
.objectid
!= key
.objectid
||
3207 found_key
.type
!= key
.type
) {
3212 key
.offset
= found_key
.offset
;
3213 btrfs_release_path(root
, path
);
3218 /* we know there's one more slot after us in the tree,
3219 * read that key so we can verify it is also a checksum item
3221 btrfs_node_key_to_cpu(path
->nodes
[1], &other_key
, path
->slots
[1] + 1);
3223 if (found_key
.objectid
< inode
->i_ino
)
3226 if (found_key
.type
!= key
.type
|| found_key
.offset
< new_size
)
3230 * if the key for the next leaf isn't a csum key from this objectid,
3231 * we can't be sure there aren't good items inside this leaf.
3234 if (other_key
.objectid
!= inode
->i_ino
|| other_key
.type
!= key
.type
)
3237 leaf_start
= btrfs_node_blockptr(path
->nodes
[1], path
->slots
[1]);
3238 leaf_gen
= btrfs_node_ptr_generation(path
->nodes
[1], path
->slots
[1]);
3240 * it is safe to delete this leaf, it contains only
3241 * csum items from this inode at an offset >= new_size
3243 ret
= btrfs_del_leaf(trans
, root
, path
, leaf_start
);
3246 if (root
->ref_cows
&& leaf_gen
< trans
->transid
) {
3247 ref
= btrfs_alloc_leaf_ref(root
, 0);
3249 ref
->root_gen
= root
->root_key
.offset
;
3250 ref
->bytenr
= leaf_start
;
3252 ref
->generation
= leaf_gen
;
3255 btrfs_sort_leaf_ref(ref
);
3257 ret
= btrfs_add_leaf_ref(root
, ref
, 0);
3259 btrfs_free_leaf_ref(root
, ref
);
3265 btrfs_release_path(root
, path
);
3267 if (other_key
.objectid
== inode
->i_ino
&&
3268 other_key
.type
== key
.type
&& other_key
.offset
> key
.offset
) {
3269 key
.offset
= other_key
.offset
;
3275 /* fixup any changes we've made to the path */
3276 path
->lowest_level
= 0;
3277 path
->keep_locks
= 0;
3278 btrfs_release_path(root
, path
);
3285 * this can truncate away extent items, csum items and directory items.
3286 * It starts at a high offset and removes keys until it can't find
3287 * any higher than new_size
3289 * csum items that cross the new i_size are truncated to the new size
3292 * min_type is the minimum key type to truncate down to. If set to 0, this
3293 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3295 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
3296 struct btrfs_root
*root
,
3297 struct inode
*inode
,
3298 u64 new_size
, u32 min_type
)
3300 struct btrfs_path
*path
;
3301 struct extent_buffer
*leaf
;
3302 struct btrfs_file_extent_item
*fi
;
3303 struct btrfs_key key
;
3304 struct btrfs_key found_key
;
3305 u64 extent_start
= 0;
3306 u64 extent_num_bytes
= 0;
3307 u64 extent_offset
= 0;
3309 u64 mask
= root
->sectorsize
- 1;
3310 u32 found_type
= (u8
)-1;
3313 int pending_del_nr
= 0;
3314 int pending_del_slot
= 0;
3315 int extent_type
= -1;
3319 u64 ino
= btrfs_ino(inode
);
3321 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
3323 if (root
->ref_cows
|| root
== root
->fs_info
->tree_root
)
3324 btrfs_drop_extent_cache(inode
, new_size
& (~mask
), (u64
)-1, 0);
3326 path
= btrfs_alloc_path();
3331 key
.offset
= (u64
)-1;
3335 path
->leave_spinning
= 1;
3336 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3343 /* there are no items in the tree for us to truncate, we're
3346 if (path
->slots
[0] == 0)
3353 leaf
= path
->nodes
[0];
3354 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3355 found_type
= btrfs_key_type(&found_key
);
3358 if (found_key
.objectid
!= ino
)
3361 if (found_type
< min_type
)
3364 item_end
= found_key
.offset
;
3365 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
3366 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3367 struct btrfs_file_extent_item
);
3368 extent_type
= btrfs_file_extent_type(leaf
, fi
);
3369 encoding
= btrfs_file_extent_compression(leaf
, fi
);
3370 encoding
|= btrfs_file_extent_encryption(leaf
, fi
);
3371 encoding
|= btrfs_file_extent_other_encoding(leaf
, fi
);
3373 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3375 btrfs_file_extent_num_bytes(leaf
, fi
);
3376 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3377 item_end
+= btrfs_file_extent_inline_len(leaf
,
3382 if (found_type
> min_type
) {
3385 if (item_end
< new_size
)
3387 if (found_key
.offset
>= new_size
)
3393 /* FIXME, shrink the extent if the ref count is only 1 */
3394 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
3397 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3399 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
3400 if (!del_item
&& !encoding
) {
3401 u64 orig_num_bytes
=
3402 btrfs_file_extent_num_bytes(leaf
, fi
);
3403 extent_num_bytes
= new_size
-
3404 found_key
.offset
+ root
->sectorsize
- 1;
3405 extent_num_bytes
= extent_num_bytes
&
3406 ~((u64
)root
->sectorsize
- 1);
3407 btrfs_set_file_extent_num_bytes(leaf
, fi
,
3409 num_dec
= (orig_num_bytes
-
3411 if (root
->ref_cows
&& extent_start
!= 0)
3412 inode_sub_bytes(inode
, num_dec
);
3413 btrfs_mark_buffer_dirty(leaf
);
3416 btrfs_file_extent_disk_num_bytes(leaf
,
3418 extent_offset
= found_key
.offset
-
3419 btrfs_file_extent_offset(leaf
, fi
);
3421 /* FIXME blocksize != 4096 */
3422 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
3423 if (extent_start
!= 0) {
3426 inode_sub_bytes(inode
, num_dec
);
3429 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3431 * we can't truncate inline items that have had
3435 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
3436 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
3437 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
3438 u32 size
= new_size
- found_key
.offset
;
3440 if (root
->ref_cows
) {
3441 inode_sub_bytes(inode
, item_end
+ 1 -
3445 btrfs_file_extent_calc_inline_size(size
);
3446 ret
= btrfs_truncate_item(trans
, root
, path
,
3449 } else if (root
->ref_cows
) {
3450 inode_sub_bytes(inode
, item_end
+ 1 -
3456 if (!pending_del_nr
) {
3457 /* no pending yet, add ourselves */
3458 pending_del_slot
= path
->slots
[0];
3460 } else if (pending_del_nr
&&
3461 path
->slots
[0] + 1 == pending_del_slot
) {
3462 /* hop on the pending chunk */
3464 pending_del_slot
= path
->slots
[0];
3471 if (found_extent
&& (root
->ref_cows
||
3472 root
== root
->fs_info
->tree_root
)) {
3473 btrfs_set_path_blocking(path
);
3474 ret
= btrfs_free_extent(trans
, root
, extent_start
,
3475 extent_num_bytes
, 0,
3476 btrfs_header_owner(leaf
),
3477 ino
, extent_offset
);
3481 if (found_type
== BTRFS_INODE_ITEM_KEY
)
3484 if (path
->slots
[0] == 0 ||
3485 path
->slots
[0] != pending_del_slot
) {
3486 if (root
->ref_cows
&&
3487 BTRFS_I(inode
)->location
.objectid
!=
3488 BTRFS_FREE_INO_OBJECTID
) {
3492 if (pending_del_nr
) {
3493 ret
= btrfs_del_items(trans
, root
, path
,
3499 btrfs_release_path(root
, path
);
3506 if (pending_del_nr
) {
3507 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
3511 btrfs_free_path(path
);
3516 * taken from block_truncate_page, but does cow as it zeros out
3517 * any bytes left in the last page in the file.
3519 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
3521 struct inode
*inode
= mapping
->host
;
3522 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3523 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3524 struct btrfs_ordered_extent
*ordered
;
3525 struct extent_state
*cached_state
= NULL
;
3527 u32 blocksize
= root
->sectorsize
;
3528 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
3529 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
3535 if ((offset
& (blocksize
- 1)) == 0)
3537 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
3543 page
= grab_cache_page(mapping
, index
);
3545 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
3549 page_start
= page_offset(page
);
3550 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3552 if (!PageUptodate(page
)) {
3553 ret
= btrfs_readpage(NULL
, page
);
3555 if (page
->mapping
!= mapping
) {
3557 page_cache_release(page
);
3560 if (!PageUptodate(page
)) {
3565 wait_on_page_writeback(page
);
3567 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
,
3569 set_page_extent_mapped(page
);
3571 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
3573 unlock_extent_cached(io_tree
, page_start
, page_end
,
3574 &cached_state
, GFP_NOFS
);
3576 page_cache_release(page
);
3577 btrfs_start_ordered_extent(inode
, ordered
, 1);
3578 btrfs_put_ordered_extent(ordered
);
3582 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
3583 EXTENT_DIRTY
| EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
,
3584 0, 0, &cached_state
, GFP_NOFS
);
3586 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
3589 unlock_extent_cached(io_tree
, page_start
, page_end
,
3590 &cached_state
, GFP_NOFS
);
3595 if (offset
!= PAGE_CACHE_SIZE
) {
3597 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
3598 flush_dcache_page(page
);
3601 ClearPageChecked(page
);
3602 set_page_dirty(page
);
3603 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
3608 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
3610 page_cache_release(page
);
3616 * This function puts in dummy file extents for the area we're creating a hole
3617 * for. So if we are truncating this file to a larger size we need to insert
3618 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3619 * the range between oldsize and size
3621 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
3623 struct btrfs_trans_handle
*trans
;
3624 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3625 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3626 struct extent_map
*em
= NULL
;
3627 struct extent_state
*cached_state
= NULL
;
3628 u64 mask
= root
->sectorsize
- 1;
3629 u64 hole_start
= (oldsize
+ mask
) & ~mask
;
3630 u64 block_end
= (size
+ mask
) & ~mask
;
3636 if (size
<= hole_start
)
3640 struct btrfs_ordered_extent
*ordered
;
3641 btrfs_wait_ordered_range(inode
, hole_start
,
3642 block_end
- hole_start
);
3643 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
3644 &cached_state
, GFP_NOFS
);
3645 ordered
= btrfs_lookup_ordered_extent(inode
, hole_start
);
3648 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
3649 &cached_state
, GFP_NOFS
);
3650 btrfs_put_ordered_extent(ordered
);
3653 cur_offset
= hole_start
;
3655 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
3656 block_end
- cur_offset
, 0);
3657 BUG_ON(IS_ERR(em
) || !em
);
3658 last_byte
= min(extent_map_end(em
), block_end
);
3659 last_byte
= (last_byte
+ mask
) & ~mask
;
3660 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
3662 hole_size
= last_byte
- cur_offset
;
3664 trans
= btrfs_start_transaction(root
, 2);
3665 if (IS_ERR(trans
)) {
3666 err
= PTR_ERR(trans
);
3669 btrfs_set_trans_block_group(trans
, inode
);
3671 err
= btrfs_drop_extents(trans
, inode
, cur_offset
,
3672 cur_offset
+ hole_size
,
3677 err
= btrfs_insert_file_extent(trans
, root
,
3678 btrfs_ino(inode
), cur_offset
, 0,
3679 0, hole_size
, 0, hole_size
,
3684 btrfs_drop_extent_cache(inode
, hole_start
,
3687 btrfs_end_transaction(trans
, root
);
3689 free_extent_map(em
);
3691 cur_offset
= last_byte
;
3692 if (cur_offset
>= block_end
)
3696 free_extent_map(em
);
3697 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
3702 static int btrfs_setsize(struct inode
*inode
, loff_t newsize
)
3704 loff_t oldsize
= i_size_read(inode
);
3707 if (newsize
== oldsize
)
3710 if (newsize
> oldsize
) {
3711 i_size_write(inode
, newsize
);
3712 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
3713 truncate_pagecache(inode
, oldsize
, newsize
);
3714 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
3716 btrfs_setsize(inode
, oldsize
);
3720 mark_inode_dirty(inode
);
3724 * We're truncating a file that used to have good data down to
3725 * zero. Make sure it gets into the ordered flush list so that
3726 * any new writes get down to disk quickly.
3729 BTRFS_I(inode
)->ordered_data_close
= 1;
3731 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3732 truncate_setsize(inode
, newsize
);
3733 ret
= btrfs_truncate(inode
);
3739 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
3741 struct inode
*inode
= dentry
->d_inode
;
3742 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3745 if (btrfs_root_readonly(root
))
3748 err
= inode_change_ok(inode
, attr
);
3752 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
3753 err
= btrfs_setsize(inode
, attr
->ia_size
);
3758 if (attr
->ia_valid
) {
3759 setattr_copy(inode
, attr
);
3760 mark_inode_dirty(inode
);
3762 if (attr
->ia_valid
& ATTR_MODE
)
3763 err
= btrfs_acl_chmod(inode
);
3769 void btrfs_evict_inode(struct inode
*inode
)
3771 struct btrfs_trans_handle
*trans
;
3772 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3776 trace_btrfs_inode_evict(inode
);
3778 truncate_inode_pages(&inode
->i_data
, 0);
3779 if (inode
->i_nlink
&& (btrfs_root_refs(&root
->root_item
) != 0 ||
3780 is_free_space_inode(root
, inode
)))
3783 if (is_bad_inode(inode
)) {
3784 btrfs_orphan_del(NULL
, inode
);
3787 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
3788 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
3790 if (root
->fs_info
->log_root_recovering
) {
3791 BUG_ON(!list_empty(&BTRFS_I(inode
)->i_orphan
));
3795 if (inode
->i_nlink
> 0) {
3796 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0);
3800 btrfs_i_size_write(inode
, 0);
3803 trans
= btrfs_start_transaction(root
, 0);
3804 BUG_ON(IS_ERR(trans
));
3805 btrfs_set_trans_block_group(trans
, inode
);
3806 trans
->block_rsv
= root
->orphan_block_rsv
;
3808 ret
= btrfs_block_rsv_check(trans
, root
,
3809 root
->orphan_block_rsv
, 0, 5);
3811 BUG_ON(ret
!= -EAGAIN
);
3812 ret
= btrfs_commit_transaction(trans
, root
);
3817 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
3821 nr
= trans
->blocks_used
;
3822 btrfs_end_transaction(trans
, root
);
3824 btrfs_btree_balance_dirty(root
, nr
);
3829 ret
= btrfs_orphan_del(trans
, inode
);
3833 if (!(root
== root
->fs_info
->tree_root
||
3834 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
3835 btrfs_return_ino(root
, btrfs_ino(inode
));
3837 nr
= trans
->blocks_used
;
3838 btrfs_end_transaction(trans
, root
);
3839 btrfs_btree_balance_dirty(root
, nr
);
3841 end_writeback(inode
);
3846 * this returns the key found in the dir entry in the location pointer.
3847 * If no dir entries were found, location->objectid is 0.
3849 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
3850 struct btrfs_key
*location
)
3852 const char *name
= dentry
->d_name
.name
;
3853 int namelen
= dentry
->d_name
.len
;
3854 struct btrfs_dir_item
*di
;
3855 struct btrfs_path
*path
;
3856 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3859 path
= btrfs_alloc_path();
3862 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(dir
), name
,
3867 if (!di
|| IS_ERR(di
))
3870 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
3872 btrfs_free_path(path
);
3875 location
->objectid
= 0;
3880 * when we hit a tree root in a directory, the btrfs part of the inode
3881 * needs to be changed to reflect the root directory of the tree root. This
3882 * is kind of like crossing a mount point.
3884 static int fixup_tree_root_location(struct btrfs_root
*root
,
3886 struct dentry
*dentry
,
3887 struct btrfs_key
*location
,
3888 struct btrfs_root
**sub_root
)
3890 struct btrfs_path
*path
;
3891 struct btrfs_root
*new_root
;
3892 struct btrfs_root_ref
*ref
;
3893 struct extent_buffer
*leaf
;
3897 path
= btrfs_alloc_path();
3904 ret
= btrfs_find_root_ref(root
->fs_info
->tree_root
, path
,
3905 BTRFS_I(dir
)->root
->root_key
.objectid
,
3906 location
->objectid
);
3913 leaf
= path
->nodes
[0];
3914 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
3915 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(dir
) ||
3916 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
3919 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
3920 (unsigned long)(ref
+ 1),
3921 dentry
->d_name
.len
);
3925 btrfs_release_path(root
->fs_info
->tree_root
, path
);
3927 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
3928 if (IS_ERR(new_root
)) {
3929 err
= PTR_ERR(new_root
);
3933 if (btrfs_root_refs(&new_root
->root_item
) == 0) {
3938 *sub_root
= new_root
;
3939 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
3940 location
->type
= BTRFS_INODE_ITEM_KEY
;
3941 location
->offset
= 0;
3944 btrfs_free_path(path
);
3948 static void inode_tree_add(struct inode
*inode
)
3950 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3951 struct btrfs_inode
*entry
;
3953 struct rb_node
*parent
;
3954 u64 ino
= btrfs_ino(inode
);
3956 p
= &root
->inode_tree
.rb_node
;
3959 if (inode_unhashed(inode
))
3962 spin_lock(&root
->inode_lock
);
3965 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
3967 if (ino
< btrfs_ino(&entry
->vfs_inode
))
3968 p
= &parent
->rb_left
;
3969 else if (ino
> btrfs_ino(&entry
->vfs_inode
))
3970 p
= &parent
->rb_right
;
3972 WARN_ON(!(entry
->vfs_inode
.i_state
&
3973 (I_WILL_FREE
| I_FREEING
)));
3974 rb_erase(parent
, &root
->inode_tree
);
3975 RB_CLEAR_NODE(parent
);
3976 spin_unlock(&root
->inode_lock
);
3980 rb_link_node(&BTRFS_I(inode
)->rb_node
, parent
, p
);
3981 rb_insert_color(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
3982 spin_unlock(&root
->inode_lock
);
3985 static void inode_tree_del(struct inode
*inode
)
3987 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3990 spin_lock(&root
->inode_lock
);
3991 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
3992 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
3993 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
3994 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
3996 spin_unlock(&root
->inode_lock
);
3999 * Free space cache has inodes in the tree root, but the tree root has a
4000 * root_refs of 0, so this could end up dropping the tree root as a
4001 * snapshot, so we need the extra !root->fs_info->tree_root check to
4002 * make sure we don't drop it.
4004 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0 &&
4005 root
!= root
->fs_info
->tree_root
) {
4006 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
4007 spin_lock(&root
->inode_lock
);
4008 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4009 spin_unlock(&root
->inode_lock
);
4011 btrfs_add_dead_root(root
);
4015 int btrfs_invalidate_inodes(struct btrfs_root
*root
)
4017 struct rb_node
*node
;
4018 struct rb_node
*prev
;
4019 struct btrfs_inode
*entry
;
4020 struct inode
*inode
;
4023 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
4025 spin_lock(&root
->inode_lock
);
4027 node
= root
->inode_tree
.rb_node
;
4031 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4033 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
4034 node
= node
->rb_left
;
4035 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
4036 node
= node
->rb_right
;
4042 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
4043 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
4047 prev
= rb_next(prev
);
4051 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4052 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
4053 inode
= igrab(&entry
->vfs_inode
);
4055 spin_unlock(&root
->inode_lock
);
4056 if (atomic_read(&inode
->i_count
) > 1)
4057 d_prune_aliases(inode
);
4059 * btrfs_drop_inode will have it removed from
4060 * the inode cache when its usage count
4065 spin_lock(&root
->inode_lock
);
4069 if (cond_resched_lock(&root
->inode_lock
))
4072 node
= rb_next(node
);
4074 spin_unlock(&root
->inode_lock
);
4078 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
4080 struct btrfs_iget_args
*args
= p
;
4081 inode
->i_ino
= args
->ino
;
4082 BTRFS_I(inode
)->root
= args
->root
;
4083 btrfs_set_inode_space_info(args
->root
, inode
);
4087 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
4089 struct btrfs_iget_args
*args
= opaque
;
4090 return args
->ino
== btrfs_ino(inode
) &&
4091 args
->root
== BTRFS_I(inode
)->root
;
4094 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
4096 struct btrfs_root
*root
)
4098 struct inode
*inode
;
4099 struct btrfs_iget_args args
;
4100 args
.ino
= objectid
;
4103 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
4104 btrfs_init_locked_inode
,
4109 /* Get an inode object given its location and corresponding root.
4110 * Returns in *is_new if the inode was read from disk
4112 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
4113 struct btrfs_root
*root
, int *new)
4115 struct inode
*inode
;
4117 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
4119 return ERR_PTR(-ENOMEM
);
4121 if (inode
->i_state
& I_NEW
) {
4122 BTRFS_I(inode
)->root
= root
;
4123 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
4124 btrfs_read_locked_inode(inode
);
4125 inode_tree_add(inode
);
4126 unlock_new_inode(inode
);
4134 static struct inode
*new_simple_dir(struct super_block
*s
,
4135 struct btrfs_key
*key
,
4136 struct btrfs_root
*root
)
4138 struct inode
*inode
= new_inode(s
);
4141 return ERR_PTR(-ENOMEM
);
4143 BTRFS_I(inode
)->root
= root
;
4144 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
4145 BTRFS_I(inode
)->dummy_inode
= 1;
4147 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
4148 inode
->i_op
= &simple_dir_inode_operations
;
4149 inode
->i_fop
= &simple_dir_operations
;
4150 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
4151 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4156 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
4158 struct inode
*inode
;
4159 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4160 struct btrfs_root
*sub_root
= root
;
4161 struct btrfs_key location
;
4165 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
4166 return ERR_PTR(-ENAMETOOLONG
);
4168 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
4171 return ERR_PTR(ret
);
4173 if (location
.objectid
== 0)
4176 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
4177 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
4181 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
4183 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
4184 ret
= fixup_tree_root_location(root
, dir
, dentry
,
4185 &location
, &sub_root
);
4188 inode
= ERR_PTR(ret
);
4190 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
4192 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
4194 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
4196 if (!IS_ERR(inode
) && root
!= sub_root
) {
4197 down_read(&root
->fs_info
->cleanup_work_sem
);
4198 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
4199 ret
= btrfs_orphan_cleanup(sub_root
);
4200 up_read(&root
->fs_info
->cleanup_work_sem
);
4202 inode
= ERR_PTR(ret
);
4208 static int btrfs_dentry_delete(const struct dentry
*dentry
)
4210 struct btrfs_root
*root
;
4212 if (!dentry
->d_inode
&& !IS_ROOT(dentry
))
4213 dentry
= dentry
->d_parent
;
4215 if (dentry
->d_inode
) {
4216 root
= BTRFS_I(dentry
->d_inode
)->root
;
4217 if (btrfs_root_refs(&root
->root_item
) == 0)
4223 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
4224 struct nameidata
*nd
)
4226 struct inode
*inode
;
4228 inode
= btrfs_lookup_dentry(dir
, dentry
);
4230 return ERR_CAST(inode
);
4232 return d_splice_alias(inode
, dentry
);
4235 static unsigned char btrfs_filetype_table
[] = {
4236 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
4239 static int btrfs_real_readdir(struct file
*filp
, void *dirent
,
4242 struct inode
*inode
= filp
->f_dentry
->d_inode
;
4243 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4244 struct btrfs_item
*item
;
4245 struct btrfs_dir_item
*di
;
4246 struct btrfs_key key
;
4247 struct btrfs_key found_key
;
4248 struct btrfs_path
*path
;
4250 struct extent_buffer
*leaf
;
4252 unsigned char d_type
;
4257 int key_type
= BTRFS_DIR_INDEX_KEY
;
4262 /* FIXME, use a real flag for deciding about the key type */
4263 if (root
->fs_info
->tree_root
== root
)
4264 key_type
= BTRFS_DIR_ITEM_KEY
;
4266 /* special case for "." */
4267 if (filp
->f_pos
== 0) {
4268 over
= filldir(dirent
, ".", 1, 1, btrfs_ino(inode
), DT_DIR
);
4273 /* special case for .., just use the back ref */
4274 if (filp
->f_pos
== 1) {
4275 u64 pino
= parent_ino(filp
->f_path
.dentry
);
4276 over
= filldir(dirent
, "..", 2,
4282 path
= btrfs_alloc_path();
4285 btrfs_set_key_type(&key
, key_type
);
4286 key
.offset
= filp
->f_pos
;
4287 key
.objectid
= btrfs_ino(inode
);
4289 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4294 leaf
= path
->nodes
[0];
4295 slot
= path
->slots
[0];
4296 if (slot
>= btrfs_header_nritems(leaf
)) {
4297 ret
= btrfs_next_leaf(root
, path
);
4305 item
= btrfs_item_nr(leaf
, slot
);
4306 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
4308 if (found_key
.objectid
!= key
.objectid
)
4310 if (btrfs_key_type(&found_key
) != key_type
)
4312 if (found_key
.offset
< filp
->f_pos
)
4315 filp
->f_pos
= found_key
.offset
;
4317 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
4319 di_total
= btrfs_item_size(leaf
, item
);
4321 while (di_cur
< di_total
) {
4322 struct btrfs_key location
;
4324 if (verify_dir_item(root
, leaf
, di
))
4327 name_len
= btrfs_dir_name_len(leaf
, di
);
4328 if (name_len
<= sizeof(tmp_name
)) {
4329 name_ptr
= tmp_name
;
4331 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
4337 read_extent_buffer(leaf
, name_ptr
,
4338 (unsigned long)(di
+ 1), name_len
);
4340 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
4341 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
4343 /* is this a reference to our own snapshot? If so
4346 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
4347 location
.objectid
== root
->root_key
.objectid
) {
4351 over
= filldir(dirent
, name_ptr
, name_len
,
4352 found_key
.offset
, location
.objectid
,
4356 if (name_ptr
!= tmp_name
)
4361 di_len
= btrfs_dir_name_len(leaf
, di
) +
4362 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
4364 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
4370 /* Reached end of directory/root. Bump pos past the last item. */
4371 if (key_type
== BTRFS_DIR_INDEX_KEY
)
4373 * 32-bit glibc will use getdents64, but then strtol -
4374 * so the last number we can serve is this.
4376 filp
->f_pos
= 0x7fffffff;
4382 btrfs_free_path(path
);
4386 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
4388 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4389 struct btrfs_trans_handle
*trans
;
4391 bool nolock
= false;
4393 if (BTRFS_I(inode
)->dummy_inode
)
4397 if (root
->fs_info
->closing
&& is_free_space_inode(root
, inode
))
4400 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
4402 trans
= btrfs_join_transaction_nolock(root
, 1);
4404 trans
= btrfs_join_transaction(root
, 1);
4406 return PTR_ERR(trans
);
4407 btrfs_set_trans_block_group(trans
, inode
);
4409 ret
= btrfs_end_transaction_nolock(trans
, root
);
4411 ret
= btrfs_commit_transaction(trans
, root
);
4417 * This is somewhat expensive, updating the tree every time the
4418 * inode changes. But, it is most likely to find the inode in cache.
4419 * FIXME, needs more benchmarking...there are no reasons other than performance
4420 * to keep or drop this code.
4422 void btrfs_dirty_inode(struct inode
*inode
)
4424 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4425 struct btrfs_trans_handle
*trans
;
4428 if (BTRFS_I(inode
)->dummy_inode
)
4431 trans
= btrfs_join_transaction(root
, 1);
4432 BUG_ON(IS_ERR(trans
));
4433 btrfs_set_trans_block_group(trans
, inode
);
4435 ret
= btrfs_update_inode(trans
, root
, inode
);
4436 if (ret
&& ret
== -ENOSPC
) {
4437 /* whoops, lets try again with the full transaction */
4438 btrfs_end_transaction(trans
, root
);
4439 trans
= btrfs_start_transaction(root
, 1);
4440 if (IS_ERR(trans
)) {
4441 if (printk_ratelimit()) {
4442 printk(KERN_ERR
"btrfs: fail to "
4443 "dirty inode %llu error %ld\n",
4444 (unsigned long long)btrfs_ino(inode
),
4449 btrfs_set_trans_block_group(trans
, inode
);
4451 ret
= btrfs_update_inode(trans
, root
, inode
);
4453 if (printk_ratelimit()) {
4454 printk(KERN_ERR
"btrfs: fail to "
4455 "dirty inode %llu error %d\n",
4456 (unsigned long long)btrfs_ino(inode
),
4461 btrfs_end_transaction(trans
, root
);
4465 * find the highest existing sequence number in a directory
4466 * and then set the in-memory index_cnt variable to reflect
4467 * free sequence numbers
4469 static int btrfs_set_inode_index_count(struct inode
*inode
)
4471 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4472 struct btrfs_key key
, found_key
;
4473 struct btrfs_path
*path
;
4474 struct extent_buffer
*leaf
;
4477 key
.objectid
= btrfs_ino(inode
);
4478 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
4479 key
.offset
= (u64
)-1;
4481 path
= btrfs_alloc_path();
4485 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4488 /* FIXME: we should be able to handle this */
4494 * MAGIC NUMBER EXPLANATION:
4495 * since we search a directory based on f_pos we have to start at 2
4496 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4497 * else has to start at 2
4499 if (path
->slots
[0] == 0) {
4500 BTRFS_I(inode
)->index_cnt
= 2;
4506 leaf
= path
->nodes
[0];
4507 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4509 if (found_key
.objectid
!= btrfs_ino(inode
) ||
4510 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
4511 BTRFS_I(inode
)->index_cnt
= 2;
4515 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
4517 btrfs_free_path(path
);
4522 * helper to find a free sequence number in a given directory. This current
4523 * code is very simple, later versions will do smarter things in the btree
4525 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
4529 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
4530 ret
= btrfs_set_inode_index_count(dir
);
4535 *index
= BTRFS_I(dir
)->index_cnt
;
4536 BTRFS_I(dir
)->index_cnt
++;
4541 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
4542 struct btrfs_root
*root
,
4544 const char *name
, int name_len
,
4545 u64 ref_objectid
, u64 objectid
,
4546 u64 alloc_hint
, int mode
, u64
*index
)
4548 struct inode
*inode
;
4549 struct btrfs_inode_item
*inode_item
;
4550 struct btrfs_key
*location
;
4551 struct btrfs_path
*path
;
4552 struct btrfs_inode_ref
*ref
;
4553 struct btrfs_key key
[2];
4559 path
= btrfs_alloc_path();
4562 inode
= new_inode(root
->fs_info
->sb
);
4564 btrfs_free_path(path
);
4565 return ERR_PTR(-ENOMEM
);
4569 * we have to initialize this early, so we can reclaim the inode
4570 * number if we fail afterwards in this function.
4572 inode
->i_ino
= objectid
;
4575 trace_btrfs_inode_request(dir
);
4577 ret
= btrfs_set_inode_index(dir
, index
);
4579 btrfs_free_path(path
);
4581 return ERR_PTR(ret
);
4585 * index_cnt is ignored for everything but a dir,
4586 * btrfs_get_inode_index_count has an explanation for the magic
4589 BTRFS_I(inode
)->index_cnt
= 2;
4590 BTRFS_I(inode
)->root
= root
;
4591 BTRFS_I(inode
)->generation
= trans
->transid
;
4592 inode
->i_generation
= BTRFS_I(inode
)->generation
;
4593 btrfs_set_inode_space_info(root
, inode
);
4599 BTRFS_I(inode
)->block_group
=
4600 btrfs_find_block_group(root
, 0, alloc_hint
, owner
);
4602 key
[0].objectid
= objectid
;
4603 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
4606 key
[1].objectid
= objectid
;
4607 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
4608 key
[1].offset
= ref_objectid
;
4610 sizes
[0] = sizeof(struct btrfs_inode_item
);
4611 sizes
[1] = name_len
+ sizeof(*ref
);
4613 path
->leave_spinning
= 1;
4614 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
4618 inode_init_owner(inode
, dir
, mode
);
4619 inode_set_bytes(inode
, 0);
4620 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4621 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4622 struct btrfs_inode_item
);
4623 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
4625 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
4626 struct btrfs_inode_ref
);
4627 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
4628 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
4629 ptr
= (unsigned long)(ref
+ 1);
4630 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
4632 btrfs_mark_buffer_dirty(path
->nodes
[0]);
4633 btrfs_free_path(path
);
4635 location
= &BTRFS_I(inode
)->location
;
4636 location
->objectid
= objectid
;
4637 location
->offset
= 0;
4638 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
4640 btrfs_inherit_iflags(inode
, dir
);
4642 if ((mode
& S_IFREG
)) {
4643 if (btrfs_test_opt(root
, NODATASUM
))
4644 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
4645 if (btrfs_test_opt(root
, NODATACOW
) ||
4646 (BTRFS_I(dir
)->flags
& BTRFS_INODE_NODATACOW
))
4647 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
;
4650 insert_inode_hash(inode
);
4651 inode_tree_add(inode
);
4653 trace_btrfs_inode_new(inode
);
4658 BTRFS_I(dir
)->index_cnt
--;
4659 btrfs_free_path(path
);
4661 return ERR_PTR(ret
);
4664 static inline u8
btrfs_inode_type(struct inode
*inode
)
4666 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
4670 * utility function to add 'inode' into 'parent_inode' with
4671 * a give name and a given sequence number.
4672 * if 'add_backref' is true, also insert a backref from the
4673 * inode to the parent directory.
4675 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
4676 struct inode
*parent_inode
, struct inode
*inode
,
4677 const char *name
, int name_len
, int add_backref
, u64 index
)
4680 struct btrfs_key key
;
4681 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
4682 u64 ino
= btrfs_ino(inode
);
4683 u64 parent_ino
= btrfs_ino(parent_inode
);
4685 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4686 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
4689 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
4693 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4694 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
4695 key
.objectid
, root
->root_key
.objectid
,
4696 parent_ino
, index
, name
, name_len
);
4697 } else if (add_backref
) {
4698 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
4703 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
4705 btrfs_inode_type(inode
), index
);
4708 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
4710 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
4711 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
4716 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
4717 struct inode
*dir
, struct dentry
*dentry
,
4718 struct inode
*inode
, int backref
, u64 index
)
4720 int err
= btrfs_add_link(trans
, dir
, inode
,
4721 dentry
->d_name
.name
, dentry
->d_name
.len
,
4724 d_instantiate(dentry
, inode
);
4732 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
4733 int mode
, dev_t rdev
)
4735 struct btrfs_trans_handle
*trans
;
4736 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4737 struct inode
*inode
= NULL
;
4741 unsigned long nr
= 0;
4744 if (!new_valid_dev(rdev
))
4748 * 2 for inode item and ref
4750 * 1 for xattr if selinux is on
4752 trans
= btrfs_start_transaction(root
, 5);
4754 return PTR_ERR(trans
);
4756 btrfs_set_trans_block_group(trans
, dir
);
4758 err
= btrfs_find_free_ino(root
, &objectid
);
4762 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4763 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
4764 BTRFS_I(dir
)->block_group
, mode
, &index
);
4765 if (IS_ERR(inode
)) {
4766 err
= PTR_ERR(inode
);
4770 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
4776 btrfs_set_trans_block_group(trans
, inode
);
4777 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
4781 inode
->i_op
= &btrfs_special_inode_operations
;
4782 init_special_inode(inode
, inode
->i_mode
, rdev
);
4783 btrfs_update_inode(trans
, root
, inode
);
4785 btrfs_update_inode_block_group(trans
, inode
);
4786 btrfs_update_inode_block_group(trans
, dir
);
4788 nr
= trans
->blocks_used
;
4789 btrfs_end_transaction_throttle(trans
, root
);
4790 btrfs_btree_balance_dirty(root
, nr
);
4792 inode_dec_link_count(inode
);
4798 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
4799 int mode
, struct nameidata
*nd
)
4801 struct btrfs_trans_handle
*trans
;
4802 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4803 struct inode
*inode
= NULL
;
4806 unsigned long nr
= 0;
4811 * 2 for inode item and ref
4813 * 1 for xattr if selinux is on
4815 trans
= btrfs_start_transaction(root
, 5);
4817 return PTR_ERR(trans
);
4819 btrfs_set_trans_block_group(trans
, dir
);
4821 err
= btrfs_find_free_ino(root
, &objectid
);
4825 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4826 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
4827 BTRFS_I(dir
)->block_group
, mode
, &index
);
4828 if (IS_ERR(inode
)) {
4829 err
= PTR_ERR(inode
);
4833 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
4839 btrfs_set_trans_block_group(trans
, inode
);
4840 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
4844 inode
->i_mapping
->a_ops
= &btrfs_aops
;
4845 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
4846 inode
->i_fop
= &btrfs_file_operations
;
4847 inode
->i_op
= &btrfs_file_inode_operations
;
4848 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
4850 btrfs_update_inode_block_group(trans
, inode
);
4851 btrfs_update_inode_block_group(trans
, dir
);
4853 nr
= trans
->blocks_used
;
4854 btrfs_end_transaction_throttle(trans
, root
);
4856 inode_dec_link_count(inode
);
4859 btrfs_btree_balance_dirty(root
, nr
);
4863 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
4864 struct dentry
*dentry
)
4866 struct btrfs_trans_handle
*trans
;
4867 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4868 struct inode
*inode
= old_dentry
->d_inode
;
4870 unsigned long nr
= 0;
4874 /* do not allow sys_link's with other subvols of the same device */
4875 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
4878 if (inode
->i_nlink
== ~0U)
4881 err
= btrfs_set_inode_index(dir
, &index
);
4886 * 2 items for inode and inode ref
4887 * 2 items for dir items
4888 * 1 item for parent inode
4890 trans
= btrfs_start_transaction(root
, 5);
4891 if (IS_ERR(trans
)) {
4892 err
= PTR_ERR(trans
);
4896 btrfs_inc_nlink(inode
);
4897 inode
->i_ctime
= CURRENT_TIME
;
4899 btrfs_set_trans_block_group(trans
, dir
);
4902 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
4907 struct dentry
*parent
= dget_parent(dentry
);
4908 btrfs_update_inode_block_group(trans
, dir
);
4909 err
= btrfs_update_inode(trans
, root
, inode
);
4911 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
4915 nr
= trans
->blocks_used
;
4916 btrfs_end_transaction_throttle(trans
, root
);
4919 inode_dec_link_count(inode
);
4922 btrfs_btree_balance_dirty(root
, nr
);
4926 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
4928 struct inode
*inode
= NULL
;
4929 struct btrfs_trans_handle
*trans
;
4930 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4932 int drop_on_err
= 0;
4935 unsigned long nr
= 1;
4938 * 2 items for inode and ref
4939 * 2 items for dir items
4940 * 1 for xattr if selinux is on
4942 trans
= btrfs_start_transaction(root
, 5);
4944 return PTR_ERR(trans
);
4945 btrfs_set_trans_block_group(trans
, dir
);
4947 err
= btrfs_find_free_ino(root
, &objectid
);
4951 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4952 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
4953 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
,
4955 if (IS_ERR(inode
)) {
4956 err
= PTR_ERR(inode
);
4962 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
4966 inode
->i_op
= &btrfs_dir_inode_operations
;
4967 inode
->i_fop
= &btrfs_dir_file_operations
;
4968 btrfs_set_trans_block_group(trans
, inode
);
4970 btrfs_i_size_write(inode
, 0);
4971 err
= btrfs_update_inode(trans
, root
, inode
);
4975 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
4976 dentry
->d_name
.len
, 0, index
);
4980 d_instantiate(dentry
, inode
);
4982 btrfs_update_inode_block_group(trans
, inode
);
4983 btrfs_update_inode_block_group(trans
, dir
);
4986 nr
= trans
->blocks_used
;
4987 btrfs_end_transaction_throttle(trans
, root
);
4990 btrfs_btree_balance_dirty(root
, nr
);
4994 /* helper for btfs_get_extent. Given an existing extent in the tree,
4995 * and an extent that you want to insert, deal with overlap and insert
4996 * the new extent into the tree.
4998 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
4999 struct extent_map
*existing
,
5000 struct extent_map
*em
,
5001 u64 map_start
, u64 map_len
)
5005 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
5006 start_diff
= map_start
- em
->start
;
5007 em
->start
= map_start
;
5009 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
5010 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
5011 em
->block_start
+= start_diff
;
5012 em
->block_len
-= start_diff
;
5014 return add_extent_mapping(em_tree
, em
);
5017 static noinline
int uncompress_inline(struct btrfs_path
*path
,
5018 struct inode
*inode
, struct page
*page
,
5019 size_t pg_offset
, u64 extent_offset
,
5020 struct btrfs_file_extent_item
*item
)
5023 struct extent_buffer
*leaf
= path
->nodes
[0];
5026 unsigned long inline_size
;
5030 WARN_ON(pg_offset
!= 0);
5031 compress_type
= btrfs_file_extent_compression(leaf
, item
);
5032 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
5033 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
5034 btrfs_item_nr(leaf
, path
->slots
[0]));
5035 tmp
= kmalloc(inline_size
, GFP_NOFS
);
5038 ptr
= btrfs_file_extent_inline_start(item
);
5040 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
5042 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
5043 ret
= btrfs_decompress(compress_type
, tmp
, page
,
5044 extent_offset
, inline_size
, max_size
);
5046 char *kaddr
= kmap_atomic(page
, KM_USER0
);
5047 unsigned long copy_size
= min_t(u64
,
5048 PAGE_CACHE_SIZE
- pg_offset
,
5049 max_size
- extent_offset
);
5050 memset(kaddr
+ pg_offset
, 0, copy_size
);
5051 kunmap_atomic(kaddr
, KM_USER0
);
5058 * a bit scary, this does extent mapping from logical file offset to the disk.
5059 * the ugly parts come from merging extents from the disk with the in-ram
5060 * representation. This gets more complex because of the data=ordered code,
5061 * where the in-ram extents might be locked pending data=ordered completion.
5063 * This also copies inline extents directly into the page.
5066 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
5067 size_t pg_offset
, u64 start
, u64 len
,
5073 u64 extent_start
= 0;
5075 u64 objectid
= btrfs_ino(inode
);
5077 struct btrfs_path
*path
= NULL
;
5078 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5079 struct btrfs_file_extent_item
*item
;
5080 struct extent_buffer
*leaf
;
5081 struct btrfs_key found_key
;
5082 struct extent_map
*em
= NULL
;
5083 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
5084 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
5085 struct btrfs_trans_handle
*trans
= NULL
;
5089 read_lock(&em_tree
->lock
);
5090 em
= lookup_extent_mapping(em_tree
, start
, len
);
5092 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5093 read_unlock(&em_tree
->lock
);
5096 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
5097 free_extent_map(em
);
5098 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
5099 free_extent_map(em
);
5103 em
= alloc_extent_map(GFP_NOFS
);
5108 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5109 em
->start
= EXTENT_MAP_HOLE
;
5110 em
->orig_start
= EXTENT_MAP_HOLE
;
5112 em
->block_len
= (u64
)-1;
5115 path
= btrfs_alloc_path();
5119 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
5120 objectid
, start
, trans
!= NULL
);
5127 if (path
->slots
[0] == 0)
5132 leaf
= path
->nodes
[0];
5133 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5134 struct btrfs_file_extent_item
);
5135 /* are we inside the extent that was found? */
5136 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5137 found_type
= btrfs_key_type(&found_key
);
5138 if (found_key
.objectid
!= objectid
||
5139 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
5143 found_type
= btrfs_file_extent_type(leaf
, item
);
5144 extent_start
= found_key
.offset
;
5145 compress_type
= btrfs_file_extent_compression(leaf
, item
);
5146 if (found_type
== BTRFS_FILE_EXTENT_REG
||
5147 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5148 extent_end
= extent_start
+
5149 btrfs_file_extent_num_bytes(leaf
, item
);
5150 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
5152 size
= btrfs_file_extent_inline_len(leaf
, item
);
5153 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
5154 ~((u64
)root
->sectorsize
- 1);
5157 if (start
>= extent_end
) {
5159 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
5160 ret
= btrfs_next_leaf(root
, path
);
5167 leaf
= path
->nodes
[0];
5169 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5170 if (found_key
.objectid
!= objectid
||
5171 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
5173 if (start
+ len
<= found_key
.offset
)
5176 em
->len
= found_key
.offset
- start
;
5180 if (found_type
== BTRFS_FILE_EXTENT_REG
||
5181 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5182 em
->start
= extent_start
;
5183 em
->len
= extent_end
- extent_start
;
5184 em
->orig_start
= extent_start
-
5185 btrfs_file_extent_offset(leaf
, item
);
5186 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
5188 em
->block_start
= EXTENT_MAP_HOLE
;
5191 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
5192 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
5193 em
->compress_type
= compress_type
;
5194 em
->block_start
= bytenr
;
5195 em
->block_len
= btrfs_file_extent_disk_num_bytes(leaf
,
5198 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
5199 em
->block_start
= bytenr
;
5200 em
->block_len
= em
->len
;
5201 if (found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
5202 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
5205 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
5209 size_t extent_offset
;
5212 em
->block_start
= EXTENT_MAP_INLINE
;
5213 if (!page
|| create
) {
5214 em
->start
= extent_start
;
5215 em
->len
= extent_end
- extent_start
;
5219 size
= btrfs_file_extent_inline_len(leaf
, item
);
5220 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
5221 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
5222 size
- extent_offset
);
5223 em
->start
= extent_start
+ extent_offset
;
5224 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
5225 ~((u64
)root
->sectorsize
- 1);
5226 em
->orig_start
= EXTENT_MAP_INLINE
;
5227 if (compress_type
) {
5228 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
5229 em
->compress_type
= compress_type
;
5231 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
5232 if (create
== 0 && !PageUptodate(page
)) {
5233 if (btrfs_file_extent_compression(leaf
, item
) !=
5234 BTRFS_COMPRESS_NONE
) {
5235 ret
= uncompress_inline(path
, inode
, page
,
5237 extent_offset
, item
);
5241 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
5243 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
5244 memset(map
+ pg_offset
+ copy_size
, 0,
5245 PAGE_CACHE_SIZE
- pg_offset
-
5250 flush_dcache_page(page
);
5251 } else if (create
&& PageUptodate(page
)) {
5255 free_extent_map(em
);
5257 btrfs_release_path(root
, path
);
5258 trans
= btrfs_join_transaction(root
, 1);
5260 return ERR_CAST(trans
);
5264 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
5267 btrfs_mark_buffer_dirty(leaf
);
5269 set_extent_uptodate(io_tree
, em
->start
,
5270 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
5273 printk(KERN_ERR
"btrfs unknown found_type %d\n", found_type
);
5280 em
->block_start
= EXTENT_MAP_HOLE
;
5281 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
5283 btrfs_release_path(root
, path
);
5284 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
5285 printk(KERN_ERR
"Btrfs: bad extent! em: [%llu %llu] passed "
5286 "[%llu %llu]\n", (unsigned long long)em
->start
,
5287 (unsigned long long)em
->len
,
5288 (unsigned long long)start
,
5289 (unsigned long long)len
);
5295 write_lock(&em_tree
->lock
);
5296 ret
= add_extent_mapping(em_tree
, em
);
5297 /* it is possible that someone inserted the extent into the tree
5298 * while we had the lock dropped. It is also possible that
5299 * an overlapping map exists in the tree
5301 if (ret
== -EEXIST
) {
5302 struct extent_map
*existing
;
5306 existing
= lookup_extent_mapping(em_tree
, start
, len
);
5307 if (existing
&& (existing
->start
> start
||
5308 existing
->start
+ existing
->len
<= start
)) {
5309 free_extent_map(existing
);
5313 existing
= lookup_extent_mapping(em_tree
, em
->start
,
5316 err
= merge_extent_mapping(em_tree
, existing
,
5319 free_extent_map(existing
);
5321 free_extent_map(em
);
5326 free_extent_map(em
);
5330 free_extent_map(em
);
5335 write_unlock(&em_tree
->lock
);
5338 trace_btrfs_get_extent(root
, em
);
5341 btrfs_free_path(path
);
5343 ret
= btrfs_end_transaction(trans
, root
);
5348 free_extent_map(em
);
5349 return ERR_PTR(err
);
5354 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
5355 size_t pg_offset
, u64 start
, u64 len
,
5358 struct extent_map
*em
;
5359 struct extent_map
*hole_em
= NULL
;
5360 u64 range_start
= start
;
5366 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
5371 * if our em maps to a hole, there might
5372 * actually be delalloc bytes behind it
5374 if (em
->block_start
!= EXTENT_MAP_HOLE
)
5380 /* check to see if we've wrapped (len == -1 or similar) */
5389 /* ok, we didn't find anything, lets look for delalloc */
5390 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
5391 end
, len
, EXTENT_DELALLOC
, 1);
5392 found_end
= range_start
+ found
;
5393 if (found_end
< range_start
)
5394 found_end
= (u64
)-1;
5397 * we didn't find anything useful, return
5398 * the original results from get_extent()
5400 if (range_start
> end
|| found_end
<= start
) {
5406 /* adjust the range_start to make sure it doesn't
5407 * go backwards from the start they passed in
5409 range_start
= max(start
,range_start
);
5410 found
= found_end
- range_start
;
5413 u64 hole_start
= start
;
5416 em
= alloc_extent_map(GFP_NOFS
);
5422 * when btrfs_get_extent can't find anything it
5423 * returns one huge hole
5425 * make sure what it found really fits our range, and
5426 * adjust to make sure it is based on the start from
5430 u64 calc_end
= extent_map_end(hole_em
);
5432 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
5433 free_extent_map(hole_em
);
5436 hole_start
= max(hole_em
->start
, start
);
5437 hole_len
= calc_end
- hole_start
;
5441 if (hole_em
&& range_start
> hole_start
) {
5442 /* our hole starts before our delalloc, so we
5443 * have to return just the parts of the hole
5444 * that go until the delalloc starts
5446 em
->len
= min(hole_len
,
5447 range_start
- hole_start
);
5448 em
->start
= hole_start
;
5449 em
->orig_start
= hole_start
;
5451 * don't adjust block start at all,
5452 * it is fixed at EXTENT_MAP_HOLE
5454 em
->block_start
= hole_em
->block_start
;
5455 em
->block_len
= hole_len
;
5457 em
->start
= range_start
;
5459 em
->orig_start
= range_start
;
5460 em
->block_start
= EXTENT_MAP_DELALLOC
;
5461 em
->block_len
= found
;
5463 } else if (hole_em
) {
5468 free_extent_map(hole_em
);
5470 free_extent_map(em
);
5471 return ERR_PTR(err
);
5476 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
5477 struct extent_map
*em
,
5480 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5481 struct btrfs_trans_handle
*trans
;
5482 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
5483 struct btrfs_key ins
;
5486 bool insert
= false;
5489 * Ok if the extent map we looked up is a hole and is for the exact
5490 * range we want, there is no reason to allocate a new one, however if
5491 * it is not right then we need to free this one and drop the cache for
5494 if (em
->block_start
!= EXTENT_MAP_HOLE
|| em
->start
!= start
||
5496 free_extent_map(em
);
5499 btrfs_drop_extent_cache(inode
, start
, start
+ len
- 1, 0);
5502 trans
= btrfs_join_transaction(root
, 0);
5504 return ERR_CAST(trans
);
5506 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
5508 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
5509 ret
= btrfs_reserve_extent(trans
, root
, len
, root
->sectorsize
, 0,
5510 alloc_hint
, (u64
)-1, &ins
, 1);
5517 em
= alloc_extent_map(GFP_NOFS
);
5519 em
= ERR_PTR(-ENOMEM
);
5525 em
->orig_start
= em
->start
;
5526 em
->len
= ins
.offset
;
5528 em
->block_start
= ins
.objectid
;
5529 em
->block_len
= ins
.offset
;
5530 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5533 * We need to do this because if we're using the original em we searched
5534 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5537 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
5540 write_lock(&em_tree
->lock
);
5541 ret
= add_extent_mapping(em_tree
, em
);
5542 write_unlock(&em_tree
->lock
);
5545 btrfs_drop_extent_cache(inode
, start
, start
+ em
->len
- 1, 0);
5548 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
5549 ins
.offset
, ins
.offset
, 0);
5551 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
5555 btrfs_end_transaction(trans
, root
);
5560 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5561 * block must be cow'd
5563 static noinline
int can_nocow_odirect(struct btrfs_trans_handle
*trans
,
5564 struct inode
*inode
, u64 offset
, u64 len
)
5566 struct btrfs_path
*path
;
5568 struct extent_buffer
*leaf
;
5569 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5570 struct btrfs_file_extent_item
*fi
;
5571 struct btrfs_key key
;
5579 path
= btrfs_alloc_path();
5583 ret
= btrfs_lookup_file_extent(trans
, root
, path
, btrfs_ino(inode
),
5588 slot
= path
->slots
[0];
5591 /* can't find the item, must cow */
5598 leaf
= path
->nodes
[0];
5599 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
5600 if (key
.objectid
!= btrfs_ino(inode
) ||
5601 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
5602 /* not our file or wrong item type, must cow */
5606 if (key
.offset
> offset
) {
5607 /* Wrong offset, must cow */
5611 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
5612 found_type
= btrfs_file_extent_type(leaf
, fi
);
5613 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
5614 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
5615 /* not a regular extent, must cow */
5618 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
5619 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
5621 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
5622 if (extent_end
< offset
+ len
) {
5623 /* extent doesn't include our full range, must cow */
5627 if (btrfs_extent_readonly(root
, disk_bytenr
))
5631 * look for other files referencing this extent, if we
5632 * find any we must cow
5634 if (btrfs_cross_ref_exist(trans
, root
, btrfs_ino(inode
),
5635 key
.offset
- backref_offset
, disk_bytenr
))
5639 * adjust disk_bytenr and num_bytes to cover just the bytes
5640 * in this extent we are about to write. If there
5641 * are any csums in that range we have to cow in order
5642 * to keep the csums correct
5644 disk_bytenr
+= backref_offset
;
5645 disk_bytenr
+= offset
- key
.offset
;
5646 num_bytes
= min(offset
+ len
, extent_end
) - offset
;
5647 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
5650 * all of the above have passed, it is safe to overwrite this extent
5655 btrfs_free_path(path
);
5659 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
5660 struct buffer_head
*bh_result
, int create
)
5662 struct extent_map
*em
;
5663 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5664 u64 start
= iblock
<< inode
->i_blkbits
;
5665 u64 len
= bh_result
->b_size
;
5666 struct btrfs_trans_handle
*trans
;
5668 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
5673 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5674 * io. INLINE is special, and we could probably kludge it in here, but
5675 * it's still buffered so for safety lets just fall back to the generic
5678 * For COMPRESSED we _have_ to read the entire extent in so we can
5679 * decompress it, so there will be buffering required no matter what we
5680 * do, so go ahead and fallback to buffered.
5682 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5683 * to buffered IO. Don't blame me, this is the price we pay for using
5686 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
5687 em
->block_start
== EXTENT_MAP_INLINE
) {
5688 free_extent_map(em
);
5692 /* Just a good old fashioned hole, return */
5693 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
5694 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
5695 free_extent_map(em
);
5696 /* DIO will do one hole at a time, so just unlock a sector */
5697 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
,
5698 start
+ root
->sectorsize
- 1, GFP_NOFS
);
5703 * We don't allocate a new extent in the following cases
5705 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5707 * 2) The extent is marked as PREALLOC. We're good to go here and can
5708 * just use the extent.
5712 len
= em
->len
- (start
- em
->start
);
5716 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
5717 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
5718 em
->block_start
!= EXTENT_MAP_HOLE
)) {
5723 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
5724 type
= BTRFS_ORDERED_PREALLOC
;
5726 type
= BTRFS_ORDERED_NOCOW
;
5727 len
= min(len
, em
->len
- (start
- em
->start
));
5728 block_start
= em
->block_start
+ (start
- em
->start
);
5731 * we're not going to log anything, but we do need
5732 * to make sure the current transaction stays open
5733 * while we look for nocow cross refs
5735 trans
= btrfs_join_transaction(root
, 0);
5739 if (can_nocow_odirect(trans
, inode
, start
, len
) == 1) {
5740 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
5741 block_start
, len
, len
, type
);
5742 btrfs_end_transaction(trans
, root
);
5744 free_extent_map(em
);
5749 btrfs_end_transaction(trans
, root
);
5753 * this will cow the extent, reset the len in case we changed
5756 len
= bh_result
->b_size
;
5757 em
= btrfs_new_extent_direct(inode
, em
, start
, len
);
5760 len
= min(len
, em
->len
- (start
- em
->start
));
5762 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
- 1,
5763 EXTENT_LOCKED
| EXTENT_DELALLOC
| EXTENT_DIRTY
, 1,
5766 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
5768 bh_result
->b_size
= len
;
5769 bh_result
->b_bdev
= em
->bdev
;
5770 set_buffer_mapped(bh_result
);
5771 if (create
&& !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
5772 set_buffer_new(bh_result
);
5774 free_extent_map(em
);
5779 struct btrfs_dio_private
{
5780 struct inode
*inode
;
5787 /* number of bios pending for this dio */
5788 atomic_t pending_bios
;
5793 struct bio
*orig_bio
;
5796 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
5798 struct btrfs_dio_private
*dip
= bio
->bi_private
;
5799 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
5800 struct bio_vec
*bvec
= bio
->bi_io_vec
;
5801 struct inode
*inode
= dip
->inode
;
5802 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5804 u32
*private = dip
->csums
;
5806 start
= dip
->logical_offset
;
5808 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
5809 struct page
*page
= bvec
->bv_page
;
5812 unsigned long flags
;
5814 local_irq_save(flags
);
5815 kaddr
= kmap_atomic(page
, KM_IRQ0
);
5816 csum
= btrfs_csum_data(root
, kaddr
+ bvec
->bv_offset
,
5817 csum
, bvec
->bv_len
);
5818 btrfs_csum_final(csum
, (char *)&csum
);
5819 kunmap_atomic(kaddr
, KM_IRQ0
);
5820 local_irq_restore(flags
);
5822 flush_dcache_page(bvec
->bv_page
);
5823 if (csum
!= *private) {
5824 printk(KERN_ERR
"btrfs csum failed ino %llu off"
5825 " %llu csum %u private %u\n",
5826 (unsigned long long)btrfs_ino(inode
),
5827 (unsigned long long)start
,
5833 start
+= bvec
->bv_len
;
5836 } while (bvec
<= bvec_end
);
5838 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
5839 dip
->logical_offset
+ dip
->bytes
- 1, GFP_NOFS
);
5840 bio
->bi_private
= dip
->private;
5845 /* If we had a csum failure make sure to clear the uptodate flag */
5847 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
5848 dio_end_io(bio
, err
);
5851 static void btrfs_endio_direct_write(struct bio
*bio
, int err
)
5853 struct btrfs_dio_private
*dip
= bio
->bi_private
;
5854 struct inode
*inode
= dip
->inode
;
5855 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5856 struct btrfs_trans_handle
*trans
;
5857 struct btrfs_ordered_extent
*ordered
= NULL
;
5858 struct extent_state
*cached_state
= NULL
;
5859 u64 ordered_offset
= dip
->logical_offset
;
5860 u64 ordered_bytes
= dip
->bytes
;
5866 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
5874 trans
= btrfs_join_transaction(root
, 1);
5875 if (IS_ERR(trans
)) {
5879 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
5881 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
)) {
5882 ret
= btrfs_ordered_update_i_size(inode
, 0, ordered
);
5884 ret
= btrfs_update_inode(trans
, root
, inode
);
5889 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, ordered
->file_offset
,
5890 ordered
->file_offset
+ ordered
->len
- 1, 0,
5891 &cached_state
, GFP_NOFS
);
5893 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
)) {
5894 ret
= btrfs_mark_extent_written(trans
, inode
,
5895 ordered
->file_offset
,
5896 ordered
->file_offset
+
5903 ret
= insert_reserved_file_extent(trans
, inode
,
5904 ordered
->file_offset
,
5910 BTRFS_FILE_EXTENT_REG
);
5911 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
5912 ordered
->file_offset
, ordered
->len
);
5920 add_pending_csums(trans
, inode
, ordered
->file_offset
, &ordered
->list
);
5921 ret
= btrfs_ordered_update_i_size(inode
, 0, ordered
);
5923 btrfs_update_inode(trans
, root
, inode
);
5926 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, ordered
->file_offset
,
5927 ordered
->file_offset
+ ordered
->len
- 1,
5928 &cached_state
, GFP_NOFS
);
5930 btrfs_delalloc_release_metadata(inode
, ordered
->len
);
5931 btrfs_end_transaction(trans
, root
);
5932 ordered_offset
= ordered
->file_offset
+ ordered
->len
;
5933 btrfs_put_ordered_extent(ordered
);
5934 btrfs_put_ordered_extent(ordered
);
5938 * our bio might span multiple ordered extents. If we haven't
5939 * completed the accounting for the whole dio, go back and try again
5941 if (ordered_offset
< dip
->logical_offset
+ dip
->bytes
) {
5942 ordered_bytes
= dip
->logical_offset
+ dip
->bytes
-
5947 bio
->bi_private
= dip
->private;
5952 /* If we had an error make sure to clear the uptodate flag */
5954 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
5955 dio_end_io(bio
, err
);
5958 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
5959 struct bio
*bio
, int mirror_num
,
5960 unsigned long bio_flags
, u64 offset
)
5963 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5964 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
5969 static void btrfs_end_dio_bio(struct bio
*bio
, int err
)
5971 struct btrfs_dio_private
*dip
= bio
->bi_private
;
5974 printk(KERN_ERR
"btrfs direct IO failed ino %llu rw %lu "
5975 "sector %#Lx len %u err no %d\n",
5976 (unsigned long long)btrfs_ino(dip
->inode
), bio
->bi_rw
,
5977 (unsigned long long)bio
->bi_sector
, bio
->bi_size
, err
);
5981 * before atomic variable goto zero, we must make sure
5982 * dip->errors is perceived to be set.
5984 smp_mb__before_atomic_dec();
5987 /* if there are more bios still pending for this dio, just exit */
5988 if (!atomic_dec_and_test(&dip
->pending_bios
))
5992 bio_io_error(dip
->orig_bio
);
5994 set_bit(BIO_UPTODATE
, &dip
->orig_bio
->bi_flags
);
5995 bio_endio(dip
->orig_bio
, 0);
6001 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
6002 u64 first_sector
, gfp_t gfp_flags
)
6004 int nr_vecs
= bio_get_nr_vecs(bdev
);
6005 return btrfs_bio_alloc(bdev
, first_sector
, nr_vecs
, gfp_flags
);
6008 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
6009 int rw
, u64 file_offset
, int skip_sum
,
6010 u32
*csums
, int async_submit
)
6012 int write
= rw
& REQ_WRITE
;
6013 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6017 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
6024 if (write
&& async_submit
) {
6025 ret
= btrfs_wq_submit_bio(root
->fs_info
,
6026 inode
, rw
, bio
, 0, 0,
6028 __btrfs_submit_bio_start_direct_io
,
6029 __btrfs_submit_bio_done
);
6033 * If we aren't doing async submit, calculate the csum of the
6036 ret
= btrfs_csum_one_bio(root
, inode
, bio
, file_offset
, 1);
6039 } else if (!skip_sum
) {
6040 ret
= btrfs_lookup_bio_sums_dio(root
, inode
, bio
,
6041 file_offset
, csums
);
6047 ret
= btrfs_map_bio(root
, rw
, bio
, 0, async_submit
);
6053 static int btrfs_submit_direct_hook(int rw
, struct btrfs_dio_private
*dip
,
6056 struct inode
*inode
= dip
->inode
;
6057 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6058 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
6060 struct bio
*orig_bio
= dip
->orig_bio
;
6061 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
6062 u64 start_sector
= orig_bio
->bi_sector
;
6063 u64 file_offset
= dip
->logical_offset
;
6067 u32
*csums
= dip
->csums
;
6069 int async_submit
= 0;
6070 int write
= rw
& REQ_WRITE
;
6072 map_length
= orig_bio
->bi_size
;
6073 ret
= btrfs_map_block(map_tree
, READ
, start_sector
<< 9,
6074 &map_length
, NULL
, 0);
6080 if (map_length
>= orig_bio
->bi_size
) {
6086 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
6089 bio
->bi_private
= dip
;
6090 bio
->bi_end_io
= btrfs_end_dio_bio
;
6091 atomic_inc(&dip
->pending_bios
);
6093 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
6094 if (unlikely(map_length
< submit_len
+ bvec
->bv_len
||
6095 bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
6096 bvec
->bv_offset
) < bvec
->bv_len
)) {
6098 * inc the count before we submit the bio so
6099 * we know the end IO handler won't happen before
6100 * we inc the count. Otherwise, the dip might get freed
6101 * before we're done setting it up
6103 atomic_inc(&dip
->pending_bios
);
6104 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
,
6105 file_offset
, skip_sum
,
6106 csums
, async_submit
);
6109 atomic_dec(&dip
->pending_bios
);
6113 /* Write's use the ordered csums */
6114 if (!write
&& !skip_sum
)
6115 csums
= csums
+ nr_pages
;
6116 start_sector
+= submit_len
>> 9;
6117 file_offset
+= submit_len
;
6122 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
6123 start_sector
, GFP_NOFS
);
6126 bio
->bi_private
= dip
;
6127 bio
->bi_end_io
= btrfs_end_dio_bio
;
6129 map_length
= orig_bio
->bi_size
;
6130 ret
= btrfs_map_block(map_tree
, READ
, start_sector
<< 9,
6131 &map_length
, NULL
, 0);
6137 submit_len
+= bvec
->bv_len
;
6144 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
, file_offset
, skip_sum
,
6145 csums
, async_submit
);
6153 * before atomic variable goto zero, we must
6154 * make sure dip->errors is perceived to be set.
6156 smp_mb__before_atomic_dec();
6157 if (atomic_dec_and_test(&dip
->pending_bios
))
6158 bio_io_error(dip
->orig_bio
);
6160 /* bio_end_io() will handle error, so we needn't return it */
6164 static void btrfs_submit_direct(int rw
, struct bio
*bio
, struct inode
*inode
,
6167 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6168 struct btrfs_dio_private
*dip
;
6169 struct bio_vec
*bvec
= bio
->bi_io_vec
;
6171 int write
= rw
& REQ_WRITE
;
6174 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
6176 dip
= kmalloc(sizeof(*dip
), GFP_NOFS
);
6183 /* Write's use the ordered csum stuff, so we don't need dip->csums */
6184 if (!write
&& !skip_sum
) {
6185 dip
->csums
= kmalloc(sizeof(u32
) * bio
->bi_vcnt
, GFP_NOFS
);
6193 dip
->private = bio
->bi_private
;
6195 dip
->logical_offset
= file_offset
;
6199 dip
->bytes
+= bvec
->bv_len
;
6201 } while (bvec
<= (bio
->bi_io_vec
+ bio
->bi_vcnt
- 1));
6203 dip
->disk_bytenr
= (u64
)bio
->bi_sector
<< 9;
6204 bio
->bi_private
= dip
;
6206 dip
->orig_bio
= bio
;
6207 atomic_set(&dip
->pending_bios
, 0);
6210 bio
->bi_end_io
= btrfs_endio_direct_write
;
6212 bio
->bi_end_io
= btrfs_endio_direct_read
;
6214 ret
= btrfs_submit_direct_hook(rw
, dip
, skip_sum
);
6219 * If this is a write, we need to clean up the reserved space and kill
6220 * the ordered extent.
6223 struct btrfs_ordered_extent
*ordered
;
6224 ordered
= btrfs_lookup_ordered_extent(inode
, file_offset
);
6225 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
6226 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
6227 btrfs_free_reserved_extent(root
, ordered
->start
,
6229 btrfs_put_ordered_extent(ordered
);
6230 btrfs_put_ordered_extent(ordered
);
6232 bio_endio(bio
, ret
);
6235 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
6236 const struct iovec
*iov
, loff_t offset
,
6237 unsigned long nr_segs
)
6243 unsigned blocksize_mask
= root
->sectorsize
- 1;
6244 ssize_t retval
= -EINVAL
;
6245 loff_t end
= offset
;
6247 if (offset
& blocksize_mask
)
6250 /* Check the memory alignment. Blocks cannot straddle pages */
6251 for (seg
= 0; seg
< nr_segs
; seg
++) {
6252 addr
= (unsigned long)iov
[seg
].iov_base
;
6253 size
= iov
[seg
].iov_len
;
6255 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
6258 /* If this is a write we don't need to check anymore */
6263 * Check to make sure we don't have duplicate iov_base's in this
6264 * iovec, if so return EINVAL, otherwise we'll get csum errors
6265 * when reading back.
6267 for (i
= seg
+ 1; i
< nr_segs
; i
++) {
6268 if (iov
[seg
].iov_base
== iov
[i
].iov_base
)
6276 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
6277 const struct iovec
*iov
, loff_t offset
,
6278 unsigned long nr_segs
)
6280 struct file
*file
= iocb
->ki_filp
;
6281 struct inode
*inode
= file
->f_mapping
->host
;
6282 struct btrfs_ordered_extent
*ordered
;
6283 struct extent_state
*cached_state
= NULL
;
6284 u64 lockstart
, lockend
;
6286 int writing
= rw
& WRITE
;
6288 size_t count
= iov_length(iov
, nr_segs
);
6290 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iov
,
6296 lockend
= offset
+ count
- 1;
6299 ret
= btrfs_delalloc_reserve_space(inode
, count
);
6305 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6306 0, &cached_state
, GFP_NOFS
);
6308 * We're concerned with the entire range that we're going to be
6309 * doing DIO to, so we need to make sure theres no ordered
6310 * extents in this range.
6312 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
6313 lockend
- lockstart
+ 1);
6316 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6317 &cached_state
, GFP_NOFS
);
6318 btrfs_start_ordered_extent(inode
, ordered
, 1);
6319 btrfs_put_ordered_extent(ordered
);
6324 * we don't use btrfs_set_extent_delalloc because we don't want
6325 * the dirty or uptodate bits
6328 write_bits
= EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
;
6329 ret
= set_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6330 EXTENT_DELALLOC
, 0, NULL
, &cached_state
,
6333 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6334 lockend
, EXTENT_LOCKED
| write_bits
,
6335 1, 0, &cached_state
, GFP_NOFS
);
6340 free_extent_state(cached_state
);
6341 cached_state
= NULL
;
6343 ret
= __blockdev_direct_IO(rw
, iocb
, inode
,
6344 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
6345 iov
, offset
, nr_segs
, btrfs_get_blocks_direct
, NULL
,
6346 btrfs_submit_direct
, 0);
6348 if (ret
< 0 && ret
!= -EIOCBQUEUED
) {
6349 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, offset
,
6350 offset
+ iov_length(iov
, nr_segs
) - 1,
6351 EXTENT_LOCKED
| write_bits
, 1, 0,
6352 &cached_state
, GFP_NOFS
);
6353 } else if (ret
>= 0 && ret
< iov_length(iov
, nr_segs
)) {
6355 * We're falling back to buffered, unlock the section we didn't
6358 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, offset
+ ret
,
6359 offset
+ iov_length(iov
, nr_segs
) - 1,
6360 EXTENT_LOCKED
| write_bits
, 1, 0,
6361 &cached_state
, GFP_NOFS
);
6364 free_extent_state(cached_state
);
6368 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
6369 __u64 start
, __u64 len
)
6371 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
6374 int btrfs_readpage(struct file
*file
, struct page
*page
)
6376 struct extent_io_tree
*tree
;
6377 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6378 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
6381 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
6383 struct extent_io_tree
*tree
;
6386 if (current
->flags
& PF_MEMALLOC
) {
6387 redirty_page_for_writepage(wbc
, page
);
6391 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6392 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
6395 int btrfs_writepages(struct address_space
*mapping
,
6396 struct writeback_control
*wbc
)
6398 struct extent_io_tree
*tree
;
6400 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
6401 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
6405 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
6406 struct list_head
*pages
, unsigned nr_pages
)
6408 struct extent_io_tree
*tree
;
6409 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
6410 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
6413 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
6415 struct extent_io_tree
*tree
;
6416 struct extent_map_tree
*map
;
6419 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6420 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
6421 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
6423 ClearPagePrivate(page
);
6424 set_page_private(page
, 0);
6425 page_cache_release(page
);
6430 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
6432 if (PageWriteback(page
) || PageDirty(page
))
6434 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
6437 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
6439 struct extent_io_tree
*tree
;
6440 struct btrfs_ordered_extent
*ordered
;
6441 struct extent_state
*cached_state
= NULL
;
6442 u64 page_start
= page_offset(page
);
6443 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6447 * we have the page locked, so new writeback can't start,
6448 * and the dirty bit won't be cleared while we are here.
6450 * Wait for IO on this page so that we can safely clear
6451 * the PagePrivate2 bit and do ordered accounting
6453 wait_on_page_writeback(page
);
6455 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6457 btrfs_releasepage(page
, GFP_NOFS
);
6460 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
,
6462 ordered
= btrfs_lookup_ordered_extent(page
->mapping
->host
,
6466 * IO on this page will never be started, so we need
6467 * to account for any ordered extents now
6469 clear_extent_bit(tree
, page_start
, page_end
,
6470 EXTENT_DIRTY
| EXTENT_DELALLOC
|
6471 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
, 1, 0,
6472 &cached_state
, GFP_NOFS
);
6474 * whoever cleared the private bit is responsible
6475 * for the finish_ordered_io
6477 if (TestClearPagePrivate2(page
)) {
6478 btrfs_finish_ordered_io(page
->mapping
->host
,
6479 page_start
, page_end
);
6481 btrfs_put_ordered_extent(ordered
);
6482 cached_state
= NULL
;
6483 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
,
6486 clear_extent_bit(tree
, page_start
, page_end
,
6487 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
6488 EXTENT_DO_ACCOUNTING
, 1, 1, &cached_state
, GFP_NOFS
);
6489 __btrfs_releasepage(page
, GFP_NOFS
);
6491 ClearPageChecked(page
);
6492 if (PagePrivate(page
)) {
6493 ClearPagePrivate(page
);
6494 set_page_private(page
, 0);
6495 page_cache_release(page
);
6500 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6501 * called from a page fault handler when a page is first dirtied. Hence we must
6502 * be careful to check for EOF conditions here. We set the page up correctly
6503 * for a written page which means we get ENOSPC checking when writing into
6504 * holes and correct delalloc and unwritten extent mapping on filesystems that
6505 * support these features.
6507 * We are not allowed to take the i_mutex here so we have to play games to
6508 * protect against truncate races as the page could now be beyond EOF. Because
6509 * vmtruncate() writes the inode size before removing pages, once we have the
6510 * page lock we can determine safely if the page is beyond EOF. If it is not
6511 * beyond EOF, then the page is guaranteed safe against truncation until we
6514 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
6516 struct page
*page
= vmf
->page
;
6517 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
6518 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6519 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6520 struct btrfs_ordered_extent
*ordered
;
6521 struct extent_state
*cached_state
= NULL
;
6523 unsigned long zero_start
;
6529 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
6533 else /* -ENOSPC, -EIO, etc */
6534 ret
= VM_FAULT_SIGBUS
;
6538 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
6541 size
= i_size_read(inode
);
6542 page_start
= page_offset(page
);
6543 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6545 if ((page
->mapping
!= inode
->i_mapping
) ||
6546 (page_start
>= size
)) {
6547 /* page got truncated out from underneath us */
6550 wait_on_page_writeback(page
);
6552 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
,
6554 set_page_extent_mapped(page
);
6557 * we can't set the delalloc bits if there are pending ordered
6558 * extents. Drop our locks and wait for them to finish
6560 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
6562 unlock_extent_cached(io_tree
, page_start
, page_end
,
6563 &cached_state
, GFP_NOFS
);
6565 btrfs_start_ordered_extent(inode
, ordered
, 1);
6566 btrfs_put_ordered_extent(ordered
);
6571 * XXX - page_mkwrite gets called every time the page is dirtied, even
6572 * if it was already dirty, so for space accounting reasons we need to
6573 * clear any delalloc bits for the range we are fixing to save. There
6574 * is probably a better way to do this, but for now keep consistent with
6575 * prepare_pages in the normal write path.
6577 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
6578 EXTENT_DIRTY
| EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
,
6579 0, 0, &cached_state
, GFP_NOFS
);
6581 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
6584 unlock_extent_cached(io_tree
, page_start
, page_end
,
6585 &cached_state
, GFP_NOFS
);
6586 ret
= VM_FAULT_SIGBUS
;
6591 /* page is wholly or partially inside EOF */
6592 if (page_start
+ PAGE_CACHE_SIZE
> size
)
6593 zero_start
= size
& ~PAGE_CACHE_MASK
;
6595 zero_start
= PAGE_CACHE_SIZE
;
6597 if (zero_start
!= PAGE_CACHE_SIZE
) {
6599 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
6600 flush_dcache_page(page
);
6603 ClearPageChecked(page
);
6604 set_page_dirty(page
);
6605 SetPageUptodate(page
);
6607 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
6608 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
6610 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
6614 return VM_FAULT_LOCKED
;
6616 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
6621 static int btrfs_truncate(struct inode
*inode
)
6623 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6626 struct btrfs_trans_handle
*trans
;
6628 u64 mask
= root
->sectorsize
- 1;
6630 ret
= btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
6634 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
6635 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
6637 trans
= btrfs_start_transaction(root
, 5);
6639 return PTR_ERR(trans
);
6641 btrfs_set_trans_block_group(trans
, inode
);
6643 ret
= btrfs_orphan_add(trans
, inode
);
6645 btrfs_end_transaction(trans
, root
);
6649 nr
= trans
->blocks_used
;
6650 btrfs_end_transaction(trans
, root
);
6651 btrfs_btree_balance_dirty(root
, nr
);
6653 /* Now start a transaction for the truncate */
6654 trans
= btrfs_start_transaction(root
, 0);
6656 return PTR_ERR(trans
);
6657 btrfs_set_trans_block_group(trans
, inode
);
6658 trans
->block_rsv
= root
->orphan_block_rsv
;
6661 * setattr is responsible for setting the ordered_data_close flag,
6662 * but that is only tested during the last file release. That
6663 * could happen well after the next commit, leaving a great big
6664 * window where new writes may get lost if someone chooses to write
6665 * to this file after truncating to zero
6667 * The inode doesn't have any dirty data here, and so if we commit
6668 * this is a noop. If someone immediately starts writing to the inode
6669 * it is very likely we'll catch some of their writes in this
6670 * transaction, and the commit will find this file on the ordered
6671 * data list with good things to send down.
6673 * This is a best effort solution, there is still a window where
6674 * using truncate to replace the contents of the file will
6675 * end up with a zero length file after a crash.
6677 if (inode
->i_size
== 0 && BTRFS_I(inode
)->ordered_data_close
)
6678 btrfs_add_ordered_operation(trans
, root
, inode
);
6682 trans
= btrfs_start_transaction(root
, 0);
6684 return PTR_ERR(trans
);
6685 btrfs_set_trans_block_group(trans
, inode
);
6686 trans
->block_rsv
= root
->orphan_block_rsv
;
6689 ret
= btrfs_block_rsv_check(trans
, root
,
6690 root
->orphan_block_rsv
, 0, 5);
6691 if (ret
== -EAGAIN
) {
6692 ret
= btrfs_commit_transaction(trans
, root
);
6702 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
6704 BTRFS_EXTENT_DATA_KEY
);
6705 if (ret
!= -EAGAIN
) {
6710 ret
= btrfs_update_inode(trans
, root
, inode
);
6716 nr
= trans
->blocks_used
;
6717 btrfs_end_transaction(trans
, root
);
6719 btrfs_btree_balance_dirty(root
, nr
);
6722 if (ret
== 0 && inode
->i_nlink
> 0) {
6723 ret
= btrfs_orphan_del(trans
, inode
);
6726 } else if (ret
&& inode
->i_nlink
> 0) {
6728 * Failed to do the truncate, remove us from the in memory
6731 ret
= btrfs_orphan_del(NULL
, inode
);
6734 ret
= btrfs_update_inode(trans
, root
, inode
);
6738 nr
= trans
->blocks_used
;
6739 ret
= btrfs_end_transaction_throttle(trans
, root
);
6742 btrfs_btree_balance_dirty(root
, nr
);
6748 * create a new subvolume directory/inode (helper for the ioctl).
6750 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
6751 struct btrfs_root
*new_root
,
6752 u64 new_dirid
, u64 alloc_hint
)
6754 struct inode
*inode
;
6758 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2, new_dirid
,
6759 new_dirid
, alloc_hint
, S_IFDIR
| 0700, &index
);
6761 return PTR_ERR(inode
);
6762 inode
->i_op
= &btrfs_dir_inode_operations
;
6763 inode
->i_fop
= &btrfs_dir_file_operations
;
6766 btrfs_i_size_write(inode
, 0);
6768 err
= btrfs_update_inode(trans
, new_root
, inode
);
6775 /* helper function for file defrag and space balancing. This
6776 * forces readahead on a given range of bytes in an inode
6778 unsigned long btrfs_force_ra(struct address_space
*mapping
,
6779 struct file_ra_state
*ra
, struct file
*file
,
6780 pgoff_t offset
, pgoff_t last_index
)
6782 pgoff_t req_size
= last_index
- offset
+ 1;
6784 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
6785 return offset
+ req_size
;
6788 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
6790 struct btrfs_inode
*ei
;
6791 struct inode
*inode
;
6793 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
6798 ei
->space_info
= NULL
;
6802 ei
->last_sub_trans
= 0;
6803 ei
->logged_trans
= 0;
6804 ei
->delalloc_bytes
= 0;
6805 ei
->reserved_bytes
= 0;
6806 ei
->disk_i_size
= 0;
6808 ei
->index_cnt
= (u64
)-1;
6809 ei
->last_unlink_trans
= 0;
6811 atomic_set(&ei
->outstanding_extents
, 0);
6812 atomic_set(&ei
->reserved_extents
, 0);
6814 ei
->ordered_data_close
= 0;
6815 ei
->orphan_meta_reserved
= 0;
6816 ei
->dummy_inode
= 0;
6817 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
6819 inode
= &ei
->vfs_inode
;
6820 extent_map_tree_init(&ei
->extent_tree
, GFP_NOFS
);
6821 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
, GFP_NOFS
);
6822 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
, GFP_NOFS
);
6823 mutex_init(&ei
->log_mutex
);
6824 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
6825 INIT_LIST_HEAD(&ei
->i_orphan
);
6826 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
6827 INIT_LIST_HEAD(&ei
->ordered_operations
);
6828 RB_CLEAR_NODE(&ei
->rb_node
);
6833 static void btrfs_i_callback(struct rcu_head
*head
)
6835 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
6836 INIT_LIST_HEAD(&inode
->i_dentry
);
6837 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
6840 void btrfs_destroy_inode(struct inode
*inode
)
6842 struct btrfs_ordered_extent
*ordered
;
6843 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6845 WARN_ON(!list_empty(&inode
->i_dentry
));
6846 WARN_ON(inode
->i_data
.nrpages
);
6847 WARN_ON(atomic_read(&BTRFS_I(inode
)->outstanding_extents
));
6848 WARN_ON(atomic_read(&BTRFS_I(inode
)->reserved_extents
));
6851 * This can happen where we create an inode, but somebody else also
6852 * created the same inode and we need to destroy the one we already
6859 * Make sure we're properly removed from the ordered operation
6863 if (!list_empty(&BTRFS_I(inode
)->ordered_operations
)) {
6864 spin_lock(&root
->fs_info
->ordered_extent_lock
);
6865 list_del_init(&BTRFS_I(inode
)->ordered_operations
);
6866 spin_unlock(&root
->fs_info
->ordered_extent_lock
);
6869 if (root
== root
->fs_info
->tree_root
) {
6870 struct btrfs_block_group_cache
*block_group
;
6872 block_group
= btrfs_lookup_block_group(root
->fs_info
,
6873 BTRFS_I(inode
)->block_group
);
6874 if (block_group
&& block_group
->inode
== inode
) {
6875 spin_lock(&block_group
->lock
);
6876 block_group
->inode
= NULL
;
6877 spin_unlock(&block_group
->lock
);
6878 btrfs_put_block_group(block_group
);
6879 } else if (block_group
) {
6880 btrfs_put_block_group(block_group
);
6884 spin_lock(&root
->orphan_lock
);
6885 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
6886 printk(KERN_INFO
"BTRFS: inode %llu still on the orphan list\n",
6887 (unsigned long long)btrfs_ino(inode
));
6888 list_del_init(&BTRFS_I(inode
)->i_orphan
);
6890 spin_unlock(&root
->orphan_lock
);
6893 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
6897 printk(KERN_ERR
"btrfs found ordered "
6898 "extent %llu %llu on inode cleanup\n",
6899 (unsigned long long)ordered
->file_offset
,
6900 (unsigned long long)ordered
->len
);
6901 btrfs_remove_ordered_extent(inode
, ordered
);
6902 btrfs_put_ordered_extent(ordered
);
6903 btrfs_put_ordered_extent(ordered
);
6906 inode_tree_del(inode
);
6907 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
6909 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
6912 int btrfs_drop_inode(struct inode
*inode
)
6914 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6916 if (btrfs_root_refs(&root
->root_item
) == 0 &&
6917 !is_free_space_inode(root
, inode
))
6920 return generic_drop_inode(inode
);
6923 static void init_once(void *foo
)
6925 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
6927 inode_init_once(&ei
->vfs_inode
);
6930 void btrfs_destroy_cachep(void)
6932 if (btrfs_inode_cachep
)
6933 kmem_cache_destroy(btrfs_inode_cachep
);
6934 if (btrfs_trans_handle_cachep
)
6935 kmem_cache_destroy(btrfs_trans_handle_cachep
);
6936 if (btrfs_transaction_cachep
)
6937 kmem_cache_destroy(btrfs_transaction_cachep
);
6938 if (btrfs_path_cachep
)
6939 kmem_cache_destroy(btrfs_path_cachep
);
6940 if (btrfs_free_space_cachep
)
6941 kmem_cache_destroy(btrfs_free_space_cachep
);
6944 int btrfs_init_cachep(void)
6946 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode_cache",
6947 sizeof(struct btrfs_inode
), 0,
6948 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
6949 if (!btrfs_inode_cachep
)
6952 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle_cache",
6953 sizeof(struct btrfs_trans_handle
), 0,
6954 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6955 if (!btrfs_trans_handle_cachep
)
6958 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction_cache",
6959 sizeof(struct btrfs_transaction
), 0,
6960 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6961 if (!btrfs_transaction_cachep
)
6964 btrfs_path_cachep
= kmem_cache_create("btrfs_path_cache",
6965 sizeof(struct btrfs_path
), 0,
6966 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6967 if (!btrfs_path_cachep
)
6970 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space_cache",
6971 sizeof(struct btrfs_free_space
), 0,
6972 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6973 if (!btrfs_free_space_cachep
)
6978 btrfs_destroy_cachep();
6982 static int btrfs_getattr(struct vfsmount
*mnt
,
6983 struct dentry
*dentry
, struct kstat
*stat
)
6985 struct inode
*inode
= dentry
->d_inode
;
6986 generic_fillattr(inode
, stat
);
6987 stat
->dev
= BTRFS_I(inode
)->root
->anon_super
.s_dev
;
6988 stat
->blksize
= PAGE_CACHE_SIZE
;
6989 stat
->blocks
= (inode_get_bytes(inode
) +
6990 BTRFS_I(inode
)->delalloc_bytes
) >> 9;
6995 * If a file is moved, it will inherit the cow and compression flags of the new
6998 static void fixup_inode_flags(struct inode
*dir
, struct inode
*inode
)
7000 struct btrfs_inode
*b_dir
= BTRFS_I(dir
);
7001 struct btrfs_inode
*b_inode
= BTRFS_I(inode
);
7003 if (b_dir
->flags
& BTRFS_INODE_NODATACOW
)
7004 b_inode
->flags
|= BTRFS_INODE_NODATACOW
;
7006 b_inode
->flags
&= ~BTRFS_INODE_NODATACOW
;
7008 if (b_dir
->flags
& BTRFS_INODE_COMPRESS
)
7009 b_inode
->flags
|= BTRFS_INODE_COMPRESS
;
7011 b_inode
->flags
&= ~BTRFS_INODE_COMPRESS
;
7014 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
7015 struct inode
*new_dir
, struct dentry
*new_dentry
)
7017 struct btrfs_trans_handle
*trans
;
7018 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
7019 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
7020 struct inode
*new_inode
= new_dentry
->d_inode
;
7021 struct inode
*old_inode
= old_dentry
->d_inode
;
7022 struct timespec ctime
= CURRENT_TIME
;
7026 u64 old_ino
= btrfs_ino(old_inode
);
7028 if (btrfs_ino(new_dir
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
7031 /* we only allow rename subvolume link between subvolumes */
7032 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
7035 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
7036 (new_inode
&& btrfs_ino(new_inode
) == BTRFS_FIRST_FREE_OBJECTID
))
7039 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
7040 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
7043 * we're using rename to replace one file with another.
7044 * and the replacement file is large. Start IO on it now so
7045 * we don't add too much work to the end of the transaction
7047 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
&&
7048 old_inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
7049 filemap_flush(old_inode
->i_mapping
);
7051 /* close the racy window with snapshot create/destroy ioctl */
7052 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
7053 down_read(&root
->fs_info
->subvol_sem
);
7055 * We want to reserve the absolute worst case amount of items. So if
7056 * both inodes are subvols and we need to unlink them then that would
7057 * require 4 item modifications, but if they are both normal inodes it
7058 * would require 5 item modifications, so we'll assume their normal
7059 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7060 * should cover the worst case number of items we'll modify.
7062 trans
= btrfs_start_transaction(root
, 20);
7063 if (IS_ERR(trans
)) {
7064 ret
= PTR_ERR(trans
);
7068 btrfs_set_trans_block_group(trans
, new_dir
);
7071 btrfs_record_root_in_trans(trans
, dest
);
7073 ret
= btrfs_set_inode_index(new_dir
, &index
);
7077 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
7078 /* force full log commit if subvolume involved. */
7079 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
7081 ret
= btrfs_insert_inode_ref(trans
, dest
,
7082 new_dentry
->d_name
.name
,
7083 new_dentry
->d_name
.len
,
7085 btrfs_ino(new_dir
), index
);
7089 * this is an ugly little race, but the rename is required
7090 * to make sure that if we crash, the inode is either at the
7091 * old name or the new one. pinning the log transaction lets
7092 * us make sure we don't allow a log commit to come in after
7093 * we unlink the name but before we add the new name back in.
7095 btrfs_pin_log_trans(root
);
7098 * make sure the inode gets flushed if it is replacing
7101 if (new_inode
&& new_inode
->i_size
&& S_ISREG(old_inode
->i_mode
))
7102 btrfs_add_ordered_operation(trans
, root
, old_inode
);
7104 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
7105 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
7106 old_inode
->i_ctime
= ctime
;
7108 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
7109 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
7111 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
7112 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
7113 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
7114 old_dentry
->d_name
.name
,
7115 old_dentry
->d_name
.len
);
7117 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
7118 old_dentry
->d_inode
,
7119 old_dentry
->d_name
.name
,
7120 old_dentry
->d_name
.len
);
7122 ret
= btrfs_update_inode(trans
, root
, old_inode
);
7127 new_inode
->i_ctime
= CURRENT_TIME
;
7128 if (unlikely(btrfs_ino(new_inode
) ==
7129 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
7130 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
7131 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
7133 new_dentry
->d_name
.name
,
7134 new_dentry
->d_name
.len
);
7135 BUG_ON(new_inode
->i_nlink
== 0);
7137 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
7138 new_dentry
->d_inode
,
7139 new_dentry
->d_name
.name
,
7140 new_dentry
->d_name
.len
);
7143 if (new_inode
->i_nlink
== 0) {
7144 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
7149 fixup_inode_flags(new_dir
, old_inode
);
7151 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
7152 new_dentry
->d_name
.name
,
7153 new_dentry
->d_name
.len
, 0, index
);
7156 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
7157 struct dentry
*parent
= dget_parent(new_dentry
);
7158 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
7160 btrfs_end_log_trans(root
);
7163 btrfs_end_transaction_throttle(trans
, root
);
7165 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
7166 up_read(&root
->fs_info
->subvol_sem
);
7172 * some fairly slow code that needs optimization. This walks the list
7173 * of all the inodes with pending delalloc and forces them to disk.
7175 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
7177 struct list_head
*head
= &root
->fs_info
->delalloc_inodes
;
7178 struct btrfs_inode
*binode
;
7179 struct inode
*inode
;
7181 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
7184 spin_lock(&root
->fs_info
->delalloc_lock
);
7185 while (!list_empty(head
)) {
7186 binode
= list_entry(head
->next
, struct btrfs_inode
,
7188 inode
= igrab(&binode
->vfs_inode
);
7190 list_del_init(&binode
->delalloc_inodes
);
7191 spin_unlock(&root
->fs_info
->delalloc_lock
);
7193 filemap_flush(inode
->i_mapping
);
7195 btrfs_add_delayed_iput(inode
);
7200 spin_lock(&root
->fs_info
->delalloc_lock
);
7202 spin_unlock(&root
->fs_info
->delalloc_lock
);
7204 /* the filemap_flush will queue IO into the worker threads, but
7205 * we have to make sure the IO is actually started and that
7206 * ordered extents get created before we return
7208 atomic_inc(&root
->fs_info
->async_submit_draining
);
7209 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
7210 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
7211 wait_event(root
->fs_info
->async_submit_wait
,
7212 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
7213 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
7215 atomic_dec(&root
->fs_info
->async_submit_draining
);
7219 int btrfs_start_one_delalloc_inode(struct btrfs_root
*root
, int delay_iput
,
7222 struct btrfs_inode
*binode
;
7223 struct inode
*inode
= NULL
;
7225 spin_lock(&root
->fs_info
->delalloc_lock
);
7226 while (!list_empty(&root
->fs_info
->delalloc_inodes
)) {
7227 binode
= list_entry(root
->fs_info
->delalloc_inodes
.next
,
7228 struct btrfs_inode
, delalloc_inodes
);
7229 inode
= igrab(&binode
->vfs_inode
);
7231 list_move_tail(&binode
->delalloc_inodes
,
7232 &root
->fs_info
->delalloc_inodes
);
7236 list_del_init(&binode
->delalloc_inodes
);
7237 cond_resched_lock(&root
->fs_info
->delalloc_lock
);
7239 spin_unlock(&root
->fs_info
->delalloc_lock
);
7243 filemap_write_and_wait(inode
->i_mapping
);
7245 * We have to do this because compression doesn't
7246 * actually set PG_writeback until it submits the pages
7247 * for IO, which happens in an async thread, so we could
7248 * race and not actually wait for any writeback pages
7249 * because they've not been submitted yet. Technically
7250 * this could still be the case for the ordered stuff
7251 * since the async thread may not have started to do its
7252 * work yet. If this becomes the case then we need to
7253 * figure out a way to make sure that in writepage we
7254 * wait for any async pages to be submitted before
7255 * returning so that fdatawait does what its supposed to
7258 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
7260 filemap_flush(inode
->i_mapping
);
7263 btrfs_add_delayed_iput(inode
);
7271 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
7272 const char *symname
)
7274 struct btrfs_trans_handle
*trans
;
7275 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
7276 struct btrfs_path
*path
;
7277 struct btrfs_key key
;
7278 struct inode
*inode
= NULL
;
7286 struct btrfs_file_extent_item
*ei
;
7287 struct extent_buffer
*leaf
;
7288 unsigned long nr
= 0;
7290 name_len
= strlen(symname
) + 1;
7291 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
7292 return -ENAMETOOLONG
;
7295 * 2 items for inode item and ref
7296 * 2 items for dir items
7297 * 1 item for xattr if selinux is on
7299 trans
= btrfs_start_transaction(root
, 5);
7301 return PTR_ERR(trans
);
7303 btrfs_set_trans_block_group(trans
, dir
);
7305 err
= btrfs_find_free_ino(root
, &objectid
);
7309 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
7310 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
7311 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
,
7313 if (IS_ERR(inode
)) {
7314 err
= PTR_ERR(inode
);
7318 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
7324 btrfs_set_trans_block_group(trans
, inode
);
7325 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
7329 inode
->i_mapping
->a_ops
= &btrfs_aops
;
7330 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
7331 inode
->i_fop
= &btrfs_file_operations
;
7332 inode
->i_op
= &btrfs_file_inode_operations
;
7333 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
7335 btrfs_update_inode_block_group(trans
, inode
);
7336 btrfs_update_inode_block_group(trans
, dir
);
7340 path
= btrfs_alloc_path();
7342 key
.objectid
= btrfs_ino(inode
);
7344 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
7345 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
7346 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
7352 leaf
= path
->nodes
[0];
7353 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
7354 struct btrfs_file_extent_item
);
7355 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
7356 btrfs_set_file_extent_type(leaf
, ei
,
7357 BTRFS_FILE_EXTENT_INLINE
);
7358 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
7359 btrfs_set_file_extent_compression(leaf
, ei
, 0);
7360 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
7361 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
7363 ptr
= btrfs_file_extent_inline_start(ei
);
7364 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
7365 btrfs_mark_buffer_dirty(leaf
);
7366 btrfs_free_path(path
);
7368 inode
->i_op
= &btrfs_symlink_inode_operations
;
7369 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
7370 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
7371 inode_set_bytes(inode
, name_len
);
7372 btrfs_i_size_write(inode
, name_len
- 1);
7373 err
= btrfs_update_inode(trans
, root
, inode
);
7378 nr
= trans
->blocks_used
;
7379 btrfs_end_transaction_throttle(trans
, root
);
7381 inode_dec_link_count(inode
);
7384 btrfs_btree_balance_dirty(root
, nr
);
7388 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
7389 u64 start
, u64 num_bytes
, u64 min_size
,
7390 loff_t actual_len
, u64
*alloc_hint
,
7391 struct btrfs_trans_handle
*trans
)
7393 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7394 struct btrfs_key ins
;
7395 u64 cur_offset
= start
;
7398 bool own_trans
= true;
7402 while (num_bytes
> 0) {
7404 trans
= btrfs_start_transaction(root
, 3);
7405 if (IS_ERR(trans
)) {
7406 ret
= PTR_ERR(trans
);
7411 ret
= btrfs_reserve_extent(trans
, root
, num_bytes
, min_size
,
7412 0, *alloc_hint
, (u64
)-1, &ins
, 1);
7415 btrfs_end_transaction(trans
, root
);
7419 ret
= insert_reserved_file_extent(trans
, inode
,
7420 cur_offset
, ins
.objectid
,
7421 ins
.offset
, ins
.offset
,
7422 ins
.offset
, 0, 0, 0,
7423 BTRFS_FILE_EXTENT_PREALLOC
);
7425 btrfs_drop_extent_cache(inode
, cur_offset
,
7426 cur_offset
+ ins
.offset
-1, 0);
7428 num_bytes
-= ins
.offset
;
7429 cur_offset
+= ins
.offset
;
7430 *alloc_hint
= ins
.objectid
+ ins
.offset
;
7432 inode
->i_ctime
= CURRENT_TIME
;
7433 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
7434 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
7435 (actual_len
> inode
->i_size
) &&
7436 (cur_offset
> inode
->i_size
)) {
7437 if (cur_offset
> actual_len
)
7438 i_size
= actual_len
;
7440 i_size
= cur_offset
;
7441 i_size_write(inode
, i_size
);
7442 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
7445 ret
= btrfs_update_inode(trans
, root
, inode
);
7449 btrfs_end_transaction(trans
, root
);
7454 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
7455 u64 start
, u64 num_bytes
, u64 min_size
,
7456 loff_t actual_len
, u64
*alloc_hint
)
7458 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
7459 min_size
, actual_len
, alloc_hint
,
7463 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
7464 struct btrfs_trans_handle
*trans
, int mode
,
7465 u64 start
, u64 num_bytes
, u64 min_size
,
7466 loff_t actual_len
, u64
*alloc_hint
)
7468 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
7469 min_size
, actual_len
, alloc_hint
, trans
);
7472 static int btrfs_set_page_dirty(struct page
*page
)
7474 return __set_page_dirty_nobuffers(page
);
7477 static int btrfs_permission(struct inode
*inode
, int mask
, unsigned int flags
)
7479 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7481 if (btrfs_root_readonly(root
) && (mask
& MAY_WRITE
))
7483 if ((BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
) && (mask
& MAY_WRITE
))
7485 return generic_permission(inode
, mask
, flags
, btrfs_check_acl
);
7488 static const struct inode_operations btrfs_dir_inode_operations
= {
7489 .getattr
= btrfs_getattr
,
7490 .lookup
= btrfs_lookup
,
7491 .create
= btrfs_create
,
7492 .unlink
= btrfs_unlink
,
7494 .mkdir
= btrfs_mkdir
,
7495 .rmdir
= btrfs_rmdir
,
7496 .rename
= btrfs_rename
,
7497 .symlink
= btrfs_symlink
,
7498 .setattr
= btrfs_setattr
,
7499 .mknod
= btrfs_mknod
,
7500 .setxattr
= btrfs_setxattr
,
7501 .getxattr
= btrfs_getxattr
,
7502 .listxattr
= btrfs_listxattr
,
7503 .removexattr
= btrfs_removexattr
,
7504 .permission
= btrfs_permission
,
7506 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
7507 .lookup
= btrfs_lookup
,
7508 .permission
= btrfs_permission
,
7511 static const struct file_operations btrfs_dir_file_operations
= {
7512 .llseek
= generic_file_llseek
,
7513 .read
= generic_read_dir
,
7514 .readdir
= btrfs_real_readdir
,
7515 .unlocked_ioctl
= btrfs_ioctl
,
7516 #ifdef CONFIG_COMPAT
7517 .compat_ioctl
= btrfs_ioctl
,
7519 .release
= btrfs_release_file
,
7520 .fsync
= btrfs_sync_file
,
7523 static struct extent_io_ops btrfs_extent_io_ops
= {
7524 .fill_delalloc
= run_delalloc_range
,
7525 .submit_bio_hook
= btrfs_submit_bio_hook
,
7526 .merge_bio_hook
= btrfs_merge_bio_hook
,
7527 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
7528 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
7529 .writepage_start_hook
= btrfs_writepage_start_hook
,
7530 .readpage_io_failed_hook
= btrfs_io_failed_hook
,
7531 .set_bit_hook
= btrfs_set_bit_hook
,
7532 .clear_bit_hook
= btrfs_clear_bit_hook
,
7533 .merge_extent_hook
= btrfs_merge_extent_hook
,
7534 .split_extent_hook
= btrfs_split_extent_hook
,
7538 * btrfs doesn't support the bmap operation because swapfiles
7539 * use bmap to make a mapping of extents in the file. They assume
7540 * these extents won't change over the life of the file and they
7541 * use the bmap result to do IO directly to the drive.
7543 * the btrfs bmap call would return logical addresses that aren't
7544 * suitable for IO and they also will change frequently as COW
7545 * operations happen. So, swapfile + btrfs == corruption.
7547 * For now we're avoiding this by dropping bmap.
7549 static const struct address_space_operations btrfs_aops
= {
7550 .readpage
= btrfs_readpage
,
7551 .writepage
= btrfs_writepage
,
7552 .writepages
= btrfs_writepages
,
7553 .readpages
= btrfs_readpages
,
7554 .direct_IO
= btrfs_direct_IO
,
7555 .invalidatepage
= btrfs_invalidatepage
,
7556 .releasepage
= btrfs_releasepage
,
7557 .set_page_dirty
= btrfs_set_page_dirty
,
7558 .error_remove_page
= generic_error_remove_page
,
7561 static const struct address_space_operations btrfs_symlink_aops
= {
7562 .readpage
= btrfs_readpage
,
7563 .writepage
= btrfs_writepage
,
7564 .invalidatepage
= btrfs_invalidatepage
,
7565 .releasepage
= btrfs_releasepage
,
7568 static const struct inode_operations btrfs_file_inode_operations
= {
7569 .getattr
= btrfs_getattr
,
7570 .setattr
= btrfs_setattr
,
7571 .setxattr
= btrfs_setxattr
,
7572 .getxattr
= btrfs_getxattr
,
7573 .listxattr
= btrfs_listxattr
,
7574 .removexattr
= btrfs_removexattr
,
7575 .permission
= btrfs_permission
,
7576 .fiemap
= btrfs_fiemap
,
7578 static const struct inode_operations btrfs_special_inode_operations
= {
7579 .getattr
= btrfs_getattr
,
7580 .setattr
= btrfs_setattr
,
7581 .permission
= btrfs_permission
,
7582 .setxattr
= btrfs_setxattr
,
7583 .getxattr
= btrfs_getxattr
,
7584 .listxattr
= btrfs_listxattr
,
7585 .removexattr
= btrfs_removexattr
,
7587 static const struct inode_operations btrfs_symlink_inode_operations
= {
7588 .readlink
= generic_readlink
,
7589 .follow_link
= page_follow_link_light
,
7590 .put_link
= page_put_link
,
7591 .getattr
= btrfs_getattr
,
7592 .permission
= btrfs_permission
,
7593 .setxattr
= btrfs_setxattr
,
7594 .getxattr
= btrfs_getxattr
,
7595 .listxattr
= btrfs_listxattr
,
7596 .removexattr
= btrfs_removexattr
,
7599 const struct dentry_operations btrfs_dentry_operations
= {
7600 .d_delete
= btrfs_dentry_delete
,