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
)
97 err
= btrfs_init_acl(trans
, inode
, dir
);
99 err
= btrfs_xattr_security_init(trans
, inode
, dir
);
104 * this does all the hard work for inserting an inline extent into
105 * the btree. The caller should have done a btrfs_drop_extents so that
106 * no overlapping inline items exist in the btree
108 static noinline
int insert_inline_extent(struct btrfs_trans_handle
*trans
,
109 struct btrfs_root
*root
, struct inode
*inode
,
110 u64 start
, size_t size
, size_t compressed_size
,
111 struct page
**compressed_pages
)
113 struct btrfs_key key
;
114 struct btrfs_path
*path
;
115 struct extent_buffer
*leaf
;
116 struct page
*page
= NULL
;
119 struct btrfs_file_extent_item
*ei
;
122 size_t cur_size
= size
;
124 unsigned long offset
;
125 int use_compress
= 0;
127 if (compressed_size
&& compressed_pages
) {
129 cur_size
= compressed_size
;
132 path
= btrfs_alloc_path();
136 path
->leave_spinning
= 1;
137 btrfs_set_trans_block_group(trans
, inode
);
139 key
.objectid
= inode
->i_ino
;
141 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
142 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
144 inode_add_bytes(inode
, size
);
145 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
152 leaf
= path
->nodes
[0];
153 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
154 struct btrfs_file_extent_item
);
155 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
156 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
157 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
158 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
159 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
160 ptr
= btrfs_file_extent_inline_start(ei
);
165 while (compressed_size
> 0) {
166 cpage
= compressed_pages
[i
];
167 cur_size
= min_t(unsigned long, compressed_size
,
170 kaddr
= kmap_atomic(cpage
, KM_USER0
);
171 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
172 kunmap_atomic(kaddr
, KM_USER0
);
176 compressed_size
-= cur_size
;
178 btrfs_set_file_extent_compression(leaf
, ei
,
179 BTRFS_COMPRESS_ZLIB
);
181 page
= find_get_page(inode
->i_mapping
,
182 start
>> PAGE_CACHE_SHIFT
);
183 btrfs_set_file_extent_compression(leaf
, ei
, 0);
184 kaddr
= kmap_atomic(page
, KM_USER0
);
185 offset
= start
& (PAGE_CACHE_SIZE
- 1);
186 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
187 kunmap_atomic(kaddr
, KM_USER0
);
188 page_cache_release(page
);
190 btrfs_mark_buffer_dirty(leaf
);
191 btrfs_free_path(path
);
194 * we're an inline extent, so nobody can
195 * extend the file past i_size without locking
196 * a page we already have locked.
198 * We must do any isize and inode updates
199 * before we unlock the pages. Otherwise we
200 * could end up racing with unlink.
202 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
203 btrfs_update_inode(trans
, root
, inode
);
207 btrfs_free_path(path
);
213 * conditionally insert an inline extent into the file. This
214 * does the checks required to make sure the data is small enough
215 * to fit as an inline extent.
217 static noinline
int cow_file_range_inline(struct btrfs_trans_handle
*trans
,
218 struct btrfs_root
*root
,
219 struct inode
*inode
, u64 start
, u64 end
,
220 size_t compressed_size
,
221 struct page
**compressed_pages
)
223 u64 isize
= i_size_read(inode
);
224 u64 actual_end
= min(end
+ 1, isize
);
225 u64 inline_len
= actual_end
- start
;
226 u64 aligned_end
= (end
+ root
->sectorsize
- 1) &
227 ~((u64
)root
->sectorsize
- 1);
229 u64 data_len
= inline_len
;
233 data_len
= compressed_size
;
236 actual_end
>= PAGE_CACHE_SIZE
||
237 data_len
>= BTRFS_MAX_INLINE_DATA_SIZE(root
) ||
239 (actual_end
& (root
->sectorsize
- 1)) == 0) ||
241 data_len
> root
->fs_info
->max_inline
) {
245 ret
= btrfs_drop_extents(trans
, inode
, start
, aligned_end
,
249 if (isize
> actual_end
)
250 inline_len
= min_t(u64
, isize
, actual_end
);
251 ret
= insert_inline_extent(trans
, root
, inode
, start
,
252 inline_len
, compressed_size
,
255 btrfs_delalloc_release_metadata(inode
, end
+ 1 - start
);
256 btrfs_drop_extent_cache(inode
, start
, aligned_end
- 1, 0);
260 struct async_extent
{
265 unsigned long nr_pages
;
266 struct list_head list
;
271 struct btrfs_root
*root
;
272 struct page
*locked_page
;
275 struct list_head extents
;
276 struct btrfs_work work
;
279 static noinline
int add_async_extent(struct async_cow
*cow
,
280 u64 start
, u64 ram_size
,
283 unsigned long nr_pages
)
285 struct async_extent
*async_extent
;
287 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
288 async_extent
->start
= start
;
289 async_extent
->ram_size
= ram_size
;
290 async_extent
->compressed_size
= compressed_size
;
291 async_extent
->pages
= pages
;
292 async_extent
->nr_pages
= nr_pages
;
293 list_add_tail(&async_extent
->list
, &cow
->extents
);
298 * we create compressed extents in two phases. The first
299 * phase compresses a range of pages that have already been
300 * locked (both pages and state bits are locked).
302 * This is done inside an ordered work queue, and the compression
303 * is spread across many cpus. The actual IO submission is step
304 * two, and the ordered work queue takes care of making sure that
305 * happens in the same order things were put onto the queue by
306 * writepages and friends.
308 * If this code finds it can't get good compression, it puts an
309 * entry onto the work queue to write the uncompressed bytes. This
310 * makes sure that both compressed inodes and uncompressed inodes
311 * are written in the same order that pdflush sent them down.
313 static noinline
int compress_file_range(struct inode
*inode
,
314 struct page
*locked_page
,
316 struct async_cow
*async_cow
,
319 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
320 struct btrfs_trans_handle
*trans
;
324 u64 blocksize
= root
->sectorsize
;
326 u64 isize
= i_size_read(inode
);
328 struct page
**pages
= NULL
;
329 unsigned long nr_pages
;
330 unsigned long nr_pages_ret
= 0;
331 unsigned long total_compressed
= 0;
332 unsigned long total_in
= 0;
333 unsigned long max_compressed
= 128 * 1024;
334 unsigned long max_uncompressed
= 128 * 1024;
340 actual_end
= min_t(u64
, isize
, end
+ 1);
343 nr_pages
= (end
>> PAGE_CACHE_SHIFT
) - (start
>> PAGE_CACHE_SHIFT
) + 1;
344 nr_pages
= min(nr_pages
, (128 * 1024UL) / PAGE_CACHE_SIZE
);
347 * we don't want to send crud past the end of i_size through
348 * compression, that's just a waste of CPU time. So, if the
349 * end of the file is before the start of our current
350 * requested range of bytes, we bail out to the uncompressed
351 * cleanup code that can deal with all of this.
353 * It isn't really the fastest way to fix things, but this is a
354 * very uncommon corner.
356 if (actual_end
<= start
)
357 goto cleanup_and_bail_uncompressed
;
359 total_compressed
= actual_end
- start
;
361 /* we want to make sure that amount of ram required to uncompress
362 * an extent is reasonable, so we limit the total size in ram
363 * of a compressed extent to 128k. This is a crucial number
364 * because it also controls how easily we can spread reads across
365 * cpus for decompression.
367 * We also want to make sure the amount of IO required to do
368 * a random read is reasonably small, so we limit the size of
369 * a compressed extent to 128k.
371 total_compressed
= min(total_compressed
, max_uncompressed
);
372 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
373 num_bytes
= max(blocksize
, num_bytes
);
374 disk_num_bytes
= num_bytes
;
379 * we do compression for mount -o compress and when the
380 * inode has not been flagged as nocompress. This flag can
381 * change at any time if we discover bad compression ratios.
383 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
) &&
384 (btrfs_test_opt(root
, COMPRESS
) ||
385 (BTRFS_I(inode
)->force_compress
))) {
387 pages
= kzalloc(sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
389 ret
= btrfs_zlib_compress_pages(inode
->i_mapping
, start
,
390 total_compressed
, pages
,
391 nr_pages
, &nr_pages_ret
,
397 unsigned long offset
= total_compressed
&
398 (PAGE_CACHE_SIZE
- 1);
399 struct page
*page
= pages
[nr_pages_ret
- 1];
402 /* zero the tail end of the last page, we might be
403 * sending it down to disk
406 kaddr
= kmap_atomic(page
, KM_USER0
);
407 memset(kaddr
+ offset
, 0,
408 PAGE_CACHE_SIZE
- offset
);
409 kunmap_atomic(kaddr
, KM_USER0
);
415 trans
= btrfs_join_transaction(root
, 1);
417 btrfs_set_trans_block_group(trans
, inode
);
418 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
420 /* lets try to make an inline extent */
421 if (ret
|| total_in
< (actual_end
- start
)) {
422 /* we didn't compress the entire range, try
423 * to make an uncompressed inline extent.
425 ret
= cow_file_range_inline(trans
, root
, inode
,
426 start
, end
, 0, NULL
);
428 /* try making a compressed inline extent */
429 ret
= cow_file_range_inline(trans
, root
, inode
,
431 total_compressed
, pages
);
435 * inline extent creation worked, we don't need
436 * to create any more async work items. Unlock
437 * and free up our temp pages.
439 extent_clear_unlock_delalloc(inode
,
440 &BTRFS_I(inode
)->io_tree
,
442 EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
443 EXTENT_CLEAR_DELALLOC
|
444 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
);
446 btrfs_end_transaction(trans
, root
);
449 btrfs_end_transaction(trans
, root
);
454 * we aren't doing an inline extent round the compressed size
455 * up to a block size boundary so the allocator does sane
458 total_compressed
= (total_compressed
+ blocksize
- 1) &
462 * one last check to make sure the compression is really a
463 * win, compare the page count read with the blocks on disk
465 total_in
= (total_in
+ PAGE_CACHE_SIZE
- 1) &
466 ~(PAGE_CACHE_SIZE
- 1);
467 if (total_compressed
>= total_in
) {
470 disk_num_bytes
= total_compressed
;
471 num_bytes
= total_in
;
474 if (!will_compress
&& pages
) {
476 * the compression code ran but failed to make things smaller,
477 * free any pages it allocated and our page pointer array
479 for (i
= 0; i
< nr_pages_ret
; i
++) {
480 WARN_ON(pages
[i
]->mapping
);
481 page_cache_release(pages
[i
]);
485 total_compressed
= 0;
488 /* flag the file so we don't compress in the future */
489 if (!btrfs_test_opt(root
, FORCE_COMPRESS
) &&
490 !(BTRFS_I(inode
)->force_compress
)) {
491 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
497 /* the async work queues will take care of doing actual
498 * allocation on disk for these compressed pages,
499 * and will submit them to the elevator.
501 add_async_extent(async_cow
, start
, num_bytes
,
502 total_compressed
, pages
, nr_pages_ret
);
504 if (start
+ num_bytes
< end
&& start
+ num_bytes
< actual_end
) {
511 cleanup_and_bail_uncompressed
:
513 * No compression, but we still need to write the pages in
514 * the file we've been given so far. redirty the locked
515 * page if it corresponds to our extent and set things up
516 * for the async work queue to run cow_file_range to do
517 * the normal delalloc dance
519 if (page_offset(locked_page
) >= start
&&
520 page_offset(locked_page
) <= end
) {
521 __set_page_dirty_nobuffers(locked_page
);
522 /* unlocked later on in the async handlers */
524 add_async_extent(async_cow
, start
, end
- start
+ 1, 0, NULL
, 0);
532 for (i
= 0; i
< nr_pages_ret
; i
++) {
533 WARN_ON(pages
[i
]->mapping
);
534 page_cache_release(pages
[i
]);
542 * phase two of compressed writeback. This is the ordered portion
543 * of the code, which only gets called in the order the work was
544 * queued. We walk all the async extents created by compress_file_range
545 * and send them down to the disk.
547 static noinline
int submit_compressed_extents(struct inode
*inode
,
548 struct async_cow
*async_cow
)
550 struct async_extent
*async_extent
;
552 struct btrfs_trans_handle
*trans
;
553 struct btrfs_key ins
;
554 struct extent_map
*em
;
555 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
556 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
557 struct extent_io_tree
*io_tree
;
560 if (list_empty(&async_cow
->extents
))
564 while (!list_empty(&async_cow
->extents
)) {
565 async_extent
= list_entry(async_cow
->extents
.next
,
566 struct async_extent
, list
);
567 list_del(&async_extent
->list
);
569 io_tree
= &BTRFS_I(inode
)->io_tree
;
572 /* did the compression code fall back to uncompressed IO? */
573 if (!async_extent
->pages
) {
574 int page_started
= 0;
575 unsigned long nr_written
= 0;
577 lock_extent(io_tree
, async_extent
->start
,
578 async_extent
->start
+
579 async_extent
->ram_size
- 1, GFP_NOFS
);
581 /* allocate blocks */
582 ret
= cow_file_range(inode
, async_cow
->locked_page
,
584 async_extent
->start
+
585 async_extent
->ram_size
- 1,
586 &page_started
, &nr_written
, 0);
589 * if page_started, cow_file_range inserted an
590 * inline extent and took care of all the unlocking
591 * and IO for us. Otherwise, we need to submit
592 * all those pages down to the drive.
594 if (!page_started
&& !ret
)
595 extent_write_locked_range(io_tree
,
596 inode
, async_extent
->start
,
597 async_extent
->start
+
598 async_extent
->ram_size
- 1,
606 lock_extent(io_tree
, async_extent
->start
,
607 async_extent
->start
+ async_extent
->ram_size
- 1,
610 trans
= btrfs_join_transaction(root
, 1);
611 ret
= btrfs_reserve_extent(trans
, root
,
612 async_extent
->compressed_size
,
613 async_extent
->compressed_size
,
616 btrfs_end_transaction(trans
, root
);
620 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
621 WARN_ON(async_extent
->pages
[i
]->mapping
);
622 page_cache_release(async_extent
->pages
[i
]);
624 kfree(async_extent
->pages
);
625 async_extent
->nr_pages
= 0;
626 async_extent
->pages
= NULL
;
627 unlock_extent(io_tree
, async_extent
->start
,
628 async_extent
->start
+
629 async_extent
->ram_size
- 1, GFP_NOFS
);
634 * here we're doing allocation and writeback of the
637 btrfs_drop_extent_cache(inode
, async_extent
->start
,
638 async_extent
->start
+
639 async_extent
->ram_size
- 1, 0);
641 em
= alloc_extent_map(GFP_NOFS
);
642 em
->start
= async_extent
->start
;
643 em
->len
= async_extent
->ram_size
;
644 em
->orig_start
= em
->start
;
646 em
->block_start
= ins
.objectid
;
647 em
->block_len
= ins
.offset
;
648 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
649 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
650 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
653 write_lock(&em_tree
->lock
);
654 ret
= add_extent_mapping(em_tree
, em
);
655 write_unlock(&em_tree
->lock
);
656 if (ret
!= -EEXIST
) {
660 btrfs_drop_extent_cache(inode
, async_extent
->start
,
661 async_extent
->start
+
662 async_extent
->ram_size
- 1, 0);
665 ret
= btrfs_add_ordered_extent(inode
, async_extent
->start
,
667 async_extent
->ram_size
,
669 BTRFS_ORDERED_COMPRESSED
);
673 * clear dirty, set writeback and unlock the pages.
675 extent_clear_unlock_delalloc(inode
,
676 &BTRFS_I(inode
)->io_tree
,
678 async_extent
->start
+
679 async_extent
->ram_size
- 1,
680 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
681 EXTENT_CLEAR_UNLOCK
|
682 EXTENT_CLEAR_DELALLOC
|
683 EXTENT_CLEAR_DIRTY
| EXTENT_SET_WRITEBACK
);
685 ret
= btrfs_submit_compressed_write(inode
,
687 async_extent
->ram_size
,
689 ins
.offset
, async_extent
->pages
,
690 async_extent
->nr_pages
);
693 alloc_hint
= ins
.objectid
+ ins
.offset
;
701 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
704 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
705 struct extent_map
*em
;
708 read_lock(&em_tree
->lock
);
709 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
712 * if block start isn't an actual block number then find the
713 * first block in this inode and use that as a hint. If that
714 * block is also bogus then just don't worry about it.
716 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
718 em
= search_extent_mapping(em_tree
, 0, 0);
719 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
720 alloc_hint
= em
->block_start
;
724 alloc_hint
= em
->block_start
;
728 read_unlock(&em_tree
->lock
);
734 * when extent_io.c finds a delayed allocation range in the file,
735 * the call backs end up in this code. The basic idea is to
736 * allocate extents on disk for the range, and create ordered data structs
737 * in ram to track those extents.
739 * locked_page is the page that writepage had locked already. We use
740 * it to make sure we don't do extra locks or unlocks.
742 * *page_started is set to one if we unlock locked_page and do everything
743 * required to start IO on it. It may be clean and already done with
746 static noinline
int cow_file_range(struct inode
*inode
,
747 struct page
*locked_page
,
748 u64 start
, u64 end
, int *page_started
,
749 unsigned long *nr_written
,
752 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
753 struct btrfs_trans_handle
*trans
;
756 unsigned long ram_size
;
759 u64 blocksize
= root
->sectorsize
;
761 u64 isize
= i_size_read(inode
);
762 struct btrfs_key ins
;
763 struct extent_map
*em
;
764 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
767 BUG_ON(root
== root
->fs_info
->tree_root
);
768 trans
= btrfs_join_transaction(root
, 1);
770 btrfs_set_trans_block_group(trans
, inode
);
771 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
773 actual_end
= min_t(u64
, isize
, end
+ 1);
775 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
776 num_bytes
= max(blocksize
, num_bytes
);
777 disk_num_bytes
= num_bytes
;
781 /* lets try to make an inline extent */
782 ret
= cow_file_range_inline(trans
, root
, inode
,
783 start
, end
, 0, NULL
);
785 extent_clear_unlock_delalloc(inode
,
786 &BTRFS_I(inode
)->io_tree
,
788 EXTENT_CLEAR_UNLOCK_PAGE
|
789 EXTENT_CLEAR_UNLOCK
|
790 EXTENT_CLEAR_DELALLOC
|
792 EXTENT_SET_WRITEBACK
|
793 EXTENT_END_WRITEBACK
);
795 *nr_written
= *nr_written
+
796 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
803 BUG_ON(disk_num_bytes
>
804 btrfs_super_total_bytes(&root
->fs_info
->super_copy
));
806 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
807 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
809 while (disk_num_bytes
> 0) {
812 cur_alloc_size
= disk_num_bytes
;
813 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
814 root
->sectorsize
, 0, alloc_hint
,
818 em
= alloc_extent_map(GFP_NOFS
);
820 em
->orig_start
= em
->start
;
821 ram_size
= ins
.offset
;
822 em
->len
= ins
.offset
;
824 em
->block_start
= ins
.objectid
;
825 em
->block_len
= ins
.offset
;
826 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
827 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
830 write_lock(&em_tree
->lock
);
831 ret
= add_extent_mapping(em_tree
, em
);
832 write_unlock(&em_tree
->lock
);
833 if (ret
!= -EEXIST
) {
837 btrfs_drop_extent_cache(inode
, start
,
838 start
+ ram_size
- 1, 0);
841 cur_alloc_size
= ins
.offset
;
842 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
843 ram_size
, cur_alloc_size
, 0);
846 if (root
->root_key
.objectid
==
847 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
848 ret
= btrfs_reloc_clone_csums(inode
, start
,
853 if (disk_num_bytes
< cur_alloc_size
)
856 /* we're not doing compressed IO, don't unlock the first
857 * page (which the caller expects to stay locked), don't
858 * clear any dirty bits and don't set any writeback bits
860 * Do set the Private2 bit so we know this page was properly
861 * setup for writepage
863 op
= unlock
? EXTENT_CLEAR_UNLOCK_PAGE
: 0;
864 op
|= EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
867 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
868 start
, start
+ ram_size
- 1,
870 disk_num_bytes
-= cur_alloc_size
;
871 num_bytes
-= cur_alloc_size
;
872 alloc_hint
= ins
.objectid
+ ins
.offset
;
873 start
+= cur_alloc_size
;
877 btrfs_end_transaction(trans
, root
);
883 * work queue call back to started compression on a file and pages
885 static noinline
void async_cow_start(struct btrfs_work
*work
)
887 struct async_cow
*async_cow
;
889 async_cow
= container_of(work
, struct async_cow
, work
);
891 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
892 async_cow
->start
, async_cow
->end
, async_cow
,
895 async_cow
->inode
= NULL
;
899 * work queue call back to submit previously compressed pages
901 static noinline
void async_cow_submit(struct btrfs_work
*work
)
903 struct async_cow
*async_cow
;
904 struct btrfs_root
*root
;
905 unsigned long nr_pages
;
907 async_cow
= container_of(work
, struct async_cow
, work
);
909 root
= async_cow
->root
;
910 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
913 atomic_sub(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
915 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
917 waitqueue_active(&root
->fs_info
->async_submit_wait
))
918 wake_up(&root
->fs_info
->async_submit_wait
);
920 if (async_cow
->inode
)
921 submit_compressed_extents(async_cow
->inode
, async_cow
);
924 static noinline
void async_cow_free(struct btrfs_work
*work
)
926 struct async_cow
*async_cow
;
927 async_cow
= container_of(work
, struct async_cow
, work
);
931 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
932 u64 start
, u64 end
, int *page_started
,
933 unsigned long *nr_written
)
935 struct async_cow
*async_cow
;
936 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
937 unsigned long nr_pages
;
939 int limit
= 10 * 1024 * 1042;
941 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
942 1, 0, NULL
, GFP_NOFS
);
943 while (start
< end
) {
944 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
945 async_cow
->inode
= inode
;
946 async_cow
->root
= root
;
947 async_cow
->locked_page
= locked_page
;
948 async_cow
->start
= start
;
950 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
953 cur_end
= min(end
, start
+ 512 * 1024 - 1);
955 async_cow
->end
= cur_end
;
956 INIT_LIST_HEAD(&async_cow
->extents
);
958 async_cow
->work
.func
= async_cow_start
;
959 async_cow
->work
.ordered_func
= async_cow_submit
;
960 async_cow
->work
.ordered_free
= async_cow_free
;
961 async_cow
->work
.flags
= 0;
963 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
965 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
967 btrfs_queue_worker(&root
->fs_info
->delalloc_workers
,
970 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
971 wait_event(root
->fs_info
->async_submit_wait
,
972 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
976 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
977 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
978 wait_event(root
->fs_info
->async_submit_wait
,
979 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
983 *nr_written
+= nr_pages
;
990 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
991 u64 bytenr
, u64 num_bytes
)
994 struct btrfs_ordered_sum
*sums
;
997 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
998 bytenr
+ num_bytes
- 1, &list
);
999 if (ret
== 0 && list_empty(&list
))
1002 while (!list_empty(&list
)) {
1003 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1004 list_del(&sums
->list
);
1011 * when nowcow writeback call back. This checks for snapshots or COW copies
1012 * of the extents that exist in the file, and COWs the file as required.
1014 * If no cow copies or snapshots exist, we write directly to the existing
1017 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1018 struct page
*locked_page
,
1019 u64 start
, u64 end
, int *page_started
, int force
,
1020 unsigned long *nr_written
)
1022 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1023 struct btrfs_trans_handle
*trans
;
1024 struct extent_buffer
*leaf
;
1025 struct btrfs_path
*path
;
1026 struct btrfs_file_extent_item
*fi
;
1027 struct btrfs_key found_key
;
1039 bool nolock
= false;
1041 path
= btrfs_alloc_path();
1043 if (root
== root
->fs_info
->tree_root
) {
1045 trans
= btrfs_join_transaction_nolock(root
, 1);
1047 trans
= btrfs_join_transaction(root
, 1);
1051 cow_start
= (u64
)-1;
1054 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
1057 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1058 leaf
= path
->nodes
[0];
1059 btrfs_item_key_to_cpu(leaf
, &found_key
,
1060 path
->slots
[0] - 1);
1061 if (found_key
.objectid
== inode
->i_ino
&&
1062 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1067 leaf
= path
->nodes
[0];
1068 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1069 ret
= btrfs_next_leaf(root
, path
);
1074 leaf
= path
->nodes
[0];
1080 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1082 if (found_key
.objectid
> inode
->i_ino
||
1083 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1084 found_key
.offset
> end
)
1087 if (found_key
.offset
> cur_offset
) {
1088 extent_end
= found_key
.offset
;
1093 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1094 struct btrfs_file_extent_item
);
1095 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1097 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1098 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1099 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1100 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1101 extent_end
= found_key
.offset
+
1102 btrfs_file_extent_num_bytes(leaf
, fi
);
1103 if (extent_end
<= start
) {
1107 if (disk_bytenr
== 0)
1109 if (btrfs_file_extent_compression(leaf
, fi
) ||
1110 btrfs_file_extent_encryption(leaf
, fi
) ||
1111 btrfs_file_extent_other_encoding(leaf
, fi
))
1113 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1115 if (btrfs_extent_readonly(root
, disk_bytenr
))
1117 if (btrfs_cross_ref_exist(trans
, root
, inode
->i_ino
,
1119 extent_offset
, disk_bytenr
))
1121 disk_bytenr
+= extent_offset
;
1122 disk_bytenr
+= cur_offset
- found_key
.offset
;
1123 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1125 * force cow if csum exists in the range.
1126 * this ensure that csum for a given extent are
1127 * either valid or do not exist.
1129 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1132 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1133 extent_end
= found_key
.offset
+
1134 btrfs_file_extent_inline_len(leaf
, fi
);
1135 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1140 if (extent_end
<= start
) {
1145 if (cow_start
== (u64
)-1)
1146 cow_start
= cur_offset
;
1147 cur_offset
= extent_end
;
1148 if (cur_offset
> end
)
1154 btrfs_release_path(root
, path
);
1155 if (cow_start
!= (u64
)-1) {
1156 ret
= cow_file_range(inode
, locked_page
, cow_start
,
1157 found_key
.offset
- 1, page_started
,
1160 cow_start
= (u64
)-1;
1163 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1164 struct extent_map
*em
;
1165 struct extent_map_tree
*em_tree
;
1166 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1167 em
= alloc_extent_map(GFP_NOFS
);
1168 em
->start
= cur_offset
;
1169 em
->orig_start
= em
->start
;
1170 em
->len
= num_bytes
;
1171 em
->block_len
= num_bytes
;
1172 em
->block_start
= disk_bytenr
;
1173 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1174 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1176 write_lock(&em_tree
->lock
);
1177 ret
= add_extent_mapping(em_tree
, em
);
1178 write_unlock(&em_tree
->lock
);
1179 if (ret
!= -EEXIST
) {
1180 free_extent_map(em
);
1183 btrfs_drop_extent_cache(inode
, em
->start
,
1184 em
->start
+ em
->len
- 1, 0);
1186 type
= BTRFS_ORDERED_PREALLOC
;
1188 type
= BTRFS_ORDERED_NOCOW
;
1191 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1192 num_bytes
, num_bytes
, type
);
1195 if (root
->root_key
.objectid
==
1196 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1197 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1202 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
1203 cur_offset
, cur_offset
+ num_bytes
- 1,
1204 locked_page
, EXTENT_CLEAR_UNLOCK_PAGE
|
1205 EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
1206 EXTENT_SET_PRIVATE2
);
1207 cur_offset
= extent_end
;
1208 if (cur_offset
> end
)
1211 btrfs_release_path(root
, path
);
1213 if (cur_offset
<= end
&& cow_start
== (u64
)-1)
1214 cow_start
= cur_offset
;
1215 if (cow_start
!= (u64
)-1) {
1216 ret
= cow_file_range(inode
, locked_page
, cow_start
, end
,
1217 page_started
, nr_written
, 1);
1222 ret
= btrfs_end_transaction_nolock(trans
, root
);
1225 ret
= btrfs_end_transaction(trans
, root
);
1228 btrfs_free_path(path
);
1233 * extent_io.c call back to do delayed allocation processing
1235 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1236 u64 start
, u64 end
, int *page_started
,
1237 unsigned long *nr_written
)
1240 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1242 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
)
1243 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1244 page_started
, 1, nr_written
);
1245 else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
)
1246 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1247 page_started
, 0, nr_written
);
1248 else if (!btrfs_test_opt(root
, COMPRESS
) &&
1249 !(BTRFS_I(inode
)->force_compress
))
1250 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1251 page_started
, nr_written
, 1);
1253 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1254 page_started
, nr_written
);
1258 static int btrfs_split_extent_hook(struct inode
*inode
,
1259 struct extent_state
*orig
, u64 split
)
1261 /* not delalloc, ignore it */
1262 if (!(orig
->state
& EXTENT_DELALLOC
))
1265 atomic_inc(&BTRFS_I(inode
)->outstanding_extents
);
1270 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1271 * extents so we can keep track of new extents that are just merged onto old
1272 * extents, such as when we are doing sequential writes, so we can properly
1273 * account for the metadata space we'll need.
1275 static int btrfs_merge_extent_hook(struct inode
*inode
,
1276 struct extent_state
*new,
1277 struct extent_state
*other
)
1279 /* not delalloc, ignore it */
1280 if (!(other
->state
& EXTENT_DELALLOC
))
1283 atomic_dec(&BTRFS_I(inode
)->outstanding_extents
);
1288 * extent_io.c set_bit_hook, used to track delayed allocation
1289 * bytes in this file, and to maintain the list of inodes that
1290 * have pending delalloc work to be done.
1292 static int btrfs_set_bit_hook(struct inode
*inode
,
1293 struct extent_state
*state
, int *bits
)
1297 * set_bit and clear bit hooks normally require _irqsave/restore
1298 * but in this case, we are only testeing for the DELALLOC
1299 * bit, which is only set or cleared with irqs on
1301 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1302 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1303 u64 len
= state
->end
+ 1 - state
->start
;
1304 int do_list
= (root
->root_key
.objectid
!=
1305 BTRFS_ROOT_TREE_OBJECTID
);
1307 if (*bits
& EXTENT_FIRST_DELALLOC
)
1308 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1310 atomic_inc(&BTRFS_I(inode
)->outstanding_extents
);
1312 spin_lock(&root
->fs_info
->delalloc_lock
);
1313 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1314 root
->fs_info
->delalloc_bytes
+= len
;
1315 if (do_list
&& list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1316 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1317 &root
->fs_info
->delalloc_inodes
);
1319 spin_unlock(&root
->fs_info
->delalloc_lock
);
1325 * extent_io.c clear_bit_hook, see set_bit_hook for why
1327 static int btrfs_clear_bit_hook(struct inode
*inode
,
1328 struct extent_state
*state
, int *bits
)
1331 * set_bit and clear bit hooks normally require _irqsave/restore
1332 * but in this case, we are only testeing for the DELALLOC
1333 * bit, which is only set or cleared with irqs on
1335 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1336 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1337 u64 len
= state
->end
+ 1 - state
->start
;
1338 int do_list
= (root
->root_key
.objectid
!=
1339 BTRFS_ROOT_TREE_OBJECTID
);
1341 if (*bits
& EXTENT_FIRST_DELALLOC
)
1342 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1343 else if (!(*bits
& EXTENT_DO_ACCOUNTING
))
1344 atomic_dec(&BTRFS_I(inode
)->outstanding_extents
);
1346 if (*bits
& EXTENT_DO_ACCOUNTING
)
1347 btrfs_delalloc_release_metadata(inode
, len
);
1349 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1351 btrfs_free_reserved_data_space(inode
, len
);
1353 spin_lock(&root
->fs_info
->delalloc_lock
);
1354 root
->fs_info
->delalloc_bytes
-= len
;
1355 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1357 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1358 !list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1359 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1361 spin_unlock(&root
->fs_info
->delalloc_lock
);
1367 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1368 * we don't create bios that span stripes or chunks
1370 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
1371 size_t size
, struct bio
*bio
,
1372 unsigned long bio_flags
)
1374 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1375 struct btrfs_mapping_tree
*map_tree
;
1376 u64 logical
= (u64
)bio
->bi_sector
<< 9;
1381 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1384 length
= bio
->bi_size
;
1385 map_tree
= &root
->fs_info
->mapping_tree
;
1386 map_length
= length
;
1387 ret
= btrfs_map_block(map_tree
, READ
, logical
,
1388 &map_length
, NULL
, 0);
1390 if (map_length
< length
+ size
)
1396 * in order to insert checksums into the metadata in large chunks,
1397 * we wait until bio submission time. All the pages in the bio are
1398 * checksummed and sums are attached onto the ordered extent record.
1400 * At IO completion time the cums attached on the ordered extent record
1401 * are inserted into the btree
1403 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1404 struct bio
*bio
, int mirror_num
,
1405 unsigned long bio_flags
,
1408 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1411 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1417 * in order to insert checksums into the metadata in large chunks,
1418 * we wait until bio submission time. All the pages in the bio are
1419 * checksummed and sums are attached onto the ordered extent record.
1421 * At IO completion time the cums attached on the ordered extent record
1422 * are inserted into the btree
1424 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1425 int mirror_num
, unsigned long bio_flags
,
1428 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1429 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1433 * extent_io.c submission hook. This does the right thing for csum calculation
1434 * on write, or reading the csums from the tree before a read
1436 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1437 int mirror_num
, unsigned long bio_flags
,
1440 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1444 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1446 if (root
== root
->fs_info
->tree_root
)
1447 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 2);
1449 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
1452 if (!(rw
& REQ_WRITE
)) {
1453 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1454 return btrfs_submit_compressed_read(inode
, bio
,
1455 mirror_num
, bio_flags
);
1456 } else if (!skip_sum
)
1457 btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1459 } else if (!skip_sum
) {
1460 /* csum items have already been cloned */
1461 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1463 /* we're doing a write, do the async checksumming */
1464 return btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1465 inode
, rw
, bio
, mirror_num
,
1466 bio_flags
, bio_offset
,
1467 __btrfs_submit_bio_start
,
1468 __btrfs_submit_bio_done
);
1472 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1476 * given a list of ordered sums record them in the inode. This happens
1477 * at IO completion time based on sums calculated at bio submission time.
1479 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1480 struct inode
*inode
, u64 file_offset
,
1481 struct list_head
*list
)
1483 struct btrfs_ordered_sum
*sum
;
1485 btrfs_set_trans_block_group(trans
, inode
);
1487 list_for_each_entry(sum
, list
, list
) {
1488 btrfs_csum_file_blocks(trans
,
1489 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1494 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1495 struct extent_state
**cached_state
)
1497 if ((end
& (PAGE_CACHE_SIZE
- 1)) == 0)
1499 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1500 cached_state
, GFP_NOFS
);
1503 /* see btrfs_writepage_start_hook for details on why this is required */
1504 struct btrfs_writepage_fixup
{
1506 struct btrfs_work work
;
1509 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1511 struct btrfs_writepage_fixup
*fixup
;
1512 struct btrfs_ordered_extent
*ordered
;
1513 struct extent_state
*cached_state
= NULL
;
1515 struct inode
*inode
;
1519 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1523 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1524 ClearPageChecked(page
);
1528 inode
= page
->mapping
->host
;
1529 page_start
= page_offset(page
);
1530 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1532 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1533 &cached_state
, GFP_NOFS
);
1535 /* already ordered? We're done */
1536 if (PagePrivate2(page
))
1539 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1541 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1542 page_end
, &cached_state
, GFP_NOFS
);
1544 btrfs_start_ordered_extent(inode
, ordered
, 1);
1549 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1550 ClearPageChecked(page
);
1552 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1553 &cached_state
, GFP_NOFS
);
1556 page_cache_release(page
);
1560 * There are a few paths in the higher layers of the kernel that directly
1561 * set the page dirty bit without asking the filesystem if it is a
1562 * good idea. This causes problems because we want to make sure COW
1563 * properly happens and the data=ordered rules are followed.
1565 * In our case any range that doesn't have the ORDERED bit set
1566 * hasn't been properly setup for IO. We kick off an async process
1567 * to fix it up. The async helper will wait for ordered extents, set
1568 * the delalloc bit and make it safe to write the page.
1570 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1572 struct inode
*inode
= page
->mapping
->host
;
1573 struct btrfs_writepage_fixup
*fixup
;
1574 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1576 /* this page is properly in the ordered list */
1577 if (TestClearPagePrivate2(page
))
1580 if (PageChecked(page
))
1583 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1587 SetPageChecked(page
);
1588 page_cache_get(page
);
1589 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
1591 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
1595 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1596 struct inode
*inode
, u64 file_pos
,
1597 u64 disk_bytenr
, u64 disk_num_bytes
,
1598 u64 num_bytes
, u64 ram_bytes
,
1599 u8 compression
, u8 encryption
,
1600 u16 other_encoding
, int extent_type
)
1602 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1603 struct btrfs_file_extent_item
*fi
;
1604 struct btrfs_path
*path
;
1605 struct extent_buffer
*leaf
;
1606 struct btrfs_key ins
;
1610 path
= btrfs_alloc_path();
1613 path
->leave_spinning
= 1;
1616 * we may be replacing one extent in the tree with another.
1617 * The new extent is pinned in the extent map, and we don't want
1618 * to drop it from the cache until it is completely in the btree.
1620 * So, tell btrfs_drop_extents to leave this extent in the cache.
1621 * the caller is expected to unpin it and allow it to be merged
1624 ret
= btrfs_drop_extents(trans
, inode
, file_pos
, file_pos
+ num_bytes
,
1628 ins
.objectid
= inode
->i_ino
;
1629 ins
.offset
= file_pos
;
1630 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1631 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
, sizeof(*fi
));
1633 leaf
= path
->nodes
[0];
1634 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1635 struct btrfs_file_extent_item
);
1636 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1637 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1638 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1639 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1640 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1641 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1642 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1643 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1644 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1645 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1647 btrfs_unlock_up_safe(path
, 1);
1648 btrfs_set_lock_blocking(leaf
);
1650 btrfs_mark_buffer_dirty(leaf
);
1652 inode_add_bytes(inode
, num_bytes
);
1654 ins
.objectid
= disk_bytenr
;
1655 ins
.offset
= disk_num_bytes
;
1656 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1657 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
1658 root
->root_key
.objectid
,
1659 inode
->i_ino
, file_pos
, &ins
);
1661 btrfs_free_path(path
);
1667 * helper function for btrfs_finish_ordered_io, this
1668 * just reads in some of the csum leaves to prime them into ram
1669 * before we start the transaction. It limits the amount of btree
1670 * reads required while inside the transaction.
1672 /* as ordered data IO finishes, this gets called so we can finish
1673 * an ordered extent if the range of bytes in the file it covers are
1676 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
)
1678 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1679 struct btrfs_trans_handle
*trans
= NULL
;
1680 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
1681 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1682 struct extent_state
*cached_state
= NULL
;
1685 bool nolock
= false;
1687 ret
= btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
1691 BUG_ON(!ordered_extent
);
1693 nolock
= (root
== root
->fs_info
->tree_root
);
1695 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
1696 BUG_ON(!list_empty(&ordered_extent
->list
));
1697 ret
= btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
1700 trans
= btrfs_join_transaction_nolock(root
, 1);
1702 trans
= btrfs_join_transaction(root
, 1);
1704 btrfs_set_trans_block_group(trans
, inode
);
1705 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1706 ret
= btrfs_update_inode(trans
, root
, inode
);
1712 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
1713 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
1714 0, &cached_state
, GFP_NOFS
);
1717 trans
= btrfs_join_transaction_nolock(root
, 1);
1719 trans
= btrfs_join_transaction(root
, 1);
1720 btrfs_set_trans_block_group(trans
, inode
);
1721 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1723 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
1725 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
1727 ret
= btrfs_mark_extent_written(trans
, inode
,
1728 ordered_extent
->file_offset
,
1729 ordered_extent
->file_offset
+
1730 ordered_extent
->len
);
1733 BUG_ON(root
== root
->fs_info
->tree_root
);
1734 ret
= insert_reserved_file_extent(trans
, inode
,
1735 ordered_extent
->file_offset
,
1736 ordered_extent
->start
,
1737 ordered_extent
->disk_len
,
1738 ordered_extent
->len
,
1739 ordered_extent
->len
,
1741 BTRFS_FILE_EXTENT_REG
);
1742 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
1743 ordered_extent
->file_offset
,
1744 ordered_extent
->len
);
1747 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
1748 ordered_extent
->file_offset
+
1749 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
1751 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
1752 &ordered_extent
->list
);
1754 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
1755 ret
= btrfs_update_inode(trans
, root
, inode
);
1760 btrfs_end_transaction_nolock(trans
, root
);
1762 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
1764 btrfs_end_transaction(trans
, root
);
1768 btrfs_put_ordered_extent(ordered_extent
);
1769 /* once for the tree */
1770 btrfs_put_ordered_extent(ordered_extent
);
1775 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
1776 struct extent_state
*state
, int uptodate
)
1778 ClearPagePrivate2(page
);
1779 return btrfs_finish_ordered_io(page
->mapping
->host
, start
, end
);
1783 * When IO fails, either with EIO or csum verification fails, we
1784 * try other mirrors that might have a good copy of the data. This
1785 * io_failure_record is used to record state as we go through all the
1786 * mirrors. If another mirror has good data, the page is set up to date
1787 * and things continue. If a good mirror can't be found, the original
1788 * bio end_io callback is called to indicate things have failed.
1790 struct io_failure_record
{
1795 unsigned long bio_flags
;
1799 static int btrfs_io_failed_hook(struct bio
*failed_bio
,
1800 struct page
*page
, u64 start
, u64 end
,
1801 struct extent_state
*state
)
1803 struct io_failure_record
*failrec
= NULL
;
1805 struct extent_map
*em
;
1806 struct inode
*inode
= page
->mapping
->host
;
1807 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
1808 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1815 ret
= get_state_private(failure_tree
, start
, &private);
1817 failrec
= kmalloc(sizeof(*failrec
), GFP_NOFS
);
1820 failrec
->start
= start
;
1821 failrec
->len
= end
- start
+ 1;
1822 failrec
->last_mirror
= 0;
1823 failrec
->bio_flags
= 0;
1825 read_lock(&em_tree
->lock
);
1826 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
1827 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
1828 free_extent_map(em
);
1831 read_unlock(&em_tree
->lock
);
1833 if (!em
|| IS_ERR(em
)) {
1837 logical
= start
- em
->start
;
1838 logical
= em
->block_start
+ logical
;
1839 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
1840 logical
= em
->block_start
;
1841 failrec
->bio_flags
= EXTENT_BIO_COMPRESSED
;
1843 failrec
->logical
= logical
;
1844 free_extent_map(em
);
1845 set_extent_bits(failure_tree
, start
, end
, EXTENT_LOCKED
|
1846 EXTENT_DIRTY
, GFP_NOFS
);
1847 set_state_private(failure_tree
, start
,
1848 (u64
)(unsigned long)failrec
);
1850 failrec
= (struct io_failure_record
*)(unsigned long)private;
1852 num_copies
= btrfs_num_copies(
1853 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
1854 failrec
->logical
, failrec
->len
);
1855 failrec
->last_mirror
++;
1857 spin_lock(&BTRFS_I(inode
)->io_tree
.lock
);
1858 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
1861 if (state
&& state
->start
!= failrec
->start
)
1863 spin_unlock(&BTRFS_I(inode
)->io_tree
.lock
);
1865 if (!state
|| failrec
->last_mirror
> num_copies
) {
1866 set_state_private(failure_tree
, failrec
->start
, 0);
1867 clear_extent_bits(failure_tree
, failrec
->start
,
1868 failrec
->start
+ failrec
->len
- 1,
1869 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
1873 bio
= bio_alloc(GFP_NOFS
, 1);
1874 bio
->bi_private
= state
;
1875 bio
->bi_end_io
= failed_bio
->bi_end_io
;
1876 bio
->bi_sector
= failrec
->logical
>> 9;
1877 bio
->bi_bdev
= failed_bio
->bi_bdev
;
1880 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
1881 if (failed_bio
->bi_rw
& REQ_WRITE
)
1886 BTRFS_I(inode
)->io_tree
.ops
->submit_bio_hook(inode
, rw
, bio
,
1887 failrec
->last_mirror
,
1888 failrec
->bio_flags
, 0);
1893 * each time an IO finishes, we do a fast check in the IO failure tree
1894 * to see if we need to process or clean up an io_failure_record
1896 static int btrfs_clean_io_failures(struct inode
*inode
, u64 start
)
1899 u64 private_failure
;
1900 struct io_failure_record
*failure
;
1904 if (count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
1905 (u64
)-1, 1, EXTENT_DIRTY
)) {
1906 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
,
1907 start
, &private_failure
);
1909 failure
= (struct io_failure_record
*)(unsigned long)
1911 set_state_private(&BTRFS_I(inode
)->io_failure_tree
,
1913 clear_extent_bits(&BTRFS_I(inode
)->io_failure_tree
,
1915 failure
->start
+ failure
->len
- 1,
1916 EXTENT_DIRTY
| EXTENT_LOCKED
,
1925 * when reads are done, we need to check csums to verify the data is correct
1926 * if there's a match, we allow the bio to finish. If not, we go through
1927 * the io_failure_record routines to find good copies
1929 static int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
1930 struct extent_state
*state
)
1932 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
1933 struct inode
*inode
= page
->mapping
->host
;
1934 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1936 u64
private = ~(u32
)0;
1938 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1941 if (PageChecked(page
)) {
1942 ClearPageChecked(page
);
1946 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
1949 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
1950 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
1951 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
1956 if (state
&& state
->start
== start
) {
1957 private = state
->private;
1960 ret
= get_state_private(io_tree
, start
, &private);
1962 kaddr
= kmap_atomic(page
, KM_USER0
);
1966 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
1967 btrfs_csum_final(csum
, (char *)&csum
);
1968 if (csum
!= private)
1971 kunmap_atomic(kaddr
, KM_USER0
);
1973 /* if the io failure tree for this inode is non-empty,
1974 * check to see if we've recovered from a failed IO
1976 btrfs_clean_io_failures(inode
, start
);
1980 if (printk_ratelimit()) {
1981 printk(KERN_INFO
"btrfs csum failed ino %lu off %llu csum %u "
1982 "private %llu\n", page
->mapping
->host
->i_ino
,
1983 (unsigned long long)start
, csum
,
1984 (unsigned long long)private);
1986 memset(kaddr
+ offset
, 1, end
- start
+ 1);
1987 flush_dcache_page(page
);
1988 kunmap_atomic(kaddr
, KM_USER0
);
1994 struct delayed_iput
{
1995 struct list_head list
;
1996 struct inode
*inode
;
1999 void btrfs_add_delayed_iput(struct inode
*inode
)
2001 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2002 struct delayed_iput
*delayed
;
2004 if (atomic_add_unless(&inode
->i_count
, -1, 1))
2007 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
2008 delayed
->inode
= inode
;
2010 spin_lock(&fs_info
->delayed_iput_lock
);
2011 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
2012 spin_unlock(&fs_info
->delayed_iput_lock
);
2015 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
2018 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2019 struct delayed_iput
*delayed
;
2022 spin_lock(&fs_info
->delayed_iput_lock
);
2023 empty
= list_empty(&fs_info
->delayed_iputs
);
2024 spin_unlock(&fs_info
->delayed_iput_lock
);
2028 down_read(&root
->fs_info
->cleanup_work_sem
);
2029 spin_lock(&fs_info
->delayed_iput_lock
);
2030 list_splice_init(&fs_info
->delayed_iputs
, &list
);
2031 spin_unlock(&fs_info
->delayed_iput_lock
);
2033 while (!list_empty(&list
)) {
2034 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
2035 list_del(&delayed
->list
);
2036 iput(delayed
->inode
);
2039 up_read(&root
->fs_info
->cleanup_work_sem
);
2043 * calculate extra metadata reservation when snapshotting a subvolume
2044 * contains orphan files.
2046 void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle
*trans
,
2047 struct btrfs_pending_snapshot
*pending
,
2048 u64
*bytes_to_reserve
)
2050 struct btrfs_root
*root
;
2051 struct btrfs_block_rsv
*block_rsv
;
2055 root
= pending
->root
;
2056 if (!root
->orphan_block_rsv
|| list_empty(&root
->orphan_list
))
2059 block_rsv
= root
->orphan_block_rsv
;
2061 /* orphan block reservation for the snapshot */
2062 num_bytes
= block_rsv
->size
;
2065 * after the snapshot is created, COWing tree blocks may use more
2066 * space than it frees. So we should make sure there is enough
2069 index
= trans
->transid
& 0x1;
2070 if (block_rsv
->reserved
+ block_rsv
->freed
[index
] < block_rsv
->size
) {
2071 num_bytes
+= block_rsv
->size
-
2072 (block_rsv
->reserved
+ block_rsv
->freed
[index
]);
2075 *bytes_to_reserve
+= num_bytes
;
2078 void btrfs_orphan_post_snapshot(struct btrfs_trans_handle
*trans
,
2079 struct btrfs_pending_snapshot
*pending
)
2081 struct btrfs_root
*root
= pending
->root
;
2082 struct btrfs_root
*snap
= pending
->snap
;
2083 struct btrfs_block_rsv
*block_rsv
;
2088 if (!root
->orphan_block_rsv
|| list_empty(&root
->orphan_list
))
2091 /* refill source subvolume's orphan block reservation */
2092 block_rsv
= root
->orphan_block_rsv
;
2093 index
= trans
->transid
& 0x1;
2094 if (block_rsv
->reserved
+ block_rsv
->freed
[index
] < block_rsv
->size
) {
2095 num_bytes
= block_rsv
->size
-
2096 (block_rsv
->reserved
+ block_rsv
->freed
[index
]);
2097 ret
= btrfs_block_rsv_migrate(&pending
->block_rsv
,
2098 root
->orphan_block_rsv
,
2103 /* setup orphan block reservation for the snapshot */
2104 block_rsv
= btrfs_alloc_block_rsv(snap
);
2107 btrfs_add_durable_block_rsv(root
->fs_info
, block_rsv
);
2108 snap
->orphan_block_rsv
= block_rsv
;
2110 num_bytes
= root
->orphan_block_rsv
->size
;
2111 ret
= btrfs_block_rsv_migrate(&pending
->block_rsv
,
2112 block_rsv
, num_bytes
);
2116 /* insert orphan item for the snapshot */
2117 WARN_ON(!root
->orphan_item_inserted
);
2118 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
2119 snap
->root_key
.objectid
);
2121 snap
->orphan_item_inserted
= 1;
2125 enum btrfs_orphan_cleanup_state
{
2126 ORPHAN_CLEANUP_STARTED
= 1,
2127 ORPHAN_CLEANUP_DONE
= 2,
2131 * This is called in transaction commmit time. If there are no orphan
2132 * files in the subvolume, it removes orphan item and frees block_rsv
2135 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
2136 struct btrfs_root
*root
)
2140 if (!list_empty(&root
->orphan_list
) ||
2141 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
2144 if (root
->orphan_item_inserted
&&
2145 btrfs_root_refs(&root
->root_item
) > 0) {
2146 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
2147 root
->root_key
.objectid
);
2149 root
->orphan_item_inserted
= 0;
2152 if (root
->orphan_block_rsv
) {
2153 WARN_ON(root
->orphan_block_rsv
->size
> 0);
2154 btrfs_free_block_rsv(root
, root
->orphan_block_rsv
);
2155 root
->orphan_block_rsv
= NULL
;
2160 * This creates an orphan entry for the given inode in case something goes
2161 * wrong in the middle of an unlink/truncate.
2163 * NOTE: caller of this function should reserve 5 units of metadata for
2166 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2168 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2169 struct btrfs_block_rsv
*block_rsv
= NULL
;
2174 if (!root
->orphan_block_rsv
) {
2175 block_rsv
= btrfs_alloc_block_rsv(root
);
2179 spin_lock(&root
->orphan_lock
);
2180 if (!root
->orphan_block_rsv
) {
2181 root
->orphan_block_rsv
= block_rsv
;
2182 } else if (block_rsv
) {
2183 btrfs_free_block_rsv(root
, block_rsv
);
2187 if (list_empty(&BTRFS_I(inode
)->i_orphan
)) {
2188 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
2191 * For proper ENOSPC handling, we should do orphan
2192 * cleanup when mounting. But this introduces backward
2193 * compatibility issue.
2195 if (!xchg(&root
->orphan_item_inserted
, 1))
2202 WARN_ON(!BTRFS_I(inode
)->orphan_meta_reserved
);
2205 if (!BTRFS_I(inode
)->orphan_meta_reserved
) {
2206 BTRFS_I(inode
)->orphan_meta_reserved
= 1;
2209 spin_unlock(&root
->orphan_lock
);
2212 btrfs_add_durable_block_rsv(root
->fs_info
, block_rsv
);
2214 /* grab metadata reservation from transaction handle */
2216 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
2220 /* insert an orphan item to track this unlinked/truncated file */
2222 ret
= btrfs_insert_orphan_item(trans
, root
, inode
->i_ino
);
2226 /* insert an orphan item to track subvolume contains orphan files */
2228 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
2229 root
->root_key
.objectid
);
2236 * We have done the truncate/delete so we can go ahead and remove the orphan
2237 * item for this particular inode.
2239 int btrfs_orphan_del(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2241 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2242 int delete_item
= 0;
2243 int release_rsv
= 0;
2246 spin_lock(&root
->orphan_lock
);
2247 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
2248 list_del_init(&BTRFS_I(inode
)->i_orphan
);
2252 if (BTRFS_I(inode
)->orphan_meta_reserved
) {
2253 BTRFS_I(inode
)->orphan_meta_reserved
= 0;
2256 spin_unlock(&root
->orphan_lock
);
2258 if (trans
&& delete_item
) {
2259 ret
= btrfs_del_orphan_item(trans
, root
, inode
->i_ino
);
2264 btrfs_orphan_release_metadata(inode
);
2270 * this cleans up any orphans that may be left on the list from the last use
2273 void btrfs_orphan_cleanup(struct btrfs_root
*root
)
2275 struct btrfs_path
*path
;
2276 struct extent_buffer
*leaf
;
2277 struct btrfs_item
*item
;
2278 struct btrfs_key key
, found_key
;
2279 struct btrfs_trans_handle
*trans
;
2280 struct inode
*inode
;
2281 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
2283 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
2286 path
= btrfs_alloc_path();
2290 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
2291 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
2292 key
.offset
= (u64
)-1;
2295 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2297 printk(KERN_ERR
"Error searching slot for orphan: %d"
2303 * if ret == 0 means we found what we were searching for, which
2304 * is weird, but possible, so only screw with path if we didnt
2305 * find the key and see if we have stuff that matches
2308 if (path
->slots
[0] == 0)
2313 /* pull out the item */
2314 leaf
= path
->nodes
[0];
2315 item
= btrfs_item_nr(leaf
, path
->slots
[0]);
2316 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2318 /* make sure the item matches what we want */
2319 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
2321 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
2324 /* release the path since we're done with it */
2325 btrfs_release_path(root
, path
);
2328 * this is where we are basically btrfs_lookup, without the
2329 * crossing root thing. we store the inode number in the
2330 * offset of the orphan item.
2332 found_key
.objectid
= found_key
.offset
;
2333 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
2334 found_key
.offset
= 0;
2335 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
2336 BUG_ON(IS_ERR(inode
));
2339 * add this inode to the orphan list so btrfs_orphan_del does
2340 * the proper thing when we hit it
2342 spin_lock(&root
->orphan_lock
);
2343 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
2344 spin_unlock(&root
->orphan_lock
);
2347 * if this is a bad inode, means we actually succeeded in
2348 * removing the inode, but not the orphan record, which means
2349 * we need to manually delete the orphan since iput will just
2350 * do a destroy_inode
2352 if (is_bad_inode(inode
)) {
2353 trans
= btrfs_start_transaction(root
, 0);
2354 btrfs_orphan_del(trans
, inode
);
2355 btrfs_end_transaction(trans
, root
);
2360 /* if we have links, this was a truncate, lets do that */
2361 if (inode
->i_nlink
) {
2363 btrfs_truncate(inode
);
2368 /* this will do delete_inode and everything for us */
2371 btrfs_free_path(path
);
2373 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
2375 if (root
->orphan_block_rsv
)
2376 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
2379 if (root
->orphan_block_rsv
|| root
->orphan_item_inserted
) {
2380 trans
= btrfs_join_transaction(root
, 1);
2381 btrfs_end_transaction(trans
, root
);
2385 printk(KERN_INFO
"btrfs: unlinked %d orphans\n", nr_unlink
);
2387 printk(KERN_INFO
"btrfs: truncated %d orphans\n", nr_truncate
);
2391 * very simple check to peek ahead in the leaf looking for xattrs. If we
2392 * don't find any xattrs, we know there can't be any acls.
2394 * slot is the slot the inode is in, objectid is the objectid of the inode
2396 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
2397 int slot
, u64 objectid
)
2399 u32 nritems
= btrfs_header_nritems(leaf
);
2400 struct btrfs_key found_key
;
2404 while (slot
< nritems
) {
2405 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2407 /* we found a different objectid, there must not be acls */
2408 if (found_key
.objectid
!= objectid
)
2411 /* we found an xattr, assume we've got an acl */
2412 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
)
2416 * we found a key greater than an xattr key, there can't
2417 * be any acls later on
2419 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
2426 * it goes inode, inode backrefs, xattrs, extents,
2427 * so if there are a ton of hard links to an inode there can
2428 * be a lot of backrefs. Don't waste time searching too hard,
2429 * this is just an optimization
2434 /* we hit the end of the leaf before we found an xattr or
2435 * something larger than an xattr. We have to assume the inode
2442 * read an inode from the btree into the in-memory inode
2444 static void btrfs_read_locked_inode(struct inode
*inode
)
2446 struct btrfs_path
*path
;
2447 struct extent_buffer
*leaf
;
2448 struct btrfs_inode_item
*inode_item
;
2449 struct btrfs_timespec
*tspec
;
2450 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2451 struct btrfs_key location
;
2453 u64 alloc_group_block
;
2457 path
= btrfs_alloc_path();
2459 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
2461 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
2465 leaf
= path
->nodes
[0];
2466 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2467 struct btrfs_inode_item
);
2469 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
2470 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
2471 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
2472 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
2473 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
2475 tspec
= btrfs_inode_atime(inode_item
);
2476 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2477 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2479 tspec
= btrfs_inode_mtime(inode_item
);
2480 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2481 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2483 tspec
= btrfs_inode_ctime(inode_item
);
2484 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2485 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2487 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
2488 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
2489 BTRFS_I(inode
)->sequence
= btrfs_inode_sequence(leaf
, inode_item
);
2490 inode
->i_generation
= BTRFS_I(inode
)->generation
;
2492 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
2494 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
2495 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
2497 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
2500 * try to precache a NULL acl entry for files that don't have
2501 * any xattrs or acls
2503 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0], inode
->i_ino
);
2505 cache_no_acl(inode
);
2507 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
, 0,
2508 alloc_group_block
, 0);
2509 btrfs_free_path(path
);
2512 switch (inode
->i_mode
& S_IFMT
) {
2514 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2515 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2516 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2517 inode
->i_fop
= &btrfs_file_operations
;
2518 inode
->i_op
= &btrfs_file_inode_operations
;
2521 inode
->i_fop
= &btrfs_dir_file_operations
;
2522 if (root
== root
->fs_info
->tree_root
)
2523 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
2525 inode
->i_op
= &btrfs_dir_inode_operations
;
2528 inode
->i_op
= &btrfs_symlink_inode_operations
;
2529 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2530 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2533 inode
->i_op
= &btrfs_special_inode_operations
;
2534 init_special_inode(inode
, inode
->i_mode
, rdev
);
2538 btrfs_update_iflags(inode
);
2542 btrfs_free_path(path
);
2543 make_bad_inode(inode
);
2547 * given a leaf and an inode, copy the inode fields into the leaf
2549 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
2550 struct extent_buffer
*leaf
,
2551 struct btrfs_inode_item
*item
,
2552 struct inode
*inode
)
2554 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
2555 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
2556 btrfs_set_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
);
2557 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
2558 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
2560 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
2561 inode
->i_atime
.tv_sec
);
2562 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
2563 inode
->i_atime
.tv_nsec
);
2565 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
2566 inode
->i_mtime
.tv_sec
);
2567 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
2568 inode
->i_mtime
.tv_nsec
);
2570 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
2571 inode
->i_ctime
.tv_sec
);
2572 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
2573 inode
->i_ctime
.tv_nsec
);
2575 btrfs_set_inode_nbytes(leaf
, item
, inode_get_bytes(inode
));
2576 btrfs_set_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
);
2577 btrfs_set_inode_sequence(leaf
, item
, BTRFS_I(inode
)->sequence
);
2578 btrfs_set_inode_transid(leaf
, item
, trans
->transid
);
2579 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
2580 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
2581 btrfs_set_inode_block_group(leaf
, item
, BTRFS_I(inode
)->block_group
);
2585 * copy everything in the in-memory inode into the btree.
2587 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
2588 struct btrfs_root
*root
, struct inode
*inode
)
2590 struct btrfs_inode_item
*inode_item
;
2591 struct btrfs_path
*path
;
2592 struct extent_buffer
*leaf
;
2595 path
= btrfs_alloc_path();
2597 path
->leave_spinning
= 1;
2598 ret
= btrfs_lookup_inode(trans
, root
, path
,
2599 &BTRFS_I(inode
)->location
, 1);
2606 btrfs_unlock_up_safe(path
, 1);
2607 leaf
= path
->nodes
[0];
2608 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2609 struct btrfs_inode_item
);
2611 fill_inode_item(trans
, leaf
, inode_item
, inode
);
2612 btrfs_mark_buffer_dirty(leaf
);
2613 btrfs_set_inode_last_trans(trans
, inode
);
2616 btrfs_free_path(path
);
2622 * unlink helper that gets used here in inode.c and in the tree logging
2623 * recovery code. It remove a link in a directory with a given name, and
2624 * also drops the back refs in the inode to the directory
2626 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
2627 struct btrfs_root
*root
,
2628 struct inode
*dir
, struct inode
*inode
,
2629 const char *name
, int name_len
)
2631 struct btrfs_path
*path
;
2633 struct extent_buffer
*leaf
;
2634 struct btrfs_dir_item
*di
;
2635 struct btrfs_key key
;
2638 path
= btrfs_alloc_path();
2644 path
->leave_spinning
= 1;
2645 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
2646 name
, name_len
, -1);
2655 leaf
= path
->nodes
[0];
2656 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2657 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2660 btrfs_release_path(root
, path
);
2662 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
2664 dir
->i_ino
, &index
);
2666 printk(KERN_INFO
"btrfs failed to delete reference to %.*s, "
2667 "inode %lu parent %lu\n", name_len
, name
,
2668 inode
->i_ino
, dir
->i_ino
);
2672 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
2673 index
, name
, name_len
, -1);
2682 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2683 btrfs_release_path(root
, path
);
2685 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
2687 BUG_ON(ret
!= 0 && ret
!= -ENOENT
);
2689 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
2693 btrfs_free_path(path
);
2697 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
2698 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
2699 btrfs_update_inode(trans
, root
, dir
);
2700 btrfs_drop_nlink(inode
);
2701 ret
= btrfs_update_inode(trans
, root
, inode
);
2706 /* helper to check if there is any shared block in the path */
2707 static int check_path_shared(struct btrfs_root
*root
,
2708 struct btrfs_path
*path
)
2710 struct extent_buffer
*eb
;
2715 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
2716 if (!path
->nodes
[level
])
2718 eb
= path
->nodes
[level
];
2719 if (!btrfs_block_can_be_shared(root
, eb
))
2721 ret
= btrfs_lookup_extent_info(NULL
, root
, eb
->start
, eb
->len
,
2730 * helper to start transaction for unlink and rmdir.
2732 * unlink and rmdir are special in btrfs, they do not always free space.
2733 * so in enospc case, we should make sure they will free space before
2734 * allowing them to use the global metadata reservation.
2736 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
,
2737 struct dentry
*dentry
)
2739 struct btrfs_trans_handle
*trans
;
2740 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2741 struct btrfs_path
*path
;
2742 struct btrfs_inode_ref
*ref
;
2743 struct btrfs_dir_item
*di
;
2744 struct inode
*inode
= dentry
->d_inode
;
2750 trans
= btrfs_start_transaction(root
, 10);
2751 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
2754 if (inode
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
2755 return ERR_PTR(-ENOSPC
);
2757 /* check if there is someone else holds reference */
2758 if (S_ISDIR(inode
->i_mode
) && atomic_read(&inode
->i_count
) > 1)
2759 return ERR_PTR(-ENOSPC
);
2761 if (atomic_read(&inode
->i_count
) > 2)
2762 return ERR_PTR(-ENOSPC
);
2764 if (xchg(&root
->fs_info
->enospc_unlink
, 1))
2765 return ERR_PTR(-ENOSPC
);
2767 path
= btrfs_alloc_path();
2769 root
->fs_info
->enospc_unlink
= 0;
2770 return ERR_PTR(-ENOMEM
);
2773 trans
= btrfs_start_transaction(root
, 0);
2774 if (IS_ERR(trans
)) {
2775 btrfs_free_path(path
);
2776 root
->fs_info
->enospc_unlink
= 0;
2780 path
->skip_locking
= 1;
2781 path
->search_commit_root
= 1;
2783 ret
= btrfs_lookup_inode(trans
, root
, path
,
2784 &BTRFS_I(dir
)->location
, 0);
2790 if (check_path_shared(root
, path
))
2795 btrfs_release_path(root
, path
);
2797 ret
= btrfs_lookup_inode(trans
, root
, path
,
2798 &BTRFS_I(inode
)->location
, 0);
2804 if (check_path_shared(root
, path
))
2809 btrfs_release_path(root
, path
);
2811 if (ret
== 0 && S_ISREG(inode
->i_mode
)) {
2812 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
2813 inode
->i_ino
, (u64
)-1, 0);
2819 if (check_path_shared(root
, path
))
2821 btrfs_release_path(root
, path
);
2829 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
2830 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
2836 if (check_path_shared(root
, path
))
2842 btrfs_release_path(root
, path
);
2844 ref
= btrfs_lookup_inode_ref(trans
, root
, path
,
2845 dentry
->d_name
.name
, dentry
->d_name
.len
,
2846 inode
->i_ino
, dir
->i_ino
, 0);
2852 if (check_path_shared(root
, path
))
2854 index
= btrfs_inode_ref_index(path
->nodes
[0], ref
);
2855 btrfs_release_path(root
, path
);
2857 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
, index
,
2858 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
2863 BUG_ON(ret
== -ENOENT
);
2864 if (check_path_shared(root
, path
))
2869 btrfs_free_path(path
);
2871 btrfs_end_transaction(trans
, root
);
2872 root
->fs_info
->enospc_unlink
= 0;
2873 return ERR_PTR(err
);
2876 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
2880 static void __unlink_end_trans(struct btrfs_trans_handle
*trans
,
2881 struct btrfs_root
*root
)
2883 if (trans
->block_rsv
== &root
->fs_info
->global_block_rsv
) {
2884 BUG_ON(!root
->fs_info
->enospc_unlink
);
2885 root
->fs_info
->enospc_unlink
= 0;
2887 btrfs_end_transaction_throttle(trans
, root
);
2890 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
2892 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2893 struct btrfs_trans_handle
*trans
;
2894 struct inode
*inode
= dentry
->d_inode
;
2896 unsigned long nr
= 0;
2898 trans
= __unlink_start_trans(dir
, dentry
);
2900 return PTR_ERR(trans
);
2902 btrfs_set_trans_block_group(trans
, dir
);
2904 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
2906 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
2907 dentry
->d_name
.name
, dentry
->d_name
.len
);
2910 if (inode
->i_nlink
== 0) {
2911 ret
= btrfs_orphan_add(trans
, inode
);
2915 nr
= trans
->blocks_used
;
2916 __unlink_end_trans(trans
, root
);
2917 btrfs_btree_balance_dirty(root
, nr
);
2921 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
2922 struct btrfs_root
*root
,
2923 struct inode
*dir
, u64 objectid
,
2924 const char *name
, int name_len
)
2926 struct btrfs_path
*path
;
2927 struct extent_buffer
*leaf
;
2928 struct btrfs_dir_item
*di
;
2929 struct btrfs_key key
;
2933 path
= btrfs_alloc_path();
2937 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
2938 name
, name_len
, -1);
2939 BUG_ON(!di
|| IS_ERR(di
));
2941 leaf
= path
->nodes
[0];
2942 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2943 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
2944 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2946 btrfs_release_path(root
, path
);
2948 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
2949 objectid
, root
->root_key
.objectid
,
2950 dir
->i_ino
, &index
, name
, name_len
);
2952 BUG_ON(ret
!= -ENOENT
);
2953 di
= btrfs_search_dir_index_item(root
, path
, dir
->i_ino
,
2955 BUG_ON(!di
|| IS_ERR(di
));
2957 leaf
= path
->nodes
[0];
2958 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2959 btrfs_release_path(root
, path
);
2963 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
2964 index
, name
, name_len
, -1);
2965 BUG_ON(!di
|| IS_ERR(di
));
2967 leaf
= path
->nodes
[0];
2968 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2969 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
2970 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2972 btrfs_release_path(root
, path
);
2974 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
2975 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
2976 ret
= btrfs_update_inode(trans
, root
, dir
);
2979 btrfs_free_path(path
);
2983 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2985 struct inode
*inode
= dentry
->d_inode
;
2987 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2988 struct btrfs_trans_handle
*trans
;
2989 unsigned long nr
= 0;
2991 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
||
2992 inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)
2995 trans
= __unlink_start_trans(dir
, dentry
);
2997 return PTR_ERR(trans
);
2999 btrfs_set_trans_block_group(trans
, dir
);
3001 if (unlikely(inode
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
3002 err
= btrfs_unlink_subvol(trans
, root
, dir
,
3003 BTRFS_I(inode
)->location
.objectid
,
3004 dentry
->d_name
.name
,
3005 dentry
->d_name
.len
);
3009 err
= btrfs_orphan_add(trans
, inode
);
3013 /* now the directory is empty */
3014 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3015 dentry
->d_name
.name
, dentry
->d_name
.len
);
3017 btrfs_i_size_write(inode
, 0);
3019 nr
= trans
->blocks_used
;
3020 __unlink_end_trans(trans
, root
);
3021 btrfs_btree_balance_dirty(root
, nr
);
3028 * when truncating bytes in a file, it is possible to avoid reading
3029 * the leaves that contain only checksum items. This can be the
3030 * majority of the IO required to delete a large file, but it must
3031 * be done carefully.
3033 * The keys in the level just above the leaves are checked to make sure
3034 * the lowest key in a given leaf is a csum key, and starts at an offset
3035 * after the new size.
3037 * Then the key for the next leaf is checked to make sure it also has
3038 * a checksum item for the same file. If it does, we know our target leaf
3039 * contains only checksum items, and it can be safely freed without reading
3042 * This is just an optimization targeted at large files. It may do
3043 * nothing. It will return 0 unless things went badly.
3045 static noinline
int drop_csum_leaves(struct btrfs_trans_handle
*trans
,
3046 struct btrfs_root
*root
,
3047 struct btrfs_path
*path
,
3048 struct inode
*inode
, u64 new_size
)
3050 struct btrfs_key key
;
3053 struct btrfs_key found_key
;
3054 struct btrfs_key other_key
;
3055 struct btrfs_leaf_ref
*ref
;
3059 path
->lowest_level
= 1;
3060 key
.objectid
= inode
->i_ino
;
3061 key
.type
= BTRFS_CSUM_ITEM_KEY
;
3062 key
.offset
= new_size
;
3064 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3068 if (path
->nodes
[1] == NULL
) {
3073 btrfs_node_key_to_cpu(path
->nodes
[1], &found_key
, path
->slots
[1]);
3074 nritems
= btrfs_header_nritems(path
->nodes
[1]);
3079 if (path
->slots
[1] >= nritems
)
3082 /* did we find a key greater than anything we want to delete? */
3083 if (found_key
.objectid
> inode
->i_ino
||
3084 (found_key
.objectid
== inode
->i_ino
&& found_key
.type
> key
.type
))
3087 /* we check the next key in the node to make sure the leave contains
3088 * only checksum items. This comparison doesn't work if our
3089 * leaf is the last one in the node
3091 if (path
->slots
[1] + 1 >= nritems
) {
3093 /* search forward from the last key in the node, this
3094 * will bring us into the next node in the tree
3096 btrfs_node_key_to_cpu(path
->nodes
[1], &found_key
, nritems
- 1);
3098 /* unlikely, but we inc below, so check to be safe */
3099 if (found_key
.offset
== (u64
)-1)
3102 /* search_forward needs a path with locks held, do the
3103 * search again for the original key. It is possible
3104 * this will race with a balance and return a path that
3105 * we could modify, but this drop is just an optimization
3106 * and is allowed to miss some leaves.
3108 btrfs_release_path(root
, path
);
3111 /* setup a max key for search_forward */
3112 other_key
.offset
= (u64
)-1;
3113 other_key
.type
= key
.type
;
3114 other_key
.objectid
= key
.objectid
;
3116 path
->keep_locks
= 1;
3117 ret
= btrfs_search_forward(root
, &found_key
, &other_key
,
3119 path
->keep_locks
= 0;
3120 if (ret
|| found_key
.objectid
!= key
.objectid
||
3121 found_key
.type
!= key
.type
) {
3126 key
.offset
= found_key
.offset
;
3127 btrfs_release_path(root
, path
);
3132 /* we know there's one more slot after us in the tree,
3133 * read that key so we can verify it is also a checksum item
3135 btrfs_node_key_to_cpu(path
->nodes
[1], &other_key
, path
->slots
[1] + 1);
3137 if (found_key
.objectid
< inode
->i_ino
)
3140 if (found_key
.type
!= key
.type
|| found_key
.offset
< new_size
)
3144 * if the key for the next leaf isn't a csum key from this objectid,
3145 * we can't be sure there aren't good items inside this leaf.
3148 if (other_key
.objectid
!= inode
->i_ino
|| other_key
.type
!= key
.type
)
3151 leaf_start
= btrfs_node_blockptr(path
->nodes
[1], path
->slots
[1]);
3152 leaf_gen
= btrfs_node_ptr_generation(path
->nodes
[1], path
->slots
[1]);
3154 * it is safe to delete this leaf, it contains only
3155 * csum items from this inode at an offset >= new_size
3157 ret
= btrfs_del_leaf(trans
, root
, path
, leaf_start
);
3160 if (root
->ref_cows
&& leaf_gen
< trans
->transid
) {
3161 ref
= btrfs_alloc_leaf_ref(root
, 0);
3163 ref
->root_gen
= root
->root_key
.offset
;
3164 ref
->bytenr
= leaf_start
;
3166 ref
->generation
= leaf_gen
;
3169 btrfs_sort_leaf_ref(ref
);
3171 ret
= btrfs_add_leaf_ref(root
, ref
, 0);
3173 btrfs_free_leaf_ref(root
, ref
);
3179 btrfs_release_path(root
, path
);
3181 if (other_key
.objectid
== inode
->i_ino
&&
3182 other_key
.type
== key
.type
&& other_key
.offset
> key
.offset
) {
3183 key
.offset
= other_key
.offset
;
3189 /* fixup any changes we've made to the path */
3190 path
->lowest_level
= 0;
3191 path
->keep_locks
= 0;
3192 btrfs_release_path(root
, path
);
3199 * this can truncate away extent items, csum items and directory items.
3200 * It starts at a high offset and removes keys until it can't find
3201 * any higher than new_size
3203 * csum items that cross the new i_size are truncated to the new size
3206 * min_type is the minimum key type to truncate down to. If set to 0, this
3207 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3209 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
3210 struct btrfs_root
*root
,
3211 struct inode
*inode
,
3212 u64 new_size
, u32 min_type
)
3214 struct btrfs_path
*path
;
3215 struct extent_buffer
*leaf
;
3216 struct btrfs_file_extent_item
*fi
;
3217 struct btrfs_key key
;
3218 struct btrfs_key found_key
;
3219 u64 extent_start
= 0;
3220 u64 extent_num_bytes
= 0;
3221 u64 extent_offset
= 0;
3223 u64 mask
= root
->sectorsize
- 1;
3224 u32 found_type
= (u8
)-1;
3227 int pending_del_nr
= 0;
3228 int pending_del_slot
= 0;
3229 int extent_type
= -1;
3234 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
3236 if (root
->ref_cows
|| root
== root
->fs_info
->tree_root
)
3237 btrfs_drop_extent_cache(inode
, new_size
& (~mask
), (u64
)-1, 0);
3239 path
= btrfs_alloc_path();
3243 key
.objectid
= inode
->i_ino
;
3244 key
.offset
= (u64
)-1;
3248 path
->leave_spinning
= 1;
3249 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3256 /* there are no items in the tree for us to truncate, we're
3259 if (path
->slots
[0] == 0)
3266 leaf
= path
->nodes
[0];
3267 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3268 found_type
= btrfs_key_type(&found_key
);
3271 if (found_key
.objectid
!= inode
->i_ino
)
3274 if (found_type
< min_type
)
3277 item_end
= found_key
.offset
;
3278 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
3279 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3280 struct btrfs_file_extent_item
);
3281 extent_type
= btrfs_file_extent_type(leaf
, fi
);
3282 encoding
= btrfs_file_extent_compression(leaf
, fi
);
3283 encoding
|= btrfs_file_extent_encryption(leaf
, fi
);
3284 encoding
|= btrfs_file_extent_other_encoding(leaf
, fi
);
3286 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3288 btrfs_file_extent_num_bytes(leaf
, fi
);
3289 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3290 item_end
+= btrfs_file_extent_inline_len(leaf
,
3295 if (found_type
> min_type
) {
3298 if (item_end
< new_size
)
3300 if (found_key
.offset
>= new_size
)
3306 /* FIXME, shrink the extent if the ref count is only 1 */
3307 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
3310 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3312 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
3313 if (!del_item
&& !encoding
) {
3314 u64 orig_num_bytes
=
3315 btrfs_file_extent_num_bytes(leaf
, fi
);
3316 extent_num_bytes
= new_size
-
3317 found_key
.offset
+ root
->sectorsize
- 1;
3318 extent_num_bytes
= extent_num_bytes
&
3319 ~((u64
)root
->sectorsize
- 1);
3320 btrfs_set_file_extent_num_bytes(leaf
, fi
,
3322 num_dec
= (orig_num_bytes
-
3324 if (root
->ref_cows
&& extent_start
!= 0)
3325 inode_sub_bytes(inode
, num_dec
);
3326 btrfs_mark_buffer_dirty(leaf
);
3329 btrfs_file_extent_disk_num_bytes(leaf
,
3331 extent_offset
= found_key
.offset
-
3332 btrfs_file_extent_offset(leaf
, fi
);
3334 /* FIXME blocksize != 4096 */
3335 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
3336 if (extent_start
!= 0) {
3339 inode_sub_bytes(inode
, num_dec
);
3342 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3344 * we can't truncate inline items that have had
3348 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
3349 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
3350 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
3351 u32 size
= new_size
- found_key
.offset
;
3353 if (root
->ref_cows
) {
3354 inode_sub_bytes(inode
, item_end
+ 1 -
3358 btrfs_file_extent_calc_inline_size(size
);
3359 ret
= btrfs_truncate_item(trans
, root
, path
,
3362 } else if (root
->ref_cows
) {
3363 inode_sub_bytes(inode
, item_end
+ 1 -
3369 if (!pending_del_nr
) {
3370 /* no pending yet, add ourselves */
3371 pending_del_slot
= path
->slots
[0];
3373 } else if (pending_del_nr
&&
3374 path
->slots
[0] + 1 == pending_del_slot
) {
3375 /* hop on the pending chunk */
3377 pending_del_slot
= path
->slots
[0];
3384 if (found_extent
&& (root
->ref_cows
||
3385 root
== root
->fs_info
->tree_root
)) {
3386 btrfs_set_path_blocking(path
);
3387 ret
= btrfs_free_extent(trans
, root
, extent_start
,
3388 extent_num_bytes
, 0,
3389 btrfs_header_owner(leaf
),
3390 inode
->i_ino
, extent_offset
);
3394 if (found_type
== BTRFS_INODE_ITEM_KEY
)
3397 if (path
->slots
[0] == 0 ||
3398 path
->slots
[0] != pending_del_slot
) {
3399 if (root
->ref_cows
) {
3403 if (pending_del_nr
) {
3404 ret
= btrfs_del_items(trans
, root
, path
,
3410 btrfs_release_path(root
, path
);
3417 if (pending_del_nr
) {
3418 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
3422 btrfs_free_path(path
);
3427 * taken from block_truncate_page, but does cow as it zeros out
3428 * any bytes left in the last page in the file.
3430 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
3432 struct inode
*inode
= mapping
->host
;
3433 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3434 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3435 struct btrfs_ordered_extent
*ordered
;
3436 struct extent_state
*cached_state
= NULL
;
3438 u32 blocksize
= root
->sectorsize
;
3439 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
3440 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
3446 if ((offset
& (blocksize
- 1)) == 0)
3448 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
3454 page
= grab_cache_page(mapping
, index
);
3456 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
3460 page_start
= page_offset(page
);
3461 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3463 if (!PageUptodate(page
)) {
3464 ret
= btrfs_readpage(NULL
, page
);
3466 if (page
->mapping
!= mapping
) {
3468 page_cache_release(page
);
3471 if (!PageUptodate(page
)) {
3476 wait_on_page_writeback(page
);
3478 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
,
3480 set_page_extent_mapped(page
);
3482 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
3484 unlock_extent_cached(io_tree
, page_start
, page_end
,
3485 &cached_state
, GFP_NOFS
);
3487 page_cache_release(page
);
3488 btrfs_start_ordered_extent(inode
, ordered
, 1);
3489 btrfs_put_ordered_extent(ordered
);
3493 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
3494 EXTENT_DIRTY
| EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
,
3495 0, 0, &cached_state
, GFP_NOFS
);
3497 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
3500 unlock_extent_cached(io_tree
, page_start
, page_end
,
3501 &cached_state
, GFP_NOFS
);
3506 if (offset
!= PAGE_CACHE_SIZE
) {
3508 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
3509 flush_dcache_page(page
);
3512 ClearPageChecked(page
);
3513 set_page_dirty(page
);
3514 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
3519 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
3521 page_cache_release(page
);
3526 int btrfs_cont_expand(struct inode
*inode
, loff_t size
)
3528 struct btrfs_trans_handle
*trans
;
3529 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3530 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3531 struct extent_map
*em
= NULL
;
3532 struct extent_state
*cached_state
= NULL
;
3533 u64 mask
= root
->sectorsize
- 1;
3534 u64 hole_start
= (inode
->i_size
+ mask
) & ~mask
;
3535 u64 block_end
= (size
+ mask
) & ~mask
;
3541 if (size
<= hole_start
)
3545 struct btrfs_ordered_extent
*ordered
;
3546 btrfs_wait_ordered_range(inode
, hole_start
,
3547 block_end
- hole_start
);
3548 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
3549 &cached_state
, GFP_NOFS
);
3550 ordered
= btrfs_lookup_ordered_extent(inode
, hole_start
);
3553 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
3554 &cached_state
, GFP_NOFS
);
3555 btrfs_put_ordered_extent(ordered
);
3558 cur_offset
= hole_start
;
3560 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
3561 block_end
- cur_offset
, 0);
3562 BUG_ON(IS_ERR(em
) || !em
);
3563 last_byte
= min(extent_map_end(em
), block_end
);
3564 last_byte
= (last_byte
+ mask
) & ~mask
;
3565 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
3567 hole_size
= last_byte
- cur_offset
;
3569 trans
= btrfs_start_transaction(root
, 2);
3570 if (IS_ERR(trans
)) {
3571 err
= PTR_ERR(trans
);
3574 btrfs_set_trans_block_group(trans
, inode
);
3576 err
= btrfs_drop_extents(trans
, inode
, cur_offset
,
3577 cur_offset
+ hole_size
,
3581 err
= btrfs_insert_file_extent(trans
, root
,
3582 inode
->i_ino
, cur_offset
, 0,
3583 0, hole_size
, 0, hole_size
,
3587 btrfs_drop_extent_cache(inode
, hole_start
,
3590 btrfs_end_transaction(trans
, root
);
3592 free_extent_map(em
);
3594 cur_offset
= last_byte
;
3595 if (cur_offset
>= block_end
)
3599 free_extent_map(em
);
3600 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
3605 static int btrfs_setattr_size(struct inode
*inode
, struct iattr
*attr
)
3607 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3608 struct btrfs_trans_handle
*trans
;
3612 if (attr
->ia_size
== inode
->i_size
)
3615 if (attr
->ia_size
> inode
->i_size
) {
3616 unsigned long limit
;
3617 limit
= current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
3618 if (attr
->ia_size
> inode
->i_sb
->s_maxbytes
)
3620 if (limit
!= RLIM_INFINITY
&& attr
->ia_size
> limit
) {
3621 send_sig(SIGXFSZ
, current
, 0);
3626 trans
= btrfs_start_transaction(root
, 5);
3628 return PTR_ERR(trans
);
3630 btrfs_set_trans_block_group(trans
, inode
);
3632 ret
= btrfs_orphan_add(trans
, inode
);
3635 nr
= trans
->blocks_used
;
3636 btrfs_end_transaction(trans
, root
);
3637 btrfs_btree_balance_dirty(root
, nr
);
3639 if (attr
->ia_size
> inode
->i_size
) {
3640 ret
= btrfs_cont_expand(inode
, attr
->ia_size
);
3642 btrfs_truncate(inode
);
3646 i_size_write(inode
, attr
->ia_size
);
3647 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
3649 trans
= btrfs_start_transaction(root
, 0);
3650 BUG_ON(IS_ERR(trans
));
3651 btrfs_set_trans_block_group(trans
, inode
);
3652 trans
->block_rsv
= root
->orphan_block_rsv
;
3653 BUG_ON(!trans
->block_rsv
);
3655 ret
= btrfs_update_inode(trans
, root
, inode
);
3657 if (inode
->i_nlink
> 0) {
3658 ret
= btrfs_orphan_del(trans
, inode
);
3661 nr
= trans
->blocks_used
;
3662 btrfs_end_transaction(trans
, root
);
3663 btrfs_btree_balance_dirty(root
, nr
);
3668 * We're truncating a file that used to have good data down to
3669 * zero. Make sure it gets into the ordered flush list so that
3670 * any new writes get down to disk quickly.
3672 if (attr
->ia_size
== 0)
3673 BTRFS_I(inode
)->ordered_data_close
= 1;
3675 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3676 ret
= vmtruncate(inode
, attr
->ia_size
);
3682 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
3684 struct inode
*inode
= dentry
->d_inode
;
3687 err
= inode_change_ok(inode
, attr
);
3691 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
3692 err
= btrfs_setattr_size(inode
, attr
);
3697 if (attr
->ia_valid
) {
3698 setattr_copy(inode
, attr
);
3699 mark_inode_dirty(inode
);
3701 if (attr
->ia_valid
& ATTR_MODE
)
3702 err
= btrfs_acl_chmod(inode
);
3708 void btrfs_evict_inode(struct inode
*inode
)
3710 struct btrfs_trans_handle
*trans
;
3711 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3715 truncate_inode_pages(&inode
->i_data
, 0);
3716 if (inode
->i_nlink
&& (btrfs_root_refs(&root
->root_item
) != 0 ||
3717 root
== root
->fs_info
->tree_root
))
3720 if (is_bad_inode(inode
)) {
3721 btrfs_orphan_del(NULL
, inode
);
3724 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
3725 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
3727 if (root
->fs_info
->log_root_recovering
) {
3728 BUG_ON(!list_empty(&BTRFS_I(inode
)->i_orphan
));
3732 if (inode
->i_nlink
> 0) {
3733 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0);
3737 btrfs_i_size_write(inode
, 0);
3740 trans
= btrfs_start_transaction(root
, 0);
3741 BUG_ON(IS_ERR(trans
));
3742 btrfs_set_trans_block_group(trans
, inode
);
3743 trans
->block_rsv
= root
->orphan_block_rsv
;
3745 ret
= btrfs_block_rsv_check(trans
, root
,
3746 root
->orphan_block_rsv
, 0, 5);
3748 BUG_ON(ret
!= -EAGAIN
);
3749 ret
= btrfs_commit_transaction(trans
, root
);
3754 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
3758 nr
= trans
->blocks_used
;
3759 btrfs_end_transaction(trans
, root
);
3761 btrfs_btree_balance_dirty(root
, nr
);
3766 ret
= btrfs_orphan_del(trans
, inode
);
3770 nr
= trans
->blocks_used
;
3771 btrfs_end_transaction(trans
, root
);
3772 btrfs_btree_balance_dirty(root
, nr
);
3774 end_writeback(inode
);
3779 * this returns the key found in the dir entry in the location pointer.
3780 * If no dir entries were found, location->objectid is 0.
3782 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
3783 struct btrfs_key
*location
)
3785 const char *name
= dentry
->d_name
.name
;
3786 int namelen
= dentry
->d_name
.len
;
3787 struct btrfs_dir_item
*di
;
3788 struct btrfs_path
*path
;
3789 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3792 path
= btrfs_alloc_path();
3795 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
3800 if (!di
|| IS_ERR(di
))
3803 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
3805 btrfs_free_path(path
);
3808 location
->objectid
= 0;
3813 * when we hit a tree root in a directory, the btrfs part of the inode
3814 * needs to be changed to reflect the root directory of the tree root. This
3815 * is kind of like crossing a mount point.
3817 static int fixup_tree_root_location(struct btrfs_root
*root
,
3819 struct dentry
*dentry
,
3820 struct btrfs_key
*location
,
3821 struct btrfs_root
**sub_root
)
3823 struct btrfs_path
*path
;
3824 struct btrfs_root
*new_root
;
3825 struct btrfs_root_ref
*ref
;
3826 struct extent_buffer
*leaf
;
3830 path
= btrfs_alloc_path();
3837 ret
= btrfs_find_root_ref(root
->fs_info
->tree_root
, path
,
3838 BTRFS_I(dir
)->root
->root_key
.objectid
,
3839 location
->objectid
);
3846 leaf
= path
->nodes
[0];
3847 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
3848 if (btrfs_root_ref_dirid(leaf
, ref
) != dir
->i_ino
||
3849 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
3852 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
3853 (unsigned long)(ref
+ 1),
3854 dentry
->d_name
.len
);
3858 btrfs_release_path(root
->fs_info
->tree_root
, path
);
3860 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
3861 if (IS_ERR(new_root
)) {
3862 err
= PTR_ERR(new_root
);
3866 if (btrfs_root_refs(&new_root
->root_item
) == 0) {
3871 *sub_root
= new_root
;
3872 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
3873 location
->type
= BTRFS_INODE_ITEM_KEY
;
3874 location
->offset
= 0;
3877 btrfs_free_path(path
);
3881 static void inode_tree_add(struct inode
*inode
)
3883 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3884 struct btrfs_inode
*entry
;
3886 struct rb_node
*parent
;
3888 p
= &root
->inode_tree
.rb_node
;
3891 if (hlist_unhashed(&inode
->i_hash
))
3894 spin_lock(&root
->inode_lock
);
3897 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
3899 if (inode
->i_ino
< entry
->vfs_inode
.i_ino
)
3900 p
= &parent
->rb_left
;
3901 else if (inode
->i_ino
> entry
->vfs_inode
.i_ino
)
3902 p
= &parent
->rb_right
;
3904 WARN_ON(!(entry
->vfs_inode
.i_state
&
3905 (I_WILL_FREE
| I_FREEING
)));
3906 rb_erase(parent
, &root
->inode_tree
);
3907 RB_CLEAR_NODE(parent
);
3908 spin_unlock(&root
->inode_lock
);
3912 rb_link_node(&BTRFS_I(inode
)->rb_node
, parent
, p
);
3913 rb_insert_color(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
3914 spin_unlock(&root
->inode_lock
);
3917 static void inode_tree_del(struct inode
*inode
)
3919 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3922 spin_lock(&root
->inode_lock
);
3923 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
3924 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
3925 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
3926 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
3928 spin_unlock(&root
->inode_lock
);
3931 * Free space cache has inodes in the tree root, but the tree root has a
3932 * root_refs of 0, so this could end up dropping the tree root as a
3933 * snapshot, so we need the extra !root->fs_info->tree_root check to
3934 * make sure we don't drop it.
3936 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0 &&
3937 root
!= root
->fs_info
->tree_root
) {
3938 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
3939 spin_lock(&root
->inode_lock
);
3940 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
3941 spin_unlock(&root
->inode_lock
);
3943 btrfs_add_dead_root(root
);
3947 int btrfs_invalidate_inodes(struct btrfs_root
*root
)
3949 struct rb_node
*node
;
3950 struct rb_node
*prev
;
3951 struct btrfs_inode
*entry
;
3952 struct inode
*inode
;
3955 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
3957 spin_lock(&root
->inode_lock
);
3959 node
= root
->inode_tree
.rb_node
;
3963 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
3965 if (objectid
< entry
->vfs_inode
.i_ino
)
3966 node
= node
->rb_left
;
3967 else if (objectid
> entry
->vfs_inode
.i_ino
)
3968 node
= node
->rb_right
;
3974 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
3975 if (objectid
<= entry
->vfs_inode
.i_ino
) {
3979 prev
= rb_next(prev
);
3983 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
3984 objectid
= entry
->vfs_inode
.i_ino
+ 1;
3985 inode
= igrab(&entry
->vfs_inode
);
3987 spin_unlock(&root
->inode_lock
);
3988 if (atomic_read(&inode
->i_count
) > 1)
3989 d_prune_aliases(inode
);
3991 * btrfs_drop_inode will have it removed from
3992 * the inode cache when its usage count
3997 spin_lock(&root
->inode_lock
);
4001 if (cond_resched_lock(&root
->inode_lock
))
4004 node
= rb_next(node
);
4006 spin_unlock(&root
->inode_lock
);
4010 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
4012 struct btrfs_iget_args
*args
= p
;
4013 inode
->i_ino
= args
->ino
;
4014 BTRFS_I(inode
)->root
= args
->root
;
4015 btrfs_set_inode_space_info(args
->root
, inode
);
4019 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
4021 struct btrfs_iget_args
*args
= opaque
;
4022 return args
->ino
== inode
->i_ino
&&
4023 args
->root
== BTRFS_I(inode
)->root
;
4026 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
4028 struct btrfs_root
*root
)
4030 struct inode
*inode
;
4031 struct btrfs_iget_args args
;
4032 args
.ino
= objectid
;
4035 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
4036 btrfs_init_locked_inode
,
4041 /* Get an inode object given its location and corresponding root.
4042 * Returns in *is_new if the inode was read from disk
4044 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
4045 struct btrfs_root
*root
, int *new)
4047 struct inode
*inode
;
4049 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
4051 return ERR_PTR(-ENOMEM
);
4053 if (inode
->i_state
& I_NEW
) {
4054 BTRFS_I(inode
)->root
= root
;
4055 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
4056 btrfs_read_locked_inode(inode
);
4058 inode_tree_add(inode
);
4059 unlock_new_inode(inode
);
4067 static struct inode
*new_simple_dir(struct super_block
*s
,
4068 struct btrfs_key
*key
,
4069 struct btrfs_root
*root
)
4071 struct inode
*inode
= new_inode(s
);
4074 return ERR_PTR(-ENOMEM
);
4076 BTRFS_I(inode
)->root
= root
;
4077 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
4078 BTRFS_I(inode
)->dummy_inode
= 1;
4080 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
4081 inode
->i_op
= &simple_dir_inode_operations
;
4082 inode
->i_fop
= &simple_dir_operations
;
4083 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
4084 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4089 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
4091 struct inode
*inode
;
4092 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4093 struct btrfs_root
*sub_root
= root
;
4094 struct btrfs_key location
;
4098 dentry
->d_op
= &btrfs_dentry_operations
;
4100 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
4101 return ERR_PTR(-ENAMETOOLONG
);
4103 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
4106 return ERR_PTR(ret
);
4108 if (location
.objectid
== 0)
4111 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
4112 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
4116 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
4118 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
4119 ret
= fixup_tree_root_location(root
, dir
, dentry
,
4120 &location
, &sub_root
);
4123 inode
= ERR_PTR(ret
);
4125 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
4127 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
4129 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
4131 if (root
!= sub_root
) {
4132 down_read(&root
->fs_info
->cleanup_work_sem
);
4133 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
4134 btrfs_orphan_cleanup(sub_root
);
4135 up_read(&root
->fs_info
->cleanup_work_sem
);
4141 static int btrfs_dentry_delete(struct dentry
*dentry
)
4143 struct btrfs_root
*root
;
4145 if (!dentry
->d_inode
&& !IS_ROOT(dentry
))
4146 dentry
= dentry
->d_parent
;
4148 if (dentry
->d_inode
) {
4149 root
= BTRFS_I(dentry
->d_inode
)->root
;
4150 if (btrfs_root_refs(&root
->root_item
) == 0)
4156 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
4157 struct nameidata
*nd
)
4159 struct inode
*inode
;
4161 inode
= btrfs_lookup_dentry(dir
, dentry
);
4163 return ERR_CAST(inode
);
4165 return d_splice_alias(inode
, dentry
);
4168 static unsigned char btrfs_filetype_table
[] = {
4169 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
4172 static int btrfs_real_readdir(struct file
*filp
, void *dirent
,
4175 struct inode
*inode
= filp
->f_dentry
->d_inode
;
4176 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4177 struct btrfs_item
*item
;
4178 struct btrfs_dir_item
*di
;
4179 struct btrfs_key key
;
4180 struct btrfs_key found_key
;
4181 struct btrfs_path
*path
;
4184 struct extent_buffer
*leaf
;
4187 unsigned char d_type
;
4192 int key_type
= BTRFS_DIR_INDEX_KEY
;
4197 /* FIXME, use a real flag for deciding about the key type */
4198 if (root
->fs_info
->tree_root
== root
)
4199 key_type
= BTRFS_DIR_ITEM_KEY
;
4201 /* special case for "." */
4202 if (filp
->f_pos
== 0) {
4203 over
= filldir(dirent
, ".", 1,
4210 /* special case for .., just use the back ref */
4211 if (filp
->f_pos
== 1) {
4212 u64 pino
= parent_ino(filp
->f_path
.dentry
);
4213 over
= filldir(dirent
, "..", 2,
4219 path
= btrfs_alloc_path();
4222 btrfs_set_key_type(&key
, key_type
);
4223 key
.offset
= filp
->f_pos
;
4224 key
.objectid
= inode
->i_ino
;
4226 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4232 leaf
= path
->nodes
[0];
4233 nritems
= btrfs_header_nritems(leaf
);
4234 slot
= path
->slots
[0];
4235 if (advance
|| slot
>= nritems
) {
4236 if (slot
>= nritems
- 1) {
4237 ret
= btrfs_next_leaf(root
, path
);
4240 leaf
= path
->nodes
[0];
4241 nritems
= btrfs_header_nritems(leaf
);
4242 slot
= path
->slots
[0];
4250 item
= btrfs_item_nr(leaf
, slot
);
4251 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
4253 if (found_key
.objectid
!= key
.objectid
)
4255 if (btrfs_key_type(&found_key
) != key_type
)
4257 if (found_key
.offset
< filp
->f_pos
)
4260 filp
->f_pos
= found_key
.offset
;
4262 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
4264 di_total
= btrfs_item_size(leaf
, item
);
4266 while (di_cur
< di_total
) {
4267 struct btrfs_key location
;
4269 name_len
= btrfs_dir_name_len(leaf
, di
);
4270 if (name_len
<= sizeof(tmp_name
)) {
4271 name_ptr
= tmp_name
;
4273 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
4279 read_extent_buffer(leaf
, name_ptr
,
4280 (unsigned long)(di
+ 1), name_len
);
4282 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
4283 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
4285 /* is this a reference to our own snapshot? If so
4288 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
4289 location
.objectid
== root
->root_key
.objectid
) {
4293 over
= filldir(dirent
, name_ptr
, name_len
,
4294 found_key
.offset
, location
.objectid
,
4298 if (name_ptr
!= tmp_name
)
4303 di_len
= btrfs_dir_name_len(leaf
, di
) +
4304 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
4306 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
4310 /* Reached end of directory/root. Bump pos past the last item. */
4311 if (key_type
== BTRFS_DIR_INDEX_KEY
)
4313 * 32-bit glibc will use getdents64, but then strtol -
4314 * so the last number we can serve is this.
4316 filp
->f_pos
= 0x7fffffff;
4322 btrfs_free_path(path
);
4326 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
4328 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4329 struct btrfs_trans_handle
*trans
;
4331 bool nolock
= false;
4333 if (BTRFS_I(inode
)->dummy_inode
)
4337 nolock
= (root
->fs_info
->closing
&& root
== root
->fs_info
->tree_root
);
4339 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
4341 trans
= btrfs_join_transaction_nolock(root
, 1);
4343 trans
= btrfs_join_transaction(root
, 1);
4344 btrfs_set_trans_block_group(trans
, inode
);
4346 ret
= btrfs_end_transaction_nolock(trans
, root
);
4348 ret
= btrfs_commit_transaction(trans
, root
);
4354 * This is somewhat expensive, updating the tree every time the
4355 * inode changes. But, it is most likely to find the inode in cache.
4356 * FIXME, needs more benchmarking...there are no reasons other than performance
4357 * to keep or drop this code.
4359 void btrfs_dirty_inode(struct inode
*inode
)
4361 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4362 struct btrfs_trans_handle
*trans
;
4365 if (BTRFS_I(inode
)->dummy_inode
)
4368 trans
= btrfs_join_transaction(root
, 1);
4369 btrfs_set_trans_block_group(trans
, inode
);
4371 ret
= btrfs_update_inode(trans
, root
, inode
);
4372 if (ret
&& ret
== -ENOSPC
) {
4373 /* whoops, lets try again with the full transaction */
4374 btrfs_end_transaction(trans
, root
);
4375 trans
= btrfs_start_transaction(root
, 1);
4376 if (IS_ERR(trans
)) {
4377 if (printk_ratelimit()) {
4378 printk(KERN_ERR
"btrfs: fail to "
4379 "dirty inode %lu error %ld\n",
4380 inode
->i_ino
, PTR_ERR(trans
));
4384 btrfs_set_trans_block_group(trans
, inode
);
4386 ret
= btrfs_update_inode(trans
, root
, inode
);
4388 if (printk_ratelimit()) {
4389 printk(KERN_ERR
"btrfs: fail to "
4390 "dirty inode %lu error %d\n",
4395 btrfs_end_transaction(trans
, root
);
4399 * find the highest existing sequence number in a directory
4400 * and then set the in-memory index_cnt variable to reflect
4401 * free sequence numbers
4403 static int btrfs_set_inode_index_count(struct inode
*inode
)
4405 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4406 struct btrfs_key key
, found_key
;
4407 struct btrfs_path
*path
;
4408 struct extent_buffer
*leaf
;
4411 key
.objectid
= inode
->i_ino
;
4412 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
4413 key
.offset
= (u64
)-1;
4415 path
= btrfs_alloc_path();
4419 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4422 /* FIXME: we should be able to handle this */
4428 * MAGIC NUMBER EXPLANATION:
4429 * since we search a directory based on f_pos we have to start at 2
4430 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4431 * else has to start at 2
4433 if (path
->slots
[0] == 0) {
4434 BTRFS_I(inode
)->index_cnt
= 2;
4440 leaf
= path
->nodes
[0];
4441 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4443 if (found_key
.objectid
!= inode
->i_ino
||
4444 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
4445 BTRFS_I(inode
)->index_cnt
= 2;
4449 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
4451 btrfs_free_path(path
);
4456 * helper to find a free sequence number in a given directory. This current
4457 * code is very simple, later versions will do smarter things in the btree
4459 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
4463 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
4464 ret
= btrfs_set_inode_index_count(dir
);
4469 *index
= BTRFS_I(dir
)->index_cnt
;
4470 BTRFS_I(dir
)->index_cnt
++;
4475 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
4476 struct btrfs_root
*root
,
4478 const char *name
, int name_len
,
4479 u64 ref_objectid
, u64 objectid
,
4480 u64 alloc_hint
, int mode
, u64
*index
)
4482 struct inode
*inode
;
4483 struct btrfs_inode_item
*inode_item
;
4484 struct btrfs_key
*location
;
4485 struct btrfs_path
*path
;
4486 struct btrfs_inode_ref
*ref
;
4487 struct btrfs_key key
[2];
4493 path
= btrfs_alloc_path();
4496 inode
= new_inode(root
->fs_info
->sb
);
4498 return ERR_PTR(-ENOMEM
);
4501 ret
= btrfs_set_inode_index(dir
, index
);
4504 return ERR_PTR(ret
);
4508 * index_cnt is ignored for everything but a dir,
4509 * btrfs_get_inode_index_count has an explanation for the magic
4512 BTRFS_I(inode
)->index_cnt
= 2;
4513 BTRFS_I(inode
)->root
= root
;
4514 BTRFS_I(inode
)->generation
= trans
->transid
;
4515 btrfs_set_inode_space_info(root
, inode
);
4521 BTRFS_I(inode
)->block_group
=
4522 btrfs_find_block_group(root
, 0, alloc_hint
, owner
);
4524 key
[0].objectid
= objectid
;
4525 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
4528 key
[1].objectid
= objectid
;
4529 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
4530 key
[1].offset
= ref_objectid
;
4532 sizes
[0] = sizeof(struct btrfs_inode_item
);
4533 sizes
[1] = name_len
+ sizeof(*ref
);
4535 path
->leave_spinning
= 1;
4536 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
4540 inode_init_owner(inode
, dir
, mode
);
4541 inode
->i_ino
= objectid
;
4542 inode_set_bytes(inode
, 0);
4543 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4544 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4545 struct btrfs_inode_item
);
4546 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
4548 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
4549 struct btrfs_inode_ref
);
4550 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
4551 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
4552 ptr
= (unsigned long)(ref
+ 1);
4553 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
4555 btrfs_mark_buffer_dirty(path
->nodes
[0]);
4556 btrfs_free_path(path
);
4558 location
= &BTRFS_I(inode
)->location
;
4559 location
->objectid
= objectid
;
4560 location
->offset
= 0;
4561 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
4563 btrfs_inherit_iflags(inode
, dir
);
4565 if ((mode
& S_IFREG
)) {
4566 if (btrfs_test_opt(root
, NODATASUM
))
4567 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
4568 if (btrfs_test_opt(root
, NODATACOW
))
4569 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
;
4572 insert_inode_hash(inode
);
4573 inode_tree_add(inode
);
4577 BTRFS_I(dir
)->index_cnt
--;
4578 btrfs_free_path(path
);
4580 return ERR_PTR(ret
);
4583 static inline u8
btrfs_inode_type(struct inode
*inode
)
4585 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
4589 * utility function to add 'inode' into 'parent_inode' with
4590 * a give name and a given sequence number.
4591 * if 'add_backref' is true, also insert a backref from the
4592 * inode to the parent directory.
4594 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
4595 struct inode
*parent_inode
, struct inode
*inode
,
4596 const char *name
, int name_len
, int add_backref
, u64 index
)
4599 struct btrfs_key key
;
4600 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
4602 if (unlikely(inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4603 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
4605 key
.objectid
= inode
->i_ino
;
4606 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
4610 if (unlikely(inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4611 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
4612 key
.objectid
, root
->root_key
.objectid
,
4613 parent_inode
->i_ino
,
4614 index
, name
, name_len
);
4615 } else if (add_backref
) {
4616 ret
= btrfs_insert_inode_ref(trans
, root
,
4617 name
, name_len
, inode
->i_ino
,
4618 parent_inode
->i_ino
, index
);
4622 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
4623 parent_inode
->i_ino
, &key
,
4624 btrfs_inode_type(inode
), index
);
4627 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
4629 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
4630 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
4635 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
4636 struct dentry
*dentry
, struct inode
*inode
,
4637 int backref
, u64 index
)
4639 int err
= btrfs_add_link(trans
, dentry
->d_parent
->d_inode
,
4640 inode
, dentry
->d_name
.name
,
4641 dentry
->d_name
.len
, backref
, index
);
4643 d_instantiate(dentry
, inode
);
4651 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
4652 int mode
, dev_t rdev
)
4654 struct btrfs_trans_handle
*trans
;
4655 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4656 struct inode
*inode
= NULL
;
4660 unsigned long nr
= 0;
4663 if (!new_valid_dev(rdev
))
4666 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
4671 * 2 for inode item and ref
4673 * 1 for xattr if selinux is on
4675 trans
= btrfs_start_transaction(root
, 5);
4677 return PTR_ERR(trans
);
4679 btrfs_set_trans_block_group(trans
, dir
);
4681 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4683 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
4684 BTRFS_I(dir
)->block_group
, mode
, &index
);
4685 err
= PTR_ERR(inode
);
4689 err
= btrfs_init_inode_security(trans
, inode
, dir
);
4695 btrfs_set_trans_block_group(trans
, inode
);
4696 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0, index
);
4700 inode
->i_op
= &btrfs_special_inode_operations
;
4701 init_special_inode(inode
, inode
->i_mode
, rdev
);
4702 btrfs_update_inode(trans
, root
, inode
);
4704 btrfs_update_inode_block_group(trans
, inode
);
4705 btrfs_update_inode_block_group(trans
, dir
);
4707 nr
= trans
->blocks_used
;
4708 btrfs_end_transaction_throttle(trans
, root
);
4709 btrfs_btree_balance_dirty(root
, nr
);
4711 inode_dec_link_count(inode
);
4717 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
4718 int mode
, struct nameidata
*nd
)
4720 struct btrfs_trans_handle
*trans
;
4721 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4722 struct inode
*inode
= NULL
;
4725 unsigned long nr
= 0;
4729 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
4733 * 2 for inode item and ref
4735 * 1 for xattr if selinux is on
4737 trans
= btrfs_start_transaction(root
, 5);
4739 return PTR_ERR(trans
);
4741 btrfs_set_trans_block_group(trans
, dir
);
4743 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4745 dentry
->d_parent
->d_inode
->i_ino
,
4746 objectid
, BTRFS_I(dir
)->block_group
, mode
,
4748 err
= PTR_ERR(inode
);
4752 err
= btrfs_init_inode_security(trans
, inode
, dir
);
4758 btrfs_set_trans_block_group(trans
, inode
);
4759 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0, index
);
4763 inode
->i_mapping
->a_ops
= &btrfs_aops
;
4764 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
4765 inode
->i_fop
= &btrfs_file_operations
;
4766 inode
->i_op
= &btrfs_file_inode_operations
;
4767 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
4769 btrfs_update_inode_block_group(trans
, inode
);
4770 btrfs_update_inode_block_group(trans
, dir
);
4772 nr
= trans
->blocks_used
;
4773 btrfs_end_transaction_throttle(trans
, root
);
4775 inode_dec_link_count(inode
);
4778 btrfs_btree_balance_dirty(root
, nr
);
4782 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
4783 struct dentry
*dentry
)
4785 struct btrfs_trans_handle
*trans
;
4786 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4787 struct inode
*inode
= old_dentry
->d_inode
;
4789 unsigned long nr
= 0;
4793 if (inode
->i_nlink
== 0)
4796 /* do not allow sys_link's with other subvols of the same device */
4797 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
4800 btrfs_inc_nlink(inode
);
4802 err
= btrfs_set_inode_index(dir
, &index
);
4807 * 1 item for inode ref
4808 * 2 items for dir items
4810 trans
= btrfs_start_transaction(root
, 3);
4811 if (IS_ERR(trans
)) {
4812 err
= PTR_ERR(trans
);
4816 btrfs_set_trans_block_group(trans
, dir
);
4817 atomic_inc(&inode
->i_count
);
4819 err
= btrfs_add_nondir(trans
, dentry
, inode
, 1, index
);
4824 btrfs_update_inode_block_group(trans
, dir
);
4825 err
= btrfs_update_inode(trans
, root
, inode
);
4827 btrfs_log_new_name(trans
, inode
, NULL
, dentry
->d_parent
);
4830 nr
= trans
->blocks_used
;
4831 btrfs_end_transaction_throttle(trans
, root
);
4834 inode_dec_link_count(inode
);
4837 btrfs_btree_balance_dirty(root
, nr
);
4841 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
4843 struct inode
*inode
= NULL
;
4844 struct btrfs_trans_handle
*trans
;
4845 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4847 int drop_on_err
= 0;
4850 unsigned long nr
= 1;
4852 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
4857 * 2 items for inode and ref
4858 * 2 items for dir items
4859 * 1 for xattr if selinux is on
4861 trans
= btrfs_start_transaction(root
, 5);
4863 return PTR_ERR(trans
);
4864 btrfs_set_trans_block_group(trans
, dir
);
4866 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4868 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
4869 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
,
4871 if (IS_ERR(inode
)) {
4872 err
= PTR_ERR(inode
);
4878 err
= btrfs_init_inode_security(trans
, inode
, dir
);
4882 inode
->i_op
= &btrfs_dir_inode_operations
;
4883 inode
->i_fop
= &btrfs_dir_file_operations
;
4884 btrfs_set_trans_block_group(trans
, inode
);
4886 btrfs_i_size_write(inode
, 0);
4887 err
= btrfs_update_inode(trans
, root
, inode
);
4891 err
= btrfs_add_link(trans
, dentry
->d_parent
->d_inode
,
4892 inode
, dentry
->d_name
.name
,
4893 dentry
->d_name
.len
, 0, index
);
4897 d_instantiate(dentry
, inode
);
4899 btrfs_update_inode_block_group(trans
, inode
);
4900 btrfs_update_inode_block_group(trans
, dir
);
4903 nr
= trans
->blocks_used
;
4904 btrfs_end_transaction_throttle(trans
, root
);
4907 btrfs_btree_balance_dirty(root
, nr
);
4911 /* helper for btfs_get_extent. Given an existing extent in the tree,
4912 * and an extent that you want to insert, deal with overlap and insert
4913 * the new extent into the tree.
4915 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
4916 struct extent_map
*existing
,
4917 struct extent_map
*em
,
4918 u64 map_start
, u64 map_len
)
4922 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
4923 start_diff
= map_start
- em
->start
;
4924 em
->start
= map_start
;
4926 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
4927 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
4928 em
->block_start
+= start_diff
;
4929 em
->block_len
-= start_diff
;
4931 return add_extent_mapping(em_tree
, em
);
4934 static noinline
int uncompress_inline(struct btrfs_path
*path
,
4935 struct inode
*inode
, struct page
*page
,
4936 size_t pg_offset
, u64 extent_offset
,
4937 struct btrfs_file_extent_item
*item
)
4940 struct extent_buffer
*leaf
= path
->nodes
[0];
4943 unsigned long inline_size
;
4946 WARN_ON(pg_offset
!= 0);
4947 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
4948 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
4949 btrfs_item_nr(leaf
, path
->slots
[0]));
4950 tmp
= kmalloc(inline_size
, GFP_NOFS
);
4951 ptr
= btrfs_file_extent_inline_start(item
);
4953 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
4955 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
4956 ret
= btrfs_zlib_decompress(tmp
, page
, extent_offset
,
4957 inline_size
, max_size
);
4959 char *kaddr
= kmap_atomic(page
, KM_USER0
);
4960 unsigned long copy_size
= min_t(u64
,
4961 PAGE_CACHE_SIZE
- pg_offset
,
4962 max_size
- extent_offset
);
4963 memset(kaddr
+ pg_offset
, 0, copy_size
);
4964 kunmap_atomic(kaddr
, KM_USER0
);
4971 * a bit scary, this does extent mapping from logical file offset to the disk.
4972 * the ugly parts come from merging extents from the disk with the in-ram
4973 * representation. This gets more complex because of the data=ordered code,
4974 * where the in-ram extents might be locked pending data=ordered completion.
4976 * This also copies inline extents directly into the page.
4979 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
4980 size_t pg_offset
, u64 start
, u64 len
,
4986 u64 extent_start
= 0;
4988 u64 objectid
= inode
->i_ino
;
4990 struct btrfs_path
*path
= NULL
;
4991 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4992 struct btrfs_file_extent_item
*item
;
4993 struct extent_buffer
*leaf
;
4994 struct btrfs_key found_key
;
4995 struct extent_map
*em
= NULL
;
4996 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4997 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4998 struct btrfs_trans_handle
*trans
= NULL
;
5002 read_lock(&em_tree
->lock
);
5003 em
= lookup_extent_mapping(em_tree
, start
, len
);
5005 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5006 read_unlock(&em_tree
->lock
);
5009 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
5010 free_extent_map(em
);
5011 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
5012 free_extent_map(em
);
5016 em
= alloc_extent_map(GFP_NOFS
);
5021 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5022 em
->start
= EXTENT_MAP_HOLE
;
5023 em
->orig_start
= EXTENT_MAP_HOLE
;
5025 em
->block_len
= (u64
)-1;
5028 path
= btrfs_alloc_path();
5032 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
5033 objectid
, start
, trans
!= NULL
);
5040 if (path
->slots
[0] == 0)
5045 leaf
= path
->nodes
[0];
5046 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5047 struct btrfs_file_extent_item
);
5048 /* are we inside the extent that was found? */
5049 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5050 found_type
= btrfs_key_type(&found_key
);
5051 if (found_key
.objectid
!= objectid
||
5052 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
5056 found_type
= btrfs_file_extent_type(leaf
, item
);
5057 extent_start
= found_key
.offset
;
5058 compressed
= btrfs_file_extent_compression(leaf
, item
);
5059 if (found_type
== BTRFS_FILE_EXTENT_REG
||
5060 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5061 extent_end
= extent_start
+
5062 btrfs_file_extent_num_bytes(leaf
, item
);
5063 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
5065 size
= btrfs_file_extent_inline_len(leaf
, item
);
5066 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
5067 ~((u64
)root
->sectorsize
- 1);
5070 if (start
>= extent_end
) {
5072 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
5073 ret
= btrfs_next_leaf(root
, path
);
5080 leaf
= path
->nodes
[0];
5082 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5083 if (found_key
.objectid
!= objectid
||
5084 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
5086 if (start
+ len
<= found_key
.offset
)
5089 em
->len
= found_key
.offset
- start
;
5093 if (found_type
== BTRFS_FILE_EXTENT_REG
||
5094 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5095 em
->start
= extent_start
;
5096 em
->len
= extent_end
- extent_start
;
5097 em
->orig_start
= extent_start
-
5098 btrfs_file_extent_offset(leaf
, item
);
5099 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
5101 em
->block_start
= EXTENT_MAP_HOLE
;
5105 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
5106 em
->block_start
= bytenr
;
5107 em
->block_len
= btrfs_file_extent_disk_num_bytes(leaf
,
5110 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
5111 em
->block_start
= bytenr
;
5112 em
->block_len
= em
->len
;
5113 if (found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
5114 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
5117 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
5121 size_t extent_offset
;
5124 em
->block_start
= EXTENT_MAP_INLINE
;
5125 if (!page
|| create
) {
5126 em
->start
= extent_start
;
5127 em
->len
= extent_end
- extent_start
;
5131 size
= btrfs_file_extent_inline_len(leaf
, item
);
5132 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
5133 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
5134 size
- extent_offset
);
5135 em
->start
= extent_start
+ extent_offset
;
5136 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
5137 ~((u64
)root
->sectorsize
- 1);
5138 em
->orig_start
= EXTENT_MAP_INLINE
;
5140 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
5141 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
5142 if (create
== 0 && !PageUptodate(page
)) {
5143 if (btrfs_file_extent_compression(leaf
, item
) ==
5144 BTRFS_COMPRESS_ZLIB
) {
5145 ret
= uncompress_inline(path
, inode
, page
,
5147 extent_offset
, item
);
5151 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
5153 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
5154 memset(map
+ pg_offset
+ copy_size
, 0,
5155 PAGE_CACHE_SIZE
- pg_offset
-
5160 flush_dcache_page(page
);
5161 } else if (create
&& PageUptodate(page
)) {
5165 free_extent_map(em
);
5167 btrfs_release_path(root
, path
);
5168 trans
= btrfs_join_transaction(root
, 1);
5172 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
5175 btrfs_mark_buffer_dirty(leaf
);
5177 set_extent_uptodate(io_tree
, em
->start
,
5178 extent_map_end(em
) - 1, GFP_NOFS
);
5181 printk(KERN_ERR
"btrfs unknown found_type %d\n", found_type
);
5188 em
->block_start
= EXTENT_MAP_HOLE
;
5189 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
5191 btrfs_release_path(root
, path
);
5192 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
5193 printk(KERN_ERR
"Btrfs: bad extent! em: [%llu %llu] passed "
5194 "[%llu %llu]\n", (unsigned long long)em
->start
,
5195 (unsigned long long)em
->len
,
5196 (unsigned long long)start
,
5197 (unsigned long long)len
);
5203 write_lock(&em_tree
->lock
);
5204 ret
= add_extent_mapping(em_tree
, em
);
5205 /* it is possible that someone inserted the extent into the tree
5206 * while we had the lock dropped. It is also possible that
5207 * an overlapping map exists in the tree
5209 if (ret
== -EEXIST
) {
5210 struct extent_map
*existing
;
5214 existing
= lookup_extent_mapping(em_tree
, start
, len
);
5215 if (existing
&& (existing
->start
> start
||
5216 existing
->start
+ existing
->len
<= start
)) {
5217 free_extent_map(existing
);
5221 existing
= lookup_extent_mapping(em_tree
, em
->start
,
5224 err
= merge_extent_mapping(em_tree
, existing
,
5227 free_extent_map(existing
);
5229 free_extent_map(em
);
5234 free_extent_map(em
);
5238 free_extent_map(em
);
5243 write_unlock(&em_tree
->lock
);
5246 btrfs_free_path(path
);
5248 ret
= btrfs_end_transaction(trans
, root
);
5253 free_extent_map(em
);
5254 return ERR_PTR(err
);
5259 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
5262 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5263 struct btrfs_trans_handle
*trans
;
5264 struct extent_map
*em
;
5265 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
5266 struct btrfs_key ins
;
5270 btrfs_drop_extent_cache(inode
, start
, start
+ len
- 1, 0);
5272 trans
= btrfs_join_transaction(root
, 0);
5274 return ERR_PTR(-ENOMEM
);
5276 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
5278 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
5279 ret
= btrfs_reserve_extent(trans
, root
, len
, root
->sectorsize
, 0,
5280 alloc_hint
, (u64
)-1, &ins
, 1);
5286 em
= alloc_extent_map(GFP_NOFS
);
5288 em
= ERR_PTR(-ENOMEM
);
5293 em
->orig_start
= em
->start
;
5294 em
->len
= ins
.offset
;
5296 em
->block_start
= ins
.objectid
;
5297 em
->block_len
= ins
.offset
;
5298 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5299 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
5302 write_lock(&em_tree
->lock
);
5303 ret
= add_extent_mapping(em_tree
, em
);
5304 write_unlock(&em_tree
->lock
);
5307 btrfs_drop_extent_cache(inode
, start
, start
+ em
->len
- 1, 0);
5310 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
5311 ins
.offset
, ins
.offset
, 0);
5313 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
5317 btrfs_end_transaction(trans
, root
);
5322 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5323 * block must be cow'd
5325 static noinline
int can_nocow_odirect(struct btrfs_trans_handle
*trans
,
5326 struct inode
*inode
, u64 offset
, u64 len
)
5328 struct btrfs_path
*path
;
5330 struct extent_buffer
*leaf
;
5331 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5332 struct btrfs_file_extent_item
*fi
;
5333 struct btrfs_key key
;
5341 path
= btrfs_alloc_path();
5345 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
5350 slot
= path
->slots
[0];
5353 /* can't find the item, must cow */
5360 leaf
= path
->nodes
[0];
5361 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
5362 if (key
.objectid
!= inode
->i_ino
||
5363 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
5364 /* not our file or wrong item type, must cow */
5368 if (key
.offset
> offset
) {
5369 /* Wrong offset, must cow */
5373 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
5374 found_type
= btrfs_file_extent_type(leaf
, fi
);
5375 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
5376 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
5377 /* not a regular extent, must cow */
5380 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
5381 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
5383 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
5384 if (extent_end
< offset
+ len
) {
5385 /* extent doesn't include our full range, must cow */
5389 if (btrfs_extent_readonly(root
, disk_bytenr
))
5393 * look for other files referencing this extent, if we
5394 * find any we must cow
5396 if (btrfs_cross_ref_exist(trans
, root
, inode
->i_ino
,
5397 key
.offset
- backref_offset
, disk_bytenr
))
5401 * adjust disk_bytenr and num_bytes to cover just the bytes
5402 * in this extent we are about to write. If there
5403 * are any csums in that range we have to cow in order
5404 * to keep the csums correct
5406 disk_bytenr
+= backref_offset
;
5407 disk_bytenr
+= offset
- key
.offset
;
5408 num_bytes
= min(offset
+ len
, extent_end
) - offset
;
5409 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
5412 * all of the above have passed, it is safe to overwrite this extent
5417 btrfs_free_path(path
);
5421 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
5422 struct buffer_head
*bh_result
, int create
)
5424 struct extent_map
*em
;
5425 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5426 u64 start
= iblock
<< inode
->i_blkbits
;
5427 u64 len
= bh_result
->b_size
;
5428 struct btrfs_trans_handle
*trans
;
5430 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
5435 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5436 * io. INLINE is special, and we could probably kludge it in here, but
5437 * it's still buffered so for safety lets just fall back to the generic
5440 * For COMPRESSED we _have_ to read the entire extent in so we can
5441 * decompress it, so there will be buffering required no matter what we
5442 * do, so go ahead and fallback to buffered.
5444 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5445 * to buffered IO. Don't blame me, this is the price we pay for using
5448 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
5449 em
->block_start
== EXTENT_MAP_INLINE
) {
5450 free_extent_map(em
);
5454 /* Just a good old fashioned hole, return */
5455 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
5456 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
5457 free_extent_map(em
);
5458 /* DIO will do one hole at a time, so just unlock a sector */
5459 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
,
5460 start
+ root
->sectorsize
- 1, GFP_NOFS
);
5465 * We don't allocate a new extent in the following cases
5467 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5469 * 2) The extent is marked as PREALLOC. We're good to go here and can
5470 * just use the extent.
5474 len
= em
->len
- (start
- em
->start
);
5478 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
5479 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
5480 em
->block_start
!= EXTENT_MAP_HOLE
)) {
5485 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
5486 type
= BTRFS_ORDERED_PREALLOC
;
5488 type
= BTRFS_ORDERED_NOCOW
;
5489 len
= min(len
, em
->len
- (start
- em
->start
));
5490 block_start
= em
->block_start
+ (start
- em
->start
);
5493 * we're not going to log anything, but we do need
5494 * to make sure the current transaction stays open
5495 * while we look for nocow cross refs
5497 trans
= btrfs_join_transaction(root
, 0);
5501 if (can_nocow_odirect(trans
, inode
, start
, len
) == 1) {
5502 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
5503 block_start
, len
, len
, type
);
5504 btrfs_end_transaction(trans
, root
);
5506 free_extent_map(em
);
5511 btrfs_end_transaction(trans
, root
);
5515 * this will cow the extent, reset the len in case we changed
5518 len
= bh_result
->b_size
;
5519 free_extent_map(em
);
5520 em
= btrfs_new_extent_direct(inode
, start
, len
);
5523 len
= min(len
, em
->len
- (start
- em
->start
));
5525 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
- 1,
5526 EXTENT_LOCKED
| EXTENT_DELALLOC
| EXTENT_DIRTY
, 1,
5529 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
5531 bh_result
->b_size
= len
;
5532 bh_result
->b_bdev
= em
->bdev
;
5533 set_buffer_mapped(bh_result
);
5534 if (create
&& !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
5535 set_buffer_new(bh_result
);
5537 free_extent_map(em
);
5542 struct btrfs_dio_private
{
5543 struct inode
*inode
;
5551 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
5553 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
5554 struct bio_vec
*bvec
= bio
->bi_io_vec
;
5555 struct btrfs_dio_private
*dip
= bio
->bi_private
;
5556 struct inode
*inode
= dip
->inode
;
5557 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5559 u32
*private = dip
->csums
;
5561 start
= dip
->logical_offset
;
5563 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
5564 struct page
*page
= bvec
->bv_page
;
5567 unsigned long flags
;
5569 local_irq_save(flags
);
5570 kaddr
= kmap_atomic(page
, KM_IRQ0
);
5571 csum
= btrfs_csum_data(root
, kaddr
+ bvec
->bv_offset
,
5572 csum
, bvec
->bv_len
);
5573 btrfs_csum_final(csum
, (char *)&csum
);
5574 kunmap_atomic(kaddr
, KM_IRQ0
);
5575 local_irq_restore(flags
);
5577 flush_dcache_page(bvec
->bv_page
);
5578 if (csum
!= *private) {
5579 printk(KERN_ERR
"btrfs csum failed ino %lu off"
5580 " %llu csum %u private %u\n",
5581 inode
->i_ino
, (unsigned long long)start
,
5587 start
+= bvec
->bv_len
;
5590 } while (bvec
<= bvec_end
);
5592 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
5593 dip
->logical_offset
+ dip
->bytes
- 1, GFP_NOFS
);
5594 bio
->bi_private
= dip
->private;
5598 dio_end_io(bio
, err
);
5601 static void btrfs_endio_direct_write(struct bio
*bio
, int err
)
5603 struct btrfs_dio_private
*dip
= bio
->bi_private
;
5604 struct inode
*inode
= dip
->inode
;
5605 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5606 struct btrfs_trans_handle
*trans
;
5607 struct btrfs_ordered_extent
*ordered
= NULL
;
5608 struct extent_state
*cached_state
= NULL
;
5614 ret
= btrfs_dec_test_ordered_pending(inode
, &ordered
,
5615 dip
->logical_offset
, dip
->bytes
);
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 btrfs_put_ordered_extent(ordered
);
5678 btrfs_put_ordered_extent(ordered
);
5680 bio
->bi_private
= dip
->private;
5684 dio_end_io(bio
, err
);
5687 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
5688 struct bio
*bio
, int mirror_num
,
5689 unsigned long bio_flags
, u64 offset
)
5692 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5693 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
5698 static void btrfs_submit_direct(int rw
, struct bio
*bio
, struct inode
*inode
,
5701 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5702 struct btrfs_dio_private
*dip
;
5703 struct bio_vec
*bvec
= bio
->bi_io_vec
;
5706 int write
= rw
& REQ_WRITE
;
5709 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
5711 dip
= kmalloc(sizeof(*dip
), GFP_NOFS
);
5719 dip
->csums
= kmalloc(sizeof(u32
) * bio
->bi_vcnt
, GFP_NOFS
);
5726 dip
->private = bio
->bi_private
;
5728 dip
->logical_offset
= file_offset
;
5730 start
= dip
->logical_offset
;
5733 dip
->bytes
+= bvec
->bv_len
;
5735 } while (bvec
<= (bio
->bi_io_vec
+ bio
->bi_vcnt
- 1));
5737 dip
->disk_bytenr
= (u64
)bio
->bi_sector
<< 9;
5738 bio
->bi_private
= dip
;
5741 bio
->bi_end_io
= btrfs_endio_direct_write
;
5743 bio
->bi_end_io
= btrfs_endio_direct_read
;
5745 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
5749 if (write
&& !skip_sum
) {
5750 ret
= btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
5751 inode
, rw
, bio
, 0, 0,
5752 dip
->logical_offset
,
5753 __btrfs_submit_bio_start_direct_io
,
5754 __btrfs_submit_bio_done
);
5758 } else if (!skip_sum
)
5759 btrfs_lookup_bio_sums_dio(root
, inode
, bio
,
5760 dip
->logical_offset
, dip
->csums
);
5762 ret
= btrfs_map_bio(root
, rw
, bio
, 0, 1);
5771 * If this is a write, we need to clean up the reserved space and kill
5772 * the ordered extent.
5775 struct btrfs_ordered_extent
*ordered
;
5776 ordered
= btrfs_lookup_ordered_extent(inode
,
5777 dip
->logical_offset
);
5778 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
5779 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
5780 btrfs_free_reserved_extent(root
, ordered
->start
,
5782 btrfs_put_ordered_extent(ordered
);
5783 btrfs_put_ordered_extent(ordered
);
5785 bio_endio(bio
, ret
);
5788 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
5789 const struct iovec
*iov
, loff_t offset
,
5790 unsigned long nr_segs
)
5795 unsigned blocksize_mask
= root
->sectorsize
- 1;
5796 ssize_t retval
= -EINVAL
;
5797 loff_t end
= offset
;
5799 if (offset
& blocksize_mask
)
5802 /* Check the memory alignment. Blocks cannot straddle pages */
5803 for (seg
= 0; seg
< nr_segs
; seg
++) {
5804 addr
= (unsigned long)iov
[seg
].iov_base
;
5805 size
= iov
[seg
].iov_len
;
5807 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
5814 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
5815 const struct iovec
*iov
, loff_t offset
,
5816 unsigned long nr_segs
)
5818 struct file
*file
= iocb
->ki_filp
;
5819 struct inode
*inode
= file
->f_mapping
->host
;
5820 struct btrfs_ordered_extent
*ordered
;
5821 struct extent_state
*cached_state
= NULL
;
5822 u64 lockstart
, lockend
;
5824 int writing
= rw
& WRITE
;
5826 size_t count
= iov_length(iov
, nr_segs
);
5828 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iov
,
5834 lockend
= offset
+ count
- 1;
5837 ret
= btrfs_delalloc_reserve_space(inode
, count
);
5843 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
5844 0, &cached_state
, GFP_NOFS
);
5846 * We're concerned with the entire range that we're going to be
5847 * doing DIO to, so we need to make sure theres no ordered
5848 * extents in this range.
5850 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
5851 lockend
- lockstart
+ 1);
5854 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
5855 &cached_state
, GFP_NOFS
);
5856 btrfs_start_ordered_extent(inode
, ordered
, 1);
5857 btrfs_put_ordered_extent(ordered
);
5862 * we don't use btrfs_set_extent_delalloc because we don't want
5863 * the dirty or uptodate bits
5866 write_bits
= EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
;
5867 ret
= set_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
5868 EXTENT_DELALLOC
, 0, NULL
, &cached_state
,
5871 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
5872 lockend
, EXTENT_LOCKED
| write_bits
,
5873 1, 0, &cached_state
, GFP_NOFS
);
5878 free_extent_state(cached_state
);
5879 cached_state
= NULL
;
5881 ret
= __blockdev_direct_IO(rw
, iocb
, inode
,
5882 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
5883 iov
, offset
, nr_segs
, btrfs_get_blocks_direct
, NULL
,
5884 btrfs_submit_direct
, 0);
5886 if (ret
< 0 && ret
!= -EIOCBQUEUED
) {
5887 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, offset
,
5888 offset
+ iov_length(iov
, nr_segs
) - 1,
5889 EXTENT_LOCKED
| write_bits
, 1, 0,
5890 &cached_state
, GFP_NOFS
);
5891 } else if (ret
>= 0 && ret
< iov_length(iov
, nr_segs
)) {
5893 * We're falling back to buffered, unlock the section we didn't
5896 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, offset
+ ret
,
5897 offset
+ iov_length(iov
, nr_segs
) - 1,
5898 EXTENT_LOCKED
| write_bits
, 1, 0,
5899 &cached_state
, GFP_NOFS
);
5902 free_extent_state(cached_state
);
5906 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
5907 __u64 start
, __u64 len
)
5909 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent
);
5912 int btrfs_readpage(struct file
*file
, struct page
*page
)
5914 struct extent_io_tree
*tree
;
5915 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
5916 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
5919 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
5921 struct extent_io_tree
*tree
;
5924 if (current
->flags
& PF_MEMALLOC
) {
5925 redirty_page_for_writepage(wbc
, page
);
5929 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
5930 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
5933 int btrfs_writepages(struct address_space
*mapping
,
5934 struct writeback_control
*wbc
)
5936 struct extent_io_tree
*tree
;
5938 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
5939 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
5943 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
5944 struct list_head
*pages
, unsigned nr_pages
)
5946 struct extent_io_tree
*tree
;
5947 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
5948 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
5951 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
5953 struct extent_io_tree
*tree
;
5954 struct extent_map_tree
*map
;
5957 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
5958 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
5959 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
5961 ClearPagePrivate(page
);
5962 set_page_private(page
, 0);
5963 page_cache_release(page
);
5968 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
5970 if (PageWriteback(page
) || PageDirty(page
))
5972 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
5975 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
5977 struct extent_io_tree
*tree
;
5978 struct btrfs_ordered_extent
*ordered
;
5979 struct extent_state
*cached_state
= NULL
;
5980 u64 page_start
= page_offset(page
);
5981 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
5985 * we have the page locked, so new writeback can't start,
5986 * and the dirty bit won't be cleared while we are here.
5988 * Wait for IO on this page so that we can safely clear
5989 * the PagePrivate2 bit and do ordered accounting
5991 wait_on_page_writeback(page
);
5993 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
5995 btrfs_releasepage(page
, GFP_NOFS
);
5998 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
,
6000 ordered
= btrfs_lookup_ordered_extent(page
->mapping
->host
,
6004 * IO on this page will never be started, so we need
6005 * to account for any ordered extents now
6007 clear_extent_bit(tree
, page_start
, page_end
,
6008 EXTENT_DIRTY
| EXTENT_DELALLOC
|
6009 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
, 1, 0,
6010 &cached_state
, GFP_NOFS
);
6012 * whoever cleared the private bit is responsible
6013 * for the finish_ordered_io
6015 if (TestClearPagePrivate2(page
)) {
6016 btrfs_finish_ordered_io(page
->mapping
->host
,
6017 page_start
, page_end
);
6019 btrfs_put_ordered_extent(ordered
);
6020 cached_state
= NULL
;
6021 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
,
6024 clear_extent_bit(tree
, page_start
, page_end
,
6025 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
6026 EXTENT_DO_ACCOUNTING
, 1, 1, &cached_state
, GFP_NOFS
);
6027 __btrfs_releasepage(page
, GFP_NOFS
);
6029 ClearPageChecked(page
);
6030 if (PagePrivate(page
)) {
6031 ClearPagePrivate(page
);
6032 set_page_private(page
, 0);
6033 page_cache_release(page
);
6038 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6039 * called from a page fault handler when a page is first dirtied. Hence we must
6040 * be careful to check for EOF conditions here. We set the page up correctly
6041 * for a written page which means we get ENOSPC checking when writing into
6042 * holes and correct delalloc and unwritten extent mapping on filesystems that
6043 * support these features.
6045 * We are not allowed to take the i_mutex here so we have to play games to
6046 * protect against truncate races as the page could now be beyond EOF. Because
6047 * vmtruncate() writes the inode size before removing pages, once we have the
6048 * page lock we can determine safely if the page is beyond EOF. If it is not
6049 * beyond EOF, then the page is guaranteed safe against truncation until we
6052 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
6054 struct page
*page
= vmf
->page
;
6055 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
6056 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6057 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6058 struct btrfs_ordered_extent
*ordered
;
6059 struct extent_state
*cached_state
= NULL
;
6061 unsigned long zero_start
;
6067 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
6071 else /* -ENOSPC, -EIO, etc */
6072 ret
= VM_FAULT_SIGBUS
;
6076 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
6079 size
= i_size_read(inode
);
6080 page_start
= page_offset(page
);
6081 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6083 if ((page
->mapping
!= inode
->i_mapping
) ||
6084 (page_start
>= size
)) {
6085 /* page got truncated out from underneath us */
6088 wait_on_page_writeback(page
);
6090 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
,
6092 set_page_extent_mapped(page
);
6095 * we can't set the delalloc bits if there are pending ordered
6096 * extents. Drop our locks and wait for them to finish
6098 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
6100 unlock_extent_cached(io_tree
, page_start
, page_end
,
6101 &cached_state
, GFP_NOFS
);
6103 btrfs_start_ordered_extent(inode
, ordered
, 1);
6104 btrfs_put_ordered_extent(ordered
);
6109 * XXX - page_mkwrite gets called every time the page is dirtied, even
6110 * if it was already dirty, so for space accounting reasons we need to
6111 * clear any delalloc bits for the range we are fixing to save. There
6112 * is probably a better way to do this, but for now keep consistent with
6113 * prepare_pages in the normal write path.
6115 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
6116 EXTENT_DIRTY
| EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
,
6117 0, 0, &cached_state
, GFP_NOFS
);
6119 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
6122 unlock_extent_cached(io_tree
, page_start
, page_end
,
6123 &cached_state
, GFP_NOFS
);
6124 ret
= VM_FAULT_SIGBUS
;
6129 /* page is wholly or partially inside EOF */
6130 if (page_start
+ PAGE_CACHE_SIZE
> size
)
6131 zero_start
= size
& ~PAGE_CACHE_MASK
;
6133 zero_start
= PAGE_CACHE_SIZE
;
6135 if (zero_start
!= PAGE_CACHE_SIZE
) {
6137 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
6138 flush_dcache_page(page
);
6141 ClearPageChecked(page
);
6142 set_page_dirty(page
);
6143 SetPageUptodate(page
);
6145 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
6146 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
6148 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
6152 return VM_FAULT_LOCKED
;
6154 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
6159 static void btrfs_truncate(struct inode
*inode
)
6161 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6163 struct btrfs_trans_handle
*trans
;
6165 u64 mask
= root
->sectorsize
- 1;
6167 if (!S_ISREG(inode
->i_mode
)) {
6172 ret
= btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
6176 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
6177 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
6179 trans
= btrfs_start_transaction(root
, 0);
6180 BUG_ON(IS_ERR(trans
));
6181 btrfs_set_trans_block_group(trans
, inode
);
6182 trans
->block_rsv
= root
->orphan_block_rsv
;
6185 * setattr is responsible for setting the ordered_data_close flag,
6186 * but that is only tested during the last file release. That
6187 * could happen well after the next commit, leaving a great big
6188 * window where new writes may get lost if someone chooses to write
6189 * to this file after truncating to zero
6191 * The inode doesn't have any dirty data here, and so if we commit
6192 * this is a noop. If someone immediately starts writing to the inode
6193 * it is very likely we'll catch some of their writes in this
6194 * transaction, and the commit will find this file on the ordered
6195 * data list with good things to send down.
6197 * This is a best effort solution, there is still a window where
6198 * using truncate to replace the contents of the file will
6199 * end up with a zero length file after a crash.
6201 if (inode
->i_size
== 0 && BTRFS_I(inode
)->ordered_data_close
)
6202 btrfs_add_ordered_operation(trans
, root
, inode
);
6206 trans
= btrfs_start_transaction(root
, 0);
6207 BUG_ON(IS_ERR(trans
));
6208 btrfs_set_trans_block_group(trans
, inode
);
6209 trans
->block_rsv
= root
->orphan_block_rsv
;
6212 ret
= btrfs_block_rsv_check(trans
, root
,
6213 root
->orphan_block_rsv
, 0, 5);
6215 BUG_ON(ret
!= -EAGAIN
);
6216 ret
= btrfs_commit_transaction(trans
, root
);
6222 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
6224 BTRFS_EXTENT_DATA_KEY
);
6228 ret
= btrfs_update_inode(trans
, root
, inode
);
6231 nr
= trans
->blocks_used
;
6232 btrfs_end_transaction(trans
, root
);
6234 btrfs_btree_balance_dirty(root
, nr
);
6237 if (ret
== 0 && inode
->i_nlink
> 0) {
6238 ret
= btrfs_orphan_del(trans
, inode
);
6242 ret
= btrfs_update_inode(trans
, root
, inode
);
6245 nr
= trans
->blocks_used
;
6246 ret
= btrfs_end_transaction_throttle(trans
, root
);
6248 btrfs_btree_balance_dirty(root
, nr
);
6252 * create a new subvolume directory/inode (helper for the ioctl).
6254 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
6255 struct btrfs_root
*new_root
,
6256 u64 new_dirid
, u64 alloc_hint
)
6258 struct inode
*inode
;
6262 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2, new_dirid
,
6263 new_dirid
, alloc_hint
, S_IFDIR
| 0700, &index
);
6265 return PTR_ERR(inode
);
6266 inode
->i_op
= &btrfs_dir_inode_operations
;
6267 inode
->i_fop
= &btrfs_dir_file_operations
;
6270 btrfs_i_size_write(inode
, 0);
6272 err
= btrfs_update_inode(trans
, new_root
, inode
);
6279 /* helper function for file defrag and space balancing. This
6280 * forces readahead on a given range of bytes in an inode
6282 unsigned long btrfs_force_ra(struct address_space
*mapping
,
6283 struct file_ra_state
*ra
, struct file
*file
,
6284 pgoff_t offset
, pgoff_t last_index
)
6286 pgoff_t req_size
= last_index
- offset
+ 1;
6288 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
6289 return offset
+ req_size
;
6292 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
6294 struct btrfs_inode
*ei
;
6295 struct inode
*inode
;
6297 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
6302 ei
->space_info
= NULL
;
6306 ei
->last_sub_trans
= 0;
6307 ei
->logged_trans
= 0;
6308 ei
->delalloc_bytes
= 0;
6309 ei
->reserved_bytes
= 0;
6310 ei
->disk_i_size
= 0;
6312 ei
->index_cnt
= (u64
)-1;
6313 ei
->last_unlink_trans
= 0;
6315 spin_lock_init(&ei
->accounting_lock
);
6316 atomic_set(&ei
->outstanding_extents
, 0);
6317 ei
->reserved_extents
= 0;
6319 ei
->ordered_data_close
= 0;
6320 ei
->orphan_meta_reserved
= 0;
6321 ei
->dummy_inode
= 0;
6322 ei
->force_compress
= 0;
6324 inode
= &ei
->vfs_inode
;
6325 extent_map_tree_init(&ei
->extent_tree
, GFP_NOFS
);
6326 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
, GFP_NOFS
);
6327 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
, GFP_NOFS
);
6328 mutex_init(&ei
->log_mutex
);
6329 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
6330 INIT_LIST_HEAD(&ei
->i_orphan
);
6331 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
6332 INIT_LIST_HEAD(&ei
->ordered_operations
);
6333 RB_CLEAR_NODE(&ei
->rb_node
);
6338 void btrfs_destroy_inode(struct inode
*inode
)
6340 struct btrfs_ordered_extent
*ordered
;
6341 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6343 WARN_ON(!list_empty(&inode
->i_dentry
));
6344 WARN_ON(inode
->i_data
.nrpages
);
6345 WARN_ON(atomic_read(&BTRFS_I(inode
)->outstanding_extents
));
6346 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
6349 * This can happen where we create an inode, but somebody else also
6350 * created the same inode and we need to destroy the one we already
6357 * Make sure we're properly removed from the ordered operation
6361 if (!list_empty(&BTRFS_I(inode
)->ordered_operations
)) {
6362 spin_lock(&root
->fs_info
->ordered_extent_lock
);
6363 list_del_init(&BTRFS_I(inode
)->ordered_operations
);
6364 spin_unlock(&root
->fs_info
->ordered_extent_lock
);
6367 if (root
== root
->fs_info
->tree_root
) {
6368 struct btrfs_block_group_cache
*block_group
;
6370 block_group
= btrfs_lookup_block_group(root
->fs_info
,
6371 BTRFS_I(inode
)->block_group
);
6372 if (block_group
&& block_group
->inode
== inode
) {
6373 spin_lock(&block_group
->lock
);
6374 block_group
->inode
= NULL
;
6375 spin_unlock(&block_group
->lock
);
6376 btrfs_put_block_group(block_group
);
6377 } else if (block_group
) {
6378 btrfs_put_block_group(block_group
);
6382 spin_lock(&root
->orphan_lock
);
6383 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
6384 printk(KERN_INFO
"BTRFS: inode %lu still on the orphan list\n",
6386 list_del_init(&BTRFS_I(inode
)->i_orphan
);
6388 spin_unlock(&root
->orphan_lock
);
6391 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
6395 printk(KERN_ERR
"btrfs found ordered "
6396 "extent %llu %llu on inode cleanup\n",
6397 (unsigned long long)ordered
->file_offset
,
6398 (unsigned long long)ordered
->len
);
6399 btrfs_remove_ordered_extent(inode
, ordered
);
6400 btrfs_put_ordered_extent(ordered
);
6401 btrfs_put_ordered_extent(ordered
);
6404 inode_tree_del(inode
);
6405 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
6407 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
6410 int btrfs_drop_inode(struct inode
*inode
)
6412 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6414 if (btrfs_root_refs(&root
->root_item
) == 0 &&
6415 root
!= root
->fs_info
->tree_root
)
6418 return generic_drop_inode(inode
);
6421 static void init_once(void *foo
)
6423 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
6425 inode_init_once(&ei
->vfs_inode
);
6428 void btrfs_destroy_cachep(void)
6430 if (btrfs_inode_cachep
)
6431 kmem_cache_destroy(btrfs_inode_cachep
);
6432 if (btrfs_trans_handle_cachep
)
6433 kmem_cache_destroy(btrfs_trans_handle_cachep
);
6434 if (btrfs_transaction_cachep
)
6435 kmem_cache_destroy(btrfs_transaction_cachep
);
6436 if (btrfs_path_cachep
)
6437 kmem_cache_destroy(btrfs_path_cachep
);
6440 int btrfs_init_cachep(void)
6442 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode_cache",
6443 sizeof(struct btrfs_inode
), 0,
6444 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
6445 if (!btrfs_inode_cachep
)
6448 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle_cache",
6449 sizeof(struct btrfs_trans_handle
), 0,
6450 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6451 if (!btrfs_trans_handle_cachep
)
6454 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction_cache",
6455 sizeof(struct btrfs_transaction
), 0,
6456 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6457 if (!btrfs_transaction_cachep
)
6460 btrfs_path_cachep
= kmem_cache_create("btrfs_path_cache",
6461 sizeof(struct btrfs_path
), 0,
6462 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6463 if (!btrfs_path_cachep
)
6468 btrfs_destroy_cachep();
6472 static int btrfs_getattr(struct vfsmount
*mnt
,
6473 struct dentry
*dentry
, struct kstat
*stat
)
6475 struct inode
*inode
= dentry
->d_inode
;
6476 generic_fillattr(inode
, stat
);
6477 stat
->dev
= BTRFS_I(inode
)->root
->anon_super
.s_dev
;
6478 stat
->blksize
= PAGE_CACHE_SIZE
;
6479 stat
->blocks
= (inode_get_bytes(inode
) +
6480 BTRFS_I(inode
)->delalloc_bytes
) >> 9;
6484 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
6485 struct inode
*new_dir
, struct dentry
*new_dentry
)
6487 struct btrfs_trans_handle
*trans
;
6488 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
6489 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
6490 struct inode
*new_inode
= new_dentry
->d_inode
;
6491 struct inode
*old_inode
= old_dentry
->d_inode
;
6492 struct timespec ctime
= CURRENT_TIME
;
6497 if (new_dir
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
6500 /* we only allow rename subvolume link between subvolumes */
6501 if (old_inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
6504 if (old_inode
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
6505 (new_inode
&& new_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
))
6508 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
6509 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
6512 * we're using rename to replace one file with another.
6513 * and the replacement file is large. Start IO on it now so
6514 * we don't add too much work to the end of the transaction
6516 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
&&
6517 old_inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
6518 filemap_flush(old_inode
->i_mapping
);
6520 /* close the racy window with snapshot create/destroy ioctl */
6521 if (old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)
6522 down_read(&root
->fs_info
->subvol_sem
);
6524 * We want to reserve the absolute worst case amount of items. So if
6525 * both inodes are subvols and we need to unlink them then that would
6526 * require 4 item modifications, but if they are both normal inodes it
6527 * would require 5 item modifications, so we'll assume their normal
6528 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
6529 * should cover the worst case number of items we'll modify.
6531 trans
= btrfs_start_transaction(root
, 20);
6533 return PTR_ERR(trans
);
6535 btrfs_set_trans_block_group(trans
, new_dir
);
6538 btrfs_record_root_in_trans(trans
, dest
);
6540 ret
= btrfs_set_inode_index(new_dir
, &index
);
6544 if (unlikely(old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6545 /* force full log commit if subvolume involved. */
6546 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
6548 ret
= btrfs_insert_inode_ref(trans
, dest
,
6549 new_dentry
->d_name
.name
,
6550 new_dentry
->d_name
.len
,
6552 new_dir
->i_ino
, index
);
6556 * this is an ugly little race, but the rename is required
6557 * to make sure that if we crash, the inode is either at the
6558 * old name or the new one. pinning the log transaction lets
6559 * us make sure we don't allow a log commit to come in after
6560 * we unlink the name but before we add the new name back in.
6562 btrfs_pin_log_trans(root
);
6565 * make sure the inode gets flushed if it is replacing
6568 if (new_inode
&& new_inode
->i_size
&&
6569 old_inode
&& S_ISREG(old_inode
->i_mode
)) {
6570 btrfs_add_ordered_operation(trans
, root
, old_inode
);
6573 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
6574 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
6575 old_inode
->i_ctime
= ctime
;
6577 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
6578 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
6580 if (unlikely(old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6581 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
6582 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
6583 old_dentry
->d_name
.name
,
6584 old_dentry
->d_name
.len
);
6586 btrfs_inc_nlink(old_dentry
->d_inode
);
6587 ret
= btrfs_unlink_inode(trans
, root
, old_dir
,
6588 old_dentry
->d_inode
,
6589 old_dentry
->d_name
.name
,
6590 old_dentry
->d_name
.len
);
6595 new_inode
->i_ctime
= CURRENT_TIME
;
6596 if (unlikely(new_inode
->i_ino
==
6597 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
6598 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
6599 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
6601 new_dentry
->d_name
.name
,
6602 new_dentry
->d_name
.len
);
6603 BUG_ON(new_inode
->i_nlink
== 0);
6605 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
6606 new_dentry
->d_inode
,
6607 new_dentry
->d_name
.name
,
6608 new_dentry
->d_name
.len
);
6611 if (new_inode
->i_nlink
== 0) {
6612 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
6617 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
6618 new_dentry
->d_name
.name
,
6619 new_dentry
->d_name
.len
, 0, index
);
6622 if (old_inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
6623 btrfs_log_new_name(trans
, old_inode
, old_dir
,
6624 new_dentry
->d_parent
);
6625 btrfs_end_log_trans(root
);
6628 btrfs_end_transaction_throttle(trans
, root
);
6630 if (old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)
6631 up_read(&root
->fs_info
->subvol_sem
);
6637 * some fairly slow code that needs optimization. This walks the list
6638 * of all the inodes with pending delalloc and forces them to disk.
6640 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
6642 struct list_head
*head
= &root
->fs_info
->delalloc_inodes
;
6643 struct btrfs_inode
*binode
;
6644 struct inode
*inode
;
6646 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
6649 spin_lock(&root
->fs_info
->delalloc_lock
);
6650 while (!list_empty(head
)) {
6651 binode
= list_entry(head
->next
, struct btrfs_inode
,
6653 inode
= igrab(&binode
->vfs_inode
);
6655 list_del_init(&binode
->delalloc_inodes
);
6656 spin_unlock(&root
->fs_info
->delalloc_lock
);
6658 filemap_flush(inode
->i_mapping
);
6660 btrfs_add_delayed_iput(inode
);
6665 spin_lock(&root
->fs_info
->delalloc_lock
);
6667 spin_unlock(&root
->fs_info
->delalloc_lock
);
6669 /* the filemap_flush will queue IO into the worker threads, but
6670 * we have to make sure the IO is actually started and that
6671 * ordered extents get created before we return
6673 atomic_inc(&root
->fs_info
->async_submit_draining
);
6674 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
6675 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
6676 wait_event(root
->fs_info
->async_submit_wait
,
6677 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
6678 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
6680 atomic_dec(&root
->fs_info
->async_submit_draining
);
6684 int btrfs_start_one_delalloc_inode(struct btrfs_root
*root
, int delay_iput
)
6686 struct btrfs_inode
*binode
;
6687 struct inode
*inode
= NULL
;
6689 spin_lock(&root
->fs_info
->delalloc_lock
);
6690 while (!list_empty(&root
->fs_info
->delalloc_inodes
)) {
6691 binode
= list_entry(root
->fs_info
->delalloc_inodes
.next
,
6692 struct btrfs_inode
, delalloc_inodes
);
6693 inode
= igrab(&binode
->vfs_inode
);
6695 list_move_tail(&binode
->delalloc_inodes
,
6696 &root
->fs_info
->delalloc_inodes
);
6700 list_del_init(&binode
->delalloc_inodes
);
6701 cond_resched_lock(&root
->fs_info
->delalloc_lock
);
6703 spin_unlock(&root
->fs_info
->delalloc_lock
);
6706 write_inode_now(inode
, 0);
6708 btrfs_add_delayed_iput(inode
);
6716 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
6717 const char *symname
)
6719 struct btrfs_trans_handle
*trans
;
6720 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6721 struct btrfs_path
*path
;
6722 struct btrfs_key key
;
6723 struct inode
*inode
= NULL
;
6731 struct btrfs_file_extent_item
*ei
;
6732 struct extent_buffer
*leaf
;
6733 unsigned long nr
= 0;
6735 name_len
= strlen(symname
) + 1;
6736 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
6737 return -ENAMETOOLONG
;
6739 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
6743 * 2 items for inode item and ref
6744 * 2 items for dir items
6745 * 1 item for xattr if selinux is on
6747 trans
= btrfs_start_transaction(root
, 5);
6749 return PTR_ERR(trans
);
6751 btrfs_set_trans_block_group(trans
, dir
);
6753 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6755 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
6756 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
,
6758 err
= PTR_ERR(inode
);
6762 err
= btrfs_init_inode_security(trans
, inode
, dir
);
6768 btrfs_set_trans_block_group(trans
, inode
);
6769 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0, index
);
6773 inode
->i_mapping
->a_ops
= &btrfs_aops
;
6774 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
6775 inode
->i_fop
= &btrfs_file_operations
;
6776 inode
->i_op
= &btrfs_file_inode_operations
;
6777 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
6779 btrfs_update_inode_block_group(trans
, inode
);
6780 btrfs_update_inode_block_group(trans
, dir
);
6784 path
= btrfs_alloc_path();
6786 key
.objectid
= inode
->i_ino
;
6788 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
6789 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
6790 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
6796 leaf
= path
->nodes
[0];
6797 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
6798 struct btrfs_file_extent_item
);
6799 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
6800 btrfs_set_file_extent_type(leaf
, ei
,
6801 BTRFS_FILE_EXTENT_INLINE
);
6802 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
6803 btrfs_set_file_extent_compression(leaf
, ei
, 0);
6804 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
6805 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
6807 ptr
= btrfs_file_extent_inline_start(ei
);
6808 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
6809 btrfs_mark_buffer_dirty(leaf
);
6810 btrfs_free_path(path
);
6812 inode
->i_op
= &btrfs_symlink_inode_operations
;
6813 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
6814 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
6815 inode_set_bytes(inode
, name_len
);
6816 btrfs_i_size_write(inode
, name_len
- 1);
6817 err
= btrfs_update_inode(trans
, root
, inode
);
6822 nr
= trans
->blocks_used
;
6823 btrfs_end_transaction_throttle(trans
, root
);
6825 inode_dec_link_count(inode
);
6828 btrfs_btree_balance_dirty(root
, nr
);
6832 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
6833 u64 start
, u64 num_bytes
, u64 min_size
,
6834 loff_t actual_len
, u64
*alloc_hint
,
6835 struct btrfs_trans_handle
*trans
)
6837 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6838 struct btrfs_key ins
;
6839 u64 cur_offset
= start
;
6841 bool own_trans
= true;
6845 while (num_bytes
> 0) {
6847 trans
= btrfs_start_transaction(root
, 3);
6848 if (IS_ERR(trans
)) {
6849 ret
= PTR_ERR(trans
);
6854 ret
= btrfs_reserve_extent(trans
, root
, num_bytes
, min_size
,
6855 0, *alloc_hint
, (u64
)-1, &ins
, 1);
6858 btrfs_end_transaction(trans
, root
);
6862 ret
= insert_reserved_file_extent(trans
, inode
,
6863 cur_offset
, ins
.objectid
,
6864 ins
.offset
, ins
.offset
,
6865 ins
.offset
, 0, 0, 0,
6866 BTRFS_FILE_EXTENT_PREALLOC
);
6868 btrfs_drop_extent_cache(inode
, cur_offset
,
6869 cur_offset
+ ins
.offset
-1, 0);
6871 num_bytes
-= ins
.offset
;
6872 cur_offset
+= ins
.offset
;
6873 *alloc_hint
= ins
.objectid
+ ins
.offset
;
6875 inode
->i_ctime
= CURRENT_TIME
;
6876 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
6877 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
6878 (actual_len
> inode
->i_size
) &&
6879 (cur_offset
> inode
->i_size
)) {
6880 if (cur_offset
> actual_len
)
6881 i_size_write(inode
, actual_len
);
6883 i_size_write(inode
, cur_offset
);
6884 i_size_write(inode
, cur_offset
);
6885 btrfs_ordered_update_i_size(inode
, cur_offset
, NULL
);
6888 ret
= btrfs_update_inode(trans
, root
, inode
);
6892 btrfs_end_transaction(trans
, root
);
6897 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
6898 u64 start
, u64 num_bytes
, u64 min_size
,
6899 loff_t actual_len
, u64
*alloc_hint
)
6901 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
6902 min_size
, actual_len
, alloc_hint
,
6906 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
6907 struct btrfs_trans_handle
*trans
, int mode
,
6908 u64 start
, u64 num_bytes
, u64 min_size
,
6909 loff_t actual_len
, u64
*alloc_hint
)
6911 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
6912 min_size
, actual_len
, alloc_hint
, trans
);
6915 static long btrfs_fallocate(struct inode
*inode
, int mode
,
6916 loff_t offset
, loff_t len
)
6918 struct extent_state
*cached_state
= NULL
;
6925 u64 mask
= BTRFS_I(inode
)->root
->sectorsize
- 1;
6926 struct extent_map
*em
;
6929 alloc_start
= offset
& ~mask
;
6930 alloc_end
= (offset
+ len
+ mask
) & ~mask
;
6933 * wait for ordered IO before we have any locks. We'll loop again
6934 * below with the locks held.
6936 btrfs_wait_ordered_range(inode
, alloc_start
, alloc_end
- alloc_start
);
6938 mutex_lock(&inode
->i_mutex
);
6939 if (alloc_start
> inode
->i_size
) {
6940 ret
= btrfs_cont_expand(inode
, alloc_start
);
6945 ret
= btrfs_check_data_free_space(inode
, alloc_end
- alloc_start
);
6949 locked_end
= alloc_end
- 1;
6951 struct btrfs_ordered_extent
*ordered
;
6953 /* the extent lock is ordered inside the running
6956 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, alloc_start
,
6957 locked_end
, 0, &cached_state
, GFP_NOFS
);
6958 ordered
= btrfs_lookup_first_ordered_extent(inode
,
6961 ordered
->file_offset
+ ordered
->len
> alloc_start
&&
6962 ordered
->file_offset
< alloc_end
) {
6963 btrfs_put_ordered_extent(ordered
);
6964 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
6965 alloc_start
, locked_end
,
6966 &cached_state
, GFP_NOFS
);
6968 * we can't wait on the range with the transaction
6969 * running or with the extent lock held
6971 btrfs_wait_ordered_range(inode
, alloc_start
,
6972 alloc_end
- alloc_start
);
6975 btrfs_put_ordered_extent(ordered
);
6980 cur_offset
= alloc_start
;
6982 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
6983 alloc_end
- cur_offset
, 0);
6984 BUG_ON(IS_ERR(em
) || !em
);
6985 last_byte
= min(extent_map_end(em
), alloc_end
);
6986 last_byte
= (last_byte
+ mask
) & ~mask
;
6987 if (em
->block_start
== EXTENT_MAP_HOLE
||
6988 (cur_offset
>= inode
->i_size
&&
6989 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
6990 ret
= btrfs_prealloc_file_range(inode
, mode
, cur_offset
,
6991 last_byte
- cur_offset
,
6992 1 << inode
->i_blkbits
,
6996 free_extent_map(em
);
7000 free_extent_map(em
);
7002 cur_offset
= last_byte
;
7003 if (cur_offset
>= alloc_end
) {
7008 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, alloc_start
, locked_end
,
7009 &cached_state
, GFP_NOFS
);
7011 btrfs_free_reserved_data_space(inode
, alloc_end
- alloc_start
);
7013 mutex_unlock(&inode
->i_mutex
);
7017 static int btrfs_set_page_dirty(struct page
*page
)
7019 return __set_page_dirty_nobuffers(page
);
7022 static int btrfs_permission(struct inode
*inode
, int mask
)
7024 if ((BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
) && (mask
& MAY_WRITE
))
7026 return generic_permission(inode
, mask
, btrfs_check_acl
);
7029 static const struct inode_operations btrfs_dir_inode_operations
= {
7030 .getattr
= btrfs_getattr
,
7031 .lookup
= btrfs_lookup
,
7032 .create
= btrfs_create
,
7033 .unlink
= btrfs_unlink
,
7035 .mkdir
= btrfs_mkdir
,
7036 .rmdir
= btrfs_rmdir
,
7037 .rename
= btrfs_rename
,
7038 .symlink
= btrfs_symlink
,
7039 .setattr
= btrfs_setattr
,
7040 .mknod
= btrfs_mknod
,
7041 .setxattr
= btrfs_setxattr
,
7042 .getxattr
= btrfs_getxattr
,
7043 .listxattr
= btrfs_listxattr
,
7044 .removexattr
= btrfs_removexattr
,
7045 .permission
= btrfs_permission
,
7047 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
7048 .lookup
= btrfs_lookup
,
7049 .permission
= btrfs_permission
,
7052 static const struct file_operations btrfs_dir_file_operations
= {
7053 .llseek
= generic_file_llseek
,
7054 .read
= generic_read_dir
,
7055 .readdir
= btrfs_real_readdir
,
7056 .unlocked_ioctl
= btrfs_ioctl
,
7057 #ifdef CONFIG_COMPAT
7058 .compat_ioctl
= btrfs_ioctl
,
7060 .release
= btrfs_release_file
,
7061 .fsync
= btrfs_sync_file
,
7064 static struct extent_io_ops btrfs_extent_io_ops
= {
7065 .fill_delalloc
= run_delalloc_range
,
7066 .submit_bio_hook
= btrfs_submit_bio_hook
,
7067 .merge_bio_hook
= btrfs_merge_bio_hook
,
7068 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
7069 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
7070 .writepage_start_hook
= btrfs_writepage_start_hook
,
7071 .readpage_io_failed_hook
= btrfs_io_failed_hook
,
7072 .set_bit_hook
= btrfs_set_bit_hook
,
7073 .clear_bit_hook
= btrfs_clear_bit_hook
,
7074 .merge_extent_hook
= btrfs_merge_extent_hook
,
7075 .split_extent_hook
= btrfs_split_extent_hook
,
7079 * btrfs doesn't support the bmap operation because swapfiles
7080 * use bmap to make a mapping of extents in the file. They assume
7081 * these extents won't change over the life of the file and they
7082 * use the bmap result to do IO directly to the drive.
7084 * the btrfs bmap call would return logical addresses that aren't
7085 * suitable for IO and they also will change frequently as COW
7086 * operations happen. So, swapfile + btrfs == corruption.
7088 * For now we're avoiding this by dropping bmap.
7090 static const struct address_space_operations btrfs_aops
= {
7091 .readpage
= btrfs_readpage
,
7092 .writepage
= btrfs_writepage
,
7093 .writepages
= btrfs_writepages
,
7094 .readpages
= btrfs_readpages
,
7095 .sync_page
= block_sync_page
,
7096 .direct_IO
= btrfs_direct_IO
,
7097 .invalidatepage
= btrfs_invalidatepage
,
7098 .releasepage
= btrfs_releasepage
,
7099 .set_page_dirty
= btrfs_set_page_dirty
,
7100 .error_remove_page
= generic_error_remove_page
,
7103 static const struct address_space_operations btrfs_symlink_aops
= {
7104 .readpage
= btrfs_readpage
,
7105 .writepage
= btrfs_writepage
,
7106 .invalidatepage
= btrfs_invalidatepage
,
7107 .releasepage
= btrfs_releasepage
,
7110 static const struct inode_operations btrfs_file_inode_operations
= {
7111 .truncate
= btrfs_truncate
,
7112 .getattr
= btrfs_getattr
,
7113 .setattr
= btrfs_setattr
,
7114 .setxattr
= btrfs_setxattr
,
7115 .getxattr
= btrfs_getxattr
,
7116 .listxattr
= btrfs_listxattr
,
7117 .removexattr
= btrfs_removexattr
,
7118 .permission
= btrfs_permission
,
7119 .fallocate
= btrfs_fallocate
,
7120 .fiemap
= btrfs_fiemap
,
7122 static const struct inode_operations btrfs_special_inode_operations
= {
7123 .getattr
= btrfs_getattr
,
7124 .setattr
= btrfs_setattr
,
7125 .permission
= btrfs_permission
,
7126 .setxattr
= btrfs_setxattr
,
7127 .getxattr
= btrfs_getxattr
,
7128 .listxattr
= btrfs_listxattr
,
7129 .removexattr
= btrfs_removexattr
,
7131 static const struct inode_operations btrfs_symlink_inode_operations
= {
7132 .readlink
= generic_readlink
,
7133 .follow_link
= page_follow_link_light
,
7134 .put_link
= page_put_link
,
7135 .permission
= btrfs_permission
,
7136 .setxattr
= btrfs_setxattr
,
7137 .getxattr
= btrfs_getxattr
,
7138 .listxattr
= btrfs_listxattr
,
7139 .removexattr
= btrfs_removexattr
,
7142 const struct dentry_operations btrfs_dentry_operations
= {
7143 .d_delete
= btrfs_dentry_delete
,