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"
54 struct btrfs_iget_args
{
56 struct btrfs_root
*root
;
59 static const struct inode_operations btrfs_dir_inode_operations
;
60 static const struct inode_operations btrfs_symlink_inode_operations
;
61 static const struct inode_operations btrfs_dir_ro_inode_operations
;
62 static const struct inode_operations btrfs_special_inode_operations
;
63 static const struct inode_operations btrfs_file_inode_operations
;
64 static const struct address_space_operations btrfs_aops
;
65 static const struct address_space_operations btrfs_symlink_aops
;
66 static const struct file_operations btrfs_dir_file_operations
;
67 static struct extent_io_ops btrfs_extent_io_ops
;
69 static struct kmem_cache
*btrfs_inode_cachep
;
70 struct kmem_cache
*btrfs_trans_handle_cachep
;
71 struct kmem_cache
*btrfs_transaction_cachep
;
72 struct kmem_cache
*btrfs_path_cachep
;
75 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
76 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
77 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
78 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
79 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
80 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
81 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
82 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
85 static void btrfs_truncate(struct inode
*inode
);
86 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
);
87 static noinline
int cow_file_range(struct inode
*inode
,
88 struct page
*locked_page
,
89 u64 start
, u64 end
, int *page_started
,
90 unsigned long *nr_written
, int unlock
);
92 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
93 struct inode
*inode
, struct inode
*dir
,
94 const struct qstr
*qstr
)
98 err
= btrfs_init_acl(trans
, inode
, dir
);
100 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
105 * this does all the hard work for inserting an inline extent into
106 * the btree. The caller should have done a btrfs_drop_extents so that
107 * no overlapping inline items exist in the btree
109 static noinline
int insert_inline_extent(struct btrfs_trans_handle
*trans
,
110 struct btrfs_root
*root
, struct inode
*inode
,
111 u64 start
, size_t size
, size_t compressed_size
,
112 struct page
**compressed_pages
)
114 struct btrfs_key key
;
115 struct btrfs_path
*path
;
116 struct extent_buffer
*leaf
;
117 struct page
*page
= NULL
;
120 struct btrfs_file_extent_item
*ei
;
123 size_t cur_size
= size
;
125 unsigned long offset
;
126 int use_compress
= 0;
128 if (compressed_size
&& compressed_pages
) {
130 cur_size
= compressed_size
;
133 path
= btrfs_alloc_path();
137 path
->leave_spinning
= 1;
138 btrfs_set_trans_block_group(trans
, inode
);
140 key
.objectid
= inode
->i_ino
;
142 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
143 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
145 inode_add_bytes(inode
, size
);
146 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
153 leaf
= path
->nodes
[0];
154 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
155 struct btrfs_file_extent_item
);
156 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
157 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
158 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
159 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
160 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
161 ptr
= btrfs_file_extent_inline_start(ei
);
166 while (compressed_size
> 0) {
167 cpage
= compressed_pages
[i
];
168 cur_size
= min_t(unsigned long, compressed_size
,
171 kaddr
= kmap_atomic(cpage
, KM_USER0
);
172 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
173 kunmap_atomic(kaddr
, KM_USER0
);
177 compressed_size
-= cur_size
;
179 btrfs_set_file_extent_compression(leaf
, ei
,
180 BTRFS_COMPRESS_ZLIB
);
182 page
= find_get_page(inode
->i_mapping
,
183 start
>> PAGE_CACHE_SHIFT
);
184 btrfs_set_file_extent_compression(leaf
, ei
, 0);
185 kaddr
= kmap_atomic(page
, KM_USER0
);
186 offset
= start
& (PAGE_CACHE_SIZE
- 1);
187 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
188 kunmap_atomic(kaddr
, KM_USER0
);
189 page_cache_release(page
);
191 btrfs_mark_buffer_dirty(leaf
);
192 btrfs_free_path(path
);
195 * we're an inline extent, so nobody can
196 * extend the file past i_size without locking
197 * a page we already have locked.
199 * We must do any isize and inode updates
200 * before we unlock the pages. Otherwise we
201 * could end up racing with unlink.
203 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
204 btrfs_update_inode(trans
, root
, inode
);
208 btrfs_free_path(path
);
214 * conditionally insert an inline extent into the file. This
215 * does the checks required to make sure the data is small enough
216 * to fit as an inline extent.
218 static noinline
int cow_file_range_inline(struct btrfs_trans_handle
*trans
,
219 struct btrfs_root
*root
,
220 struct inode
*inode
, u64 start
, u64 end
,
221 size_t compressed_size
,
222 struct page
**compressed_pages
)
224 u64 isize
= i_size_read(inode
);
225 u64 actual_end
= min(end
+ 1, isize
);
226 u64 inline_len
= actual_end
- start
;
227 u64 aligned_end
= (end
+ root
->sectorsize
- 1) &
228 ~((u64
)root
->sectorsize
- 1);
230 u64 data_len
= inline_len
;
234 data_len
= compressed_size
;
237 actual_end
>= PAGE_CACHE_SIZE
||
238 data_len
>= BTRFS_MAX_INLINE_DATA_SIZE(root
) ||
240 (actual_end
& (root
->sectorsize
- 1)) == 0) ||
242 data_len
> root
->fs_info
->max_inline
) {
246 ret
= btrfs_drop_extents(trans
, inode
, start
, aligned_end
,
250 if (isize
> actual_end
)
251 inline_len
= min_t(u64
, isize
, actual_end
);
252 ret
= insert_inline_extent(trans
, root
, inode
, start
,
253 inline_len
, compressed_size
,
256 btrfs_delalloc_release_metadata(inode
, end
+ 1 - start
);
257 btrfs_drop_extent_cache(inode
, start
, aligned_end
- 1, 0);
261 struct async_extent
{
266 unsigned long nr_pages
;
267 struct list_head list
;
272 struct btrfs_root
*root
;
273 struct page
*locked_page
;
276 struct list_head extents
;
277 struct btrfs_work work
;
280 static noinline
int add_async_extent(struct async_cow
*cow
,
281 u64 start
, u64 ram_size
,
284 unsigned long nr_pages
)
286 struct async_extent
*async_extent
;
288 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
289 async_extent
->start
= start
;
290 async_extent
->ram_size
= ram_size
;
291 async_extent
->compressed_size
= compressed_size
;
292 async_extent
->pages
= pages
;
293 async_extent
->nr_pages
= nr_pages
;
294 list_add_tail(&async_extent
->list
, &cow
->extents
);
299 * we create compressed extents in two phases. The first
300 * phase compresses a range of pages that have already been
301 * locked (both pages and state bits are locked).
303 * This is done inside an ordered work queue, and the compression
304 * is spread across many cpus. The actual IO submission is step
305 * two, and the ordered work queue takes care of making sure that
306 * happens in the same order things were put onto the queue by
307 * writepages and friends.
309 * If this code finds it can't get good compression, it puts an
310 * entry onto the work queue to write the uncompressed bytes. This
311 * makes sure that both compressed inodes and uncompressed inodes
312 * are written in the same order that pdflush sent them down.
314 static noinline
int compress_file_range(struct inode
*inode
,
315 struct page
*locked_page
,
317 struct async_cow
*async_cow
,
320 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
321 struct btrfs_trans_handle
*trans
;
323 u64 blocksize
= root
->sectorsize
;
325 u64 isize
= i_size_read(inode
);
327 struct page
**pages
= NULL
;
328 unsigned long nr_pages
;
329 unsigned long nr_pages_ret
= 0;
330 unsigned long total_compressed
= 0;
331 unsigned long total_in
= 0;
332 unsigned long max_compressed
= 128 * 1024;
333 unsigned long max_uncompressed
= 128 * 1024;
337 actual_end
= min_t(u64
, isize
, end
+ 1);
340 nr_pages
= (end
>> PAGE_CACHE_SHIFT
) - (start
>> PAGE_CACHE_SHIFT
) + 1;
341 nr_pages
= min(nr_pages
, (128 * 1024UL) / PAGE_CACHE_SIZE
);
344 * we don't want to send crud past the end of i_size through
345 * compression, that's just a waste of CPU time. So, if the
346 * end of the file is before the start of our current
347 * requested range of bytes, we bail out to the uncompressed
348 * cleanup code that can deal with all of this.
350 * It isn't really the fastest way to fix things, but this is a
351 * very uncommon corner.
353 if (actual_end
<= start
)
354 goto cleanup_and_bail_uncompressed
;
356 total_compressed
= actual_end
- start
;
358 /* we want to make sure that amount of ram required to uncompress
359 * an extent is reasonable, so we limit the total size in ram
360 * of a compressed extent to 128k. This is a crucial number
361 * because it also controls how easily we can spread reads across
362 * cpus for decompression.
364 * We also want to make sure the amount of IO required to do
365 * a random read is reasonably small, so we limit the size of
366 * a compressed extent to 128k.
368 total_compressed
= min(total_compressed
, max_uncompressed
);
369 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
370 num_bytes
= max(blocksize
, num_bytes
);
375 * we do compression for mount -o compress and when the
376 * inode has not been flagged as nocompress. This flag can
377 * change at any time if we discover bad compression ratios.
379 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
) &&
380 (btrfs_test_opt(root
, COMPRESS
) ||
381 (BTRFS_I(inode
)->force_compress
))) {
383 pages
= kzalloc(sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
385 ret
= btrfs_zlib_compress_pages(inode
->i_mapping
, start
,
386 total_compressed
, pages
,
387 nr_pages
, &nr_pages_ret
,
393 unsigned long offset
= total_compressed
&
394 (PAGE_CACHE_SIZE
- 1);
395 struct page
*page
= pages
[nr_pages_ret
- 1];
398 /* zero the tail end of the last page, we might be
399 * sending it down to disk
402 kaddr
= kmap_atomic(page
, KM_USER0
);
403 memset(kaddr
+ offset
, 0,
404 PAGE_CACHE_SIZE
- offset
);
405 kunmap_atomic(kaddr
, KM_USER0
);
411 trans
= btrfs_join_transaction(root
, 1);
413 btrfs_set_trans_block_group(trans
, inode
);
414 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
416 /* lets try to make an inline extent */
417 if (ret
|| total_in
< (actual_end
- start
)) {
418 /* we didn't compress the entire range, try
419 * to make an uncompressed inline extent.
421 ret
= cow_file_range_inline(trans
, root
, inode
,
422 start
, end
, 0, NULL
);
424 /* try making a compressed inline extent */
425 ret
= cow_file_range_inline(trans
, root
, inode
,
427 total_compressed
, pages
);
431 * inline extent creation worked, we don't need
432 * to create any more async work items. Unlock
433 * and free up our temp pages.
435 extent_clear_unlock_delalloc(inode
,
436 &BTRFS_I(inode
)->io_tree
,
438 EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
439 EXTENT_CLEAR_DELALLOC
|
440 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
);
442 btrfs_end_transaction(trans
, root
);
445 btrfs_end_transaction(trans
, root
);
450 * we aren't doing an inline extent round the compressed size
451 * up to a block size boundary so the allocator does sane
454 total_compressed
= (total_compressed
+ blocksize
- 1) &
458 * one last check to make sure the compression is really a
459 * win, compare the page count read with the blocks on disk
461 total_in
= (total_in
+ PAGE_CACHE_SIZE
- 1) &
462 ~(PAGE_CACHE_SIZE
- 1);
463 if (total_compressed
>= total_in
) {
466 num_bytes
= total_in
;
469 if (!will_compress
&& pages
) {
471 * the compression code ran but failed to make things smaller,
472 * free any pages it allocated and our page pointer array
474 for (i
= 0; i
< nr_pages_ret
; i
++) {
475 WARN_ON(pages
[i
]->mapping
);
476 page_cache_release(pages
[i
]);
480 total_compressed
= 0;
483 /* flag the file so we don't compress in the future */
484 if (!btrfs_test_opt(root
, FORCE_COMPRESS
) &&
485 !(BTRFS_I(inode
)->force_compress
)) {
486 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
492 /* the async work queues will take care of doing actual
493 * allocation on disk for these compressed pages,
494 * and will submit them to the elevator.
496 add_async_extent(async_cow
, start
, num_bytes
,
497 total_compressed
, pages
, nr_pages_ret
);
499 if (start
+ num_bytes
< end
) {
506 cleanup_and_bail_uncompressed
:
508 * No compression, but we still need to write the pages in
509 * the file we've been given so far. redirty the locked
510 * page if it corresponds to our extent and set things up
511 * for the async work queue to run cow_file_range to do
512 * the normal delalloc dance
514 if (page_offset(locked_page
) >= start
&&
515 page_offset(locked_page
) <= end
) {
516 __set_page_dirty_nobuffers(locked_page
);
517 /* unlocked later on in the async handlers */
519 add_async_extent(async_cow
, start
, end
- start
+ 1, 0, NULL
, 0);
527 for (i
= 0; i
< nr_pages_ret
; i
++) {
528 WARN_ON(pages
[i
]->mapping
);
529 page_cache_release(pages
[i
]);
537 * phase two of compressed writeback. This is the ordered portion
538 * of the code, which only gets called in the order the work was
539 * queued. We walk all the async extents created by compress_file_range
540 * and send them down to the disk.
542 static noinline
int submit_compressed_extents(struct inode
*inode
,
543 struct async_cow
*async_cow
)
545 struct async_extent
*async_extent
;
547 struct btrfs_trans_handle
*trans
;
548 struct btrfs_key ins
;
549 struct extent_map
*em
;
550 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
551 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
552 struct extent_io_tree
*io_tree
;
555 if (list_empty(&async_cow
->extents
))
559 while (!list_empty(&async_cow
->extents
)) {
560 async_extent
= list_entry(async_cow
->extents
.next
,
561 struct async_extent
, list
);
562 list_del(&async_extent
->list
);
564 io_tree
= &BTRFS_I(inode
)->io_tree
;
567 /* did the compression code fall back to uncompressed IO? */
568 if (!async_extent
->pages
) {
569 int page_started
= 0;
570 unsigned long nr_written
= 0;
572 lock_extent(io_tree
, async_extent
->start
,
573 async_extent
->start
+
574 async_extent
->ram_size
- 1, GFP_NOFS
);
576 /* allocate blocks */
577 ret
= cow_file_range(inode
, async_cow
->locked_page
,
579 async_extent
->start
+
580 async_extent
->ram_size
- 1,
581 &page_started
, &nr_written
, 0);
584 * if page_started, cow_file_range inserted an
585 * inline extent and took care of all the unlocking
586 * and IO for us. Otherwise, we need to submit
587 * all those pages down to the drive.
589 if (!page_started
&& !ret
)
590 extent_write_locked_range(io_tree
,
591 inode
, async_extent
->start
,
592 async_extent
->start
+
593 async_extent
->ram_size
- 1,
601 lock_extent(io_tree
, async_extent
->start
,
602 async_extent
->start
+ async_extent
->ram_size
- 1,
605 trans
= btrfs_join_transaction(root
, 1);
606 ret
= btrfs_reserve_extent(trans
, root
,
607 async_extent
->compressed_size
,
608 async_extent
->compressed_size
,
611 btrfs_end_transaction(trans
, root
);
615 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
616 WARN_ON(async_extent
->pages
[i
]->mapping
);
617 page_cache_release(async_extent
->pages
[i
]);
619 kfree(async_extent
->pages
);
620 async_extent
->nr_pages
= 0;
621 async_extent
->pages
= NULL
;
622 unlock_extent(io_tree
, async_extent
->start
,
623 async_extent
->start
+
624 async_extent
->ram_size
- 1, GFP_NOFS
);
629 * here we're doing allocation and writeback of the
632 btrfs_drop_extent_cache(inode
, async_extent
->start
,
633 async_extent
->start
+
634 async_extent
->ram_size
- 1, 0);
636 em
= alloc_extent_map(GFP_NOFS
);
637 em
->start
= async_extent
->start
;
638 em
->len
= async_extent
->ram_size
;
639 em
->orig_start
= em
->start
;
641 em
->block_start
= ins
.objectid
;
642 em
->block_len
= ins
.offset
;
643 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
644 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
645 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
648 write_lock(&em_tree
->lock
);
649 ret
= add_extent_mapping(em_tree
, em
);
650 write_unlock(&em_tree
->lock
);
651 if (ret
!= -EEXIST
) {
655 btrfs_drop_extent_cache(inode
, async_extent
->start
,
656 async_extent
->start
+
657 async_extent
->ram_size
- 1, 0);
660 ret
= btrfs_add_ordered_extent(inode
, async_extent
->start
,
662 async_extent
->ram_size
,
664 BTRFS_ORDERED_COMPRESSED
);
668 * clear dirty, set writeback and unlock the pages.
670 extent_clear_unlock_delalloc(inode
,
671 &BTRFS_I(inode
)->io_tree
,
673 async_extent
->start
+
674 async_extent
->ram_size
- 1,
675 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
676 EXTENT_CLEAR_UNLOCK
|
677 EXTENT_CLEAR_DELALLOC
|
678 EXTENT_CLEAR_DIRTY
| EXTENT_SET_WRITEBACK
);
680 ret
= btrfs_submit_compressed_write(inode
,
682 async_extent
->ram_size
,
684 ins
.offset
, async_extent
->pages
,
685 async_extent
->nr_pages
);
688 alloc_hint
= ins
.objectid
+ ins
.offset
;
696 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
699 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
700 struct extent_map
*em
;
703 read_lock(&em_tree
->lock
);
704 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
707 * if block start isn't an actual block number then find the
708 * first block in this inode and use that as a hint. If that
709 * block is also bogus then just don't worry about it.
711 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
713 em
= search_extent_mapping(em_tree
, 0, 0);
714 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
715 alloc_hint
= em
->block_start
;
719 alloc_hint
= em
->block_start
;
723 read_unlock(&em_tree
->lock
);
729 * when extent_io.c finds a delayed allocation range in the file,
730 * the call backs end up in this code. The basic idea is to
731 * allocate extents on disk for the range, and create ordered data structs
732 * in ram to track those extents.
734 * locked_page is the page that writepage had locked already. We use
735 * it to make sure we don't do extra locks or unlocks.
737 * *page_started is set to one if we unlock locked_page and do everything
738 * required to start IO on it. It may be clean and already done with
741 static noinline
int cow_file_range(struct inode
*inode
,
742 struct page
*locked_page
,
743 u64 start
, u64 end
, int *page_started
,
744 unsigned long *nr_written
,
747 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
748 struct btrfs_trans_handle
*trans
;
751 unsigned long ram_size
;
754 u64 blocksize
= root
->sectorsize
;
755 struct btrfs_key ins
;
756 struct extent_map
*em
;
757 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
760 BUG_ON(root
== root
->fs_info
->tree_root
);
761 trans
= btrfs_join_transaction(root
, 1);
763 btrfs_set_trans_block_group(trans
, inode
);
764 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
766 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
767 num_bytes
= max(blocksize
, num_bytes
);
768 disk_num_bytes
= num_bytes
;
772 /* lets try to make an inline extent */
773 ret
= cow_file_range_inline(trans
, root
, inode
,
774 start
, end
, 0, NULL
);
776 extent_clear_unlock_delalloc(inode
,
777 &BTRFS_I(inode
)->io_tree
,
779 EXTENT_CLEAR_UNLOCK_PAGE
|
780 EXTENT_CLEAR_UNLOCK
|
781 EXTENT_CLEAR_DELALLOC
|
783 EXTENT_SET_WRITEBACK
|
784 EXTENT_END_WRITEBACK
);
786 *nr_written
= *nr_written
+
787 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
794 BUG_ON(disk_num_bytes
>
795 btrfs_super_total_bytes(&root
->fs_info
->super_copy
));
797 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
798 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
800 while (disk_num_bytes
> 0) {
803 cur_alloc_size
= disk_num_bytes
;
804 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
805 root
->sectorsize
, 0, alloc_hint
,
809 em
= alloc_extent_map(GFP_NOFS
);
811 em
->orig_start
= em
->start
;
812 ram_size
= ins
.offset
;
813 em
->len
= ins
.offset
;
815 em
->block_start
= ins
.objectid
;
816 em
->block_len
= ins
.offset
;
817 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
818 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
821 write_lock(&em_tree
->lock
);
822 ret
= add_extent_mapping(em_tree
, em
);
823 write_unlock(&em_tree
->lock
);
824 if (ret
!= -EEXIST
) {
828 btrfs_drop_extent_cache(inode
, start
,
829 start
+ ram_size
- 1, 0);
832 cur_alloc_size
= ins
.offset
;
833 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
834 ram_size
, cur_alloc_size
, 0);
837 if (root
->root_key
.objectid
==
838 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
839 ret
= btrfs_reloc_clone_csums(inode
, start
,
844 if (disk_num_bytes
< cur_alloc_size
)
847 /* we're not doing compressed IO, don't unlock the first
848 * page (which the caller expects to stay locked), don't
849 * clear any dirty bits and don't set any writeback bits
851 * Do set the Private2 bit so we know this page was properly
852 * setup for writepage
854 op
= unlock
? EXTENT_CLEAR_UNLOCK_PAGE
: 0;
855 op
|= EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
858 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
859 start
, start
+ ram_size
- 1,
861 disk_num_bytes
-= cur_alloc_size
;
862 num_bytes
-= cur_alloc_size
;
863 alloc_hint
= ins
.objectid
+ ins
.offset
;
864 start
+= cur_alloc_size
;
868 btrfs_end_transaction(trans
, root
);
874 * work queue call back to started compression on a file and pages
876 static noinline
void async_cow_start(struct btrfs_work
*work
)
878 struct async_cow
*async_cow
;
880 async_cow
= container_of(work
, struct async_cow
, work
);
882 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
883 async_cow
->start
, async_cow
->end
, async_cow
,
886 async_cow
->inode
= NULL
;
890 * work queue call back to submit previously compressed pages
892 static noinline
void async_cow_submit(struct btrfs_work
*work
)
894 struct async_cow
*async_cow
;
895 struct btrfs_root
*root
;
896 unsigned long nr_pages
;
898 async_cow
= container_of(work
, struct async_cow
, work
);
900 root
= async_cow
->root
;
901 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
904 atomic_sub(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
906 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
908 waitqueue_active(&root
->fs_info
->async_submit_wait
))
909 wake_up(&root
->fs_info
->async_submit_wait
);
911 if (async_cow
->inode
)
912 submit_compressed_extents(async_cow
->inode
, async_cow
);
915 static noinline
void async_cow_free(struct btrfs_work
*work
)
917 struct async_cow
*async_cow
;
918 async_cow
= container_of(work
, struct async_cow
, work
);
922 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
923 u64 start
, u64 end
, int *page_started
,
924 unsigned long *nr_written
)
926 struct async_cow
*async_cow
;
927 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
928 unsigned long nr_pages
;
930 int limit
= 10 * 1024 * 1042;
932 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
933 1, 0, NULL
, GFP_NOFS
);
934 while (start
< end
) {
935 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
936 async_cow
->inode
= inode
;
937 async_cow
->root
= root
;
938 async_cow
->locked_page
= locked_page
;
939 async_cow
->start
= start
;
941 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
944 cur_end
= min(end
, start
+ 512 * 1024 - 1);
946 async_cow
->end
= cur_end
;
947 INIT_LIST_HEAD(&async_cow
->extents
);
949 async_cow
->work
.func
= async_cow_start
;
950 async_cow
->work
.ordered_func
= async_cow_submit
;
951 async_cow
->work
.ordered_free
= async_cow_free
;
952 async_cow
->work
.flags
= 0;
954 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
956 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
958 btrfs_queue_worker(&root
->fs_info
->delalloc_workers
,
961 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
962 wait_event(root
->fs_info
->async_submit_wait
,
963 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
967 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
968 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
969 wait_event(root
->fs_info
->async_submit_wait
,
970 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
974 *nr_written
+= nr_pages
;
981 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
982 u64 bytenr
, u64 num_bytes
)
985 struct btrfs_ordered_sum
*sums
;
988 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
989 bytenr
+ num_bytes
- 1, &list
);
990 if (ret
== 0 && list_empty(&list
))
993 while (!list_empty(&list
)) {
994 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
995 list_del(&sums
->list
);
1002 * when nowcow writeback call back. This checks for snapshots or COW copies
1003 * of the extents that exist in the file, and COWs the file as required.
1005 * If no cow copies or snapshots exist, we write directly to the existing
1008 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1009 struct page
*locked_page
,
1010 u64 start
, u64 end
, int *page_started
, int force
,
1011 unsigned long *nr_written
)
1013 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1014 struct btrfs_trans_handle
*trans
;
1015 struct extent_buffer
*leaf
;
1016 struct btrfs_path
*path
;
1017 struct btrfs_file_extent_item
*fi
;
1018 struct btrfs_key found_key
;
1030 bool nolock
= false;
1032 path
= btrfs_alloc_path();
1034 if (root
== root
->fs_info
->tree_root
) {
1036 trans
= btrfs_join_transaction_nolock(root
, 1);
1038 trans
= btrfs_join_transaction(root
, 1);
1042 cow_start
= (u64
)-1;
1045 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
1048 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1049 leaf
= path
->nodes
[0];
1050 btrfs_item_key_to_cpu(leaf
, &found_key
,
1051 path
->slots
[0] - 1);
1052 if (found_key
.objectid
== inode
->i_ino
&&
1053 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1058 leaf
= path
->nodes
[0];
1059 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1060 ret
= btrfs_next_leaf(root
, path
);
1065 leaf
= path
->nodes
[0];
1071 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1073 if (found_key
.objectid
> inode
->i_ino
||
1074 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1075 found_key
.offset
> end
)
1078 if (found_key
.offset
> cur_offset
) {
1079 extent_end
= found_key
.offset
;
1084 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1085 struct btrfs_file_extent_item
);
1086 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1088 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1089 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1090 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1091 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1092 extent_end
= found_key
.offset
+
1093 btrfs_file_extent_num_bytes(leaf
, fi
);
1094 if (extent_end
<= start
) {
1098 if (disk_bytenr
== 0)
1100 if (btrfs_file_extent_compression(leaf
, fi
) ||
1101 btrfs_file_extent_encryption(leaf
, fi
) ||
1102 btrfs_file_extent_other_encoding(leaf
, fi
))
1104 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1106 if (btrfs_extent_readonly(root
, disk_bytenr
))
1108 if (btrfs_cross_ref_exist(trans
, root
, inode
->i_ino
,
1110 extent_offset
, disk_bytenr
))
1112 disk_bytenr
+= extent_offset
;
1113 disk_bytenr
+= cur_offset
- found_key
.offset
;
1114 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1116 * force cow if csum exists in the range.
1117 * this ensure that csum for a given extent are
1118 * either valid or do not exist.
1120 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1123 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1124 extent_end
= found_key
.offset
+
1125 btrfs_file_extent_inline_len(leaf
, fi
);
1126 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1131 if (extent_end
<= start
) {
1136 if (cow_start
== (u64
)-1)
1137 cow_start
= cur_offset
;
1138 cur_offset
= extent_end
;
1139 if (cur_offset
> end
)
1145 btrfs_release_path(root
, path
);
1146 if (cow_start
!= (u64
)-1) {
1147 ret
= cow_file_range(inode
, locked_page
, cow_start
,
1148 found_key
.offset
- 1, page_started
,
1151 cow_start
= (u64
)-1;
1154 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1155 struct extent_map
*em
;
1156 struct extent_map_tree
*em_tree
;
1157 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1158 em
= alloc_extent_map(GFP_NOFS
);
1159 em
->start
= cur_offset
;
1160 em
->orig_start
= em
->start
;
1161 em
->len
= num_bytes
;
1162 em
->block_len
= num_bytes
;
1163 em
->block_start
= disk_bytenr
;
1164 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1165 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1167 write_lock(&em_tree
->lock
);
1168 ret
= add_extent_mapping(em_tree
, em
);
1169 write_unlock(&em_tree
->lock
);
1170 if (ret
!= -EEXIST
) {
1171 free_extent_map(em
);
1174 btrfs_drop_extent_cache(inode
, em
->start
,
1175 em
->start
+ em
->len
- 1, 0);
1177 type
= BTRFS_ORDERED_PREALLOC
;
1179 type
= BTRFS_ORDERED_NOCOW
;
1182 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1183 num_bytes
, num_bytes
, type
);
1186 if (root
->root_key
.objectid
==
1187 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1188 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1193 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
1194 cur_offset
, cur_offset
+ num_bytes
- 1,
1195 locked_page
, EXTENT_CLEAR_UNLOCK_PAGE
|
1196 EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
1197 EXTENT_SET_PRIVATE2
);
1198 cur_offset
= extent_end
;
1199 if (cur_offset
> end
)
1202 btrfs_release_path(root
, path
);
1204 if (cur_offset
<= end
&& cow_start
== (u64
)-1)
1205 cow_start
= cur_offset
;
1206 if (cow_start
!= (u64
)-1) {
1207 ret
= cow_file_range(inode
, locked_page
, cow_start
, end
,
1208 page_started
, nr_written
, 1);
1213 ret
= btrfs_end_transaction_nolock(trans
, root
);
1216 ret
= btrfs_end_transaction(trans
, root
);
1219 btrfs_free_path(path
);
1224 * extent_io.c call back to do delayed allocation processing
1226 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1227 u64 start
, u64 end
, int *page_started
,
1228 unsigned long *nr_written
)
1231 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1233 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
)
1234 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1235 page_started
, 1, nr_written
);
1236 else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
)
1237 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1238 page_started
, 0, nr_written
);
1239 else if (!btrfs_test_opt(root
, COMPRESS
) &&
1240 !(BTRFS_I(inode
)->force_compress
))
1241 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1242 page_started
, nr_written
, 1);
1244 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1245 page_started
, nr_written
);
1249 static int btrfs_split_extent_hook(struct inode
*inode
,
1250 struct extent_state
*orig
, u64 split
)
1252 /* not delalloc, ignore it */
1253 if (!(orig
->state
& EXTENT_DELALLOC
))
1256 atomic_inc(&BTRFS_I(inode
)->outstanding_extents
);
1261 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1262 * extents so we can keep track of new extents that are just merged onto old
1263 * extents, such as when we are doing sequential writes, so we can properly
1264 * account for the metadata space we'll need.
1266 static int btrfs_merge_extent_hook(struct inode
*inode
,
1267 struct extent_state
*new,
1268 struct extent_state
*other
)
1270 /* not delalloc, ignore it */
1271 if (!(other
->state
& EXTENT_DELALLOC
))
1274 atomic_dec(&BTRFS_I(inode
)->outstanding_extents
);
1279 * extent_io.c set_bit_hook, used to track delayed allocation
1280 * bytes in this file, and to maintain the list of inodes that
1281 * have pending delalloc work to be done.
1283 static int btrfs_set_bit_hook(struct inode
*inode
,
1284 struct extent_state
*state
, int *bits
)
1288 * set_bit and clear bit hooks normally require _irqsave/restore
1289 * but in this case, we are only testeing for the DELALLOC
1290 * bit, which is only set or cleared with irqs on
1292 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1293 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1294 u64 len
= state
->end
+ 1 - state
->start
;
1295 int do_list
= (root
->root_key
.objectid
!=
1296 BTRFS_ROOT_TREE_OBJECTID
);
1298 if (*bits
& EXTENT_FIRST_DELALLOC
)
1299 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1301 atomic_inc(&BTRFS_I(inode
)->outstanding_extents
);
1303 spin_lock(&root
->fs_info
->delalloc_lock
);
1304 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1305 root
->fs_info
->delalloc_bytes
+= len
;
1306 if (do_list
&& list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1307 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1308 &root
->fs_info
->delalloc_inodes
);
1310 spin_unlock(&root
->fs_info
->delalloc_lock
);
1316 * extent_io.c clear_bit_hook, see set_bit_hook for why
1318 static int btrfs_clear_bit_hook(struct inode
*inode
,
1319 struct extent_state
*state
, int *bits
)
1322 * set_bit and clear bit hooks normally require _irqsave/restore
1323 * but in this case, we are only testeing for the DELALLOC
1324 * bit, which is only set or cleared with irqs on
1326 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1327 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1328 u64 len
= state
->end
+ 1 - state
->start
;
1329 int do_list
= (root
->root_key
.objectid
!=
1330 BTRFS_ROOT_TREE_OBJECTID
);
1332 if (*bits
& EXTENT_FIRST_DELALLOC
)
1333 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1334 else if (!(*bits
& EXTENT_DO_ACCOUNTING
))
1335 atomic_dec(&BTRFS_I(inode
)->outstanding_extents
);
1337 if (*bits
& EXTENT_DO_ACCOUNTING
)
1338 btrfs_delalloc_release_metadata(inode
, len
);
1340 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1342 btrfs_free_reserved_data_space(inode
, len
);
1344 spin_lock(&root
->fs_info
->delalloc_lock
);
1345 root
->fs_info
->delalloc_bytes
-= len
;
1346 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1348 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1349 !list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1350 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1352 spin_unlock(&root
->fs_info
->delalloc_lock
);
1358 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1359 * we don't create bios that span stripes or chunks
1361 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
1362 size_t size
, struct bio
*bio
,
1363 unsigned long bio_flags
)
1365 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1366 struct btrfs_mapping_tree
*map_tree
;
1367 u64 logical
= (u64
)bio
->bi_sector
<< 9;
1372 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1375 length
= bio
->bi_size
;
1376 map_tree
= &root
->fs_info
->mapping_tree
;
1377 map_length
= length
;
1378 ret
= btrfs_map_block(map_tree
, READ
, logical
,
1379 &map_length
, NULL
, 0);
1381 if (map_length
< length
+ size
)
1387 * in order to insert checksums into the metadata in large chunks,
1388 * we wait until bio submission time. All the pages in the bio are
1389 * checksummed and sums are attached onto the ordered extent record.
1391 * At IO completion time the cums attached on the ordered extent record
1392 * are inserted into the btree
1394 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1395 struct bio
*bio
, int mirror_num
,
1396 unsigned long bio_flags
,
1399 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1402 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1408 * in order to insert checksums into the metadata in large chunks,
1409 * we wait until bio submission time. All the pages in the bio are
1410 * checksummed and sums are attached onto the ordered extent record.
1412 * At IO completion time the cums attached on the ordered extent record
1413 * are inserted into the btree
1415 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1416 int mirror_num
, unsigned long bio_flags
,
1419 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1420 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1424 * extent_io.c submission hook. This does the right thing for csum calculation
1425 * on write, or reading the csums from the tree before a read
1427 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1428 int mirror_num
, unsigned long bio_flags
,
1431 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1435 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1437 if (root
== root
->fs_info
->tree_root
)
1438 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 2);
1440 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
1443 if (!(rw
& REQ_WRITE
)) {
1444 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1445 return btrfs_submit_compressed_read(inode
, bio
,
1446 mirror_num
, bio_flags
);
1447 } else if (!skip_sum
)
1448 btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1450 } else if (!skip_sum
) {
1451 /* csum items have already been cloned */
1452 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1454 /* we're doing a write, do the async checksumming */
1455 return btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1456 inode
, rw
, bio
, mirror_num
,
1457 bio_flags
, bio_offset
,
1458 __btrfs_submit_bio_start
,
1459 __btrfs_submit_bio_done
);
1463 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1467 * given a list of ordered sums record them in the inode. This happens
1468 * at IO completion time based on sums calculated at bio submission time.
1470 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1471 struct inode
*inode
, u64 file_offset
,
1472 struct list_head
*list
)
1474 struct btrfs_ordered_sum
*sum
;
1476 btrfs_set_trans_block_group(trans
, inode
);
1478 list_for_each_entry(sum
, list
, list
) {
1479 btrfs_csum_file_blocks(trans
,
1480 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1485 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1486 struct extent_state
**cached_state
)
1488 if ((end
& (PAGE_CACHE_SIZE
- 1)) == 0)
1490 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1491 cached_state
, GFP_NOFS
);
1494 /* see btrfs_writepage_start_hook for details on why this is required */
1495 struct btrfs_writepage_fixup
{
1497 struct btrfs_work work
;
1500 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1502 struct btrfs_writepage_fixup
*fixup
;
1503 struct btrfs_ordered_extent
*ordered
;
1504 struct extent_state
*cached_state
= NULL
;
1506 struct inode
*inode
;
1510 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1514 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1515 ClearPageChecked(page
);
1519 inode
= page
->mapping
->host
;
1520 page_start
= page_offset(page
);
1521 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1523 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1524 &cached_state
, GFP_NOFS
);
1526 /* already ordered? We're done */
1527 if (PagePrivate2(page
))
1530 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1532 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1533 page_end
, &cached_state
, GFP_NOFS
);
1535 btrfs_start_ordered_extent(inode
, ordered
, 1);
1540 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1541 ClearPageChecked(page
);
1543 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1544 &cached_state
, GFP_NOFS
);
1547 page_cache_release(page
);
1551 * There are a few paths in the higher layers of the kernel that directly
1552 * set the page dirty bit without asking the filesystem if it is a
1553 * good idea. This causes problems because we want to make sure COW
1554 * properly happens and the data=ordered rules are followed.
1556 * In our case any range that doesn't have the ORDERED bit set
1557 * hasn't been properly setup for IO. We kick off an async process
1558 * to fix it up. The async helper will wait for ordered extents, set
1559 * the delalloc bit and make it safe to write the page.
1561 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1563 struct inode
*inode
= page
->mapping
->host
;
1564 struct btrfs_writepage_fixup
*fixup
;
1565 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1567 /* this page is properly in the ordered list */
1568 if (TestClearPagePrivate2(page
))
1571 if (PageChecked(page
))
1574 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1578 SetPageChecked(page
);
1579 page_cache_get(page
);
1580 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
1582 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
1586 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1587 struct inode
*inode
, u64 file_pos
,
1588 u64 disk_bytenr
, u64 disk_num_bytes
,
1589 u64 num_bytes
, u64 ram_bytes
,
1590 u8 compression
, u8 encryption
,
1591 u16 other_encoding
, int extent_type
)
1593 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1594 struct btrfs_file_extent_item
*fi
;
1595 struct btrfs_path
*path
;
1596 struct extent_buffer
*leaf
;
1597 struct btrfs_key ins
;
1601 path
= btrfs_alloc_path();
1604 path
->leave_spinning
= 1;
1607 * we may be replacing one extent in the tree with another.
1608 * The new extent is pinned in the extent map, and we don't want
1609 * to drop it from the cache until it is completely in the btree.
1611 * So, tell btrfs_drop_extents to leave this extent in the cache.
1612 * the caller is expected to unpin it and allow it to be merged
1615 ret
= btrfs_drop_extents(trans
, inode
, file_pos
, file_pos
+ num_bytes
,
1619 ins
.objectid
= inode
->i_ino
;
1620 ins
.offset
= file_pos
;
1621 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1622 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
, sizeof(*fi
));
1624 leaf
= path
->nodes
[0];
1625 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1626 struct btrfs_file_extent_item
);
1627 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1628 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1629 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1630 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1631 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1632 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1633 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1634 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1635 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1636 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1638 btrfs_unlock_up_safe(path
, 1);
1639 btrfs_set_lock_blocking(leaf
);
1641 btrfs_mark_buffer_dirty(leaf
);
1643 inode_add_bytes(inode
, num_bytes
);
1645 ins
.objectid
= disk_bytenr
;
1646 ins
.offset
= disk_num_bytes
;
1647 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1648 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
1649 root
->root_key
.objectid
,
1650 inode
->i_ino
, file_pos
, &ins
);
1652 btrfs_free_path(path
);
1658 * helper function for btrfs_finish_ordered_io, this
1659 * just reads in some of the csum leaves to prime them into ram
1660 * before we start the transaction. It limits the amount of btree
1661 * reads required while inside the transaction.
1663 /* as ordered data IO finishes, this gets called so we can finish
1664 * an ordered extent if the range of bytes in the file it covers are
1667 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
)
1669 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1670 struct btrfs_trans_handle
*trans
= NULL
;
1671 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
1672 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1673 struct extent_state
*cached_state
= NULL
;
1676 bool nolock
= false;
1678 ret
= btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
1682 BUG_ON(!ordered_extent
);
1684 nolock
= (root
== root
->fs_info
->tree_root
);
1686 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
1687 BUG_ON(!list_empty(&ordered_extent
->list
));
1688 ret
= btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
1691 trans
= btrfs_join_transaction_nolock(root
, 1);
1693 trans
= btrfs_join_transaction(root
, 1);
1695 btrfs_set_trans_block_group(trans
, inode
);
1696 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1697 ret
= btrfs_update_inode(trans
, root
, inode
);
1703 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
1704 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
1705 0, &cached_state
, GFP_NOFS
);
1708 trans
= btrfs_join_transaction_nolock(root
, 1);
1710 trans
= btrfs_join_transaction(root
, 1);
1711 btrfs_set_trans_block_group(trans
, inode
);
1712 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1714 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
1716 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
1718 ret
= btrfs_mark_extent_written(trans
, inode
,
1719 ordered_extent
->file_offset
,
1720 ordered_extent
->file_offset
+
1721 ordered_extent
->len
);
1724 BUG_ON(root
== root
->fs_info
->tree_root
);
1725 ret
= insert_reserved_file_extent(trans
, inode
,
1726 ordered_extent
->file_offset
,
1727 ordered_extent
->start
,
1728 ordered_extent
->disk_len
,
1729 ordered_extent
->len
,
1730 ordered_extent
->len
,
1732 BTRFS_FILE_EXTENT_REG
);
1733 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
1734 ordered_extent
->file_offset
,
1735 ordered_extent
->len
);
1738 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
1739 ordered_extent
->file_offset
+
1740 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
1742 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
1743 &ordered_extent
->list
);
1745 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
1746 ret
= btrfs_update_inode(trans
, root
, inode
);
1751 btrfs_end_transaction_nolock(trans
, root
);
1753 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
1755 btrfs_end_transaction(trans
, root
);
1759 btrfs_put_ordered_extent(ordered_extent
);
1760 /* once for the tree */
1761 btrfs_put_ordered_extent(ordered_extent
);
1766 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
1767 struct extent_state
*state
, int uptodate
)
1769 ClearPagePrivate2(page
);
1770 return btrfs_finish_ordered_io(page
->mapping
->host
, start
, end
);
1774 * When IO fails, either with EIO or csum verification fails, we
1775 * try other mirrors that might have a good copy of the data. This
1776 * io_failure_record is used to record state as we go through all the
1777 * mirrors. If another mirror has good data, the page is set up to date
1778 * and things continue. If a good mirror can't be found, the original
1779 * bio end_io callback is called to indicate things have failed.
1781 struct io_failure_record
{
1786 unsigned long bio_flags
;
1790 static int btrfs_io_failed_hook(struct bio
*failed_bio
,
1791 struct page
*page
, u64 start
, u64 end
,
1792 struct extent_state
*state
)
1794 struct io_failure_record
*failrec
= NULL
;
1796 struct extent_map
*em
;
1797 struct inode
*inode
= page
->mapping
->host
;
1798 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
1799 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1806 ret
= get_state_private(failure_tree
, start
, &private);
1808 failrec
= kmalloc(sizeof(*failrec
), GFP_NOFS
);
1811 failrec
->start
= start
;
1812 failrec
->len
= end
- start
+ 1;
1813 failrec
->last_mirror
= 0;
1814 failrec
->bio_flags
= 0;
1816 read_lock(&em_tree
->lock
);
1817 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
1818 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
1819 free_extent_map(em
);
1822 read_unlock(&em_tree
->lock
);
1824 if (!em
|| IS_ERR(em
)) {
1828 logical
= start
- em
->start
;
1829 logical
= em
->block_start
+ logical
;
1830 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
1831 logical
= em
->block_start
;
1832 failrec
->bio_flags
= EXTENT_BIO_COMPRESSED
;
1834 failrec
->logical
= logical
;
1835 free_extent_map(em
);
1836 set_extent_bits(failure_tree
, start
, end
, EXTENT_LOCKED
|
1837 EXTENT_DIRTY
, GFP_NOFS
);
1838 set_state_private(failure_tree
, start
,
1839 (u64
)(unsigned long)failrec
);
1841 failrec
= (struct io_failure_record
*)(unsigned long)private;
1843 num_copies
= btrfs_num_copies(
1844 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
1845 failrec
->logical
, failrec
->len
);
1846 failrec
->last_mirror
++;
1848 spin_lock(&BTRFS_I(inode
)->io_tree
.lock
);
1849 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
1852 if (state
&& state
->start
!= failrec
->start
)
1854 spin_unlock(&BTRFS_I(inode
)->io_tree
.lock
);
1856 if (!state
|| failrec
->last_mirror
> num_copies
) {
1857 set_state_private(failure_tree
, failrec
->start
, 0);
1858 clear_extent_bits(failure_tree
, failrec
->start
,
1859 failrec
->start
+ failrec
->len
- 1,
1860 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
1864 bio
= bio_alloc(GFP_NOFS
, 1);
1865 bio
->bi_private
= state
;
1866 bio
->bi_end_io
= failed_bio
->bi_end_io
;
1867 bio
->bi_sector
= failrec
->logical
>> 9;
1868 bio
->bi_bdev
= failed_bio
->bi_bdev
;
1871 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
1872 if (failed_bio
->bi_rw
& REQ_WRITE
)
1877 BTRFS_I(inode
)->io_tree
.ops
->submit_bio_hook(inode
, rw
, bio
,
1878 failrec
->last_mirror
,
1879 failrec
->bio_flags
, 0);
1884 * each time an IO finishes, we do a fast check in the IO failure tree
1885 * to see if we need to process or clean up an io_failure_record
1887 static int btrfs_clean_io_failures(struct inode
*inode
, u64 start
)
1890 u64 private_failure
;
1891 struct io_failure_record
*failure
;
1895 if (count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
1896 (u64
)-1, 1, EXTENT_DIRTY
)) {
1897 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
,
1898 start
, &private_failure
);
1900 failure
= (struct io_failure_record
*)(unsigned long)
1902 set_state_private(&BTRFS_I(inode
)->io_failure_tree
,
1904 clear_extent_bits(&BTRFS_I(inode
)->io_failure_tree
,
1906 failure
->start
+ failure
->len
- 1,
1907 EXTENT_DIRTY
| EXTENT_LOCKED
,
1916 * when reads are done, we need to check csums to verify the data is correct
1917 * if there's a match, we allow the bio to finish. If not, we go through
1918 * the io_failure_record routines to find good copies
1920 static int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
1921 struct extent_state
*state
)
1923 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
1924 struct inode
*inode
= page
->mapping
->host
;
1925 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1927 u64
private = ~(u32
)0;
1929 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1932 if (PageChecked(page
)) {
1933 ClearPageChecked(page
);
1937 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
1940 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
1941 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
1942 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
1947 if (state
&& state
->start
== start
) {
1948 private = state
->private;
1951 ret
= get_state_private(io_tree
, start
, &private);
1953 kaddr
= kmap_atomic(page
, KM_USER0
);
1957 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
1958 btrfs_csum_final(csum
, (char *)&csum
);
1959 if (csum
!= private)
1962 kunmap_atomic(kaddr
, KM_USER0
);
1964 /* if the io failure tree for this inode is non-empty,
1965 * check to see if we've recovered from a failed IO
1967 btrfs_clean_io_failures(inode
, start
);
1971 if (printk_ratelimit()) {
1972 printk(KERN_INFO
"btrfs csum failed ino %lu off %llu csum %u "
1973 "private %llu\n", page
->mapping
->host
->i_ino
,
1974 (unsigned long long)start
, csum
,
1975 (unsigned long long)private);
1977 memset(kaddr
+ offset
, 1, end
- start
+ 1);
1978 flush_dcache_page(page
);
1979 kunmap_atomic(kaddr
, KM_USER0
);
1985 struct delayed_iput
{
1986 struct list_head list
;
1987 struct inode
*inode
;
1990 void btrfs_add_delayed_iput(struct inode
*inode
)
1992 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
1993 struct delayed_iput
*delayed
;
1995 if (atomic_add_unless(&inode
->i_count
, -1, 1))
1998 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
1999 delayed
->inode
= inode
;
2001 spin_lock(&fs_info
->delayed_iput_lock
);
2002 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
2003 spin_unlock(&fs_info
->delayed_iput_lock
);
2006 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
2009 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2010 struct delayed_iput
*delayed
;
2013 spin_lock(&fs_info
->delayed_iput_lock
);
2014 empty
= list_empty(&fs_info
->delayed_iputs
);
2015 spin_unlock(&fs_info
->delayed_iput_lock
);
2019 down_read(&root
->fs_info
->cleanup_work_sem
);
2020 spin_lock(&fs_info
->delayed_iput_lock
);
2021 list_splice_init(&fs_info
->delayed_iputs
, &list
);
2022 spin_unlock(&fs_info
->delayed_iput_lock
);
2024 while (!list_empty(&list
)) {
2025 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
2026 list_del(&delayed
->list
);
2027 iput(delayed
->inode
);
2030 up_read(&root
->fs_info
->cleanup_work_sem
);
2034 * calculate extra metadata reservation when snapshotting a subvolume
2035 * contains orphan files.
2037 void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle
*trans
,
2038 struct btrfs_pending_snapshot
*pending
,
2039 u64
*bytes_to_reserve
)
2041 struct btrfs_root
*root
;
2042 struct btrfs_block_rsv
*block_rsv
;
2046 root
= pending
->root
;
2047 if (!root
->orphan_block_rsv
|| list_empty(&root
->orphan_list
))
2050 block_rsv
= root
->orphan_block_rsv
;
2052 /* orphan block reservation for the snapshot */
2053 num_bytes
= block_rsv
->size
;
2056 * after the snapshot is created, COWing tree blocks may use more
2057 * space than it frees. So we should make sure there is enough
2060 index
= trans
->transid
& 0x1;
2061 if (block_rsv
->reserved
+ block_rsv
->freed
[index
] < block_rsv
->size
) {
2062 num_bytes
+= block_rsv
->size
-
2063 (block_rsv
->reserved
+ block_rsv
->freed
[index
]);
2066 *bytes_to_reserve
+= num_bytes
;
2069 void btrfs_orphan_post_snapshot(struct btrfs_trans_handle
*trans
,
2070 struct btrfs_pending_snapshot
*pending
)
2072 struct btrfs_root
*root
= pending
->root
;
2073 struct btrfs_root
*snap
= pending
->snap
;
2074 struct btrfs_block_rsv
*block_rsv
;
2079 if (!root
->orphan_block_rsv
|| list_empty(&root
->orphan_list
))
2082 /* refill source subvolume's orphan block reservation */
2083 block_rsv
= root
->orphan_block_rsv
;
2084 index
= trans
->transid
& 0x1;
2085 if (block_rsv
->reserved
+ block_rsv
->freed
[index
] < block_rsv
->size
) {
2086 num_bytes
= block_rsv
->size
-
2087 (block_rsv
->reserved
+ block_rsv
->freed
[index
]);
2088 ret
= btrfs_block_rsv_migrate(&pending
->block_rsv
,
2089 root
->orphan_block_rsv
,
2094 /* setup orphan block reservation for the snapshot */
2095 block_rsv
= btrfs_alloc_block_rsv(snap
);
2098 btrfs_add_durable_block_rsv(root
->fs_info
, block_rsv
);
2099 snap
->orphan_block_rsv
= block_rsv
;
2101 num_bytes
= root
->orphan_block_rsv
->size
;
2102 ret
= btrfs_block_rsv_migrate(&pending
->block_rsv
,
2103 block_rsv
, num_bytes
);
2107 /* insert orphan item for the snapshot */
2108 WARN_ON(!root
->orphan_item_inserted
);
2109 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
2110 snap
->root_key
.objectid
);
2112 snap
->orphan_item_inserted
= 1;
2116 enum btrfs_orphan_cleanup_state
{
2117 ORPHAN_CLEANUP_STARTED
= 1,
2118 ORPHAN_CLEANUP_DONE
= 2,
2122 * This is called in transaction commmit time. If there are no orphan
2123 * files in the subvolume, it removes orphan item and frees block_rsv
2126 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
2127 struct btrfs_root
*root
)
2131 if (!list_empty(&root
->orphan_list
) ||
2132 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
2135 if (root
->orphan_item_inserted
&&
2136 btrfs_root_refs(&root
->root_item
) > 0) {
2137 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
2138 root
->root_key
.objectid
);
2140 root
->orphan_item_inserted
= 0;
2143 if (root
->orphan_block_rsv
) {
2144 WARN_ON(root
->orphan_block_rsv
->size
> 0);
2145 btrfs_free_block_rsv(root
, root
->orphan_block_rsv
);
2146 root
->orphan_block_rsv
= NULL
;
2151 * This creates an orphan entry for the given inode in case something goes
2152 * wrong in the middle of an unlink/truncate.
2154 * NOTE: caller of this function should reserve 5 units of metadata for
2157 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2159 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2160 struct btrfs_block_rsv
*block_rsv
= NULL
;
2165 if (!root
->orphan_block_rsv
) {
2166 block_rsv
= btrfs_alloc_block_rsv(root
);
2170 spin_lock(&root
->orphan_lock
);
2171 if (!root
->orphan_block_rsv
) {
2172 root
->orphan_block_rsv
= block_rsv
;
2173 } else if (block_rsv
) {
2174 btrfs_free_block_rsv(root
, block_rsv
);
2178 if (list_empty(&BTRFS_I(inode
)->i_orphan
)) {
2179 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
2182 * For proper ENOSPC handling, we should do orphan
2183 * cleanup when mounting. But this introduces backward
2184 * compatibility issue.
2186 if (!xchg(&root
->orphan_item_inserted
, 1))
2193 WARN_ON(!BTRFS_I(inode
)->orphan_meta_reserved
);
2196 if (!BTRFS_I(inode
)->orphan_meta_reserved
) {
2197 BTRFS_I(inode
)->orphan_meta_reserved
= 1;
2200 spin_unlock(&root
->orphan_lock
);
2203 btrfs_add_durable_block_rsv(root
->fs_info
, block_rsv
);
2205 /* grab metadata reservation from transaction handle */
2207 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
2211 /* insert an orphan item to track this unlinked/truncated file */
2213 ret
= btrfs_insert_orphan_item(trans
, root
, inode
->i_ino
);
2217 /* insert an orphan item to track subvolume contains orphan files */
2219 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
2220 root
->root_key
.objectid
);
2227 * We have done the truncate/delete so we can go ahead and remove the orphan
2228 * item for this particular inode.
2230 int btrfs_orphan_del(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2232 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2233 int delete_item
= 0;
2234 int release_rsv
= 0;
2237 spin_lock(&root
->orphan_lock
);
2238 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
2239 list_del_init(&BTRFS_I(inode
)->i_orphan
);
2243 if (BTRFS_I(inode
)->orphan_meta_reserved
) {
2244 BTRFS_I(inode
)->orphan_meta_reserved
= 0;
2247 spin_unlock(&root
->orphan_lock
);
2249 if (trans
&& delete_item
) {
2250 ret
= btrfs_del_orphan_item(trans
, root
, inode
->i_ino
);
2255 btrfs_orphan_release_metadata(inode
);
2261 * this cleans up any orphans that may be left on the list from the last use
2264 void btrfs_orphan_cleanup(struct btrfs_root
*root
)
2266 struct btrfs_path
*path
;
2267 struct extent_buffer
*leaf
;
2268 struct btrfs_key key
, found_key
;
2269 struct btrfs_trans_handle
*trans
;
2270 struct inode
*inode
;
2271 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
2273 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
2276 path
= btrfs_alloc_path();
2280 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
2281 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
2282 key
.offset
= (u64
)-1;
2285 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2287 printk(KERN_ERR
"Error searching slot for orphan: %d"
2293 * if ret == 0 means we found what we were searching for, which
2294 * is weird, but possible, so only screw with path if we didnt
2295 * find the key and see if we have stuff that matches
2298 if (path
->slots
[0] == 0)
2303 /* pull out the item */
2304 leaf
= path
->nodes
[0];
2305 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2307 /* make sure the item matches what we want */
2308 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
2310 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
2313 /* release the path since we're done with it */
2314 btrfs_release_path(root
, path
);
2317 * this is where we are basically btrfs_lookup, without the
2318 * crossing root thing. we store the inode number in the
2319 * offset of the orphan item.
2321 found_key
.objectid
= found_key
.offset
;
2322 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
2323 found_key
.offset
= 0;
2324 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
2325 BUG_ON(IS_ERR(inode
));
2328 * add this inode to the orphan list so btrfs_orphan_del does
2329 * the proper thing when we hit it
2331 spin_lock(&root
->orphan_lock
);
2332 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
2333 spin_unlock(&root
->orphan_lock
);
2336 * if this is a bad inode, means we actually succeeded in
2337 * removing the inode, but not the orphan record, which means
2338 * we need to manually delete the orphan since iput will just
2339 * do a destroy_inode
2341 if (is_bad_inode(inode
)) {
2342 trans
= btrfs_start_transaction(root
, 0);
2343 btrfs_orphan_del(trans
, inode
);
2344 btrfs_end_transaction(trans
, root
);
2349 /* if we have links, this was a truncate, lets do that */
2350 if (inode
->i_nlink
) {
2352 btrfs_truncate(inode
);
2357 /* this will do delete_inode and everything for us */
2360 btrfs_free_path(path
);
2362 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
2364 if (root
->orphan_block_rsv
)
2365 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
2368 if (root
->orphan_block_rsv
|| root
->orphan_item_inserted
) {
2369 trans
= btrfs_join_transaction(root
, 1);
2370 btrfs_end_transaction(trans
, root
);
2374 printk(KERN_INFO
"btrfs: unlinked %d orphans\n", nr_unlink
);
2376 printk(KERN_INFO
"btrfs: truncated %d orphans\n", nr_truncate
);
2380 * very simple check to peek ahead in the leaf looking for xattrs. If we
2381 * don't find any xattrs, we know there can't be any acls.
2383 * slot is the slot the inode is in, objectid is the objectid of the inode
2385 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
2386 int slot
, u64 objectid
)
2388 u32 nritems
= btrfs_header_nritems(leaf
);
2389 struct btrfs_key found_key
;
2393 while (slot
< nritems
) {
2394 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2396 /* we found a different objectid, there must not be acls */
2397 if (found_key
.objectid
!= objectid
)
2400 /* we found an xattr, assume we've got an acl */
2401 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
)
2405 * we found a key greater than an xattr key, there can't
2406 * be any acls later on
2408 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
2415 * it goes inode, inode backrefs, xattrs, extents,
2416 * so if there are a ton of hard links to an inode there can
2417 * be a lot of backrefs. Don't waste time searching too hard,
2418 * this is just an optimization
2423 /* we hit the end of the leaf before we found an xattr or
2424 * something larger than an xattr. We have to assume the inode
2431 * read an inode from the btree into the in-memory inode
2433 static void btrfs_read_locked_inode(struct inode
*inode
)
2435 struct btrfs_path
*path
;
2436 struct extent_buffer
*leaf
;
2437 struct btrfs_inode_item
*inode_item
;
2438 struct btrfs_timespec
*tspec
;
2439 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2440 struct btrfs_key location
;
2442 u64 alloc_group_block
;
2446 path
= btrfs_alloc_path();
2448 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
2450 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
2454 leaf
= path
->nodes
[0];
2455 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2456 struct btrfs_inode_item
);
2458 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
2459 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
2460 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
2461 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
2462 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
2464 tspec
= btrfs_inode_atime(inode_item
);
2465 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2466 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2468 tspec
= btrfs_inode_mtime(inode_item
);
2469 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2470 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2472 tspec
= btrfs_inode_ctime(inode_item
);
2473 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2474 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2476 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
2477 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
2478 BTRFS_I(inode
)->sequence
= btrfs_inode_sequence(leaf
, inode_item
);
2479 inode
->i_generation
= BTRFS_I(inode
)->generation
;
2481 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
2483 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
2484 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
2486 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
2489 * try to precache a NULL acl entry for files that don't have
2490 * any xattrs or acls
2492 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0], inode
->i_ino
);
2494 cache_no_acl(inode
);
2496 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
, 0,
2497 alloc_group_block
, 0);
2498 btrfs_free_path(path
);
2501 switch (inode
->i_mode
& S_IFMT
) {
2503 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2504 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2505 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2506 inode
->i_fop
= &btrfs_file_operations
;
2507 inode
->i_op
= &btrfs_file_inode_operations
;
2510 inode
->i_fop
= &btrfs_dir_file_operations
;
2511 if (root
== root
->fs_info
->tree_root
)
2512 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
2514 inode
->i_op
= &btrfs_dir_inode_operations
;
2517 inode
->i_op
= &btrfs_symlink_inode_operations
;
2518 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2519 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2522 inode
->i_op
= &btrfs_special_inode_operations
;
2523 init_special_inode(inode
, inode
->i_mode
, rdev
);
2527 btrfs_update_iflags(inode
);
2531 btrfs_free_path(path
);
2532 make_bad_inode(inode
);
2536 * given a leaf and an inode, copy the inode fields into the leaf
2538 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
2539 struct extent_buffer
*leaf
,
2540 struct btrfs_inode_item
*item
,
2541 struct inode
*inode
)
2543 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
2544 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
2545 btrfs_set_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
);
2546 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
2547 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
2549 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
2550 inode
->i_atime
.tv_sec
);
2551 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
2552 inode
->i_atime
.tv_nsec
);
2554 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
2555 inode
->i_mtime
.tv_sec
);
2556 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
2557 inode
->i_mtime
.tv_nsec
);
2559 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
2560 inode
->i_ctime
.tv_sec
);
2561 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
2562 inode
->i_ctime
.tv_nsec
);
2564 btrfs_set_inode_nbytes(leaf
, item
, inode_get_bytes(inode
));
2565 btrfs_set_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
);
2566 btrfs_set_inode_sequence(leaf
, item
, BTRFS_I(inode
)->sequence
);
2567 btrfs_set_inode_transid(leaf
, item
, trans
->transid
);
2568 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
2569 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
2570 btrfs_set_inode_block_group(leaf
, item
, BTRFS_I(inode
)->block_group
);
2574 * copy everything in the in-memory inode into the btree.
2576 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
2577 struct btrfs_root
*root
, struct inode
*inode
)
2579 struct btrfs_inode_item
*inode_item
;
2580 struct btrfs_path
*path
;
2581 struct extent_buffer
*leaf
;
2584 path
= btrfs_alloc_path();
2586 path
->leave_spinning
= 1;
2587 ret
= btrfs_lookup_inode(trans
, root
, path
,
2588 &BTRFS_I(inode
)->location
, 1);
2595 btrfs_unlock_up_safe(path
, 1);
2596 leaf
= path
->nodes
[0];
2597 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2598 struct btrfs_inode_item
);
2600 fill_inode_item(trans
, leaf
, inode_item
, inode
);
2601 btrfs_mark_buffer_dirty(leaf
);
2602 btrfs_set_inode_last_trans(trans
, inode
);
2605 btrfs_free_path(path
);
2611 * unlink helper that gets used here in inode.c and in the tree logging
2612 * recovery code. It remove a link in a directory with a given name, and
2613 * also drops the back refs in the inode to the directory
2615 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
2616 struct btrfs_root
*root
,
2617 struct inode
*dir
, struct inode
*inode
,
2618 const char *name
, int name_len
)
2620 struct btrfs_path
*path
;
2622 struct extent_buffer
*leaf
;
2623 struct btrfs_dir_item
*di
;
2624 struct btrfs_key key
;
2627 path
= btrfs_alloc_path();
2633 path
->leave_spinning
= 1;
2634 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
2635 name
, name_len
, -1);
2644 leaf
= path
->nodes
[0];
2645 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2646 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2649 btrfs_release_path(root
, path
);
2651 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
2653 dir
->i_ino
, &index
);
2655 printk(KERN_INFO
"btrfs failed to delete reference to %.*s, "
2656 "inode %lu parent %lu\n", name_len
, name
,
2657 inode
->i_ino
, dir
->i_ino
);
2661 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
2662 index
, name
, name_len
, -1);
2671 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2672 btrfs_release_path(root
, path
);
2674 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
2676 BUG_ON(ret
!= 0 && ret
!= -ENOENT
);
2678 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
2683 btrfs_free_path(path
);
2687 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
2688 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
2689 btrfs_update_inode(trans
, root
, dir
);
2690 btrfs_drop_nlink(inode
);
2691 ret
= btrfs_update_inode(trans
, root
, inode
);
2696 /* helper to check if there is any shared block in the path */
2697 static int check_path_shared(struct btrfs_root
*root
,
2698 struct btrfs_path
*path
)
2700 struct extent_buffer
*eb
;
2703 int uninitialized_var(ret
);
2705 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
2706 if (!path
->nodes
[level
])
2708 eb
= path
->nodes
[level
];
2709 if (!btrfs_block_can_be_shared(root
, eb
))
2711 ret
= btrfs_lookup_extent_info(NULL
, root
, eb
->start
, eb
->len
,
2716 return ret
; /* XXX callers? */
2720 * helper to start transaction for unlink and rmdir.
2722 * unlink and rmdir are special in btrfs, they do not always free space.
2723 * so in enospc case, we should make sure they will free space before
2724 * allowing them to use the global metadata reservation.
2726 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
,
2727 struct dentry
*dentry
)
2729 struct btrfs_trans_handle
*trans
;
2730 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2731 struct btrfs_path
*path
;
2732 struct btrfs_inode_ref
*ref
;
2733 struct btrfs_dir_item
*di
;
2734 struct inode
*inode
= dentry
->d_inode
;
2740 trans
= btrfs_start_transaction(root
, 10);
2741 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
2744 if (inode
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
2745 return ERR_PTR(-ENOSPC
);
2747 /* check if there is someone else holds reference */
2748 if (S_ISDIR(inode
->i_mode
) && atomic_read(&inode
->i_count
) > 1)
2749 return ERR_PTR(-ENOSPC
);
2751 if (atomic_read(&inode
->i_count
) > 2)
2752 return ERR_PTR(-ENOSPC
);
2754 if (xchg(&root
->fs_info
->enospc_unlink
, 1))
2755 return ERR_PTR(-ENOSPC
);
2757 path
= btrfs_alloc_path();
2759 root
->fs_info
->enospc_unlink
= 0;
2760 return ERR_PTR(-ENOMEM
);
2763 trans
= btrfs_start_transaction(root
, 0);
2764 if (IS_ERR(trans
)) {
2765 btrfs_free_path(path
);
2766 root
->fs_info
->enospc_unlink
= 0;
2770 path
->skip_locking
= 1;
2771 path
->search_commit_root
= 1;
2773 ret
= btrfs_lookup_inode(trans
, root
, path
,
2774 &BTRFS_I(dir
)->location
, 0);
2780 if (check_path_shared(root
, path
))
2785 btrfs_release_path(root
, path
);
2787 ret
= btrfs_lookup_inode(trans
, root
, path
,
2788 &BTRFS_I(inode
)->location
, 0);
2794 if (check_path_shared(root
, path
))
2799 btrfs_release_path(root
, path
);
2801 if (ret
== 0 && S_ISREG(inode
->i_mode
)) {
2802 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
2803 inode
->i_ino
, (u64
)-1, 0);
2809 if (check_path_shared(root
, path
))
2811 btrfs_release_path(root
, path
);
2819 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
2820 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
2826 if (check_path_shared(root
, path
))
2832 btrfs_release_path(root
, path
);
2834 ref
= btrfs_lookup_inode_ref(trans
, root
, path
,
2835 dentry
->d_name
.name
, dentry
->d_name
.len
,
2836 inode
->i_ino
, dir
->i_ino
, 0);
2842 if (check_path_shared(root
, path
))
2844 index
= btrfs_inode_ref_index(path
->nodes
[0], ref
);
2845 btrfs_release_path(root
, path
);
2847 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
, index
,
2848 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
2853 BUG_ON(ret
== -ENOENT
);
2854 if (check_path_shared(root
, path
))
2859 btrfs_free_path(path
);
2861 btrfs_end_transaction(trans
, root
);
2862 root
->fs_info
->enospc_unlink
= 0;
2863 return ERR_PTR(err
);
2866 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
2870 static void __unlink_end_trans(struct btrfs_trans_handle
*trans
,
2871 struct btrfs_root
*root
)
2873 if (trans
->block_rsv
== &root
->fs_info
->global_block_rsv
) {
2874 BUG_ON(!root
->fs_info
->enospc_unlink
);
2875 root
->fs_info
->enospc_unlink
= 0;
2877 btrfs_end_transaction_throttle(trans
, root
);
2880 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
2882 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2883 struct btrfs_trans_handle
*trans
;
2884 struct inode
*inode
= dentry
->d_inode
;
2886 unsigned long nr
= 0;
2888 trans
= __unlink_start_trans(dir
, dentry
);
2890 return PTR_ERR(trans
);
2892 btrfs_set_trans_block_group(trans
, dir
);
2894 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
2896 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
2897 dentry
->d_name
.name
, dentry
->d_name
.len
);
2900 if (inode
->i_nlink
== 0) {
2901 ret
= btrfs_orphan_add(trans
, inode
);
2905 nr
= trans
->blocks_used
;
2906 __unlink_end_trans(trans
, root
);
2907 btrfs_btree_balance_dirty(root
, nr
);
2911 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
2912 struct btrfs_root
*root
,
2913 struct inode
*dir
, u64 objectid
,
2914 const char *name
, int name_len
)
2916 struct btrfs_path
*path
;
2917 struct extent_buffer
*leaf
;
2918 struct btrfs_dir_item
*di
;
2919 struct btrfs_key key
;
2923 path
= btrfs_alloc_path();
2927 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
2928 name
, name_len
, -1);
2929 BUG_ON(!di
|| IS_ERR(di
));
2931 leaf
= path
->nodes
[0];
2932 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2933 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
2934 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2936 btrfs_release_path(root
, path
);
2938 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
2939 objectid
, root
->root_key
.objectid
,
2940 dir
->i_ino
, &index
, name
, name_len
);
2942 BUG_ON(ret
!= -ENOENT
);
2943 di
= btrfs_search_dir_index_item(root
, path
, dir
->i_ino
,
2945 BUG_ON(!di
|| IS_ERR(di
));
2947 leaf
= path
->nodes
[0];
2948 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2949 btrfs_release_path(root
, path
);
2953 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
2954 index
, name
, name_len
, -1);
2955 BUG_ON(!di
|| IS_ERR(di
));
2957 leaf
= path
->nodes
[0];
2958 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2959 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
2960 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2962 btrfs_release_path(root
, path
);
2964 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
2965 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
2966 ret
= btrfs_update_inode(trans
, root
, dir
);
2969 btrfs_free_path(path
);
2973 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2975 struct inode
*inode
= dentry
->d_inode
;
2977 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2978 struct btrfs_trans_handle
*trans
;
2979 unsigned long nr
= 0;
2981 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
||
2982 inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)
2985 trans
= __unlink_start_trans(dir
, dentry
);
2987 return PTR_ERR(trans
);
2989 btrfs_set_trans_block_group(trans
, dir
);
2991 if (unlikely(inode
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
2992 err
= btrfs_unlink_subvol(trans
, root
, dir
,
2993 BTRFS_I(inode
)->location
.objectid
,
2994 dentry
->d_name
.name
,
2995 dentry
->d_name
.len
);
2999 err
= btrfs_orphan_add(trans
, inode
);
3003 /* now the directory is empty */
3004 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3005 dentry
->d_name
.name
, dentry
->d_name
.len
);
3007 btrfs_i_size_write(inode
, 0);
3009 nr
= trans
->blocks_used
;
3010 __unlink_end_trans(trans
, root
);
3011 btrfs_btree_balance_dirty(root
, nr
);
3018 * when truncating bytes in a file, it is possible to avoid reading
3019 * the leaves that contain only checksum items. This can be the
3020 * majority of the IO required to delete a large file, but it must
3021 * be done carefully.
3023 * The keys in the level just above the leaves are checked to make sure
3024 * the lowest key in a given leaf is a csum key, and starts at an offset
3025 * after the new size.
3027 * Then the key for the next leaf is checked to make sure it also has
3028 * a checksum item for the same file. If it does, we know our target leaf
3029 * contains only checksum items, and it can be safely freed without reading
3032 * This is just an optimization targeted at large files. It may do
3033 * nothing. It will return 0 unless things went badly.
3035 static noinline
int drop_csum_leaves(struct btrfs_trans_handle
*trans
,
3036 struct btrfs_root
*root
,
3037 struct btrfs_path
*path
,
3038 struct inode
*inode
, u64 new_size
)
3040 struct btrfs_key key
;
3043 struct btrfs_key found_key
;
3044 struct btrfs_key other_key
;
3045 struct btrfs_leaf_ref
*ref
;
3049 path
->lowest_level
= 1;
3050 key
.objectid
= inode
->i_ino
;
3051 key
.type
= BTRFS_CSUM_ITEM_KEY
;
3052 key
.offset
= new_size
;
3054 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3058 if (path
->nodes
[1] == NULL
) {
3063 btrfs_node_key_to_cpu(path
->nodes
[1], &found_key
, path
->slots
[1]);
3064 nritems
= btrfs_header_nritems(path
->nodes
[1]);
3069 if (path
->slots
[1] >= nritems
)
3072 /* did we find a key greater than anything we want to delete? */
3073 if (found_key
.objectid
> inode
->i_ino
||
3074 (found_key
.objectid
== inode
->i_ino
&& found_key
.type
> key
.type
))
3077 /* we check the next key in the node to make sure the leave contains
3078 * only checksum items. This comparison doesn't work if our
3079 * leaf is the last one in the node
3081 if (path
->slots
[1] + 1 >= nritems
) {
3083 /* search forward from the last key in the node, this
3084 * will bring us into the next node in the tree
3086 btrfs_node_key_to_cpu(path
->nodes
[1], &found_key
, nritems
- 1);
3088 /* unlikely, but we inc below, so check to be safe */
3089 if (found_key
.offset
== (u64
)-1)
3092 /* search_forward needs a path with locks held, do the
3093 * search again for the original key. It is possible
3094 * this will race with a balance and return a path that
3095 * we could modify, but this drop is just an optimization
3096 * and is allowed to miss some leaves.
3098 btrfs_release_path(root
, path
);
3101 /* setup a max key for search_forward */
3102 other_key
.offset
= (u64
)-1;
3103 other_key
.type
= key
.type
;
3104 other_key
.objectid
= key
.objectid
;
3106 path
->keep_locks
= 1;
3107 ret
= btrfs_search_forward(root
, &found_key
, &other_key
,
3109 path
->keep_locks
= 0;
3110 if (ret
|| found_key
.objectid
!= key
.objectid
||
3111 found_key
.type
!= key
.type
) {
3116 key
.offset
= found_key
.offset
;
3117 btrfs_release_path(root
, path
);
3122 /* we know there's one more slot after us in the tree,
3123 * read that key so we can verify it is also a checksum item
3125 btrfs_node_key_to_cpu(path
->nodes
[1], &other_key
, path
->slots
[1] + 1);
3127 if (found_key
.objectid
< inode
->i_ino
)
3130 if (found_key
.type
!= key
.type
|| found_key
.offset
< new_size
)
3134 * if the key for the next leaf isn't a csum key from this objectid,
3135 * we can't be sure there aren't good items inside this leaf.
3138 if (other_key
.objectid
!= inode
->i_ino
|| other_key
.type
!= key
.type
)
3141 leaf_start
= btrfs_node_blockptr(path
->nodes
[1], path
->slots
[1]);
3142 leaf_gen
= btrfs_node_ptr_generation(path
->nodes
[1], path
->slots
[1]);
3144 * it is safe to delete this leaf, it contains only
3145 * csum items from this inode at an offset >= new_size
3147 ret
= btrfs_del_leaf(trans
, root
, path
, leaf_start
);
3150 if (root
->ref_cows
&& leaf_gen
< trans
->transid
) {
3151 ref
= btrfs_alloc_leaf_ref(root
, 0);
3153 ref
->root_gen
= root
->root_key
.offset
;
3154 ref
->bytenr
= leaf_start
;
3156 ref
->generation
= leaf_gen
;
3159 btrfs_sort_leaf_ref(ref
);
3161 ret
= btrfs_add_leaf_ref(root
, ref
, 0);
3163 btrfs_free_leaf_ref(root
, ref
);
3169 btrfs_release_path(root
, path
);
3171 if (other_key
.objectid
== inode
->i_ino
&&
3172 other_key
.type
== key
.type
&& other_key
.offset
> key
.offset
) {
3173 key
.offset
= other_key
.offset
;
3179 /* fixup any changes we've made to the path */
3180 path
->lowest_level
= 0;
3181 path
->keep_locks
= 0;
3182 btrfs_release_path(root
, path
);
3189 * this can truncate away extent items, csum items and directory items.
3190 * It starts at a high offset and removes keys until it can't find
3191 * any higher than new_size
3193 * csum items that cross the new i_size are truncated to the new size
3196 * min_type is the minimum key type to truncate down to. If set to 0, this
3197 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3199 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
3200 struct btrfs_root
*root
,
3201 struct inode
*inode
,
3202 u64 new_size
, u32 min_type
)
3204 struct btrfs_path
*path
;
3205 struct extent_buffer
*leaf
;
3206 struct btrfs_file_extent_item
*fi
;
3207 struct btrfs_key key
;
3208 struct btrfs_key found_key
;
3209 u64 extent_start
= 0;
3210 u64 extent_num_bytes
= 0;
3211 u64 extent_offset
= 0;
3213 u64 mask
= root
->sectorsize
- 1;
3214 u32 found_type
= (u8
)-1;
3217 int pending_del_nr
= 0;
3218 int pending_del_slot
= 0;
3219 int extent_type
= -1;
3224 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
3226 if (root
->ref_cows
|| root
== root
->fs_info
->tree_root
)
3227 btrfs_drop_extent_cache(inode
, new_size
& (~mask
), (u64
)-1, 0);
3229 path
= btrfs_alloc_path();
3233 key
.objectid
= inode
->i_ino
;
3234 key
.offset
= (u64
)-1;
3238 path
->leave_spinning
= 1;
3239 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3246 /* there are no items in the tree for us to truncate, we're
3249 if (path
->slots
[0] == 0)
3256 leaf
= path
->nodes
[0];
3257 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3258 found_type
= btrfs_key_type(&found_key
);
3261 if (found_key
.objectid
!= inode
->i_ino
)
3264 if (found_type
< min_type
)
3267 item_end
= found_key
.offset
;
3268 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
3269 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3270 struct btrfs_file_extent_item
);
3271 extent_type
= btrfs_file_extent_type(leaf
, fi
);
3272 encoding
= btrfs_file_extent_compression(leaf
, fi
);
3273 encoding
|= btrfs_file_extent_encryption(leaf
, fi
);
3274 encoding
|= btrfs_file_extent_other_encoding(leaf
, fi
);
3276 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3278 btrfs_file_extent_num_bytes(leaf
, fi
);
3279 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3280 item_end
+= btrfs_file_extent_inline_len(leaf
,
3285 if (found_type
> min_type
) {
3288 if (item_end
< new_size
)
3290 if (found_key
.offset
>= new_size
)
3296 /* FIXME, shrink the extent if the ref count is only 1 */
3297 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
3300 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3302 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
3303 if (!del_item
&& !encoding
) {
3304 u64 orig_num_bytes
=
3305 btrfs_file_extent_num_bytes(leaf
, fi
);
3306 extent_num_bytes
= new_size
-
3307 found_key
.offset
+ root
->sectorsize
- 1;
3308 extent_num_bytes
= extent_num_bytes
&
3309 ~((u64
)root
->sectorsize
- 1);
3310 btrfs_set_file_extent_num_bytes(leaf
, fi
,
3312 num_dec
= (orig_num_bytes
-
3314 if (root
->ref_cows
&& extent_start
!= 0)
3315 inode_sub_bytes(inode
, num_dec
);
3316 btrfs_mark_buffer_dirty(leaf
);
3319 btrfs_file_extent_disk_num_bytes(leaf
,
3321 extent_offset
= found_key
.offset
-
3322 btrfs_file_extent_offset(leaf
, fi
);
3324 /* FIXME blocksize != 4096 */
3325 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
3326 if (extent_start
!= 0) {
3329 inode_sub_bytes(inode
, num_dec
);
3332 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3334 * we can't truncate inline items that have had
3338 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
3339 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
3340 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
3341 u32 size
= new_size
- found_key
.offset
;
3343 if (root
->ref_cows
) {
3344 inode_sub_bytes(inode
, item_end
+ 1 -
3348 btrfs_file_extent_calc_inline_size(size
);
3349 ret
= btrfs_truncate_item(trans
, root
, path
,
3352 } else if (root
->ref_cows
) {
3353 inode_sub_bytes(inode
, item_end
+ 1 -
3359 if (!pending_del_nr
) {
3360 /* no pending yet, add ourselves */
3361 pending_del_slot
= path
->slots
[0];
3363 } else if (pending_del_nr
&&
3364 path
->slots
[0] + 1 == pending_del_slot
) {
3365 /* hop on the pending chunk */
3367 pending_del_slot
= path
->slots
[0];
3374 if (found_extent
&& (root
->ref_cows
||
3375 root
== root
->fs_info
->tree_root
)) {
3376 btrfs_set_path_blocking(path
);
3377 ret
= btrfs_free_extent(trans
, root
, extent_start
,
3378 extent_num_bytes
, 0,
3379 btrfs_header_owner(leaf
),
3380 inode
->i_ino
, extent_offset
);
3384 if (found_type
== BTRFS_INODE_ITEM_KEY
)
3387 if (path
->slots
[0] == 0 ||
3388 path
->slots
[0] != pending_del_slot
) {
3389 if (root
->ref_cows
) {
3393 if (pending_del_nr
) {
3394 ret
= btrfs_del_items(trans
, root
, path
,
3400 btrfs_release_path(root
, path
);
3407 if (pending_del_nr
) {
3408 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
3412 btrfs_free_path(path
);
3417 * taken from block_truncate_page, but does cow as it zeros out
3418 * any bytes left in the last page in the file.
3420 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
3422 struct inode
*inode
= mapping
->host
;
3423 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3424 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3425 struct btrfs_ordered_extent
*ordered
;
3426 struct extent_state
*cached_state
= NULL
;
3428 u32 blocksize
= root
->sectorsize
;
3429 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
3430 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
3436 if ((offset
& (blocksize
- 1)) == 0)
3438 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
3444 page
= grab_cache_page(mapping
, index
);
3446 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
3450 page_start
= page_offset(page
);
3451 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3453 if (!PageUptodate(page
)) {
3454 ret
= btrfs_readpage(NULL
, page
);
3456 if (page
->mapping
!= mapping
) {
3458 page_cache_release(page
);
3461 if (!PageUptodate(page
)) {
3466 wait_on_page_writeback(page
);
3468 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
,
3470 set_page_extent_mapped(page
);
3472 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
3474 unlock_extent_cached(io_tree
, page_start
, page_end
,
3475 &cached_state
, GFP_NOFS
);
3477 page_cache_release(page
);
3478 btrfs_start_ordered_extent(inode
, ordered
, 1);
3479 btrfs_put_ordered_extent(ordered
);
3483 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
3484 EXTENT_DIRTY
| EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
,
3485 0, 0, &cached_state
, GFP_NOFS
);
3487 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
3490 unlock_extent_cached(io_tree
, page_start
, page_end
,
3491 &cached_state
, GFP_NOFS
);
3496 if (offset
!= PAGE_CACHE_SIZE
) {
3498 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
3499 flush_dcache_page(page
);
3502 ClearPageChecked(page
);
3503 set_page_dirty(page
);
3504 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
3509 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
3511 page_cache_release(page
);
3516 int btrfs_cont_expand(struct inode
*inode
, loff_t size
)
3518 struct btrfs_trans_handle
*trans
;
3519 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3520 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3521 struct extent_map
*em
= NULL
;
3522 struct extent_state
*cached_state
= NULL
;
3523 u64 mask
= root
->sectorsize
- 1;
3524 u64 hole_start
= (inode
->i_size
+ mask
) & ~mask
;
3525 u64 block_end
= (size
+ mask
) & ~mask
;
3531 if (size
<= hole_start
)
3535 struct btrfs_ordered_extent
*ordered
;
3536 btrfs_wait_ordered_range(inode
, hole_start
,
3537 block_end
- hole_start
);
3538 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
3539 &cached_state
, GFP_NOFS
);
3540 ordered
= btrfs_lookup_ordered_extent(inode
, hole_start
);
3543 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
3544 &cached_state
, GFP_NOFS
);
3545 btrfs_put_ordered_extent(ordered
);
3548 cur_offset
= hole_start
;
3550 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
3551 block_end
- cur_offset
, 0);
3552 BUG_ON(IS_ERR(em
) || !em
);
3553 last_byte
= min(extent_map_end(em
), block_end
);
3554 last_byte
= (last_byte
+ mask
) & ~mask
;
3555 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
3557 hole_size
= last_byte
- cur_offset
;
3559 trans
= btrfs_start_transaction(root
, 2);
3560 if (IS_ERR(trans
)) {
3561 err
= PTR_ERR(trans
);
3564 btrfs_set_trans_block_group(trans
, inode
);
3566 err
= btrfs_drop_extents(trans
, inode
, cur_offset
,
3567 cur_offset
+ hole_size
,
3571 err
= btrfs_insert_file_extent(trans
, root
,
3572 inode
->i_ino
, cur_offset
, 0,
3573 0, hole_size
, 0, hole_size
,
3577 btrfs_drop_extent_cache(inode
, hole_start
,
3580 btrfs_end_transaction(trans
, root
);
3582 free_extent_map(em
);
3584 cur_offset
= last_byte
;
3585 if (cur_offset
>= block_end
)
3589 free_extent_map(em
);
3590 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
3595 static int btrfs_setattr_size(struct inode
*inode
, struct iattr
*attr
)
3597 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3598 struct btrfs_trans_handle
*trans
;
3602 if (attr
->ia_size
== inode
->i_size
)
3605 if (attr
->ia_size
> inode
->i_size
) {
3606 unsigned long limit
;
3607 limit
= current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
3608 if (attr
->ia_size
> inode
->i_sb
->s_maxbytes
)
3610 if (limit
!= RLIM_INFINITY
&& attr
->ia_size
> limit
) {
3611 send_sig(SIGXFSZ
, current
, 0);
3616 trans
= btrfs_start_transaction(root
, 5);
3618 return PTR_ERR(trans
);
3620 btrfs_set_trans_block_group(trans
, inode
);
3622 ret
= btrfs_orphan_add(trans
, inode
);
3625 nr
= trans
->blocks_used
;
3626 btrfs_end_transaction(trans
, root
);
3627 btrfs_btree_balance_dirty(root
, nr
);
3629 if (attr
->ia_size
> inode
->i_size
) {
3630 ret
= btrfs_cont_expand(inode
, attr
->ia_size
);
3632 btrfs_truncate(inode
);
3636 i_size_write(inode
, attr
->ia_size
);
3637 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
3639 trans
= btrfs_start_transaction(root
, 0);
3640 BUG_ON(IS_ERR(trans
));
3641 btrfs_set_trans_block_group(trans
, inode
);
3642 trans
->block_rsv
= root
->orphan_block_rsv
;
3643 BUG_ON(!trans
->block_rsv
);
3645 ret
= btrfs_update_inode(trans
, root
, inode
);
3647 if (inode
->i_nlink
> 0) {
3648 ret
= btrfs_orphan_del(trans
, inode
);
3651 nr
= trans
->blocks_used
;
3652 btrfs_end_transaction(trans
, root
);
3653 btrfs_btree_balance_dirty(root
, nr
);
3658 * We're truncating a file that used to have good data down to
3659 * zero. Make sure it gets into the ordered flush list so that
3660 * any new writes get down to disk quickly.
3662 if (attr
->ia_size
== 0)
3663 BTRFS_I(inode
)->ordered_data_close
= 1;
3665 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3666 ret
= vmtruncate(inode
, attr
->ia_size
);
3672 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
3674 struct inode
*inode
= dentry
->d_inode
;
3677 err
= inode_change_ok(inode
, attr
);
3681 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
3682 err
= btrfs_setattr_size(inode
, attr
);
3687 if (attr
->ia_valid
) {
3688 setattr_copy(inode
, attr
);
3689 mark_inode_dirty(inode
);
3691 if (attr
->ia_valid
& ATTR_MODE
)
3692 err
= btrfs_acl_chmod(inode
);
3698 void btrfs_evict_inode(struct inode
*inode
)
3700 struct btrfs_trans_handle
*trans
;
3701 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3705 truncate_inode_pages(&inode
->i_data
, 0);
3706 if (inode
->i_nlink
&& (btrfs_root_refs(&root
->root_item
) != 0 ||
3707 root
== root
->fs_info
->tree_root
))
3710 if (is_bad_inode(inode
)) {
3711 btrfs_orphan_del(NULL
, inode
);
3714 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
3715 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
3717 if (root
->fs_info
->log_root_recovering
) {
3718 BUG_ON(!list_empty(&BTRFS_I(inode
)->i_orphan
));
3722 if (inode
->i_nlink
> 0) {
3723 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0);
3727 btrfs_i_size_write(inode
, 0);
3730 trans
= btrfs_start_transaction(root
, 0);
3731 BUG_ON(IS_ERR(trans
));
3732 btrfs_set_trans_block_group(trans
, inode
);
3733 trans
->block_rsv
= root
->orphan_block_rsv
;
3735 ret
= btrfs_block_rsv_check(trans
, root
,
3736 root
->orphan_block_rsv
, 0, 5);
3738 BUG_ON(ret
!= -EAGAIN
);
3739 ret
= btrfs_commit_transaction(trans
, root
);
3744 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
3748 nr
= trans
->blocks_used
;
3749 btrfs_end_transaction(trans
, root
);
3751 btrfs_btree_balance_dirty(root
, nr
);
3756 ret
= btrfs_orphan_del(trans
, inode
);
3760 nr
= trans
->blocks_used
;
3761 btrfs_end_transaction(trans
, root
);
3762 btrfs_btree_balance_dirty(root
, nr
);
3764 end_writeback(inode
);
3769 * this returns the key found in the dir entry in the location pointer.
3770 * If no dir entries were found, location->objectid is 0.
3772 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
3773 struct btrfs_key
*location
)
3775 const char *name
= dentry
->d_name
.name
;
3776 int namelen
= dentry
->d_name
.len
;
3777 struct btrfs_dir_item
*di
;
3778 struct btrfs_path
*path
;
3779 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3782 path
= btrfs_alloc_path();
3785 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
3790 if (!di
|| IS_ERR(di
))
3793 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
3795 btrfs_free_path(path
);
3798 location
->objectid
= 0;
3803 * when we hit a tree root in a directory, the btrfs part of the inode
3804 * needs to be changed to reflect the root directory of the tree root. This
3805 * is kind of like crossing a mount point.
3807 static int fixup_tree_root_location(struct btrfs_root
*root
,
3809 struct dentry
*dentry
,
3810 struct btrfs_key
*location
,
3811 struct btrfs_root
**sub_root
)
3813 struct btrfs_path
*path
;
3814 struct btrfs_root
*new_root
;
3815 struct btrfs_root_ref
*ref
;
3816 struct extent_buffer
*leaf
;
3820 path
= btrfs_alloc_path();
3827 ret
= btrfs_find_root_ref(root
->fs_info
->tree_root
, path
,
3828 BTRFS_I(dir
)->root
->root_key
.objectid
,
3829 location
->objectid
);
3836 leaf
= path
->nodes
[0];
3837 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
3838 if (btrfs_root_ref_dirid(leaf
, ref
) != dir
->i_ino
||
3839 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
3842 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
3843 (unsigned long)(ref
+ 1),
3844 dentry
->d_name
.len
);
3848 btrfs_release_path(root
->fs_info
->tree_root
, path
);
3850 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
3851 if (IS_ERR(new_root
)) {
3852 err
= PTR_ERR(new_root
);
3856 if (btrfs_root_refs(&new_root
->root_item
) == 0) {
3861 *sub_root
= new_root
;
3862 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
3863 location
->type
= BTRFS_INODE_ITEM_KEY
;
3864 location
->offset
= 0;
3867 btrfs_free_path(path
);
3871 static void inode_tree_add(struct inode
*inode
)
3873 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3874 struct btrfs_inode
*entry
;
3876 struct rb_node
*parent
;
3878 p
= &root
->inode_tree
.rb_node
;
3881 if (inode_unhashed(inode
))
3884 spin_lock(&root
->inode_lock
);
3887 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
3889 if (inode
->i_ino
< entry
->vfs_inode
.i_ino
)
3890 p
= &parent
->rb_left
;
3891 else if (inode
->i_ino
> entry
->vfs_inode
.i_ino
)
3892 p
= &parent
->rb_right
;
3894 WARN_ON(!(entry
->vfs_inode
.i_state
&
3895 (I_WILL_FREE
| I_FREEING
)));
3896 rb_erase(parent
, &root
->inode_tree
);
3897 RB_CLEAR_NODE(parent
);
3898 spin_unlock(&root
->inode_lock
);
3902 rb_link_node(&BTRFS_I(inode
)->rb_node
, parent
, p
);
3903 rb_insert_color(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
3904 spin_unlock(&root
->inode_lock
);
3907 static void inode_tree_del(struct inode
*inode
)
3909 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3912 spin_lock(&root
->inode_lock
);
3913 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
3914 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
3915 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
3916 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
3918 spin_unlock(&root
->inode_lock
);
3921 * Free space cache has inodes in the tree root, but the tree root has a
3922 * root_refs of 0, so this could end up dropping the tree root as a
3923 * snapshot, so we need the extra !root->fs_info->tree_root check to
3924 * make sure we don't drop it.
3926 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0 &&
3927 root
!= root
->fs_info
->tree_root
) {
3928 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
3929 spin_lock(&root
->inode_lock
);
3930 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
3931 spin_unlock(&root
->inode_lock
);
3933 btrfs_add_dead_root(root
);
3937 int btrfs_invalidate_inodes(struct btrfs_root
*root
)
3939 struct rb_node
*node
;
3940 struct rb_node
*prev
;
3941 struct btrfs_inode
*entry
;
3942 struct inode
*inode
;
3945 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
3947 spin_lock(&root
->inode_lock
);
3949 node
= root
->inode_tree
.rb_node
;
3953 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
3955 if (objectid
< entry
->vfs_inode
.i_ino
)
3956 node
= node
->rb_left
;
3957 else if (objectid
> entry
->vfs_inode
.i_ino
)
3958 node
= node
->rb_right
;
3964 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
3965 if (objectid
<= entry
->vfs_inode
.i_ino
) {
3969 prev
= rb_next(prev
);
3973 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
3974 objectid
= entry
->vfs_inode
.i_ino
+ 1;
3975 inode
= igrab(&entry
->vfs_inode
);
3977 spin_unlock(&root
->inode_lock
);
3978 if (atomic_read(&inode
->i_count
) > 1)
3979 d_prune_aliases(inode
);
3981 * btrfs_drop_inode will have it removed from
3982 * the inode cache when its usage count
3987 spin_lock(&root
->inode_lock
);
3991 if (cond_resched_lock(&root
->inode_lock
))
3994 node
= rb_next(node
);
3996 spin_unlock(&root
->inode_lock
);
4000 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
4002 struct btrfs_iget_args
*args
= p
;
4003 inode
->i_ino
= args
->ino
;
4004 BTRFS_I(inode
)->root
= args
->root
;
4005 btrfs_set_inode_space_info(args
->root
, inode
);
4009 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
4011 struct btrfs_iget_args
*args
= opaque
;
4012 return args
->ino
== inode
->i_ino
&&
4013 args
->root
== BTRFS_I(inode
)->root
;
4016 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
4018 struct btrfs_root
*root
)
4020 struct inode
*inode
;
4021 struct btrfs_iget_args args
;
4022 args
.ino
= objectid
;
4025 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
4026 btrfs_init_locked_inode
,
4031 /* Get an inode object given its location and corresponding root.
4032 * Returns in *is_new if the inode was read from disk
4034 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
4035 struct btrfs_root
*root
, int *new)
4037 struct inode
*inode
;
4039 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
4041 return ERR_PTR(-ENOMEM
);
4043 if (inode
->i_state
& I_NEW
) {
4044 BTRFS_I(inode
)->root
= root
;
4045 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
4046 btrfs_read_locked_inode(inode
);
4048 inode_tree_add(inode
);
4049 unlock_new_inode(inode
);
4057 static struct inode
*new_simple_dir(struct super_block
*s
,
4058 struct btrfs_key
*key
,
4059 struct btrfs_root
*root
)
4061 struct inode
*inode
= new_inode(s
);
4064 return ERR_PTR(-ENOMEM
);
4066 BTRFS_I(inode
)->root
= root
;
4067 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
4068 BTRFS_I(inode
)->dummy_inode
= 1;
4070 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
4071 inode
->i_op
= &simple_dir_inode_operations
;
4072 inode
->i_fop
= &simple_dir_operations
;
4073 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
4074 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4079 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
4081 struct inode
*inode
;
4082 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4083 struct btrfs_root
*sub_root
= root
;
4084 struct btrfs_key location
;
4088 d_set_d_op(dentry
, &btrfs_dentry_operations
);
4090 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
4091 return ERR_PTR(-ENAMETOOLONG
);
4093 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
4096 return ERR_PTR(ret
);
4098 if (location
.objectid
== 0)
4101 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
4102 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
4106 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
4108 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
4109 ret
= fixup_tree_root_location(root
, dir
, dentry
,
4110 &location
, &sub_root
);
4113 inode
= ERR_PTR(ret
);
4115 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
4117 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
4119 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
4121 if (root
!= sub_root
) {
4122 down_read(&root
->fs_info
->cleanup_work_sem
);
4123 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
4124 btrfs_orphan_cleanup(sub_root
);
4125 up_read(&root
->fs_info
->cleanup_work_sem
);
4131 static int btrfs_dentry_delete(const struct dentry
*dentry
)
4133 struct btrfs_root
*root
;
4135 if (!dentry
->d_inode
&& !IS_ROOT(dentry
))
4136 dentry
= dentry
->d_parent
;
4138 if (dentry
->d_inode
) {
4139 root
= BTRFS_I(dentry
->d_inode
)->root
;
4140 if (btrfs_root_refs(&root
->root_item
) == 0)
4146 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
4147 struct nameidata
*nd
)
4149 struct inode
*inode
;
4151 inode
= btrfs_lookup_dentry(dir
, dentry
);
4153 return ERR_CAST(inode
);
4155 return d_splice_alias(inode
, dentry
);
4158 static unsigned char btrfs_filetype_table
[] = {
4159 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
4162 static int btrfs_real_readdir(struct file
*filp
, void *dirent
,
4165 struct inode
*inode
= filp
->f_dentry
->d_inode
;
4166 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4167 struct btrfs_item
*item
;
4168 struct btrfs_dir_item
*di
;
4169 struct btrfs_key key
;
4170 struct btrfs_key found_key
;
4171 struct btrfs_path
*path
;
4174 struct extent_buffer
*leaf
;
4177 unsigned char d_type
;
4182 int key_type
= BTRFS_DIR_INDEX_KEY
;
4187 /* FIXME, use a real flag for deciding about the key type */
4188 if (root
->fs_info
->tree_root
== root
)
4189 key_type
= BTRFS_DIR_ITEM_KEY
;
4191 /* special case for "." */
4192 if (filp
->f_pos
== 0) {
4193 over
= filldir(dirent
, ".", 1,
4200 /* special case for .., just use the back ref */
4201 if (filp
->f_pos
== 1) {
4202 u64 pino
= parent_ino(filp
->f_path
.dentry
);
4203 over
= filldir(dirent
, "..", 2,
4209 path
= btrfs_alloc_path();
4212 btrfs_set_key_type(&key
, key_type
);
4213 key
.offset
= filp
->f_pos
;
4214 key
.objectid
= inode
->i_ino
;
4216 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4222 leaf
= path
->nodes
[0];
4223 nritems
= btrfs_header_nritems(leaf
);
4224 slot
= path
->slots
[0];
4225 if (advance
|| slot
>= nritems
) {
4226 if (slot
>= nritems
- 1) {
4227 ret
= btrfs_next_leaf(root
, path
);
4230 leaf
= path
->nodes
[0];
4231 nritems
= btrfs_header_nritems(leaf
);
4232 slot
= path
->slots
[0];
4240 item
= btrfs_item_nr(leaf
, slot
);
4241 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
4243 if (found_key
.objectid
!= key
.objectid
)
4245 if (btrfs_key_type(&found_key
) != key_type
)
4247 if (found_key
.offset
< filp
->f_pos
)
4250 filp
->f_pos
= found_key
.offset
;
4252 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
4254 di_total
= btrfs_item_size(leaf
, item
);
4256 while (di_cur
< di_total
) {
4257 struct btrfs_key location
;
4259 name_len
= btrfs_dir_name_len(leaf
, di
);
4260 if (name_len
<= sizeof(tmp_name
)) {
4261 name_ptr
= tmp_name
;
4263 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
4269 read_extent_buffer(leaf
, name_ptr
,
4270 (unsigned long)(di
+ 1), name_len
);
4272 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
4273 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
4275 /* is this a reference to our own snapshot? If so
4278 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
4279 location
.objectid
== root
->root_key
.objectid
) {
4283 over
= filldir(dirent
, name_ptr
, name_len
,
4284 found_key
.offset
, location
.objectid
,
4288 if (name_ptr
!= tmp_name
)
4293 di_len
= btrfs_dir_name_len(leaf
, di
) +
4294 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
4296 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
4300 /* Reached end of directory/root. Bump pos past the last item. */
4301 if (key_type
== BTRFS_DIR_INDEX_KEY
)
4303 * 32-bit glibc will use getdents64, but then strtol -
4304 * so the last number we can serve is this.
4306 filp
->f_pos
= 0x7fffffff;
4312 btrfs_free_path(path
);
4316 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
4318 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4319 struct btrfs_trans_handle
*trans
;
4321 bool nolock
= false;
4323 if (BTRFS_I(inode
)->dummy_inode
)
4327 nolock
= (root
->fs_info
->closing
&& root
== root
->fs_info
->tree_root
);
4329 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
4331 trans
= btrfs_join_transaction_nolock(root
, 1);
4333 trans
= btrfs_join_transaction(root
, 1);
4334 btrfs_set_trans_block_group(trans
, inode
);
4336 ret
= btrfs_end_transaction_nolock(trans
, root
);
4338 ret
= btrfs_commit_transaction(trans
, root
);
4344 * This is somewhat expensive, updating the tree every time the
4345 * inode changes. But, it is most likely to find the inode in cache.
4346 * FIXME, needs more benchmarking...there are no reasons other than performance
4347 * to keep or drop this code.
4349 void btrfs_dirty_inode(struct inode
*inode
)
4351 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4352 struct btrfs_trans_handle
*trans
;
4355 if (BTRFS_I(inode
)->dummy_inode
)
4358 trans
= btrfs_join_transaction(root
, 1);
4359 btrfs_set_trans_block_group(trans
, inode
);
4361 ret
= btrfs_update_inode(trans
, root
, inode
);
4362 if (ret
&& ret
== -ENOSPC
) {
4363 /* whoops, lets try again with the full transaction */
4364 btrfs_end_transaction(trans
, root
);
4365 trans
= btrfs_start_transaction(root
, 1);
4366 if (IS_ERR(trans
)) {
4367 if (printk_ratelimit()) {
4368 printk(KERN_ERR
"btrfs: fail to "
4369 "dirty inode %lu error %ld\n",
4370 inode
->i_ino
, PTR_ERR(trans
));
4374 btrfs_set_trans_block_group(trans
, inode
);
4376 ret
= btrfs_update_inode(trans
, root
, inode
);
4378 if (printk_ratelimit()) {
4379 printk(KERN_ERR
"btrfs: fail to "
4380 "dirty inode %lu error %d\n",
4385 btrfs_end_transaction(trans
, root
);
4389 * find the highest existing sequence number in a directory
4390 * and then set the in-memory index_cnt variable to reflect
4391 * free sequence numbers
4393 static int btrfs_set_inode_index_count(struct inode
*inode
)
4395 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4396 struct btrfs_key key
, found_key
;
4397 struct btrfs_path
*path
;
4398 struct extent_buffer
*leaf
;
4401 key
.objectid
= inode
->i_ino
;
4402 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
4403 key
.offset
= (u64
)-1;
4405 path
= btrfs_alloc_path();
4409 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4412 /* FIXME: we should be able to handle this */
4418 * MAGIC NUMBER EXPLANATION:
4419 * since we search a directory based on f_pos we have to start at 2
4420 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4421 * else has to start at 2
4423 if (path
->slots
[0] == 0) {
4424 BTRFS_I(inode
)->index_cnt
= 2;
4430 leaf
= path
->nodes
[0];
4431 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4433 if (found_key
.objectid
!= inode
->i_ino
||
4434 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
4435 BTRFS_I(inode
)->index_cnt
= 2;
4439 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
4441 btrfs_free_path(path
);
4446 * helper to find a free sequence number in a given directory. This current
4447 * code is very simple, later versions will do smarter things in the btree
4449 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
4453 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
4454 ret
= btrfs_set_inode_index_count(dir
);
4459 *index
= BTRFS_I(dir
)->index_cnt
;
4460 BTRFS_I(dir
)->index_cnt
++;
4465 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
4466 struct btrfs_root
*root
,
4468 const char *name
, int name_len
,
4469 u64 ref_objectid
, u64 objectid
,
4470 u64 alloc_hint
, int mode
, u64
*index
)
4472 struct inode
*inode
;
4473 struct btrfs_inode_item
*inode_item
;
4474 struct btrfs_key
*location
;
4475 struct btrfs_path
*path
;
4476 struct btrfs_inode_ref
*ref
;
4477 struct btrfs_key key
[2];
4483 path
= btrfs_alloc_path();
4486 inode
= new_inode(root
->fs_info
->sb
);
4488 return ERR_PTR(-ENOMEM
);
4491 ret
= btrfs_set_inode_index(dir
, index
);
4494 return ERR_PTR(ret
);
4498 * index_cnt is ignored for everything but a dir,
4499 * btrfs_get_inode_index_count has an explanation for the magic
4502 BTRFS_I(inode
)->index_cnt
= 2;
4503 BTRFS_I(inode
)->root
= root
;
4504 BTRFS_I(inode
)->generation
= trans
->transid
;
4505 inode
->i_generation
= BTRFS_I(inode
)->generation
;
4506 btrfs_set_inode_space_info(root
, inode
);
4512 BTRFS_I(inode
)->block_group
=
4513 btrfs_find_block_group(root
, 0, alloc_hint
, owner
);
4515 key
[0].objectid
= objectid
;
4516 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
4519 key
[1].objectid
= objectid
;
4520 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
4521 key
[1].offset
= ref_objectid
;
4523 sizes
[0] = sizeof(struct btrfs_inode_item
);
4524 sizes
[1] = name_len
+ sizeof(*ref
);
4526 path
->leave_spinning
= 1;
4527 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
4531 inode_init_owner(inode
, dir
, mode
);
4532 inode
->i_ino
= objectid
;
4533 inode_set_bytes(inode
, 0);
4534 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4535 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4536 struct btrfs_inode_item
);
4537 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
4539 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
4540 struct btrfs_inode_ref
);
4541 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
4542 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
4543 ptr
= (unsigned long)(ref
+ 1);
4544 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
4546 btrfs_mark_buffer_dirty(path
->nodes
[0]);
4547 btrfs_free_path(path
);
4549 location
= &BTRFS_I(inode
)->location
;
4550 location
->objectid
= objectid
;
4551 location
->offset
= 0;
4552 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
4554 btrfs_inherit_iflags(inode
, dir
);
4556 if ((mode
& S_IFREG
)) {
4557 if (btrfs_test_opt(root
, NODATASUM
))
4558 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
4559 if (btrfs_test_opt(root
, NODATACOW
))
4560 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
;
4563 insert_inode_hash(inode
);
4564 inode_tree_add(inode
);
4568 BTRFS_I(dir
)->index_cnt
--;
4569 btrfs_free_path(path
);
4571 return ERR_PTR(ret
);
4574 static inline u8
btrfs_inode_type(struct inode
*inode
)
4576 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
4580 * utility function to add 'inode' into 'parent_inode' with
4581 * a give name and a given sequence number.
4582 * if 'add_backref' is true, also insert a backref from the
4583 * inode to the parent directory.
4585 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
4586 struct inode
*parent_inode
, struct inode
*inode
,
4587 const char *name
, int name_len
, int add_backref
, u64 index
)
4590 struct btrfs_key key
;
4591 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
4593 if (unlikely(inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4594 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
4596 key
.objectid
= inode
->i_ino
;
4597 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
4601 if (unlikely(inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4602 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
4603 key
.objectid
, root
->root_key
.objectid
,
4604 parent_inode
->i_ino
,
4605 index
, name
, name_len
);
4606 } else if (add_backref
) {
4607 ret
= btrfs_insert_inode_ref(trans
, root
,
4608 name
, name_len
, inode
->i_ino
,
4609 parent_inode
->i_ino
, index
);
4613 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
4614 parent_inode
->i_ino
, &key
,
4615 btrfs_inode_type(inode
), index
);
4618 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
4620 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
4621 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
4626 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
4627 struct inode
*dir
, struct dentry
*dentry
,
4628 struct inode
*inode
, int backref
, u64 index
)
4630 int err
= btrfs_add_link(trans
, dir
, inode
,
4631 dentry
->d_name
.name
, dentry
->d_name
.len
,
4634 d_instantiate(dentry
, inode
);
4642 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
4643 int mode
, dev_t rdev
)
4645 struct btrfs_trans_handle
*trans
;
4646 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4647 struct inode
*inode
= NULL
;
4651 unsigned long nr
= 0;
4654 if (!new_valid_dev(rdev
))
4657 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
4662 * 2 for inode item and ref
4664 * 1 for xattr if selinux is on
4666 trans
= btrfs_start_transaction(root
, 5);
4668 return PTR_ERR(trans
);
4670 btrfs_set_trans_block_group(trans
, dir
);
4672 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4673 dentry
->d_name
.len
, dir
->i_ino
, objectid
,
4674 BTRFS_I(dir
)->block_group
, mode
, &index
);
4675 err
= PTR_ERR(inode
);
4679 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
4685 btrfs_set_trans_block_group(trans
, inode
);
4686 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
4690 inode
->i_op
= &btrfs_special_inode_operations
;
4691 init_special_inode(inode
, inode
->i_mode
, rdev
);
4692 btrfs_update_inode(trans
, root
, inode
);
4694 btrfs_update_inode_block_group(trans
, inode
);
4695 btrfs_update_inode_block_group(trans
, dir
);
4697 nr
= trans
->blocks_used
;
4698 btrfs_end_transaction_throttle(trans
, root
);
4699 btrfs_btree_balance_dirty(root
, nr
);
4701 inode_dec_link_count(inode
);
4707 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
4708 int mode
, struct nameidata
*nd
)
4710 struct btrfs_trans_handle
*trans
;
4711 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4712 struct inode
*inode
= NULL
;
4715 unsigned long nr
= 0;
4719 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
4723 * 2 for inode item and ref
4725 * 1 for xattr if selinux is on
4727 trans
= btrfs_start_transaction(root
, 5);
4729 return PTR_ERR(trans
);
4731 btrfs_set_trans_block_group(trans
, dir
);
4733 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4734 dentry
->d_name
.len
, dir
->i_ino
, objectid
,
4735 BTRFS_I(dir
)->block_group
, mode
, &index
);
4736 err
= PTR_ERR(inode
);
4740 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
4746 btrfs_set_trans_block_group(trans
, inode
);
4747 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
4751 inode
->i_mapping
->a_ops
= &btrfs_aops
;
4752 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
4753 inode
->i_fop
= &btrfs_file_operations
;
4754 inode
->i_op
= &btrfs_file_inode_operations
;
4755 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
4757 btrfs_update_inode_block_group(trans
, inode
);
4758 btrfs_update_inode_block_group(trans
, dir
);
4760 nr
= trans
->blocks_used
;
4761 btrfs_end_transaction_throttle(trans
, root
);
4763 inode_dec_link_count(inode
);
4766 btrfs_btree_balance_dirty(root
, nr
);
4770 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
4771 struct dentry
*dentry
)
4773 struct btrfs_trans_handle
*trans
;
4774 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4775 struct inode
*inode
= old_dentry
->d_inode
;
4777 unsigned long nr
= 0;
4781 if (inode
->i_nlink
== 0)
4784 /* do not allow sys_link's with other subvols of the same device */
4785 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
4788 btrfs_inc_nlink(inode
);
4789 inode
->i_ctime
= CURRENT_TIME
;
4791 err
= btrfs_set_inode_index(dir
, &index
);
4796 * 1 item for inode ref
4797 * 2 items for dir items
4799 trans
= btrfs_start_transaction(root
, 3);
4800 if (IS_ERR(trans
)) {
4801 err
= PTR_ERR(trans
);
4805 btrfs_set_trans_block_group(trans
, dir
);
4808 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
4813 struct dentry
*parent
= dget_parent(dentry
);
4814 btrfs_update_inode_block_group(trans
, dir
);
4815 err
= btrfs_update_inode(trans
, root
, inode
);
4817 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
4821 nr
= trans
->blocks_used
;
4822 btrfs_end_transaction_throttle(trans
, root
);
4825 inode_dec_link_count(inode
);
4828 btrfs_btree_balance_dirty(root
, nr
);
4832 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
4834 struct inode
*inode
= NULL
;
4835 struct btrfs_trans_handle
*trans
;
4836 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4838 int drop_on_err
= 0;
4841 unsigned long nr
= 1;
4843 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
4848 * 2 items for inode and ref
4849 * 2 items for dir items
4850 * 1 for xattr if selinux is on
4852 trans
= btrfs_start_transaction(root
, 5);
4854 return PTR_ERR(trans
);
4855 btrfs_set_trans_block_group(trans
, dir
);
4857 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4858 dentry
->d_name
.len
, dir
->i_ino
, objectid
,
4859 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
,
4861 if (IS_ERR(inode
)) {
4862 err
= PTR_ERR(inode
);
4868 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
4872 inode
->i_op
= &btrfs_dir_inode_operations
;
4873 inode
->i_fop
= &btrfs_dir_file_operations
;
4874 btrfs_set_trans_block_group(trans
, inode
);
4876 btrfs_i_size_write(inode
, 0);
4877 err
= btrfs_update_inode(trans
, root
, inode
);
4881 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
4882 dentry
->d_name
.len
, 0, index
);
4886 d_instantiate(dentry
, inode
);
4888 btrfs_update_inode_block_group(trans
, inode
);
4889 btrfs_update_inode_block_group(trans
, dir
);
4892 nr
= trans
->blocks_used
;
4893 btrfs_end_transaction_throttle(trans
, root
);
4896 btrfs_btree_balance_dirty(root
, nr
);
4900 /* helper for btfs_get_extent. Given an existing extent in the tree,
4901 * and an extent that you want to insert, deal with overlap and insert
4902 * the new extent into the tree.
4904 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
4905 struct extent_map
*existing
,
4906 struct extent_map
*em
,
4907 u64 map_start
, u64 map_len
)
4911 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
4912 start_diff
= map_start
- em
->start
;
4913 em
->start
= map_start
;
4915 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
4916 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
4917 em
->block_start
+= start_diff
;
4918 em
->block_len
-= start_diff
;
4920 return add_extent_mapping(em_tree
, em
);
4923 static noinline
int uncompress_inline(struct btrfs_path
*path
,
4924 struct inode
*inode
, struct page
*page
,
4925 size_t pg_offset
, u64 extent_offset
,
4926 struct btrfs_file_extent_item
*item
)
4929 struct extent_buffer
*leaf
= path
->nodes
[0];
4932 unsigned long inline_size
;
4935 WARN_ON(pg_offset
!= 0);
4936 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
4937 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
4938 btrfs_item_nr(leaf
, path
->slots
[0]));
4939 tmp
= kmalloc(inline_size
, GFP_NOFS
);
4940 ptr
= btrfs_file_extent_inline_start(item
);
4942 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
4944 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
4945 ret
= btrfs_zlib_decompress(tmp
, page
, extent_offset
,
4946 inline_size
, max_size
);
4948 char *kaddr
= kmap_atomic(page
, KM_USER0
);
4949 unsigned long copy_size
= min_t(u64
,
4950 PAGE_CACHE_SIZE
- pg_offset
,
4951 max_size
- extent_offset
);
4952 memset(kaddr
+ pg_offset
, 0, copy_size
);
4953 kunmap_atomic(kaddr
, KM_USER0
);
4960 * a bit scary, this does extent mapping from logical file offset to the disk.
4961 * the ugly parts come from merging extents from the disk with the in-ram
4962 * representation. This gets more complex because of the data=ordered code,
4963 * where the in-ram extents might be locked pending data=ordered completion.
4965 * This also copies inline extents directly into the page.
4968 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
4969 size_t pg_offset
, u64 start
, u64 len
,
4975 u64 extent_start
= 0;
4977 u64 objectid
= inode
->i_ino
;
4979 struct btrfs_path
*path
= NULL
;
4980 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4981 struct btrfs_file_extent_item
*item
;
4982 struct extent_buffer
*leaf
;
4983 struct btrfs_key found_key
;
4984 struct extent_map
*em
= NULL
;
4985 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4986 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4987 struct btrfs_trans_handle
*trans
= NULL
;
4991 read_lock(&em_tree
->lock
);
4992 em
= lookup_extent_mapping(em_tree
, start
, len
);
4994 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4995 read_unlock(&em_tree
->lock
);
4998 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
4999 free_extent_map(em
);
5000 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
5001 free_extent_map(em
);
5005 em
= alloc_extent_map(GFP_NOFS
);
5010 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5011 em
->start
= EXTENT_MAP_HOLE
;
5012 em
->orig_start
= EXTENT_MAP_HOLE
;
5014 em
->block_len
= (u64
)-1;
5017 path
= btrfs_alloc_path();
5021 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
5022 objectid
, start
, trans
!= NULL
);
5029 if (path
->slots
[0] == 0)
5034 leaf
= path
->nodes
[0];
5035 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5036 struct btrfs_file_extent_item
);
5037 /* are we inside the extent that was found? */
5038 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5039 found_type
= btrfs_key_type(&found_key
);
5040 if (found_key
.objectid
!= objectid
||
5041 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
5045 found_type
= btrfs_file_extent_type(leaf
, item
);
5046 extent_start
= found_key
.offset
;
5047 compressed
= btrfs_file_extent_compression(leaf
, item
);
5048 if (found_type
== BTRFS_FILE_EXTENT_REG
||
5049 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5050 extent_end
= extent_start
+
5051 btrfs_file_extent_num_bytes(leaf
, item
);
5052 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
5054 size
= btrfs_file_extent_inline_len(leaf
, item
);
5055 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
5056 ~((u64
)root
->sectorsize
- 1);
5059 if (start
>= extent_end
) {
5061 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
5062 ret
= btrfs_next_leaf(root
, path
);
5069 leaf
= path
->nodes
[0];
5071 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5072 if (found_key
.objectid
!= objectid
||
5073 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
5075 if (start
+ len
<= found_key
.offset
)
5078 em
->len
= found_key
.offset
- start
;
5082 if (found_type
== BTRFS_FILE_EXTENT_REG
||
5083 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5084 em
->start
= extent_start
;
5085 em
->len
= extent_end
- extent_start
;
5086 em
->orig_start
= extent_start
-
5087 btrfs_file_extent_offset(leaf
, item
);
5088 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
5090 em
->block_start
= EXTENT_MAP_HOLE
;
5094 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
5095 em
->block_start
= bytenr
;
5096 em
->block_len
= btrfs_file_extent_disk_num_bytes(leaf
,
5099 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
5100 em
->block_start
= bytenr
;
5101 em
->block_len
= em
->len
;
5102 if (found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
5103 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
5106 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
5110 size_t extent_offset
;
5113 em
->block_start
= EXTENT_MAP_INLINE
;
5114 if (!page
|| create
) {
5115 em
->start
= extent_start
;
5116 em
->len
= extent_end
- extent_start
;
5120 size
= btrfs_file_extent_inline_len(leaf
, item
);
5121 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
5122 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
5123 size
- extent_offset
);
5124 em
->start
= extent_start
+ extent_offset
;
5125 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
5126 ~((u64
)root
->sectorsize
- 1);
5127 em
->orig_start
= EXTENT_MAP_INLINE
;
5129 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
5130 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
5131 if (create
== 0 && !PageUptodate(page
)) {
5132 if (btrfs_file_extent_compression(leaf
, item
) ==
5133 BTRFS_COMPRESS_ZLIB
) {
5134 ret
= uncompress_inline(path
, inode
, page
,
5136 extent_offset
, item
);
5140 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
5142 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
5143 memset(map
+ pg_offset
+ copy_size
, 0,
5144 PAGE_CACHE_SIZE
- pg_offset
-
5149 flush_dcache_page(page
);
5150 } else if (create
&& PageUptodate(page
)) {
5154 free_extent_map(em
);
5156 btrfs_release_path(root
, path
);
5157 trans
= btrfs_join_transaction(root
, 1);
5161 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
5164 btrfs_mark_buffer_dirty(leaf
);
5166 set_extent_uptodate(io_tree
, em
->start
,
5167 extent_map_end(em
) - 1, GFP_NOFS
);
5170 printk(KERN_ERR
"btrfs unknown found_type %d\n", found_type
);
5177 em
->block_start
= EXTENT_MAP_HOLE
;
5178 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
5180 btrfs_release_path(root
, path
);
5181 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
5182 printk(KERN_ERR
"Btrfs: bad extent! em: [%llu %llu] passed "
5183 "[%llu %llu]\n", (unsigned long long)em
->start
,
5184 (unsigned long long)em
->len
,
5185 (unsigned long long)start
,
5186 (unsigned long long)len
);
5192 write_lock(&em_tree
->lock
);
5193 ret
= add_extent_mapping(em_tree
, em
);
5194 /* it is possible that someone inserted the extent into the tree
5195 * while we had the lock dropped. It is also possible that
5196 * an overlapping map exists in the tree
5198 if (ret
== -EEXIST
) {
5199 struct extent_map
*existing
;
5203 existing
= lookup_extent_mapping(em_tree
, start
, len
);
5204 if (existing
&& (existing
->start
> start
||
5205 existing
->start
+ existing
->len
<= start
)) {
5206 free_extent_map(existing
);
5210 existing
= lookup_extent_mapping(em_tree
, em
->start
,
5213 err
= merge_extent_mapping(em_tree
, existing
,
5216 free_extent_map(existing
);
5218 free_extent_map(em
);
5223 free_extent_map(em
);
5227 free_extent_map(em
);
5232 write_unlock(&em_tree
->lock
);
5235 btrfs_free_path(path
);
5237 ret
= btrfs_end_transaction(trans
, root
);
5242 free_extent_map(em
);
5243 return ERR_PTR(err
);
5248 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
5251 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5252 struct btrfs_trans_handle
*trans
;
5253 struct extent_map
*em
;
5254 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
5255 struct btrfs_key ins
;
5259 btrfs_drop_extent_cache(inode
, start
, start
+ len
- 1, 0);
5261 trans
= btrfs_join_transaction(root
, 0);
5263 return ERR_PTR(-ENOMEM
);
5265 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
5267 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
5268 ret
= btrfs_reserve_extent(trans
, root
, len
, root
->sectorsize
, 0,
5269 alloc_hint
, (u64
)-1, &ins
, 1);
5275 em
= alloc_extent_map(GFP_NOFS
);
5277 em
= ERR_PTR(-ENOMEM
);
5282 em
->orig_start
= em
->start
;
5283 em
->len
= ins
.offset
;
5285 em
->block_start
= ins
.objectid
;
5286 em
->block_len
= ins
.offset
;
5287 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5288 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
5291 write_lock(&em_tree
->lock
);
5292 ret
= add_extent_mapping(em_tree
, em
);
5293 write_unlock(&em_tree
->lock
);
5296 btrfs_drop_extent_cache(inode
, start
, start
+ em
->len
- 1, 0);
5299 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
5300 ins
.offset
, ins
.offset
, 0);
5302 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
5306 btrfs_end_transaction(trans
, root
);
5311 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5312 * block must be cow'd
5314 static noinline
int can_nocow_odirect(struct btrfs_trans_handle
*trans
,
5315 struct inode
*inode
, u64 offset
, u64 len
)
5317 struct btrfs_path
*path
;
5319 struct extent_buffer
*leaf
;
5320 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5321 struct btrfs_file_extent_item
*fi
;
5322 struct btrfs_key key
;
5330 path
= btrfs_alloc_path();
5334 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
5339 slot
= path
->slots
[0];
5342 /* can't find the item, must cow */
5349 leaf
= path
->nodes
[0];
5350 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
5351 if (key
.objectid
!= inode
->i_ino
||
5352 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
5353 /* not our file or wrong item type, must cow */
5357 if (key
.offset
> offset
) {
5358 /* Wrong offset, must cow */
5362 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
5363 found_type
= btrfs_file_extent_type(leaf
, fi
);
5364 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
5365 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
5366 /* not a regular extent, must cow */
5369 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
5370 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
5372 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
5373 if (extent_end
< offset
+ len
) {
5374 /* extent doesn't include our full range, must cow */
5378 if (btrfs_extent_readonly(root
, disk_bytenr
))
5382 * look for other files referencing this extent, if we
5383 * find any we must cow
5385 if (btrfs_cross_ref_exist(trans
, root
, inode
->i_ino
,
5386 key
.offset
- backref_offset
, disk_bytenr
))
5390 * adjust disk_bytenr and num_bytes to cover just the bytes
5391 * in this extent we are about to write. If there
5392 * are any csums in that range we have to cow in order
5393 * to keep the csums correct
5395 disk_bytenr
+= backref_offset
;
5396 disk_bytenr
+= offset
- key
.offset
;
5397 num_bytes
= min(offset
+ len
, extent_end
) - offset
;
5398 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
5401 * all of the above have passed, it is safe to overwrite this extent
5406 btrfs_free_path(path
);
5410 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
5411 struct buffer_head
*bh_result
, int create
)
5413 struct extent_map
*em
;
5414 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5415 u64 start
= iblock
<< inode
->i_blkbits
;
5416 u64 len
= bh_result
->b_size
;
5417 struct btrfs_trans_handle
*trans
;
5419 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
5424 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5425 * io. INLINE is special, and we could probably kludge it in here, but
5426 * it's still buffered so for safety lets just fall back to the generic
5429 * For COMPRESSED we _have_ to read the entire extent in so we can
5430 * decompress it, so there will be buffering required no matter what we
5431 * do, so go ahead and fallback to buffered.
5433 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5434 * to buffered IO. Don't blame me, this is the price we pay for using
5437 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
5438 em
->block_start
== EXTENT_MAP_INLINE
) {
5439 free_extent_map(em
);
5443 /* Just a good old fashioned hole, return */
5444 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
5445 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
5446 free_extent_map(em
);
5447 /* DIO will do one hole at a time, so just unlock a sector */
5448 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
,
5449 start
+ root
->sectorsize
- 1, GFP_NOFS
);
5454 * We don't allocate a new extent in the following cases
5456 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5458 * 2) The extent is marked as PREALLOC. We're good to go here and can
5459 * just use the extent.
5463 len
= em
->len
- (start
- em
->start
);
5467 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
5468 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
5469 em
->block_start
!= EXTENT_MAP_HOLE
)) {
5474 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
5475 type
= BTRFS_ORDERED_PREALLOC
;
5477 type
= BTRFS_ORDERED_NOCOW
;
5478 len
= min(len
, em
->len
- (start
- em
->start
));
5479 block_start
= em
->block_start
+ (start
- em
->start
);
5482 * we're not going to log anything, but we do need
5483 * to make sure the current transaction stays open
5484 * while we look for nocow cross refs
5486 trans
= btrfs_join_transaction(root
, 0);
5490 if (can_nocow_odirect(trans
, inode
, start
, len
) == 1) {
5491 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
5492 block_start
, len
, len
, type
);
5493 btrfs_end_transaction(trans
, root
);
5495 free_extent_map(em
);
5500 btrfs_end_transaction(trans
, root
);
5504 * this will cow the extent, reset the len in case we changed
5507 len
= bh_result
->b_size
;
5508 free_extent_map(em
);
5509 em
= btrfs_new_extent_direct(inode
, start
, len
);
5512 len
= min(len
, em
->len
- (start
- em
->start
));
5514 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
- 1,
5515 EXTENT_LOCKED
| EXTENT_DELALLOC
| EXTENT_DIRTY
, 1,
5518 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
5520 bh_result
->b_size
= len
;
5521 bh_result
->b_bdev
= em
->bdev
;
5522 set_buffer_mapped(bh_result
);
5523 if (create
&& !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
5524 set_buffer_new(bh_result
);
5526 free_extent_map(em
);
5531 struct btrfs_dio_private
{
5532 struct inode
*inode
;
5539 /* number of bios pending for this dio */
5540 atomic_t pending_bios
;
5545 struct bio
*orig_bio
;
5548 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
5550 struct btrfs_dio_private
*dip
= bio
->bi_private
;
5551 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
5552 struct bio_vec
*bvec
= bio
->bi_io_vec
;
5553 struct inode
*inode
= dip
->inode
;
5554 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5556 u32
*private = dip
->csums
;
5558 start
= dip
->logical_offset
;
5560 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
5561 struct page
*page
= bvec
->bv_page
;
5564 unsigned long flags
;
5566 local_irq_save(flags
);
5567 kaddr
= kmap_atomic(page
, KM_IRQ0
);
5568 csum
= btrfs_csum_data(root
, kaddr
+ bvec
->bv_offset
,
5569 csum
, bvec
->bv_len
);
5570 btrfs_csum_final(csum
, (char *)&csum
);
5571 kunmap_atomic(kaddr
, KM_IRQ0
);
5572 local_irq_restore(flags
);
5574 flush_dcache_page(bvec
->bv_page
);
5575 if (csum
!= *private) {
5576 printk(KERN_ERR
"btrfs csum failed ino %lu off"
5577 " %llu csum %u private %u\n",
5578 inode
->i_ino
, (unsigned long long)start
,
5584 start
+= bvec
->bv_len
;
5587 } while (bvec
<= bvec_end
);
5589 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
5590 dip
->logical_offset
+ dip
->bytes
- 1, GFP_NOFS
);
5591 bio
->bi_private
= dip
->private;
5595 dio_end_io(bio
, err
);
5598 static void btrfs_endio_direct_write(struct bio
*bio
, int err
)
5600 struct btrfs_dio_private
*dip
= bio
->bi_private
;
5601 struct inode
*inode
= dip
->inode
;
5602 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5603 struct btrfs_trans_handle
*trans
;
5604 struct btrfs_ordered_extent
*ordered
= NULL
;
5605 struct extent_state
*cached_state
= NULL
;
5606 u64 ordered_offset
= dip
->logical_offset
;
5607 u64 ordered_bytes
= dip
->bytes
;
5613 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
5621 trans
= btrfs_join_transaction(root
, 1);
5626 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
5628 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
)) {
5629 ret
= btrfs_ordered_update_i_size(inode
, 0, ordered
);
5631 ret
= btrfs_update_inode(trans
, root
, inode
);
5636 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, ordered
->file_offset
,
5637 ordered
->file_offset
+ ordered
->len
- 1, 0,
5638 &cached_state
, GFP_NOFS
);
5640 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
)) {
5641 ret
= btrfs_mark_extent_written(trans
, inode
,
5642 ordered
->file_offset
,
5643 ordered
->file_offset
+
5650 ret
= insert_reserved_file_extent(trans
, inode
,
5651 ordered
->file_offset
,
5657 BTRFS_FILE_EXTENT_REG
);
5658 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
5659 ordered
->file_offset
, ordered
->len
);
5667 add_pending_csums(trans
, inode
, ordered
->file_offset
, &ordered
->list
);
5668 btrfs_ordered_update_i_size(inode
, 0, ordered
);
5669 btrfs_update_inode(trans
, root
, inode
);
5671 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, ordered
->file_offset
,
5672 ordered
->file_offset
+ ordered
->len
- 1,
5673 &cached_state
, GFP_NOFS
);
5675 btrfs_delalloc_release_metadata(inode
, ordered
->len
);
5676 btrfs_end_transaction(trans
, root
);
5677 ordered_offset
= ordered
->file_offset
+ ordered
->len
;
5678 btrfs_put_ordered_extent(ordered
);
5679 btrfs_put_ordered_extent(ordered
);
5683 * our bio might span multiple ordered extents. If we haven't
5684 * completed the accounting for the whole dio, go back and try again
5686 if (ordered_offset
< dip
->logical_offset
+ dip
->bytes
) {
5687 ordered_bytes
= dip
->logical_offset
+ dip
->bytes
-
5692 bio
->bi_private
= dip
->private;
5696 dio_end_io(bio
, err
);
5699 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
5700 struct bio
*bio
, int mirror_num
,
5701 unsigned long bio_flags
, u64 offset
)
5704 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5705 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
5710 static void btrfs_end_dio_bio(struct bio
*bio
, int err
)
5712 struct btrfs_dio_private
*dip
= bio
->bi_private
;
5715 printk(KERN_ERR
"btrfs direct IO failed ino %lu rw %lu "
5716 "sector %#Lx len %u err no %d\n",
5717 dip
->inode
->i_ino
, bio
->bi_rw
,
5718 (unsigned long long)bio
->bi_sector
, bio
->bi_size
, err
);
5722 * before atomic variable goto zero, we must make sure
5723 * dip->errors is perceived to be set.
5725 smp_mb__before_atomic_dec();
5728 /* if there are more bios still pending for this dio, just exit */
5729 if (!atomic_dec_and_test(&dip
->pending_bios
))
5733 bio_io_error(dip
->orig_bio
);
5735 set_bit(BIO_UPTODATE
, &dip
->orig_bio
->bi_flags
);
5736 bio_endio(dip
->orig_bio
, 0);
5742 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
5743 u64 first_sector
, gfp_t gfp_flags
)
5745 int nr_vecs
= bio_get_nr_vecs(bdev
);
5746 return btrfs_bio_alloc(bdev
, first_sector
, nr_vecs
, gfp_flags
);
5749 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
5750 int rw
, u64 file_offset
, int skip_sum
,
5753 int write
= rw
& REQ_WRITE
;
5754 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5758 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
5762 if (write
&& !skip_sum
) {
5763 ret
= btrfs_wq_submit_bio(root
->fs_info
,
5764 inode
, rw
, bio
, 0, 0,
5766 __btrfs_submit_bio_start_direct_io
,
5767 __btrfs_submit_bio_done
);
5769 } else if (!skip_sum
)
5770 btrfs_lookup_bio_sums_dio(root
, inode
, bio
,
5771 file_offset
, csums
);
5773 ret
= btrfs_map_bio(root
, rw
, bio
, 0, 1);
5779 static int btrfs_submit_direct_hook(int rw
, struct btrfs_dio_private
*dip
,
5782 struct inode
*inode
= dip
->inode
;
5783 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5784 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
5786 struct bio
*orig_bio
= dip
->orig_bio
;
5787 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
5788 u64 start_sector
= orig_bio
->bi_sector
;
5789 u64 file_offset
= dip
->logical_offset
;
5793 u32
*csums
= dip
->csums
;
5796 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
5799 bio
->bi_private
= dip
;
5800 bio
->bi_end_io
= btrfs_end_dio_bio
;
5801 atomic_inc(&dip
->pending_bios
);
5803 map_length
= orig_bio
->bi_size
;
5804 ret
= btrfs_map_block(map_tree
, READ
, start_sector
<< 9,
5805 &map_length
, NULL
, 0);
5811 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
5812 if (unlikely(map_length
< submit_len
+ bvec
->bv_len
||
5813 bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
5814 bvec
->bv_offset
) < bvec
->bv_len
)) {
5816 * inc the count before we submit the bio so
5817 * we know the end IO handler won't happen before
5818 * we inc the count. Otherwise, the dip might get freed
5819 * before we're done setting it up
5821 atomic_inc(&dip
->pending_bios
);
5822 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
,
5823 file_offset
, skip_sum
,
5827 atomic_dec(&dip
->pending_bios
);
5832 csums
= csums
+ nr_pages
;
5833 start_sector
+= submit_len
>> 9;
5834 file_offset
+= submit_len
;
5839 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
5840 start_sector
, GFP_NOFS
);
5843 bio
->bi_private
= dip
;
5844 bio
->bi_end_io
= btrfs_end_dio_bio
;
5846 map_length
= orig_bio
->bi_size
;
5847 ret
= btrfs_map_block(map_tree
, READ
, start_sector
<< 9,
5848 &map_length
, NULL
, 0);
5854 submit_len
+= bvec
->bv_len
;
5860 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
, file_offset
, skip_sum
,
5869 * before atomic variable goto zero, we must
5870 * make sure dip->errors is perceived to be set.
5872 smp_mb__before_atomic_dec();
5873 if (atomic_dec_and_test(&dip
->pending_bios
))
5874 bio_io_error(dip
->orig_bio
);
5876 /* bio_end_io() will handle error, so we needn't return it */
5880 static void btrfs_submit_direct(int rw
, struct bio
*bio
, struct inode
*inode
,
5883 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5884 struct btrfs_dio_private
*dip
;
5885 struct bio_vec
*bvec
= bio
->bi_io_vec
;
5887 int write
= rw
& REQ_WRITE
;
5890 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
5892 dip
= kmalloc(sizeof(*dip
), GFP_NOFS
);
5900 dip
->csums
= kmalloc(sizeof(u32
) * bio
->bi_vcnt
, GFP_NOFS
);
5907 dip
->private = bio
->bi_private
;
5909 dip
->logical_offset
= file_offset
;
5913 dip
->bytes
+= bvec
->bv_len
;
5915 } while (bvec
<= (bio
->bi_io_vec
+ bio
->bi_vcnt
- 1));
5917 dip
->disk_bytenr
= (u64
)bio
->bi_sector
<< 9;
5918 bio
->bi_private
= dip
;
5920 dip
->orig_bio
= bio
;
5921 atomic_set(&dip
->pending_bios
, 0);
5924 bio
->bi_end_io
= btrfs_endio_direct_write
;
5926 bio
->bi_end_io
= btrfs_endio_direct_read
;
5928 ret
= btrfs_submit_direct_hook(rw
, dip
, skip_sum
);
5933 * If this is a write, we need to clean up the reserved space and kill
5934 * the ordered extent.
5937 struct btrfs_ordered_extent
*ordered
;
5938 ordered
= btrfs_lookup_ordered_extent(inode
, file_offset
);
5939 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
5940 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
5941 btrfs_free_reserved_extent(root
, ordered
->start
,
5943 btrfs_put_ordered_extent(ordered
);
5944 btrfs_put_ordered_extent(ordered
);
5946 bio_endio(bio
, ret
);
5949 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
5950 const struct iovec
*iov
, loff_t offset
,
5951 unsigned long nr_segs
)
5956 unsigned blocksize_mask
= root
->sectorsize
- 1;
5957 ssize_t retval
= -EINVAL
;
5958 loff_t end
= offset
;
5960 if (offset
& blocksize_mask
)
5963 /* Check the memory alignment. Blocks cannot straddle pages */
5964 for (seg
= 0; seg
< nr_segs
; seg
++) {
5965 addr
= (unsigned long)iov
[seg
].iov_base
;
5966 size
= iov
[seg
].iov_len
;
5968 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
5975 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
5976 const struct iovec
*iov
, loff_t offset
,
5977 unsigned long nr_segs
)
5979 struct file
*file
= iocb
->ki_filp
;
5980 struct inode
*inode
= file
->f_mapping
->host
;
5981 struct btrfs_ordered_extent
*ordered
;
5982 struct extent_state
*cached_state
= NULL
;
5983 u64 lockstart
, lockend
;
5985 int writing
= rw
& WRITE
;
5987 size_t count
= iov_length(iov
, nr_segs
);
5989 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iov
,
5995 lockend
= offset
+ count
- 1;
5998 ret
= btrfs_delalloc_reserve_space(inode
, count
);
6004 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6005 0, &cached_state
, GFP_NOFS
);
6007 * We're concerned with the entire range that we're going to be
6008 * doing DIO to, so we need to make sure theres no ordered
6009 * extents in this range.
6011 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
6012 lockend
- lockstart
+ 1);
6015 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6016 &cached_state
, GFP_NOFS
);
6017 btrfs_start_ordered_extent(inode
, ordered
, 1);
6018 btrfs_put_ordered_extent(ordered
);
6023 * we don't use btrfs_set_extent_delalloc because we don't want
6024 * the dirty or uptodate bits
6027 write_bits
= EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
;
6028 ret
= set_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6029 EXTENT_DELALLOC
, 0, NULL
, &cached_state
,
6032 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6033 lockend
, EXTENT_LOCKED
| write_bits
,
6034 1, 0, &cached_state
, GFP_NOFS
);
6039 free_extent_state(cached_state
);
6040 cached_state
= NULL
;
6042 ret
= __blockdev_direct_IO(rw
, iocb
, inode
,
6043 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
6044 iov
, offset
, nr_segs
, btrfs_get_blocks_direct
, NULL
,
6045 btrfs_submit_direct
, 0);
6047 if (ret
< 0 && ret
!= -EIOCBQUEUED
) {
6048 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, offset
,
6049 offset
+ iov_length(iov
, nr_segs
) - 1,
6050 EXTENT_LOCKED
| write_bits
, 1, 0,
6051 &cached_state
, GFP_NOFS
);
6052 } else if (ret
>= 0 && ret
< iov_length(iov
, nr_segs
)) {
6054 * We're falling back to buffered, unlock the section we didn't
6057 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, offset
+ ret
,
6058 offset
+ iov_length(iov
, nr_segs
) - 1,
6059 EXTENT_LOCKED
| write_bits
, 1, 0,
6060 &cached_state
, GFP_NOFS
);
6063 free_extent_state(cached_state
);
6067 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
6068 __u64 start
, __u64 len
)
6070 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent
);
6073 int btrfs_readpage(struct file
*file
, struct page
*page
)
6075 struct extent_io_tree
*tree
;
6076 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6077 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
6080 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
6082 struct extent_io_tree
*tree
;
6085 if (current
->flags
& PF_MEMALLOC
) {
6086 redirty_page_for_writepage(wbc
, page
);
6090 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6091 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
6094 int btrfs_writepages(struct address_space
*mapping
,
6095 struct writeback_control
*wbc
)
6097 struct extent_io_tree
*tree
;
6099 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
6100 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
6104 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
6105 struct list_head
*pages
, unsigned nr_pages
)
6107 struct extent_io_tree
*tree
;
6108 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
6109 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
6112 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
6114 struct extent_io_tree
*tree
;
6115 struct extent_map_tree
*map
;
6118 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6119 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
6120 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
6122 ClearPagePrivate(page
);
6123 set_page_private(page
, 0);
6124 page_cache_release(page
);
6129 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
6131 if (PageWriteback(page
) || PageDirty(page
))
6133 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
6136 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
6138 struct extent_io_tree
*tree
;
6139 struct btrfs_ordered_extent
*ordered
;
6140 struct extent_state
*cached_state
= NULL
;
6141 u64 page_start
= page_offset(page
);
6142 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6146 * we have the page locked, so new writeback can't start,
6147 * and the dirty bit won't be cleared while we are here.
6149 * Wait for IO on this page so that we can safely clear
6150 * the PagePrivate2 bit and do ordered accounting
6152 wait_on_page_writeback(page
);
6154 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6156 btrfs_releasepage(page
, GFP_NOFS
);
6159 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
,
6161 ordered
= btrfs_lookup_ordered_extent(page
->mapping
->host
,
6165 * IO on this page will never be started, so we need
6166 * to account for any ordered extents now
6168 clear_extent_bit(tree
, page_start
, page_end
,
6169 EXTENT_DIRTY
| EXTENT_DELALLOC
|
6170 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
, 1, 0,
6171 &cached_state
, GFP_NOFS
);
6173 * whoever cleared the private bit is responsible
6174 * for the finish_ordered_io
6176 if (TestClearPagePrivate2(page
)) {
6177 btrfs_finish_ordered_io(page
->mapping
->host
,
6178 page_start
, page_end
);
6180 btrfs_put_ordered_extent(ordered
);
6181 cached_state
= NULL
;
6182 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
,
6185 clear_extent_bit(tree
, page_start
, page_end
,
6186 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
6187 EXTENT_DO_ACCOUNTING
, 1, 1, &cached_state
, GFP_NOFS
);
6188 __btrfs_releasepage(page
, GFP_NOFS
);
6190 ClearPageChecked(page
);
6191 if (PagePrivate(page
)) {
6192 ClearPagePrivate(page
);
6193 set_page_private(page
, 0);
6194 page_cache_release(page
);
6199 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6200 * called from a page fault handler when a page is first dirtied. Hence we must
6201 * be careful to check for EOF conditions here. We set the page up correctly
6202 * for a written page which means we get ENOSPC checking when writing into
6203 * holes and correct delalloc and unwritten extent mapping on filesystems that
6204 * support these features.
6206 * We are not allowed to take the i_mutex here so we have to play games to
6207 * protect against truncate races as the page could now be beyond EOF. Because
6208 * vmtruncate() writes the inode size before removing pages, once we have the
6209 * page lock we can determine safely if the page is beyond EOF. If it is not
6210 * beyond EOF, then the page is guaranteed safe against truncation until we
6213 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
6215 struct page
*page
= vmf
->page
;
6216 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
6217 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6218 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6219 struct btrfs_ordered_extent
*ordered
;
6220 struct extent_state
*cached_state
= NULL
;
6222 unsigned long zero_start
;
6228 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
6232 else /* -ENOSPC, -EIO, etc */
6233 ret
= VM_FAULT_SIGBUS
;
6237 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
6240 size
= i_size_read(inode
);
6241 page_start
= page_offset(page
);
6242 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6244 if ((page
->mapping
!= inode
->i_mapping
) ||
6245 (page_start
>= size
)) {
6246 /* page got truncated out from underneath us */
6249 wait_on_page_writeback(page
);
6251 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
,
6253 set_page_extent_mapped(page
);
6256 * we can't set the delalloc bits if there are pending ordered
6257 * extents. Drop our locks and wait for them to finish
6259 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
6261 unlock_extent_cached(io_tree
, page_start
, page_end
,
6262 &cached_state
, GFP_NOFS
);
6264 btrfs_start_ordered_extent(inode
, ordered
, 1);
6265 btrfs_put_ordered_extent(ordered
);
6270 * XXX - page_mkwrite gets called every time the page is dirtied, even
6271 * if it was already dirty, so for space accounting reasons we need to
6272 * clear any delalloc bits for the range we are fixing to save. There
6273 * is probably a better way to do this, but for now keep consistent with
6274 * prepare_pages in the normal write path.
6276 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
6277 EXTENT_DIRTY
| EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
,
6278 0, 0, &cached_state
, GFP_NOFS
);
6280 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
6283 unlock_extent_cached(io_tree
, page_start
, page_end
,
6284 &cached_state
, GFP_NOFS
);
6285 ret
= VM_FAULT_SIGBUS
;
6290 /* page is wholly or partially inside EOF */
6291 if (page_start
+ PAGE_CACHE_SIZE
> size
)
6292 zero_start
= size
& ~PAGE_CACHE_MASK
;
6294 zero_start
= PAGE_CACHE_SIZE
;
6296 if (zero_start
!= PAGE_CACHE_SIZE
) {
6298 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
6299 flush_dcache_page(page
);
6302 ClearPageChecked(page
);
6303 set_page_dirty(page
);
6304 SetPageUptodate(page
);
6306 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
6307 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
6309 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
6313 return VM_FAULT_LOCKED
;
6315 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
6320 static void btrfs_truncate(struct inode
*inode
)
6322 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6324 struct btrfs_trans_handle
*trans
;
6326 u64 mask
= root
->sectorsize
- 1;
6328 if (!S_ISREG(inode
->i_mode
)) {
6333 ret
= btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
6337 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
6338 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
6340 trans
= btrfs_start_transaction(root
, 0);
6341 BUG_ON(IS_ERR(trans
));
6342 btrfs_set_trans_block_group(trans
, inode
);
6343 trans
->block_rsv
= root
->orphan_block_rsv
;
6346 * setattr is responsible for setting the ordered_data_close flag,
6347 * but that is only tested during the last file release. That
6348 * could happen well after the next commit, leaving a great big
6349 * window where new writes may get lost if someone chooses to write
6350 * to this file after truncating to zero
6352 * The inode doesn't have any dirty data here, and so if we commit
6353 * this is a noop. If someone immediately starts writing to the inode
6354 * it is very likely we'll catch some of their writes in this
6355 * transaction, and the commit will find this file on the ordered
6356 * data list with good things to send down.
6358 * This is a best effort solution, there is still a window where
6359 * using truncate to replace the contents of the file will
6360 * end up with a zero length file after a crash.
6362 if (inode
->i_size
== 0 && BTRFS_I(inode
)->ordered_data_close
)
6363 btrfs_add_ordered_operation(trans
, root
, inode
);
6367 trans
= btrfs_start_transaction(root
, 0);
6368 BUG_ON(IS_ERR(trans
));
6369 btrfs_set_trans_block_group(trans
, inode
);
6370 trans
->block_rsv
= root
->orphan_block_rsv
;
6373 ret
= btrfs_block_rsv_check(trans
, root
,
6374 root
->orphan_block_rsv
, 0, 5);
6376 BUG_ON(ret
!= -EAGAIN
);
6377 ret
= btrfs_commit_transaction(trans
, root
);
6383 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
6385 BTRFS_EXTENT_DATA_KEY
);
6389 ret
= btrfs_update_inode(trans
, root
, inode
);
6392 nr
= trans
->blocks_used
;
6393 btrfs_end_transaction(trans
, root
);
6395 btrfs_btree_balance_dirty(root
, nr
);
6398 if (ret
== 0 && inode
->i_nlink
> 0) {
6399 ret
= btrfs_orphan_del(trans
, inode
);
6403 ret
= btrfs_update_inode(trans
, root
, inode
);
6406 nr
= trans
->blocks_used
;
6407 ret
= btrfs_end_transaction_throttle(trans
, root
);
6409 btrfs_btree_balance_dirty(root
, nr
);
6413 * create a new subvolume directory/inode (helper for the ioctl).
6415 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
6416 struct btrfs_root
*new_root
,
6417 u64 new_dirid
, u64 alloc_hint
)
6419 struct inode
*inode
;
6423 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2, new_dirid
,
6424 new_dirid
, alloc_hint
, S_IFDIR
| 0700, &index
);
6426 return PTR_ERR(inode
);
6427 inode
->i_op
= &btrfs_dir_inode_operations
;
6428 inode
->i_fop
= &btrfs_dir_file_operations
;
6431 btrfs_i_size_write(inode
, 0);
6433 err
= btrfs_update_inode(trans
, new_root
, inode
);
6440 /* helper function for file defrag and space balancing. This
6441 * forces readahead on a given range of bytes in an inode
6443 unsigned long btrfs_force_ra(struct address_space
*mapping
,
6444 struct file_ra_state
*ra
, struct file
*file
,
6445 pgoff_t offset
, pgoff_t last_index
)
6447 pgoff_t req_size
= last_index
- offset
+ 1;
6449 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
6450 return offset
+ req_size
;
6453 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
6455 struct btrfs_inode
*ei
;
6456 struct inode
*inode
;
6458 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
6463 ei
->space_info
= NULL
;
6467 ei
->last_sub_trans
= 0;
6468 ei
->logged_trans
= 0;
6469 ei
->delalloc_bytes
= 0;
6470 ei
->reserved_bytes
= 0;
6471 ei
->disk_i_size
= 0;
6473 ei
->index_cnt
= (u64
)-1;
6474 ei
->last_unlink_trans
= 0;
6476 spin_lock_init(&ei
->accounting_lock
);
6477 atomic_set(&ei
->outstanding_extents
, 0);
6478 ei
->reserved_extents
= 0;
6480 ei
->ordered_data_close
= 0;
6481 ei
->orphan_meta_reserved
= 0;
6482 ei
->dummy_inode
= 0;
6483 ei
->force_compress
= 0;
6485 inode
= &ei
->vfs_inode
;
6486 extent_map_tree_init(&ei
->extent_tree
, GFP_NOFS
);
6487 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
, GFP_NOFS
);
6488 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
, GFP_NOFS
);
6489 mutex_init(&ei
->log_mutex
);
6490 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
6491 INIT_LIST_HEAD(&ei
->i_orphan
);
6492 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
6493 INIT_LIST_HEAD(&ei
->ordered_operations
);
6494 RB_CLEAR_NODE(&ei
->rb_node
);
6499 static void btrfs_i_callback(struct rcu_head
*head
)
6501 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
6502 INIT_LIST_HEAD(&inode
->i_dentry
);
6503 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
6506 void btrfs_destroy_inode(struct inode
*inode
)
6508 struct btrfs_ordered_extent
*ordered
;
6509 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6511 WARN_ON(!list_empty(&inode
->i_dentry
));
6512 WARN_ON(inode
->i_data
.nrpages
);
6513 WARN_ON(atomic_read(&BTRFS_I(inode
)->outstanding_extents
));
6514 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
6517 * This can happen where we create an inode, but somebody else also
6518 * created the same inode and we need to destroy the one we already
6525 * Make sure we're properly removed from the ordered operation
6529 if (!list_empty(&BTRFS_I(inode
)->ordered_operations
)) {
6530 spin_lock(&root
->fs_info
->ordered_extent_lock
);
6531 list_del_init(&BTRFS_I(inode
)->ordered_operations
);
6532 spin_unlock(&root
->fs_info
->ordered_extent_lock
);
6535 if (root
== root
->fs_info
->tree_root
) {
6536 struct btrfs_block_group_cache
*block_group
;
6538 block_group
= btrfs_lookup_block_group(root
->fs_info
,
6539 BTRFS_I(inode
)->block_group
);
6540 if (block_group
&& block_group
->inode
== inode
) {
6541 spin_lock(&block_group
->lock
);
6542 block_group
->inode
= NULL
;
6543 spin_unlock(&block_group
->lock
);
6544 btrfs_put_block_group(block_group
);
6545 } else if (block_group
) {
6546 btrfs_put_block_group(block_group
);
6550 spin_lock(&root
->orphan_lock
);
6551 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
6552 printk(KERN_INFO
"BTRFS: inode %lu still on the orphan list\n",
6554 list_del_init(&BTRFS_I(inode
)->i_orphan
);
6556 spin_unlock(&root
->orphan_lock
);
6559 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
6563 printk(KERN_ERR
"btrfs found ordered "
6564 "extent %llu %llu on inode cleanup\n",
6565 (unsigned long long)ordered
->file_offset
,
6566 (unsigned long long)ordered
->len
);
6567 btrfs_remove_ordered_extent(inode
, ordered
);
6568 btrfs_put_ordered_extent(ordered
);
6569 btrfs_put_ordered_extent(ordered
);
6572 inode_tree_del(inode
);
6573 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
6575 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
6578 int btrfs_drop_inode(struct inode
*inode
)
6580 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6582 if (btrfs_root_refs(&root
->root_item
) == 0 &&
6583 root
!= root
->fs_info
->tree_root
)
6586 return generic_drop_inode(inode
);
6589 static void init_once(void *foo
)
6591 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
6593 inode_init_once(&ei
->vfs_inode
);
6596 void btrfs_destroy_cachep(void)
6598 if (btrfs_inode_cachep
)
6599 kmem_cache_destroy(btrfs_inode_cachep
);
6600 if (btrfs_trans_handle_cachep
)
6601 kmem_cache_destroy(btrfs_trans_handle_cachep
);
6602 if (btrfs_transaction_cachep
)
6603 kmem_cache_destroy(btrfs_transaction_cachep
);
6604 if (btrfs_path_cachep
)
6605 kmem_cache_destroy(btrfs_path_cachep
);
6608 int btrfs_init_cachep(void)
6610 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode_cache",
6611 sizeof(struct btrfs_inode
), 0,
6612 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
6613 if (!btrfs_inode_cachep
)
6616 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle_cache",
6617 sizeof(struct btrfs_trans_handle
), 0,
6618 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6619 if (!btrfs_trans_handle_cachep
)
6622 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction_cache",
6623 sizeof(struct btrfs_transaction
), 0,
6624 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6625 if (!btrfs_transaction_cachep
)
6628 btrfs_path_cachep
= kmem_cache_create("btrfs_path_cache",
6629 sizeof(struct btrfs_path
), 0,
6630 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6631 if (!btrfs_path_cachep
)
6636 btrfs_destroy_cachep();
6640 static int btrfs_getattr(struct vfsmount
*mnt
,
6641 struct dentry
*dentry
, struct kstat
*stat
)
6643 struct inode
*inode
= dentry
->d_inode
;
6644 generic_fillattr(inode
, stat
);
6645 stat
->dev
= BTRFS_I(inode
)->root
->anon_super
.s_dev
;
6646 stat
->blksize
= PAGE_CACHE_SIZE
;
6647 stat
->blocks
= (inode_get_bytes(inode
) +
6648 BTRFS_I(inode
)->delalloc_bytes
) >> 9;
6652 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
6653 struct inode
*new_dir
, struct dentry
*new_dentry
)
6655 struct btrfs_trans_handle
*trans
;
6656 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
6657 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
6658 struct inode
*new_inode
= new_dentry
->d_inode
;
6659 struct inode
*old_inode
= old_dentry
->d_inode
;
6660 struct timespec ctime
= CURRENT_TIME
;
6665 if (new_dir
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
6668 /* we only allow rename subvolume link between subvolumes */
6669 if (old_inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
6672 if (old_inode
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
6673 (new_inode
&& new_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
))
6676 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
6677 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
6680 * we're using rename to replace one file with another.
6681 * and the replacement file is large. Start IO on it now so
6682 * we don't add too much work to the end of the transaction
6684 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
&&
6685 old_inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
6686 filemap_flush(old_inode
->i_mapping
);
6688 /* close the racy window with snapshot create/destroy ioctl */
6689 if (old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)
6690 down_read(&root
->fs_info
->subvol_sem
);
6692 * We want to reserve the absolute worst case amount of items. So if
6693 * both inodes are subvols and we need to unlink them then that would
6694 * require 4 item modifications, but if they are both normal inodes it
6695 * would require 5 item modifications, so we'll assume their normal
6696 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
6697 * should cover the worst case number of items we'll modify.
6699 trans
= btrfs_start_transaction(root
, 20);
6701 return PTR_ERR(trans
);
6703 btrfs_set_trans_block_group(trans
, new_dir
);
6706 btrfs_record_root_in_trans(trans
, dest
);
6708 ret
= btrfs_set_inode_index(new_dir
, &index
);
6712 if (unlikely(old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6713 /* force full log commit if subvolume involved. */
6714 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
6716 ret
= btrfs_insert_inode_ref(trans
, dest
,
6717 new_dentry
->d_name
.name
,
6718 new_dentry
->d_name
.len
,
6720 new_dir
->i_ino
, index
);
6724 * this is an ugly little race, but the rename is required
6725 * to make sure that if we crash, the inode is either at the
6726 * old name or the new one. pinning the log transaction lets
6727 * us make sure we don't allow a log commit to come in after
6728 * we unlink the name but before we add the new name back in.
6730 btrfs_pin_log_trans(root
);
6733 * make sure the inode gets flushed if it is replacing
6736 if (new_inode
&& new_inode
->i_size
&&
6737 old_inode
&& S_ISREG(old_inode
->i_mode
)) {
6738 btrfs_add_ordered_operation(trans
, root
, old_inode
);
6741 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
6742 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
6743 old_inode
->i_ctime
= ctime
;
6745 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
6746 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
6748 if (unlikely(old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6749 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
6750 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
6751 old_dentry
->d_name
.name
,
6752 old_dentry
->d_name
.len
);
6754 btrfs_inc_nlink(old_dentry
->d_inode
);
6755 ret
= btrfs_unlink_inode(trans
, root
, old_dir
,
6756 old_dentry
->d_inode
,
6757 old_dentry
->d_name
.name
,
6758 old_dentry
->d_name
.len
);
6763 new_inode
->i_ctime
= CURRENT_TIME
;
6764 if (unlikely(new_inode
->i_ino
==
6765 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
6766 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
6767 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
6769 new_dentry
->d_name
.name
,
6770 new_dentry
->d_name
.len
);
6771 BUG_ON(new_inode
->i_nlink
== 0);
6773 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
6774 new_dentry
->d_inode
,
6775 new_dentry
->d_name
.name
,
6776 new_dentry
->d_name
.len
);
6779 if (new_inode
->i_nlink
== 0) {
6780 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
6785 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
6786 new_dentry
->d_name
.name
,
6787 new_dentry
->d_name
.len
, 0, index
);
6790 if (old_inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
6791 struct dentry
*parent
= dget_parent(new_dentry
);
6792 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
6794 btrfs_end_log_trans(root
);
6797 btrfs_end_transaction_throttle(trans
, root
);
6799 if (old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)
6800 up_read(&root
->fs_info
->subvol_sem
);
6806 * some fairly slow code that needs optimization. This walks the list
6807 * of all the inodes with pending delalloc and forces them to disk.
6809 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
6811 struct list_head
*head
= &root
->fs_info
->delalloc_inodes
;
6812 struct btrfs_inode
*binode
;
6813 struct inode
*inode
;
6815 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
6818 spin_lock(&root
->fs_info
->delalloc_lock
);
6819 while (!list_empty(head
)) {
6820 binode
= list_entry(head
->next
, struct btrfs_inode
,
6822 inode
= igrab(&binode
->vfs_inode
);
6824 list_del_init(&binode
->delalloc_inodes
);
6825 spin_unlock(&root
->fs_info
->delalloc_lock
);
6827 filemap_flush(inode
->i_mapping
);
6829 btrfs_add_delayed_iput(inode
);
6834 spin_lock(&root
->fs_info
->delalloc_lock
);
6836 spin_unlock(&root
->fs_info
->delalloc_lock
);
6838 /* the filemap_flush will queue IO into the worker threads, but
6839 * we have to make sure the IO is actually started and that
6840 * ordered extents get created before we return
6842 atomic_inc(&root
->fs_info
->async_submit_draining
);
6843 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
6844 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
6845 wait_event(root
->fs_info
->async_submit_wait
,
6846 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
6847 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
6849 atomic_dec(&root
->fs_info
->async_submit_draining
);
6853 int btrfs_start_one_delalloc_inode(struct btrfs_root
*root
, int delay_iput
,
6856 struct btrfs_inode
*binode
;
6857 struct inode
*inode
= NULL
;
6859 spin_lock(&root
->fs_info
->delalloc_lock
);
6860 while (!list_empty(&root
->fs_info
->delalloc_inodes
)) {
6861 binode
= list_entry(root
->fs_info
->delalloc_inodes
.next
,
6862 struct btrfs_inode
, delalloc_inodes
);
6863 inode
= igrab(&binode
->vfs_inode
);
6865 list_move_tail(&binode
->delalloc_inodes
,
6866 &root
->fs_info
->delalloc_inodes
);
6870 list_del_init(&binode
->delalloc_inodes
);
6871 cond_resched_lock(&root
->fs_info
->delalloc_lock
);
6873 spin_unlock(&root
->fs_info
->delalloc_lock
);
6877 filemap_write_and_wait(inode
->i_mapping
);
6879 * We have to do this because compression doesn't
6880 * actually set PG_writeback until it submits the pages
6881 * for IO, which happens in an async thread, so we could
6882 * race and not actually wait for any writeback pages
6883 * because they've not been submitted yet. Technically
6884 * this could still be the case for the ordered stuff
6885 * since the async thread may not have started to do its
6886 * work yet. If this becomes the case then we need to
6887 * figure out a way to make sure that in writepage we
6888 * wait for any async pages to be submitted before
6889 * returning so that fdatawait does what its supposed to
6892 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
6894 filemap_flush(inode
->i_mapping
);
6897 btrfs_add_delayed_iput(inode
);
6905 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
6906 const char *symname
)
6908 struct btrfs_trans_handle
*trans
;
6909 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6910 struct btrfs_path
*path
;
6911 struct btrfs_key key
;
6912 struct inode
*inode
= NULL
;
6920 struct btrfs_file_extent_item
*ei
;
6921 struct extent_buffer
*leaf
;
6922 unsigned long nr
= 0;
6924 name_len
= strlen(symname
) + 1;
6925 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
6926 return -ENAMETOOLONG
;
6928 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
6932 * 2 items for inode item and ref
6933 * 2 items for dir items
6934 * 1 item for xattr if selinux is on
6936 trans
= btrfs_start_transaction(root
, 5);
6938 return PTR_ERR(trans
);
6940 btrfs_set_trans_block_group(trans
, dir
);
6942 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6943 dentry
->d_name
.len
, dir
->i_ino
, objectid
,
6944 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
,
6946 err
= PTR_ERR(inode
);
6950 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6956 btrfs_set_trans_block_group(trans
, inode
);
6957 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
6961 inode
->i_mapping
->a_ops
= &btrfs_aops
;
6962 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
6963 inode
->i_fop
= &btrfs_file_operations
;
6964 inode
->i_op
= &btrfs_file_inode_operations
;
6965 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
6967 btrfs_update_inode_block_group(trans
, inode
);
6968 btrfs_update_inode_block_group(trans
, dir
);
6972 path
= btrfs_alloc_path();
6974 key
.objectid
= inode
->i_ino
;
6976 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
6977 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
6978 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
6984 leaf
= path
->nodes
[0];
6985 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
6986 struct btrfs_file_extent_item
);
6987 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
6988 btrfs_set_file_extent_type(leaf
, ei
,
6989 BTRFS_FILE_EXTENT_INLINE
);
6990 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
6991 btrfs_set_file_extent_compression(leaf
, ei
, 0);
6992 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
6993 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
6995 ptr
= btrfs_file_extent_inline_start(ei
);
6996 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
6997 btrfs_mark_buffer_dirty(leaf
);
6998 btrfs_free_path(path
);
7000 inode
->i_op
= &btrfs_symlink_inode_operations
;
7001 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
7002 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
7003 inode_set_bytes(inode
, name_len
);
7004 btrfs_i_size_write(inode
, name_len
- 1);
7005 err
= btrfs_update_inode(trans
, root
, inode
);
7010 nr
= trans
->blocks_used
;
7011 btrfs_end_transaction_throttle(trans
, root
);
7013 inode_dec_link_count(inode
);
7016 btrfs_btree_balance_dirty(root
, nr
);
7020 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
7021 u64 start
, u64 num_bytes
, u64 min_size
,
7022 loff_t actual_len
, u64
*alloc_hint
,
7023 struct btrfs_trans_handle
*trans
)
7025 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7026 struct btrfs_key ins
;
7027 u64 cur_offset
= start
;
7030 bool own_trans
= true;
7034 while (num_bytes
> 0) {
7036 trans
= btrfs_start_transaction(root
, 3);
7037 if (IS_ERR(trans
)) {
7038 ret
= PTR_ERR(trans
);
7043 ret
= btrfs_reserve_extent(trans
, root
, num_bytes
, min_size
,
7044 0, *alloc_hint
, (u64
)-1, &ins
, 1);
7047 btrfs_end_transaction(trans
, root
);
7051 ret
= insert_reserved_file_extent(trans
, inode
,
7052 cur_offset
, ins
.objectid
,
7053 ins
.offset
, ins
.offset
,
7054 ins
.offset
, 0, 0, 0,
7055 BTRFS_FILE_EXTENT_PREALLOC
);
7057 btrfs_drop_extent_cache(inode
, cur_offset
,
7058 cur_offset
+ ins
.offset
-1, 0);
7060 num_bytes
-= ins
.offset
;
7061 cur_offset
+= ins
.offset
;
7062 *alloc_hint
= ins
.objectid
+ ins
.offset
;
7064 inode
->i_ctime
= CURRENT_TIME
;
7065 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
7066 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
7067 (actual_len
> inode
->i_size
) &&
7068 (cur_offset
> inode
->i_size
)) {
7069 if (cur_offset
> actual_len
)
7070 i_size
= actual_len
;
7072 i_size
= cur_offset
;
7073 i_size_write(inode
, i_size
);
7074 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
7077 ret
= btrfs_update_inode(trans
, root
, inode
);
7081 btrfs_end_transaction(trans
, root
);
7086 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
7087 u64 start
, u64 num_bytes
, u64 min_size
,
7088 loff_t actual_len
, u64
*alloc_hint
)
7090 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
7091 min_size
, actual_len
, alloc_hint
,
7095 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
7096 struct btrfs_trans_handle
*trans
, int mode
,
7097 u64 start
, u64 num_bytes
, u64 min_size
,
7098 loff_t actual_len
, u64
*alloc_hint
)
7100 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
7101 min_size
, actual_len
, alloc_hint
, trans
);
7104 static long btrfs_fallocate(struct inode
*inode
, int mode
,
7105 loff_t offset
, loff_t len
)
7107 struct extent_state
*cached_state
= NULL
;
7114 u64 mask
= BTRFS_I(inode
)->root
->sectorsize
- 1;
7115 struct extent_map
*em
;
7118 alloc_start
= offset
& ~mask
;
7119 alloc_end
= (offset
+ len
+ mask
) & ~mask
;
7122 * wait for ordered IO before we have any locks. We'll loop again
7123 * below with the locks held.
7125 btrfs_wait_ordered_range(inode
, alloc_start
, alloc_end
- alloc_start
);
7127 mutex_lock(&inode
->i_mutex
);
7128 ret
= inode_newsize_ok(inode
, alloc_end
);
7132 if (alloc_start
> inode
->i_size
) {
7133 ret
= btrfs_cont_expand(inode
, alloc_start
);
7138 ret
= btrfs_check_data_free_space(inode
, alloc_end
- alloc_start
);
7142 locked_end
= alloc_end
- 1;
7144 struct btrfs_ordered_extent
*ordered
;
7146 /* the extent lock is ordered inside the running
7149 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, alloc_start
,
7150 locked_end
, 0, &cached_state
, GFP_NOFS
);
7151 ordered
= btrfs_lookup_first_ordered_extent(inode
,
7154 ordered
->file_offset
+ ordered
->len
> alloc_start
&&
7155 ordered
->file_offset
< alloc_end
) {
7156 btrfs_put_ordered_extent(ordered
);
7157 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
7158 alloc_start
, locked_end
,
7159 &cached_state
, GFP_NOFS
);
7161 * we can't wait on the range with the transaction
7162 * running or with the extent lock held
7164 btrfs_wait_ordered_range(inode
, alloc_start
,
7165 alloc_end
- alloc_start
);
7168 btrfs_put_ordered_extent(ordered
);
7173 cur_offset
= alloc_start
;
7175 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
7176 alloc_end
- cur_offset
, 0);
7177 BUG_ON(IS_ERR(em
) || !em
);
7178 last_byte
= min(extent_map_end(em
), alloc_end
);
7179 last_byte
= (last_byte
+ mask
) & ~mask
;
7180 if (em
->block_start
== EXTENT_MAP_HOLE
||
7181 (cur_offset
>= inode
->i_size
&&
7182 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
7183 ret
= btrfs_prealloc_file_range(inode
, mode
, cur_offset
,
7184 last_byte
- cur_offset
,
7185 1 << inode
->i_blkbits
,
7189 free_extent_map(em
);
7193 free_extent_map(em
);
7195 cur_offset
= last_byte
;
7196 if (cur_offset
>= alloc_end
) {
7201 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, alloc_start
, locked_end
,
7202 &cached_state
, GFP_NOFS
);
7204 btrfs_free_reserved_data_space(inode
, alloc_end
- alloc_start
);
7206 mutex_unlock(&inode
->i_mutex
);
7210 static int btrfs_set_page_dirty(struct page
*page
)
7212 return __set_page_dirty_nobuffers(page
);
7215 static int btrfs_permission(struct inode
*inode
, int mask
, unsigned int flags
)
7217 if ((BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
) && (mask
& MAY_WRITE
))
7219 return generic_permission(inode
, mask
, flags
, btrfs_check_acl
);
7222 static const struct inode_operations btrfs_dir_inode_operations
= {
7223 .getattr
= btrfs_getattr
,
7224 .lookup
= btrfs_lookup
,
7225 .create
= btrfs_create
,
7226 .unlink
= btrfs_unlink
,
7228 .mkdir
= btrfs_mkdir
,
7229 .rmdir
= btrfs_rmdir
,
7230 .rename
= btrfs_rename
,
7231 .symlink
= btrfs_symlink
,
7232 .setattr
= btrfs_setattr
,
7233 .mknod
= btrfs_mknod
,
7234 .setxattr
= btrfs_setxattr
,
7235 .getxattr
= btrfs_getxattr
,
7236 .listxattr
= btrfs_listxattr
,
7237 .removexattr
= btrfs_removexattr
,
7238 .permission
= btrfs_permission
,
7240 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
7241 .lookup
= btrfs_lookup
,
7242 .permission
= btrfs_permission
,
7245 static const struct file_operations btrfs_dir_file_operations
= {
7246 .llseek
= generic_file_llseek
,
7247 .read
= generic_read_dir
,
7248 .readdir
= btrfs_real_readdir
,
7249 .unlocked_ioctl
= btrfs_ioctl
,
7250 #ifdef CONFIG_COMPAT
7251 .compat_ioctl
= btrfs_ioctl
,
7253 .release
= btrfs_release_file
,
7254 .fsync
= btrfs_sync_file
,
7257 static struct extent_io_ops btrfs_extent_io_ops
= {
7258 .fill_delalloc
= run_delalloc_range
,
7259 .submit_bio_hook
= btrfs_submit_bio_hook
,
7260 .merge_bio_hook
= btrfs_merge_bio_hook
,
7261 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
7262 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
7263 .writepage_start_hook
= btrfs_writepage_start_hook
,
7264 .readpage_io_failed_hook
= btrfs_io_failed_hook
,
7265 .set_bit_hook
= btrfs_set_bit_hook
,
7266 .clear_bit_hook
= btrfs_clear_bit_hook
,
7267 .merge_extent_hook
= btrfs_merge_extent_hook
,
7268 .split_extent_hook
= btrfs_split_extent_hook
,
7272 * btrfs doesn't support the bmap operation because swapfiles
7273 * use bmap to make a mapping of extents in the file. They assume
7274 * these extents won't change over the life of the file and they
7275 * use the bmap result to do IO directly to the drive.
7277 * the btrfs bmap call would return logical addresses that aren't
7278 * suitable for IO and they also will change frequently as COW
7279 * operations happen. So, swapfile + btrfs == corruption.
7281 * For now we're avoiding this by dropping bmap.
7283 static const struct address_space_operations btrfs_aops
= {
7284 .readpage
= btrfs_readpage
,
7285 .writepage
= btrfs_writepage
,
7286 .writepages
= btrfs_writepages
,
7287 .readpages
= btrfs_readpages
,
7288 .sync_page
= block_sync_page
,
7289 .direct_IO
= btrfs_direct_IO
,
7290 .invalidatepage
= btrfs_invalidatepage
,
7291 .releasepage
= btrfs_releasepage
,
7292 .set_page_dirty
= btrfs_set_page_dirty
,
7293 .error_remove_page
= generic_error_remove_page
,
7296 static const struct address_space_operations btrfs_symlink_aops
= {
7297 .readpage
= btrfs_readpage
,
7298 .writepage
= btrfs_writepage
,
7299 .invalidatepage
= btrfs_invalidatepage
,
7300 .releasepage
= btrfs_releasepage
,
7303 static const struct inode_operations btrfs_file_inode_operations
= {
7304 .truncate
= btrfs_truncate
,
7305 .getattr
= btrfs_getattr
,
7306 .setattr
= btrfs_setattr
,
7307 .setxattr
= btrfs_setxattr
,
7308 .getxattr
= btrfs_getxattr
,
7309 .listxattr
= btrfs_listxattr
,
7310 .removexattr
= btrfs_removexattr
,
7311 .permission
= btrfs_permission
,
7312 .fallocate
= btrfs_fallocate
,
7313 .fiemap
= btrfs_fiemap
,
7315 static const struct inode_operations btrfs_special_inode_operations
= {
7316 .getattr
= btrfs_getattr
,
7317 .setattr
= btrfs_setattr
,
7318 .permission
= btrfs_permission
,
7319 .setxattr
= btrfs_setxattr
,
7320 .getxattr
= btrfs_getxattr
,
7321 .listxattr
= btrfs_listxattr
,
7322 .removexattr
= btrfs_removexattr
,
7324 static const struct inode_operations btrfs_symlink_inode_operations
= {
7325 .readlink
= generic_readlink
,
7326 .follow_link
= page_follow_link_light
,
7327 .put_link
= page_put_link
,
7328 .getattr
= btrfs_getattr
,
7329 .permission
= btrfs_permission
,
7330 .setxattr
= btrfs_setxattr
,
7331 .getxattr
= btrfs_getxattr
,
7332 .listxattr
= btrfs_listxattr
,
7333 .removexattr
= btrfs_removexattr
,
7336 const struct dentry_operations btrfs_dentry_operations
= {
7337 .d_delete
= btrfs_dentry_delete
,