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 trans
= btrfs_join_transaction(root
, 1);
769 btrfs_set_trans_block_group(trans
, inode
);
770 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
772 actual_end
= min_t(u64
, isize
, end
+ 1);
774 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
775 num_bytes
= max(blocksize
, num_bytes
);
776 disk_num_bytes
= num_bytes
;
780 /* lets try to make an inline extent */
781 ret
= cow_file_range_inline(trans
, root
, inode
,
782 start
, end
, 0, NULL
);
784 extent_clear_unlock_delalloc(inode
,
785 &BTRFS_I(inode
)->io_tree
,
787 EXTENT_CLEAR_UNLOCK_PAGE
|
788 EXTENT_CLEAR_UNLOCK
|
789 EXTENT_CLEAR_DELALLOC
|
791 EXTENT_SET_WRITEBACK
|
792 EXTENT_END_WRITEBACK
);
794 *nr_written
= *nr_written
+
795 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
802 BUG_ON(disk_num_bytes
>
803 btrfs_super_total_bytes(&root
->fs_info
->super_copy
));
805 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
806 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
808 while (disk_num_bytes
> 0) {
811 cur_alloc_size
= disk_num_bytes
;
812 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
813 root
->sectorsize
, 0, alloc_hint
,
817 em
= alloc_extent_map(GFP_NOFS
);
819 em
->orig_start
= em
->start
;
820 ram_size
= ins
.offset
;
821 em
->len
= ins
.offset
;
823 em
->block_start
= ins
.objectid
;
824 em
->block_len
= ins
.offset
;
825 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
826 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
829 write_lock(&em_tree
->lock
);
830 ret
= add_extent_mapping(em_tree
, em
);
831 write_unlock(&em_tree
->lock
);
832 if (ret
!= -EEXIST
) {
836 btrfs_drop_extent_cache(inode
, start
,
837 start
+ ram_size
- 1, 0);
840 cur_alloc_size
= ins
.offset
;
841 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
842 ram_size
, cur_alloc_size
, 0);
845 if (root
->root_key
.objectid
==
846 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
847 ret
= btrfs_reloc_clone_csums(inode
, start
,
852 if (disk_num_bytes
< cur_alloc_size
)
855 /* we're not doing compressed IO, don't unlock the first
856 * page (which the caller expects to stay locked), don't
857 * clear any dirty bits and don't set any writeback bits
859 * Do set the Private2 bit so we know this page was properly
860 * setup for writepage
862 op
= unlock
? EXTENT_CLEAR_UNLOCK_PAGE
: 0;
863 op
|= EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
866 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
867 start
, start
+ ram_size
- 1,
869 disk_num_bytes
-= cur_alloc_size
;
870 num_bytes
-= cur_alloc_size
;
871 alloc_hint
= ins
.objectid
+ ins
.offset
;
872 start
+= cur_alloc_size
;
876 btrfs_end_transaction(trans
, root
);
882 * work queue call back to started compression on a file and pages
884 static noinline
void async_cow_start(struct btrfs_work
*work
)
886 struct async_cow
*async_cow
;
888 async_cow
= container_of(work
, struct async_cow
, work
);
890 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
891 async_cow
->start
, async_cow
->end
, async_cow
,
894 async_cow
->inode
= NULL
;
898 * work queue call back to submit previously compressed pages
900 static noinline
void async_cow_submit(struct btrfs_work
*work
)
902 struct async_cow
*async_cow
;
903 struct btrfs_root
*root
;
904 unsigned long nr_pages
;
906 async_cow
= container_of(work
, struct async_cow
, work
);
908 root
= async_cow
->root
;
909 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
912 atomic_sub(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
914 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
916 waitqueue_active(&root
->fs_info
->async_submit_wait
))
917 wake_up(&root
->fs_info
->async_submit_wait
);
919 if (async_cow
->inode
)
920 submit_compressed_extents(async_cow
->inode
, async_cow
);
923 static noinline
void async_cow_free(struct btrfs_work
*work
)
925 struct async_cow
*async_cow
;
926 async_cow
= container_of(work
, struct async_cow
, work
);
930 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
931 u64 start
, u64 end
, int *page_started
,
932 unsigned long *nr_written
)
934 struct async_cow
*async_cow
;
935 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
936 unsigned long nr_pages
;
938 int limit
= 10 * 1024 * 1042;
940 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
941 1, 0, NULL
, GFP_NOFS
);
942 while (start
< end
) {
943 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
944 async_cow
->inode
= inode
;
945 async_cow
->root
= root
;
946 async_cow
->locked_page
= locked_page
;
947 async_cow
->start
= start
;
949 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
952 cur_end
= min(end
, start
+ 512 * 1024 - 1);
954 async_cow
->end
= cur_end
;
955 INIT_LIST_HEAD(&async_cow
->extents
);
957 async_cow
->work
.func
= async_cow_start
;
958 async_cow
->work
.ordered_func
= async_cow_submit
;
959 async_cow
->work
.ordered_free
= async_cow_free
;
960 async_cow
->work
.flags
= 0;
962 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
964 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
966 btrfs_queue_worker(&root
->fs_info
->delalloc_workers
,
969 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
970 wait_event(root
->fs_info
->async_submit_wait
,
971 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
975 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
976 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
977 wait_event(root
->fs_info
->async_submit_wait
,
978 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
982 *nr_written
+= nr_pages
;
989 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
990 u64 bytenr
, u64 num_bytes
)
993 struct btrfs_ordered_sum
*sums
;
996 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
997 bytenr
+ num_bytes
- 1, &list
);
998 if (ret
== 0 && list_empty(&list
))
1001 while (!list_empty(&list
)) {
1002 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1003 list_del(&sums
->list
);
1010 * when nowcow writeback call back. This checks for snapshots or COW copies
1011 * of the extents that exist in the file, and COWs the file as required.
1013 * If no cow copies or snapshots exist, we write directly to the existing
1016 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1017 struct page
*locked_page
,
1018 u64 start
, u64 end
, int *page_started
, int force
,
1019 unsigned long *nr_written
)
1021 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1022 struct btrfs_trans_handle
*trans
;
1023 struct extent_buffer
*leaf
;
1024 struct btrfs_path
*path
;
1025 struct btrfs_file_extent_item
*fi
;
1026 struct btrfs_key found_key
;
1039 path
= btrfs_alloc_path();
1041 trans
= btrfs_join_transaction(root
, 1);
1044 cow_start
= (u64
)-1;
1047 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
1050 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1051 leaf
= path
->nodes
[0];
1052 btrfs_item_key_to_cpu(leaf
, &found_key
,
1053 path
->slots
[0] - 1);
1054 if (found_key
.objectid
== inode
->i_ino
&&
1055 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1060 leaf
= path
->nodes
[0];
1061 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1062 ret
= btrfs_next_leaf(root
, path
);
1067 leaf
= path
->nodes
[0];
1073 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1075 if (found_key
.objectid
> inode
->i_ino
||
1076 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1077 found_key
.offset
> end
)
1080 if (found_key
.offset
> cur_offset
) {
1081 extent_end
= found_key
.offset
;
1086 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1087 struct btrfs_file_extent_item
);
1088 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1090 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1091 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1092 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1093 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1094 extent_end
= found_key
.offset
+
1095 btrfs_file_extent_num_bytes(leaf
, fi
);
1096 if (extent_end
<= start
) {
1100 if (disk_bytenr
== 0)
1102 if (btrfs_file_extent_compression(leaf
, fi
) ||
1103 btrfs_file_extent_encryption(leaf
, fi
) ||
1104 btrfs_file_extent_other_encoding(leaf
, fi
))
1106 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1108 if (btrfs_extent_readonly(root
, disk_bytenr
))
1110 if (btrfs_cross_ref_exist(trans
, root
, inode
->i_ino
,
1112 extent_offset
, disk_bytenr
))
1114 disk_bytenr
+= extent_offset
;
1115 disk_bytenr
+= cur_offset
- found_key
.offset
;
1116 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1118 * force cow if csum exists in the range.
1119 * this ensure that csum for a given extent are
1120 * either valid or do not exist.
1122 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1125 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1126 extent_end
= found_key
.offset
+
1127 btrfs_file_extent_inline_len(leaf
, fi
);
1128 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1133 if (extent_end
<= start
) {
1138 if (cow_start
== (u64
)-1)
1139 cow_start
= cur_offset
;
1140 cur_offset
= extent_end
;
1141 if (cur_offset
> end
)
1147 btrfs_release_path(root
, path
);
1148 if (cow_start
!= (u64
)-1) {
1149 ret
= cow_file_range(inode
, locked_page
, cow_start
,
1150 found_key
.offset
- 1, page_started
,
1153 cow_start
= (u64
)-1;
1156 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1157 struct extent_map
*em
;
1158 struct extent_map_tree
*em_tree
;
1159 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1160 em
= alloc_extent_map(GFP_NOFS
);
1161 em
->start
= cur_offset
;
1162 em
->orig_start
= em
->start
;
1163 em
->len
= num_bytes
;
1164 em
->block_len
= num_bytes
;
1165 em
->block_start
= disk_bytenr
;
1166 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1167 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1169 write_lock(&em_tree
->lock
);
1170 ret
= add_extent_mapping(em_tree
, em
);
1171 write_unlock(&em_tree
->lock
);
1172 if (ret
!= -EEXIST
) {
1173 free_extent_map(em
);
1176 btrfs_drop_extent_cache(inode
, em
->start
,
1177 em
->start
+ em
->len
- 1, 0);
1179 type
= BTRFS_ORDERED_PREALLOC
;
1181 type
= BTRFS_ORDERED_NOCOW
;
1184 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1185 num_bytes
, num_bytes
, type
);
1188 if (root
->root_key
.objectid
==
1189 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1190 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1195 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
1196 cur_offset
, cur_offset
+ num_bytes
- 1,
1197 locked_page
, EXTENT_CLEAR_UNLOCK_PAGE
|
1198 EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
1199 EXTENT_SET_PRIVATE2
);
1200 cur_offset
= extent_end
;
1201 if (cur_offset
> end
)
1204 btrfs_release_path(root
, path
);
1206 if (cur_offset
<= end
&& cow_start
== (u64
)-1)
1207 cow_start
= cur_offset
;
1208 if (cow_start
!= (u64
)-1) {
1209 ret
= cow_file_range(inode
, locked_page
, cow_start
, end
,
1210 page_started
, nr_written
, 1);
1214 ret
= btrfs_end_transaction(trans
, root
);
1216 btrfs_free_path(path
);
1221 * extent_io.c call back to do delayed allocation processing
1223 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1224 u64 start
, u64 end
, int *page_started
,
1225 unsigned long *nr_written
)
1228 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1230 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
)
1231 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1232 page_started
, 1, nr_written
);
1233 else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
)
1234 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1235 page_started
, 0, nr_written
);
1236 else if (!btrfs_test_opt(root
, COMPRESS
) &&
1237 !(BTRFS_I(inode
)->force_compress
))
1238 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1239 page_started
, nr_written
, 1);
1241 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1242 page_started
, nr_written
);
1246 static int btrfs_split_extent_hook(struct inode
*inode
,
1247 struct extent_state
*orig
, u64 split
)
1249 /* not delalloc, ignore it */
1250 if (!(orig
->state
& EXTENT_DELALLOC
))
1253 atomic_inc(&BTRFS_I(inode
)->outstanding_extents
);
1258 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1259 * extents so we can keep track of new extents that are just merged onto old
1260 * extents, such as when we are doing sequential writes, so we can properly
1261 * account for the metadata space we'll need.
1263 static int btrfs_merge_extent_hook(struct inode
*inode
,
1264 struct extent_state
*new,
1265 struct extent_state
*other
)
1267 /* not delalloc, ignore it */
1268 if (!(other
->state
& EXTENT_DELALLOC
))
1271 atomic_dec(&BTRFS_I(inode
)->outstanding_extents
);
1276 * extent_io.c set_bit_hook, used to track delayed allocation
1277 * bytes in this file, and to maintain the list of inodes that
1278 * have pending delalloc work to be done.
1280 static int btrfs_set_bit_hook(struct inode
*inode
,
1281 struct extent_state
*state
, int *bits
)
1285 * set_bit and clear bit hooks normally require _irqsave/restore
1286 * but in this case, we are only testeing for the DELALLOC
1287 * bit, which is only set or cleared with irqs on
1289 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1290 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1291 u64 len
= state
->end
+ 1 - state
->start
;
1293 if (*bits
& EXTENT_FIRST_DELALLOC
)
1294 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1296 atomic_inc(&BTRFS_I(inode
)->outstanding_extents
);
1298 spin_lock(&root
->fs_info
->delalloc_lock
);
1299 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1300 root
->fs_info
->delalloc_bytes
+= len
;
1301 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1302 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1303 &root
->fs_info
->delalloc_inodes
);
1305 spin_unlock(&root
->fs_info
->delalloc_lock
);
1311 * extent_io.c clear_bit_hook, see set_bit_hook for why
1313 static int btrfs_clear_bit_hook(struct inode
*inode
,
1314 struct extent_state
*state
, int *bits
)
1317 * set_bit and clear bit hooks normally require _irqsave/restore
1318 * but in this case, we are only testeing for the DELALLOC
1319 * bit, which is only set or cleared with irqs on
1321 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1322 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1323 u64 len
= state
->end
+ 1 - state
->start
;
1325 if (*bits
& EXTENT_FIRST_DELALLOC
)
1326 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1327 else if (!(*bits
& EXTENT_DO_ACCOUNTING
))
1328 atomic_dec(&BTRFS_I(inode
)->outstanding_extents
);
1330 if (*bits
& EXTENT_DO_ACCOUNTING
)
1331 btrfs_delalloc_release_metadata(inode
, len
);
1333 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
)
1334 btrfs_free_reserved_data_space(inode
, len
);
1336 spin_lock(&root
->fs_info
->delalloc_lock
);
1337 root
->fs_info
->delalloc_bytes
-= len
;
1338 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1340 if (BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1341 !list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1342 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1344 spin_unlock(&root
->fs_info
->delalloc_lock
);
1350 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1351 * we don't create bios that span stripes or chunks
1353 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
1354 size_t size
, struct bio
*bio
,
1355 unsigned long bio_flags
)
1357 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1358 struct btrfs_mapping_tree
*map_tree
;
1359 u64 logical
= (u64
)bio
->bi_sector
<< 9;
1364 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1367 length
= bio
->bi_size
;
1368 map_tree
= &root
->fs_info
->mapping_tree
;
1369 map_length
= length
;
1370 ret
= btrfs_map_block(map_tree
, READ
, logical
,
1371 &map_length
, NULL
, 0);
1373 if (map_length
< length
+ size
)
1379 * in order to insert checksums into the metadata in large chunks,
1380 * we wait until bio submission time. All the pages in the bio are
1381 * checksummed and sums are attached onto the ordered extent record.
1383 * At IO completion time the cums attached on the ordered extent record
1384 * are inserted into the btree
1386 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1387 struct bio
*bio
, int mirror_num
,
1388 unsigned long bio_flags
,
1391 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1394 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1400 * in order to insert checksums into the metadata in large chunks,
1401 * we wait until bio submission time. All the pages in the bio are
1402 * checksummed and sums are attached onto the ordered extent record.
1404 * At IO completion time the cums attached on the ordered extent record
1405 * are inserted into the btree
1407 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1408 int mirror_num
, unsigned long bio_flags
,
1411 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1412 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1416 * extent_io.c submission hook. This does the right thing for csum calculation
1417 * on write, or reading the csums from the tree before a read
1419 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1420 int mirror_num
, unsigned long bio_flags
,
1423 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1427 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1429 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
1432 if (!(rw
& (1 << BIO_RW
))) {
1433 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1434 return btrfs_submit_compressed_read(inode
, bio
,
1435 mirror_num
, bio_flags
);
1436 } else if (!skip_sum
)
1437 btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1439 } else if (!skip_sum
) {
1440 /* csum items have already been cloned */
1441 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1443 /* we're doing a write, do the async checksumming */
1444 return btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1445 inode
, rw
, bio
, mirror_num
,
1446 bio_flags
, bio_offset
,
1447 __btrfs_submit_bio_start
,
1448 __btrfs_submit_bio_done
);
1452 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1456 * given a list of ordered sums record them in the inode. This happens
1457 * at IO completion time based on sums calculated at bio submission time.
1459 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1460 struct inode
*inode
, u64 file_offset
,
1461 struct list_head
*list
)
1463 struct btrfs_ordered_sum
*sum
;
1465 btrfs_set_trans_block_group(trans
, inode
);
1467 list_for_each_entry(sum
, list
, list
) {
1468 btrfs_csum_file_blocks(trans
,
1469 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1474 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1475 struct extent_state
**cached_state
)
1477 if ((end
& (PAGE_CACHE_SIZE
- 1)) == 0)
1479 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1480 cached_state
, GFP_NOFS
);
1483 /* see btrfs_writepage_start_hook for details on why this is required */
1484 struct btrfs_writepage_fixup
{
1486 struct btrfs_work work
;
1489 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1491 struct btrfs_writepage_fixup
*fixup
;
1492 struct btrfs_ordered_extent
*ordered
;
1493 struct extent_state
*cached_state
= NULL
;
1495 struct inode
*inode
;
1499 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1503 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1504 ClearPageChecked(page
);
1508 inode
= page
->mapping
->host
;
1509 page_start
= page_offset(page
);
1510 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1512 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1513 &cached_state
, GFP_NOFS
);
1515 /* already ordered? We're done */
1516 if (PagePrivate2(page
))
1519 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1521 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1522 page_end
, &cached_state
, GFP_NOFS
);
1524 btrfs_start_ordered_extent(inode
, ordered
, 1);
1529 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1530 ClearPageChecked(page
);
1532 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1533 &cached_state
, GFP_NOFS
);
1536 page_cache_release(page
);
1540 * There are a few paths in the higher layers of the kernel that directly
1541 * set the page dirty bit without asking the filesystem if it is a
1542 * good idea. This causes problems because we want to make sure COW
1543 * properly happens and the data=ordered rules are followed.
1545 * In our case any range that doesn't have the ORDERED bit set
1546 * hasn't been properly setup for IO. We kick off an async process
1547 * to fix it up. The async helper will wait for ordered extents, set
1548 * the delalloc bit and make it safe to write the page.
1550 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1552 struct inode
*inode
= page
->mapping
->host
;
1553 struct btrfs_writepage_fixup
*fixup
;
1554 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1556 /* this page is properly in the ordered list */
1557 if (TestClearPagePrivate2(page
))
1560 if (PageChecked(page
))
1563 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1567 SetPageChecked(page
);
1568 page_cache_get(page
);
1569 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
1571 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
1575 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1576 struct inode
*inode
, u64 file_pos
,
1577 u64 disk_bytenr
, u64 disk_num_bytes
,
1578 u64 num_bytes
, u64 ram_bytes
,
1579 u8 compression
, u8 encryption
,
1580 u16 other_encoding
, int extent_type
)
1582 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1583 struct btrfs_file_extent_item
*fi
;
1584 struct btrfs_path
*path
;
1585 struct extent_buffer
*leaf
;
1586 struct btrfs_key ins
;
1590 path
= btrfs_alloc_path();
1593 path
->leave_spinning
= 1;
1596 * we may be replacing one extent in the tree with another.
1597 * The new extent is pinned in the extent map, and we don't want
1598 * to drop it from the cache until it is completely in the btree.
1600 * So, tell btrfs_drop_extents to leave this extent in the cache.
1601 * the caller is expected to unpin it and allow it to be merged
1604 ret
= btrfs_drop_extents(trans
, inode
, file_pos
, file_pos
+ num_bytes
,
1608 ins
.objectid
= inode
->i_ino
;
1609 ins
.offset
= file_pos
;
1610 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1611 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
, sizeof(*fi
));
1613 leaf
= path
->nodes
[0];
1614 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1615 struct btrfs_file_extent_item
);
1616 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1617 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1618 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1619 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1620 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1621 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1622 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1623 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1624 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1625 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1627 btrfs_unlock_up_safe(path
, 1);
1628 btrfs_set_lock_blocking(leaf
);
1630 btrfs_mark_buffer_dirty(leaf
);
1632 inode_add_bytes(inode
, num_bytes
);
1634 ins
.objectid
= disk_bytenr
;
1635 ins
.offset
= disk_num_bytes
;
1636 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1637 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
1638 root
->root_key
.objectid
,
1639 inode
->i_ino
, file_pos
, &ins
);
1641 btrfs_free_path(path
);
1647 * helper function for btrfs_finish_ordered_io, this
1648 * just reads in some of the csum leaves to prime them into ram
1649 * before we start the transaction. It limits the amount of btree
1650 * reads required while inside the transaction.
1652 /* as ordered data IO finishes, this gets called so we can finish
1653 * an ordered extent if the range of bytes in the file it covers are
1656 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
)
1658 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1659 struct btrfs_trans_handle
*trans
= NULL
;
1660 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
1661 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1662 struct extent_state
*cached_state
= NULL
;
1666 ret
= btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
1670 BUG_ON(!ordered_extent
);
1672 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
1673 BUG_ON(!list_empty(&ordered_extent
->list
));
1674 ret
= btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
1676 trans
= btrfs_join_transaction(root
, 1);
1677 btrfs_set_trans_block_group(trans
, inode
);
1678 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1679 ret
= btrfs_update_inode(trans
, root
, inode
);
1685 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
1686 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
1687 0, &cached_state
, GFP_NOFS
);
1689 trans
= btrfs_join_transaction(root
, 1);
1690 btrfs_set_trans_block_group(trans
, inode
);
1691 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1693 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
1695 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
1697 ret
= btrfs_mark_extent_written(trans
, inode
,
1698 ordered_extent
->file_offset
,
1699 ordered_extent
->file_offset
+
1700 ordered_extent
->len
);
1703 ret
= insert_reserved_file_extent(trans
, inode
,
1704 ordered_extent
->file_offset
,
1705 ordered_extent
->start
,
1706 ordered_extent
->disk_len
,
1707 ordered_extent
->len
,
1708 ordered_extent
->len
,
1710 BTRFS_FILE_EXTENT_REG
);
1711 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
1712 ordered_extent
->file_offset
,
1713 ordered_extent
->len
);
1716 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
1717 ordered_extent
->file_offset
+
1718 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
1720 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
1721 &ordered_extent
->list
);
1723 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
1724 ret
= btrfs_update_inode(trans
, root
, inode
);
1727 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
1729 btrfs_end_transaction(trans
, root
);
1731 btrfs_put_ordered_extent(ordered_extent
);
1732 /* once for the tree */
1733 btrfs_put_ordered_extent(ordered_extent
);
1738 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
1739 struct extent_state
*state
, int uptodate
)
1741 ClearPagePrivate2(page
);
1742 return btrfs_finish_ordered_io(page
->mapping
->host
, start
, end
);
1746 * When IO fails, either with EIO or csum verification fails, we
1747 * try other mirrors that might have a good copy of the data. This
1748 * io_failure_record is used to record state as we go through all the
1749 * mirrors. If another mirror has good data, the page is set up to date
1750 * and things continue. If a good mirror can't be found, the original
1751 * bio end_io callback is called to indicate things have failed.
1753 struct io_failure_record
{
1758 unsigned long bio_flags
;
1762 static int btrfs_io_failed_hook(struct bio
*failed_bio
,
1763 struct page
*page
, u64 start
, u64 end
,
1764 struct extent_state
*state
)
1766 struct io_failure_record
*failrec
= NULL
;
1768 struct extent_map
*em
;
1769 struct inode
*inode
= page
->mapping
->host
;
1770 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
1771 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1778 ret
= get_state_private(failure_tree
, start
, &private);
1780 failrec
= kmalloc(sizeof(*failrec
), GFP_NOFS
);
1783 failrec
->start
= start
;
1784 failrec
->len
= end
- start
+ 1;
1785 failrec
->last_mirror
= 0;
1786 failrec
->bio_flags
= 0;
1788 read_lock(&em_tree
->lock
);
1789 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
1790 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
1791 free_extent_map(em
);
1794 read_unlock(&em_tree
->lock
);
1796 if (!em
|| IS_ERR(em
)) {
1800 logical
= start
- em
->start
;
1801 logical
= em
->block_start
+ logical
;
1802 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
1803 logical
= em
->block_start
;
1804 failrec
->bio_flags
= EXTENT_BIO_COMPRESSED
;
1806 failrec
->logical
= logical
;
1807 free_extent_map(em
);
1808 set_extent_bits(failure_tree
, start
, end
, EXTENT_LOCKED
|
1809 EXTENT_DIRTY
, GFP_NOFS
);
1810 set_state_private(failure_tree
, start
,
1811 (u64
)(unsigned long)failrec
);
1813 failrec
= (struct io_failure_record
*)(unsigned long)private;
1815 num_copies
= btrfs_num_copies(
1816 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
1817 failrec
->logical
, failrec
->len
);
1818 failrec
->last_mirror
++;
1820 spin_lock(&BTRFS_I(inode
)->io_tree
.lock
);
1821 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
1824 if (state
&& state
->start
!= failrec
->start
)
1826 spin_unlock(&BTRFS_I(inode
)->io_tree
.lock
);
1828 if (!state
|| failrec
->last_mirror
> num_copies
) {
1829 set_state_private(failure_tree
, failrec
->start
, 0);
1830 clear_extent_bits(failure_tree
, failrec
->start
,
1831 failrec
->start
+ failrec
->len
- 1,
1832 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
1836 bio
= bio_alloc(GFP_NOFS
, 1);
1837 bio
->bi_private
= state
;
1838 bio
->bi_end_io
= failed_bio
->bi_end_io
;
1839 bio
->bi_sector
= failrec
->logical
>> 9;
1840 bio
->bi_bdev
= failed_bio
->bi_bdev
;
1843 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
1844 if (failed_bio
->bi_rw
& (1 << BIO_RW
))
1849 BTRFS_I(inode
)->io_tree
.ops
->submit_bio_hook(inode
, rw
, bio
,
1850 failrec
->last_mirror
,
1851 failrec
->bio_flags
, 0);
1856 * each time an IO finishes, we do a fast check in the IO failure tree
1857 * to see if we need to process or clean up an io_failure_record
1859 static int btrfs_clean_io_failures(struct inode
*inode
, u64 start
)
1862 u64 private_failure
;
1863 struct io_failure_record
*failure
;
1867 if (count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
1868 (u64
)-1, 1, EXTENT_DIRTY
)) {
1869 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
,
1870 start
, &private_failure
);
1872 failure
= (struct io_failure_record
*)(unsigned long)
1874 set_state_private(&BTRFS_I(inode
)->io_failure_tree
,
1876 clear_extent_bits(&BTRFS_I(inode
)->io_failure_tree
,
1878 failure
->start
+ failure
->len
- 1,
1879 EXTENT_DIRTY
| EXTENT_LOCKED
,
1888 * when reads are done, we need to check csums to verify the data is correct
1889 * if there's a match, we allow the bio to finish. If not, we go through
1890 * the io_failure_record routines to find good copies
1892 static int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
1893 struct extent_state
*state
)
1895 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
1896 struct inode
*inode
= page
->mapping
->host
;
1897 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1899 u64
private = ~(u32
)0;
1901 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1904 if (PageChecked(page
)) {
1905 ClearPageChecked(page
);
1909 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
1912 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
1913 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
1914 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
1919 if (state
&& state
->start
== start
) {
1920 private = state
->private;
1923 ret
= get_state_private(io_tree
, start
, &private);
1925 kaddr
= kmap_atomic(page
, KM_USER0
);
1929 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
1930 btrfs_csum_final(csum
, (char *)&csum
);
1931 if (csum
!= private)
1934 kunmap_atomic(kaddr
, KM_USER0
);
1936 /* if the io failure tree for this inode is non-empty,
1937 * check to see if we've recovered from a failed IO
1939 btrfs_clean_io_failures(inode
, start
);
1943 if (printk_ratelimit()) {
1944 printk(KERN_INFO
"btrfs csum failed ino %lu off %llu csum %u "
1945 "private %llu\n", page
->mapping
->host
->i_ino
,
1946 (unsigned long long)start
, csum
,
1947 (unsigned long long)private);
1949 memset(kaddr
+ offset
, 1, end
- start
+ 1);
1950 flush_dcache_page(page
);
1951 kunmap_atomic(kaddr
, KM_USER0
);
1957 struct delayed_iput
{
1958 struct list_head list
;
1959 struct inode
*inode
;
1962 void btrfs_add_delayed_iput(struct inode
*inode
)
1964 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
1965 struct delayed_iput
*delayed
;
1967 if (atomic_add_unless(&inode
->i_count
, -1, 1))
1970 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
1971 delayed
->inode
= inode
;
1973 spin_lock(&fs_info
->delayed_iput_lock
);
1974 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
1975 spin_unlock(&fs_info
->delayed_iput_lock
);
1978 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
1981 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1982 struct delayed_iput
*delayed
;
1985 spin_lock(&fs_info
->delayed_iput_lock
);
1986 empty
= list_empty(&fs_info
->delayed_iputs
);
1987 spin_unlock(&fs_info
->delayed_iput_lock
);
1991 down_read(&root
->fs_info
->cleanup_work_sem
);
1992 spin_lock(&fs_info
->delayed_iput_lock
);
1993 list_splice_init(&fs_info
->delayed_iputs
, &list
);
1994 spin_unlock(&fs_info
->delayed_iput_lock
);
1996 while (!list_empty(&list
)) {
1997 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
1998 list_del(&delayed
->list
);
1999 iput(delayed
->inode
);
2002 up_read(&root
->fs_info
->cleanup_work_sem
);
2006 * calculate extra metadata reservation when snapshotting a subvolume
2007 * contains orphan files.
2009 void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle
*trans
,
2010 struct btrfs_pending_snapshot
*pending
,
2011 u64
*bytes_to_reserve
)
2013 struct btrfs_root
*root
;
2014 struct btrfs_block_rsv
*block_rsv
;
2018 root
= pending
->root
;
2019 if (!root
->orphan_block_rsv
|| list_empty(&root
->orphan_list
))
2022 block_rsv
= root
->orphan_block_rsv
;
2024 /* orphan block reservation for the snapshot */
2025 num_bytes
= block_rsv
->size
;
2028 * after the snapshot is created, COWing tree blocks may use more
2029 * space than it frees. So we should make sure there is enough
2032 index
= trans
->transid
& 0x1;
2033 if (block_rsv
->reserved
+ block_rsv
->freed
[index
] < block_rsv
->size
) {
2034 num_bytes
+= block_rsv
->size
-
2035 (block_rsv
->reserved
+ block_rsv
->freed
[index
]);
2038 *bytes_to_reserve
+= num_bytes
;
2041 void btrfs_orphan_post_snapshot(struct btrfs_trans_handle
*trans
,
2042 struct btrfs_pending_snapshot
*pending
)
2044 struct btrfs_root
*root
= pending
->root
;
2045 struct btrfs_root
*snap
= pending
->snap
;
2046 struct btrfs_block_rsv
*block_rsv
;
2051 if (!root
->orphan_block_rsv
|| list_empty(&root
->orphan_list
))
2054 /* refill source subvolume's orphan block reservation */
2055 block_rsv
= root
->orphan_block_rsv
;
2056 index
= trans
->transid
& 0x1;
2057 if (block_rsv
->reserved
+ block_rsv
->freed
[index
] < block_rsv
->size
) {
2058 num_bytes
= block_rsv
->size
-
2059 (block_rsv
->reserved
+ block_rsv
->freed
[index
]);
2060 ret
= btrfs_block_rsv_migrate(&pending
->block_rsv
,
2061 root
->orphan_block_rsv
,
2066 /* setup orphan block reservation for the snapshot */
2067 block_rsv
= btrfs_alloc_block_rsv(snap
);
2070 btrfs_add_durable_block_rsv(root
->fs_info
, block_rsv
);
2071 snap
->orphan_block_rsv
= block_rsv
;
2073 num_bytes
= root
->orphan_block_rsv
->size
;
2074 ret
= btrfs_block_rsv_migrate(&pending
->block_rsv
,
2075 block_rsv
, num_bytes
);
2079 /* insert orphan item for the snapshot */
2080 WARN_ON(!root
->orphan_item_inserted
);
2081 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
2082 snap
->root_key
.objectid
);
2084 snap
->orphan_item_inserted
= 1;
2088 enum btrfs_orphan_cleanup_state
{
2089 ORPHAN_CLEANUP_STARTED
= 1,
2090 ORPHAN_CLEANUP_DONE
= 2,
2094 * This is called in transaction commmit time. If there are no orphan
2095 * files in the subvolume, it removes orphan item and frees block_rsv
2098 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
2099 struct btrfs_root
*root
)
2103 if (!list_empty(&root
->orphan_list
) ||
2104 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
2107 if (root
->orphan_item_inserted
&&
2108 btrfs_root_refs(&root
->root_item
) > 0) {
2109 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
2110 root
->root_key
.objectid
);
2112 root
->orphan_item_inserted
= 0;
2115 if (root
->orphan_block_rsv
) {
2116 WARN_ON(root
->orphan_block_rsv
->size
> 0);
2117 btrfs_free_block_rsv(root
, root
->orphan_block_rsv
);
2118 root
->orphan_block_rsv
= NULL
;
2123 * This creates an orphan entry for the given inode in case something goes
2124 * wrong in the middle of an unlink/truncate.
2126 * NOTE: caller of this function should reserve 5 units of metadata for
2129 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2131 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2132 struct btrfs_block_rsv
*block_rsv
= NULL
;
2137 if (!root
->orphan_block_rsv
) {
2138 block_rsv
= btrfs_alloc_block_rsv(root
);
2142 spin_lock(&root
->orphan_lock
);
2143 if (!root
->orphan_block_rsv
) {
2144 root
->orphan_block_rsv
= block_rsv
;
2145 } else if (block_rsv
) {
2146 btrfs_free_block_rsv(root
, block_rsv
);
2150 if (list_empty(&BTRFS_I(inode
)->i_orphan
)) {
2151 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
2154 * For proper ENOSPC handling, we should do orphan
2155 * cleanup when mounting. But this introduces backward
2156 * compatibility issue.
2158 if (!xchg(&root
->orphan_item_inserted
, 1))
2165 WARN_ON(!BTRFS_I(inode
)->orphan_meta_reserved
);
2168 if (!BTRFS_I(inode
)->orphan_meta_reserved
) {
2169 BTRFS_I(inode
)->orphan_meta_reserved
= 1;
2172 spin_unlock(&root
->orphan_lock
);
2175 btrfs_add_durable_block_rsv(root
->fs_info
, block_rsv
);
2177 /* grab metadata reservation from transaction handle */
2179 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
2183 /* insert an orphan item to track this unlinked/truncated file */
2185 ret
= btrfs_insert_orphan_item(trans
, root
, inode
->i_ino
);
2189 /* insert an orphan item to track subvolume contains orphan files */
2191 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
2192 root
->root_key
.objectid
);
2199 * We have done the truncate/delete so we can go ahead and remove the orphan
2200 * item for this particular inode.
2202 int btrfs_orphan_del(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2204 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2205 int delete_item
= 0;
2206 int release_rsv
= 0;
2209 spin_lock(&root
->orphan_lock
);
2210 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
2211 list_del_init(&BTRFS_I(inode
)->i_orphan
);
2215 if (BTRFS_I(inode
)->orphan_meta_reserved
) {
2216 BTRFS_I(inode
)->orphan_meta_reserved
= 0;
2219 spin_unlock(&root
->orphan_lock
);
2221 if (trans
&& delete_item
) {
2222 ret
= btrfs_del_orphan_item(trans
, root
, inode
->i_ino
);
2227 btrfs_orphan_release_metadata(inode
);
2233 * this cleans up any orphans that may be left on the list from the last use
2236 void btrfs_orphan_cleanup(struct btrfs_root
*root
)
2238 struct btrfs_path
*path
;
2239 struct extent_buffer
*leaf
;
2240 struct btrfs_item
*item
;
2241 struct btrfs_key key
, found_key
;
2242 struct btrfs_trans_handle
*trans
;
2243 struct inode
*inode
;
2244 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
2246 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
2249 path
= btrfs_alloc_path();
2253 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
2254 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
2255 key
.offset
= (u64
)-1;
2258 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2260 printk(KERN_ERR
"Error searching slot for orphan: %d"
2266 * if ret == 0 means we found what we were searching for, which
2267 * is weird, but possible, so only screw with path if we didnt
2268 * find the key and see if we have stuff that matches
2271 if (path
->slots
[0] == 0)
2276 /* pull out the item */
2277 leaf
= path
->nodes
[0];
2278 item
= btrfs_item_nr(leaf
, path
->slots
[0]);
2279 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2281 /* make sure the item matches what we want */
2282 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
2284 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
2287 /* release the path since we're done with it */
2288 btrfs_release_path(root
, path
);
2291 * this is where we are basically btrfs_lookup, without the
2292 * crossing root thing. we store the inode number in the
2293 * offset of the orphan item.
2295 found_key
.objectid
= found_key
.offset
;
2296 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
2297 found_key
.offset
= 0;
2298 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
2299 BUG_ON(IS_ERR(inode
));
2302 * add this inode to the orphan list so btrfs_orphan_del does
2303 * the proper thing when we hit it
2305 spin_lock(&root
->orphan_lock
);
2306 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
2307 spin_unlock(&root
->orphan_lock
);
2310 * if this is a bad inode, means we actually succeeded in
2311 * removing the inode, but not the orphan record, which means
2312 * we need to manually delete the orphan since iput will just
2313 * do a destroy_inode
2315 if (is_bad_inode(inode
)) {
2316 trans
= btrfs_start_transaction(root
, 0);
2317 btrfs_orphan_del(trans
, inode
);
2318 btrfs_end_transaction(trans
, root
);
2323 /* if we have links, this was a truncate, lets do that */
2324 if (inode
->i_nlink
) {
2326 btrfs_truncate(inode
);
2331 /* this will do delete_inode and everything for us */
2334 btrfs_free_path(path
);
2336 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
2338 if (root
->orphan_block_rsv
)
2339 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
2342 if (root
->orphan_block_rsv
|| root
->orphan_item_inserted
) {
2343 trans
= btrfs_join_transaction(root
, 1);
2344 btrfs_end_transaction(trans
, root
);
2348 printk(KERN_INFO
"btrfs: unlinked %d orphans\n", nr_unlink
);
2350 printk(KERN_INFO
"btrfs: truncated %d orphans\n", nr_truncate
);
2354 * very simple check to peek ahead in the leaf looking for xattrs. If we
2355 * don't find any xattrs, we know there can't be any acls.
2357 * slot is the slot the inode is in, objectid is the objectid of the inode
2359 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
2360 int slot
, u64 objectid
)
2362 u32 nritems
= btrfs_header_nritems(leaf
);
2363 struct btrfs_key found_key
;
2367 while (slot
< nritems
) {
2368 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2370 /* we found a different objectid, there must not be acls */
2371 if (found_key
.objectid
!= objectid
)
2374 /* we found an xattr, assume we've got an acl */
2375 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
)
2379 * we found a key greater than an xattr key, there can't
2380 * be any acls later on
2382 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
2389 * it goes inode, inode backrefs, xattrs, extents,
2390 * so if there are a ton of hard links to an inode there can
2391 * be a lot of backrefs. Don't waste time searching too hard,
2392 * this is just an optimization
2397 /* we hit the end of the leaf before we found an xattr or
2398 * something larger than an xattr. We have to assume the inode
2405 * read an inode from the btree into the in-memory inode
2407 static void btrfs_read_locked_inode(struct inode
*inode
)
2409 struct btrfs_path
*path
;
2410 struct extent_buffer
*leaf
;
2411 struct btrfs_inode_item
*inode_item
;
2412 struct btrfs_timespec
*tspec
;
2413 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2414 struct btrfs_key location
;
2416 u64 alloc_group_block
;
2420 path
= btrfs_alloc_path();
2422 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
2424 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
2428 leaf
= path
->nodes
[0];
2429 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2430 struct btrfs_inode_item
);
2432 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
2433 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
2434 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
2435 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
2436 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
2438 tspec
= btrfs_inode_atime(inode_item
);
2439 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2440 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2442 tspec
= btrfs_inode_mtime(inode_item
);
2443 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2444 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2446 tspec
= btrfs_inode_ctime(inode_item
);
2447 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2448 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2450 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
2451 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
2452 BTRFS_I(inode
)->sequence
= btrfs_inode_sequence(leaf
, inode_item
);
2453 inode
->i_generation
= BTRFS_I(inode
)->generation
;
2455 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
2457 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
2458 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
2460 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
2463 * try to precache a NULL acl entry for files that don't have
2464 * any xattrs or acls
2466 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0], inode
->i_ino
);
2468 cache_no_acl(inode
);
2470 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
, 0,
2471 alloc_group_block
, 0);
2472 btrfs_free_path(path
);
2475 switch (inode
->i_mode
& S_IFMT
) {
2477 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2478 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2479 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2480 inode
->i_fop
= &btrfs_file_operations
;
2481 inode
->i_op
= &btrfs_file_inode_operations
;
2484 inode
->i_fop
= &btrfs_dir_file_operations
;
2485 if (root
== root
->fs_info
->tree_root
)
2486 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
2488 inode
->i_op
= &btrfs_dir_inode_operations
;
2491 inode
->i_op
= &btrfs_symlink_inode_operations
;
2492 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2493 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2496 inode
->i_op
= &btrfs_special_inode_operations
;
2497 init_special_inode(inode
, inode
->i_mode
, rdev
);
2501 btrfs_update_iflags(inode
);
2505 btrfs_free_path(path
);
2506 make_bad_inode(inode
);
2510 * given a leaf and an inode, copy the inode fields into the leaf
2512 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
2513 struct extent_buffer
*leaf
,
2514 struct btrfs_inode_item
*item
,
2515 struct inode
*inode
)
2517 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
2518 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
2519 btrfs_set_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
);
2520 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
2521 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
2523 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
2524 inode
->i_atime
.tv_sec
);
2525 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
2526 inode
->i_atime
.tv_nsec
);
2528 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
2529 inode
->i_mtime
.tv_sec
);
2530 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
2531 inode
->i_mtime
.tv_nsec
);
2533 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
2534 inode
->i_ctime
.tv_sec
);
2535 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
2536 inode
->i_ctime
.tv_nsec
);
2538 btrfs_set_inode_nbytes(leaf
, item
, inode_get_bytes(inode
));
2539 btrfs_set_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
);
2540 btrfs_set_inode_sequence(leaf
, item
, BTRFS_I(inode
)->sequence
);
2541 btrfs_set_inode_transid(leaf
, item
, trans
->transid
);
2542 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
2543 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
2544 btrfs_set_inode_block_group(leaf
, item
, BTRFS_I(inode
)->block_group
);
2548 * copy everything in the in-memory inode into the btree.
2550 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
2551 struct btrfs_root
*root
, struct inode
*inode
)
2553 struct btrfs_inode_item
*inode_item
;
2554 struct btrfs_path
*path
;
2555 struct extent_buffer
*leaf
;
2558 path
= btrfs_alloc_path();
2560 path
->leave_spinning
= 1;
2561 ret
= btrfs_lookup_inode(trans
, root
, path
,
2562 &BTRFS_I(inode
)->location
, 1);
2569 btrfs_unlock_up_safe(path
, 1);
2570 leaf
= path
->nodes
[0];
2571 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2572 struct btrfs_inode_item
);
2574 fill_inode_item(trans
, leaf
, inode_item
, inode
);
2575 btrfs_mark_buffer_dirty(leaf
);
2576 btrfs_set_inode_last_trans(trans
, inode
);
2579 btrfs_free_path(path
);
2585 * unlink helper that gets used here in inode.c and in the tree logging
2586 * recovery code. It remove a link in a directory with a given name, and
2587 * also drops the back refs in the inode to the directory
2589 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
2590 struct btrfs_root
*root
,
2591 struct inode
*dir
, struct inode
*inode
,
2592 const char *name
, int name_len
)
2594 struct btrfs_path
*path
;
2596 struct extent_buffer
*leaf
;
2597 struct btrfs_dir_item
*di
;
2598 struct btrfs_key key
;
2601 path
= btrfs_alloc_path();
2607 path
->leave_spinning
= 1;
2608 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
2609 name
, name_len
, -1);
2618 leaf
= path
->nodes
[0];
2619 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2620 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2623 btrfs_release_path(root
, path
);
2625 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
2627 dir
->i_ino
, &index
);
2629 printk(KERN_INFO
"btrfs failed to delete reference to %.*s, "
2630 "inode %lu parent %lu\n", name_len
, name
,
2631 inode
->i_ino
, dir
->i_ino
);
2635 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
2636 index
, name
, name_len
, -1);
2645 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2646 btrfs_release_path(root
, path
);
2648 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
2650 BUG_ON(ret
!= 0 && ret
!= -ENOENT
);
2652 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
2656 btrfs_free_path(path
);
2660 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
2661 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
2662 btrfs_update_inode(trans
, root
, dir
);
2663 btrfs_drop_nlink(inode
);
2664 ret
= btrfs_update_inode(trans
, root
, inode
);
2669 /* helper to check if there is any shared block in the path */
2670 static int check_path_shared(struct btrfs_root
*root
,
2671 struct btrfs_path
*path
)
2673 struct extent_buffer
*eb
;
2678 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
2679 if (!path
->nodes
[level
])
2681 eb
= path
->nodes
[level
];
2682 if (!btrfs_block_can_be_shared(root
, eb
))
2684 ret
= btrfs_lookup_extent_info(NULL
, root
, eb
->start
, eb
->len
,
2693 * helper to start transaction for unlink and rmdir.
2695 * unlink and rmdir are special in btrfs, they do not always free space.
2696 * so in enospc case, we should make sure they will free space before
2697 * allowing them to use the global metadata reservation.
2699 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
,
2700 struct dentry
*dentry
)
2702 struct btrfs_trans_handle
*trans
;
2703 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2704 struct btrfs_path
*path
;
2705 struct btrfs_inode_ref
*ref
;
2706 struct btrfs_dir_item
*di
;
2707 struct inode
*inode
= dentry
->d_inode
;
2713 trans
= btrfs_start_transaction(root
, 10);
2714 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
2717 if (inode
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
2718 return ERR_PTR(-ENOSPC
);
2720 /* check if there is someone else holds reference */
2721 if (S_ISDIR(inode
->i_mode
) && atomic_read(&inode
->i_count
) > 1)
2722 return ERR_PTR(-ENOSPC
);
2724 if (atomic_read(&inode
->i_count
) > 2)
2725 return ERR_PTR(-ENOSPC
);
2727 if (xchg(&root
->fs_info
->enospc_unlink
, 1))
2728 return ERR_PTR(-ENOSPC
);
2730 path
= btrfs_alloc_path();
2732 root
->fs_info
->enospc_unlink
= 0;
2733 return ERR_PTR(-ENOMEM
);
2736 trans
= btrfs_start_transaction(root
, 0);
2737 if (IS_ERR(trans
)) {
2738 btrfs_free_path(path
);
2739 root
->fs_info
->enospc_unlink
= 0;
2743 path
->skip_locking
= 1;
2744 path
->search_commit_root
= 1;
2746 ret
= btrfs_lookup_inode(trans
, root
, path
,
2747 &BTRFS_I(dir
)->location
, 0);
2753 if (check_path_shared(root
, path
))
2758 btrfs_release_path(root
, path
);
2760 ret
= btrfs_lookup_inode(trans
, root
, path
,
2761 &BTRFS_I(inode
)->location
, 0);
2767 if (check_path_shared(root
, path
))
2772 btrfs_release_path(root
, path
);
2774 if (ret
== 0 && S_ISREG(inode
->i_mode
)) {
2775 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
2776 inode
->i_ino
, (u64
)-1, 0);
2782 if (check_path_shared(root
, path
))
2784 btrfs_release_path(root
, path
);
2792 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
2793 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
2799 if (check_path_shared(root
, path
))
2805 btrfs_release_path(root
, path
);
2807 ref
= btrfs_lookup_inode_ref(trans
, root
, path
,
2808 dentry
->d_name
.name
, dentry
->d_name
.len
,
2809 inode
->i_ino
, dir
->i_ino
, 0);
2815 if (check_path_shared(root
, path
))
2817 index
= btrfs_inode_ref_index(path
->nodes
[0], ref
);
2818 btrfs_release_path(root
, path
);
2820 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
, index
,
2821 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
2826 BUG_ON(ret
== -ENOENT
);
2827 if (check_path_shared(root
, path
))
2832 btrfs_free_path(path
);
2834 btrfs_end_transaction(trans
, root
);
2835 root
->fs_info
->enospc_unlink
= 0;
2836 return ERR_PTR(err
);
2839 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
2843 static void __unlink_end_trans(struct btrfs_trans_handle
*trans
,
2844 struct btrfs_root
*root
)
2846 if (trans
->block_rsv
== &root
->fs_info
->global_block_rsv
) {
2847 BUG_ON(!root
->fs_info
->enospc_unlink
);
2848 root
->fs_info
->enospc_unlink
= 0;
2850 btrfs_end_transaction_throttle(trans
, root
);
2853 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
2855 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2856 struct btrfs_trans_handle
*trans
;
2857 struct inode
*inode
= dentry
->d_inode
;
2859 unsigned long nr
= 0;
2861 trans
= __unlink_start_trans(dir
, dentry
);
2863 return PTR_ERR(trans
);
2865 btrfs_set_trans_block_group(trans
, dir
);
2867 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
2869 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
2870 dentry
->d_name
.name
, dentry
->d_name
.len
);
2873 if (inode
->i_nlink
== 0) {
2874 ret
= btrfs_orphan_add(trans
, inode
);
2878 nr
= trans
->blocks_used
;
2879 __unlink_end_trans(trans
, root
);
2880 btrfs_btree_balance_dirty(root
, nr
);
2884 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
2885 struct btrfs_root
*root
,
2886 struct inode
*dir
, u64 objectid
,
2887 const char *name
, int name_len
)
2889 struct btrfs_path
*path
;
2890 struct extent_buffer
*leaf
;
2891 struct btrfs_dir_item
*di
;
2892 struct btrfs_key key
;
2896 path
= btrfs_alloc_path();
2900 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
2901 name
, name_len
, -1);
2902 BUG_ON(!di
|| IS_ERR(di
));
2904 leaf
= path
->nodes
[0];
2905 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2906 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
2907 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2909 btrfs_release_path(root
, path
);
2911 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
2912 objectid
, root
->root_key
.objectid
,
2913 dir
->i_ino
, &index
, name
, name_len
);
2915 BUG_ON(ret
!= -ENOENT
);
2916 di
= btrfs_search_dir_index_item(root
, path
, dir
->i_ino
,
2918 BUG_ON(!di
|| IS_ERR(di
));
2920 leaf
= path
->nodes
[0];
2921 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2922 btrfs_release_path(root
, path
);
2926 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
2927 index
, name
, name_len
, -1);
2928 BUG_ON(!di
|| IS_ERR(di
));
2930 leaf
= path
->nodes
[0];
2931 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2932 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
2933 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2935 btrfs_release_path(root
, path
);
2937 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
2938 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
2939 ret
= btrfs_update_inode(trans
, root
, dir
);
2941 dir
->i_sb
->s_dirt
= 1;
2943 btrfs_free_path(path
);
2947 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2949 struct inode
*inode
= dentry
->d_inode
;
2951 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2952 struct btrfs_trans_handle
*trans
;
2953 unsigned long nr
= 0;
2955 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
||
2956 inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)
2959 trans
= __unlink_start_trans(dir
, dentry
);
2961 return PTR_ERR(trans
);
2963 btrfs_set_trans_block_group(trans
, dir
);
2965 if (unlikely(inode
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
2966 err
= btrfs_unlink_subvol(trans
, root
, dir
,
2967 BTRFS_I(inode
)->location
.objectid
,
2968 dentry
->d_name
.name
,
2969 dentry
->d_name
.len
);
2973 err
= btrfs_orphan_add(trans
, inode
);
2977 /* now the directory is empty */
2978 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
2979 dentry
->d_name
.name
, dentry
->d_name
.len
);
2981 btrfs_i_size_write(inode
, 0);
2983 nr
= trans
->blocks_used
;
2984 __unlink_end_trans(trans
, root
);
2985 btrfs_btree_balance_dirty(root
, nr
);
2992 * when truncating bytes in a file, it is possible to avoid reading
2993 * the leaves that contain only checksum items. This can be the
2994 * majority of the IO required to delete a large file, but it must
2995 * be done carefully.
2997 * The keys in the level just above the leaves are checked to make sure
2998 * the lowest key in a given leaf is a csum key, and starts at an offset
2999 * after the new size.
3001 * Then the key for the next leaf is checked to make sure it also has
3002 * a checksum item for the same file. If it does, we know our target leaf
3003 * contains only checksum items, and it can be safely freed without reading
3006 * This is just an optimization targeted at large files. It may do
3007 * nothing. It will return 0 unless things went badly.
3009 static noinline
int drop_csum_leaves(struct btrfs_trans_handle
*trans
,
3010 struct btrfs_root
*root
,
3011 struct btrfs_path
*path
,
3012 struct inode
*inode
, u64 new_size
)
3014 struct btrfs_key key
;
3017 struct btrfs_key found_key
;
3018 struct btrfs_key other_key
;
3019 struct btrfs_leaf_ref
*ref
;
3023 path
->lowest_level
= 1;
3024 key
.objectid
= inode
->i_ino
;
3025 key
.type
= BTRFS_CSUM_ITEM_KEY
;
3026 key
.offset
= new_size
;
3028 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3032 if (path
->nodes
[1] == NULL
) {
3037 btrfs_node_key_to_cpu(path
->nodes
[1], &found_key
, path
->slots
[1]);
3038 nritems
= btrfs_header_nritems(path
->nodes
[1]);
3043 if (path
->slots
[1] >= nritems
)
3046 /* did we find a key greater than anything we want to delete? */
3047 if (found_key
.objectid
> inode
->i_ino
||
3048 (found_key
.objectid
== inode
->i_ino
&& found_key
.type
> key
.type
))
3051 /* we check the next key in the node to make sure the leave contains
3052 * only checksum items. This comparison doesn't work if our
3053 * leaf is the last one in the node
3055 if (path
->slots
[1] + 1 >= nritems
) {
3057 /* search forward from the last key in the node, this
3058 * will bring us into the next node in the tree
3060 btrfs_node_key_to_cpu(path
->nodes
[1], &found_key
, nritems
- 1);
3062 /* unlikely, but we inc below, so check to be safe */
3063 if (found_key
.offset
== (u64
)-1)
3066 /* search_forward needs a path with locks held, do the
3067 * search again for the original key. It is possible
3068 * this will race with a balance and return a path that
3069 * we could modify, but this drop is just an optimization
3070 * and is allowed to miss some leaves.
3072 btrfs_release_path(root
, path
);
3075 /* setup a max key for search_forward */
3076 other_key
.offset
= (u64
)-1;
3077 other_key
.type
= key
.type
;
3078 other_key
.objectid
= key
.objectid
;
3080 path
->keep_locks
= 1;
3081 ret
= btrfs_search_forward(root
, &found_key
, &other_key
,
3083 path
->keep_locks
= 0;
3084 if (ret
|| found_key
.objectid
!= key
.objectid
||
3085 found_key
.type
!= key
.type
) {
3090 key
.offset
= found_key
.offset
;
3091 btrfs_release_path(root
, path
);
3096 /* we know there's one more slot after us in the tree,
3097 * read that key so we can verify it is also a checksum item
3099 btrfs_node_key_to_cpu(path
->nodes
[1], &other_key
, path
->slots
[1] + 1);
3101 if (found_key
.objectid
< inode
->i_ino
)
3104 if (found_key
.type
!= key
.type
|| found_key
.offset
< new_size
)
3108 * if the key for the next leaf isn't a csum key from this objectid,
3109 * we can't be sure there aren't good items inside this leaf.
3112 if (other_key
.objectid
!= inode
->i_ino
|| other_key
.type
!= key
.type
)
3115 leaf_start
= btrfs_node_blockptr(path
->nodes
[1], path
->slots
[1]);
3116 leaf_gen
= btrfs_node_ptr_generation(path
->nodes
[1], path
->slots
[1]);
3118 * it is safe to delete this leaf, it contains only
3119 * csum items from this inode at an offset >= new_size
3121 ret
= btrfs_del_leaf(trans
, root
, path
, leaf_start
);
3124 if (root
->ref_cows
&& leaf_gen
< trans
->transid
) {
3125 ref
= btrfs_alloc_leaf_ref(root
, 0);
3127 ref
->root_gen
= root
->root_key
.offset
;
3128 ref
->bytenr
= leaf_start
;
3130 ref
->generation
= leaf_gen
;
3133 btrfs_sort_leaf_ref(ref
);
3135 ret
= btrfs_add_leaf_ref(root
, ref
, 0);
3137 btrfs_free_leaf_ref(root
, ref
);
3143 btrfs_release_path(root
, path
);
3145 if (other_key
.objectid
== inode
->i_ino
&&
3146 other_key
.type
== key
.type
&& other_key
.offset
> key
.offset
) {
3147 key
.offset
= other_key
.offset
;
3153 /* fixup any changes we've made to the path */
3154 path
->lowest_level
= 0;
3155 path
->keep_locks
= 0;
3156 btrfs_release_path(root
, path
);
3163 * this can truncate away extent items, csum items and directory items.
3164 * It starts at a high offset and removes keys until it can't find
3165 * any higher than new_size
3167 * csum items that cross the new i_size are truncated to the new size
3170 * min_type is the minimum key type to truncate down to. If set to 0, this
3171 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3173 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
3174 struct btrfs_root
*root
,
3175 struct inode
*inode
,
3176 u64 new_size
, u32 min_type
)
3178 struct btrfs_path
*path
;
3179 struct extent_buffer
*leaf
;
3180 struct btrfs_file_extent_item
*fi
;
3181 struct btrfs_key key
;
3182 struct btrfs_key found_key
;
3183 u64 extent_start
= 0;
3184 u64 extent_num_bytes
= 0;
3185 u64 extent_offset
= 0;
3187 u64 mask
= root
->sectorsize
- 1;
3188 u32 found_type
= (u8
)-1;
3191 int pending_del_nr
= 0;
3192 int pending_del_slot
= 0;
3193 int extent_type
= -1;
3198 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
3201 btrfs_drop_extent_cache(inode
, new_size
& (~mask
), (u64
)-1, 0);
3203 path
= btrfs_alloc_path();
3207 key
.objectid
= inode
->i_ino
;
3208 key
.offset
= (u64
)-1;
3212 path
->leave_spinning
= 1;
3213 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3220 /* there are no items in the tree for us to truncate, we're
3223 if (path
->slots
[0] == 0)
3230 leaf
= path
->nodes
[0];
3231 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3232 found_type
= btrfs_key_type(&found_key
);
3235 if (found_key
.objectid
!= inode
->i_ino
)
3238 if (found_type
< min_type
)
3241 item_end
= found_key
.offset
;
3242 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
3243 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3244 struct btrfs_file_extent_item
);
3245 extent_type
= btrfs_file_extent_type(leaf
, fi
);
3246 encoding
= btrfs_file_extent_compression(leaf
, fi
);
3247 encoding
|= btrfs_file_extent_encryption(leaf
, fi
);
3248 encoding
|= btrfs_file_extent_other_encoding(leaf
, fi
);
3250 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3252 btrfs_file_extent_num_bytes(leaf
, fi
);
3253 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3254 item_end
+= btrfs_file_extent_inline_len(leaf
,
3259 if (found_type
> min_type
) {
3262 if (item_end
< new_size
)
3264 if (found_key
.offset
>= new_size
)
3270 /* FIXME, shrink the extent if the ref count is only 1 */
3271 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
3274 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3276 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
3277 if (!del_item
&& !encoding
) {
3278 u64 orig_num_bytes
=
3279 btrfs_file_extent_num_bytes(leaf
, fi
);
3280 extent_num_bytes
= new_size
-
3281 found_key
.offset
+ root
->sectorsize
- 1;
3282 extent_num_bytes
= extent_num_bytes
&
3283 ~((u64
)root
->sectorsize
- 1);
3284 btrfs_set_file_extent_num_bytes(leaf
, fi
,
3286 num_dec
= (orig_num_bytes
-
3288 if (root
->ref_cows
&& extent_start
!= 0)
3289 inode_sub_bytes(inode
, num_dec
);
3290 btrfs_mark_buffer_dirty(leaf
);
3293 btrfs_file_extent_disk_num_bytes(leaf
,
3295 extent_offset
= found_key
.offset
-
3296 btrfs_file_extent_offset(leaf
, fi
);
3298 /* FIXME blocksize != 4096 */
3299 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
3300 if (extent_start
!= 0) {
3303 inode_sub_bytes(inode
, num_dec
);
3306 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3308 * we can't truncate inline items that have had
3312 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
3313 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
3314 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
3315 u32 size
= new_size
- found_key
.offset
;
3317 if (root
->ref_cows
) {
3318 inode_sub_bytes(inode
, item_end
+ 1 -
3322 btrfs_file_extent_calc_inline_size(size
);
3323 ret
= btrfs_truncate_item(trans
, root
, path
,
3326 } else if (root
->ref_cows
) {
3327 inode_sub_bytes(inode
, item_end
+ 1 -
3333 if (!pending_del_nr
) {
3334 /* no pending yet, add ourselves */
3335 pending_del_slot
= path
->slots
[0];
3337 } else if (pending_del_nr
&&
3338 path
->slots
[0] + 1 == pending_del_slot
) {
3339 /* hop on the pending chunk */
3341 pending_del_slot
= path
->slots
[0];
3348 if (found_extent
&& root
->ref_cows
) {
3349 btrfs_set_path_blocking(path
);
3350 ret
= btrfs_free_extent(trans
, root
, extent_start
,
3351 extent_num_bytes
, 0,
3352 btrfs_header_owner(leaf
),
3353 inode
->i_ino
, extent_offset
);
3357 if (found_type
== BTRFS_INODE_ITEM_KEY
)
3360 if (path
->slots
[0] == 0 ||
3361 path
->slots
[0] != pending_del_slot
) {
3362 if (root
->ref_cows
) {
3366 if (pending_del_nr
) {
3367 ret
= btrfs_del_items(trans
, root
, path
,
3373 btrfs_release_path(root
, path
);
3380 if (pending_del_nr
) {
3381 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
3385 btrfs_free_path(path
);
3390 * taken from block_truncate_page, but does cow as it zeros out
3391 * any bytes left in the last page in the file.
3393 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
3395 struct inode
*inode
= mapping
->host
;
3396 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3397 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3398 struct btrfs_ordered_extent
*ordered
;
3399 struct extent_state
*cached_state
= NULL
;
3401 u32 blocksize
= root
->sectorsize
;
3402 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
3403 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
3409 if ((offset
& (blocksize
- 1)) == 0)
3411 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
3417 page
= grab_cache_page(mapping
, index
);
3419 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
3423 page_start
= page_offset(page
);
3424 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3426 if (!PageUptodate(page
)) {
3427 ret
= btrfs_readpage(NULL
, page
);
3429 if (page
->mapping
!= mapping
) {
3431 page_cache_release(page
);
3434 if (!PageUptodate(page
)) {
3439 wait_on_page_writeback(page
);
3441 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
,
3443 set_page_extent_mapped(page
);
3445 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
3447 unlock_extent_cached(io_tree
, page_start
, page_end
,
3448 &cached_state
, GFP_NOFS
);
3450 page_cache_release(page
);
3451 btrfs_start_ordered_extent(inode
, ordered
, 1);
3452 btrfs_put_ordered_extent(ordered
);
3456 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
3457 EXTENT_DIRTY
| EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
,
3458 0, 0, &cached_state
, GFP_NOFS
);
3460 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
3463 unlock_extent_cached(io_tree
, page_start
, page_end
,
3464 &cached_state
, GFP_NOFS
);
3469 if (offset
!= PAGE_CACHE_SIZE
) {
3471 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
3472 flush_dcache_page(page
);
3475 ClearPageChecked(page
);
3476 set_page_dirty(page
);
3477 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
3482 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
3484 page_cache_release(page
);
3489 int btrfs_cont_expand(struct inode
*inode
, loff_t size
)
3491 struct btrfs_trans_handle
*trans
;
3492 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3493 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3494 struct extent_map
*em
= NULL
;
3495 struct extent_state
*cached_state
= NULL
;
3496 u64 mask
= root
->sectorsize
- 1;
3497 u64 hole_start
= (inode
->i_size
+ mask
) & ~mask
;
3498 u64 block_end
= (size
+ mask
) & ~mask
;
3504 if (size
<= hole_start
)
3508 struct btrfs_ordered_extent
*ordered
;
3509 btrfs_wait_ordered_range(inode
, hole_start
,
3510 block_end
- hole_start
);
3511 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
3512 &cached_state
, GFP_NOFS
);
3513 ordered
= btrfs_lookup_ordered_extent(inode
, hole_start
);
3516 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
3517 &cached_state
, GFP_NOFS
);
3518 btrfs_put_ordered_extent(ordered
);
3521 cur_offset
= hole_start
;
3523 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
3524 block_end
- cur_offset
, 0);
3525 BUG_ON(IS_ERR(em
) || !em
);
3526 last_byte
= min(extent_map_end(em
), block_end
);
3527 last_byte
= (last_byte
+ mask
) & ~mask
;
3528 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
3530 hole_size
= last_byte
- cur_offset
;
3532 trans
= btrfs_start_transaction(root
, 2);
3533 if (IS_ERR(trans
)) {
3534 err
= PTR_ERR(trans
);
3537 btrfs_set_trans_block_group(trans
, inode
);
3539 err
= btrfs_drop_extents(trans
, inode
, cur_offset
,
3540 cur_offset
+ hole_size
,
3544 err
= btrfs_insert_file_extent(trans
, root
,
3545 inode
->i_ino
, cur_offset
, 0,
3546 0, hole_size
, 0, hole_size
,
3550 btrfs_drop_extent_cache(inode
, hole_start
,
3553 btrfs_end_transaction(trans
, root
);
3555 free_extent_map(em
);
3557 cur_offset
= last_byte
;
3558 if (cur_offset
>= block_end
)
3562 free_extent_map(em
);
3563 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
3568 static int btrfs_setattr_size(struct inode
*inode
, struct iattr
*attr
)
3570 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3571 struct btrfs_trans_handle
*trans
;
3575 if (attr
->ia_size
== inode
->i_size
)
3578 if (attr
->ia_size
> inode
->i_size
) {
3579 unsigned long limit
;
3580 limit
= current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
3581 if (attr
->ia_size
> inode
->i_sb
->s_maxbytes
)
3583 if (limit
!= RLIM_INFINITY
&& attr
->ia_size
> limit
) {
3584 send_sig(SIGXFSZ
, current
, 0);
3589 trans
= btrfs_start_transaction(root
, 5);
3591 return PTR_ERR(trans
);
3593 btrfs_set_trans_block_group(trans
, inode
);
3595 ret
= btrfs_orphan_add(trans
, inode
);
3598 nr
= trans
->blocks_used
;
3599 btrfs_end_transaction(trans
, root
);
3600 btrfs_btree_balance_dirty(root
, nr
);
3602 if (attr
->ia_size
> inode
->i_size
) {
3603 ret
= btrfs_cont_expand(inode
, attr
->ia_size
);
3605 btrfs_truncate(inode
);
3609 i_size_write(inode
, attr
->ia_size
);
3610 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
3612 trans
= btrfs_start_transaction(root
, 0);
3613 BUG_ON(IS_ERR(trans
));
3614 btrfs_set_trans_block_group(trans
, inode
);
3615 trans
->block_rsv
= root
->orphan_block_rsv
;
3616 BUG_ON(!trans
->block_rsv
);
3618 ret
= btrfs_update_inode(trans
, root
, inode
);
3620 if (inode
->i_nlink
> 0) {
3621 ret
= btrfs_orphan_del(trans
, inode
);
3624 nr
= trans
->blocks_used
;
3625 btrfs_end_transaction(trans
, root
);
3626 btrfs_btree_balance_dirty(root
, nr
);
3631 * We're truncating a file that used to have good data down to
3632 * zero. Make sure it gets into the ordered flush list so that
3633 * any new writes get down to disk quickly.
3635 if (attr
->ia_size
== 0)
3636 BTRFS_I(inode
)->ordered_data_close
= 1;
3638 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3639 ret
= vmtruncate(inode
, attr
->ia_size
);
3645 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
3647 struct inode
*inode
= dentry
->d_inode
;
3650 err
= inode_change_ok(inode
, attr
);
3654 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
3655 err
= btrfs_setattr_size(inode
, attr
);
3660 if (attr
->ia_valid
) {
3661 setattr_copy(inode
, attr
);
3662 mark_inode_dirty(inode
);
3664 if (attr
->ia_valid
& ATTR_MODE
)
3665 err
= btrfs_acl_chmod(inode
);
3671 void btrfs_evict_inode(struct inode
*inode
)
3673 struct btrfs_trans_handle
*trans
;
3674 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3678 truncate_inode_pages(&inode
->i_data
, 0);
3679 if (inode
->i_nlink
&& btrfs_root_refs(&root
->root_item
) != 0)
3682 if (is_bad_inode(inode
)) {
3683 btrfs_orphan_del(NULL
, inode
);
3686 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
3687 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
3689 if (root
->fs_info
->log_root_recovering
) {
3690 BUG_ON(!list_empty(&BTRFS_I(inode
)->i_orphan
));
3694 if (inode
->i_nlink
> 0) {
3695 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0);
3699 btrfs_i_size_write(inode
, 0);
3702 trans
= btrfs_start_transaction(root
, 0);
3703 BUG_ON(IS_ERR(trans
));
3704 btrfs_set_trans_block_group(trans
, inode
);
3705 trans
->block_rsv
= root
->orphan_block_rsv
;
3707 ret
= btrfs_block_rsv_check(trans
, root
,
3708 root
->orphan_block_rsv
, 0, 5);
3710 BUG_ON(ret
!= -EAGAIN
);
3711 ret
= btrfs_commit_transaction(trans
, root
);
3716 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
3720 nr
= trans
->blocks_used
;
3721 btrfs_end_transaction(trans
, root
);
3723 btrfs_btree_balance_dirty(root
, nr
);
3728 ret
= btrfs_orphan_del(trans
, inode
);
3732 nr
= trans
->blocks_used
;
3733 btrfs_end_transaction(trans
, root
);
3734 btrfs_btree_balance_dirty(root
, nr
);
3736 end_writeback(inode
);
3741 * this returns the key found in the dir entry in the location pointer.
3742 * If no dir entries were found, location->objectid is 0.
3744 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
3745 struct btrfs_key
*location
)
3747 const char *name
= dentry
->d_name
.name
;
3748 int namelen
= dentry
->d_name
.len
;
3749 struct btrfs_dir_item
*di
;
3750 struct btrfs_path
*path
;
3751 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3754 path
= btrfs_alloc_path();
3757 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
3762 if (!di
|| IS_ERR(di
))
3765 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
3767 btrfs_free_path(path
);
3770 location
->objectid
= 0;
3775 * when we hit a tree root in a directory, the btrfs part of the inode
3776 * needs to be changed to reflect the root directory of the tree root. This
3777 * is kind of like crossing a mount point.
3779 static int fixup_tree_root_location(struct btrfs_root
*root
,
3781 struct dentry
*dentry
,
3782 struct btrfs_key
*location
,
3783 struct btrfs_root
**sub_root
)
3785 struct btrfs_path
*path
;
3786 struct btrfs_root
*new_root
;
3787 struct btrfs_root_ref
*ref
;
3788 struct extent_buffer
*leaf
;
3792 path
= btrfs_alloc_path();
3799 ret
= btrfs_find_root_ref(root
->fs_info
->tree_root
, path
,
3800 BTRFS_I(dir
)->root
->root_key
.objectid
,
3801 location
->objectid
);
3808 leaf
= path
->nodes
[0];
3809 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
3810 if (btrfs_root_ref_dirid(leaf
, ref
) != dir
->i_ino
||
3811 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
3814 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
3815 (unsigned long)(ref
+ 1),
3816 dentry
->d_name
.len
);
3820 btrfs_release_path(root
->fs_info
->tree_root
, path
);
3822 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
3823 if (IS_ERR(new_root
)) {
3824 err
= PTR_ERR(new_root
);
3828 if (btrfs_root_refs(&new_root
->root_item
) == 0) {
3833 *sub_root
= new_root
;
3834 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
3835 location
->type
= BTRFS_INODE_ITEM_KEY
;
3836 location
->offset
= 0;
3839 btrfs_free_path(path
);
3843 static void inode_tree_add(struct inode
*inode
)
3845 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3846 struct btrfs_inode
*entry
;
3848 struct rb_node
*parent
;
3850 p
= &root
->inode_tree
.rb_node
;
3853 if (hlist_unhashed(&inode
->i_hash
))
3856 spin_lock(&root
->inode_lock
);
3859 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
3861 if (inode
->i_ino
< entry
->vfs_inode
.i_ino
)
3862 p
= &parent
->rb_left
;
3863 else if (inode
->i_ino
> entry
->vfs_inode
.i_ino
)
3864 p
= &parent
->rb_right
;
3866 WARN_ON(!(entry
->vfs_inode
.i_state
&
3867 (I_WILL_FREE
| I_FREEING
)));
3868 rb_erase(parent
, &root
->inode_tree
);
3869 RB_CLEAR_NODE(parent
);
3870 spin_unlock(&root
->inode_lock
);
3874 rb_link_node(&BTRFS_I(inode
)->rb_node
, parent
, p
);
3875 rb_insert_color(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
3876 spin_unlock(&root
->inode_lock
);
3879 static void inode_tree_del(struct inode
*inode
)
3881 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3884 spin_lock(&root
->inode_lock
);
3885 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
3886 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
3887 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
3888 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
3890 spin_unlock(&root
->inode_lock
);
3892 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0) {
3893 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
3894 spin_lock(&root
->inode_lock
);
3895 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
3896 spin_unlock(&root
->inode_lock
);
3898 btrfs_add_dead_root(root
);
3902 int btrfs_invalidate_inodes(struct btrfs_root
*root
)
3904 struct rb_node
*node
;
3905 struct rb_node
*prev
;
3906 struct btrfs_inode
*entry
;
3907 struct inode
*inode
;
3910 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
3912 spin_lock(&root
->inode_lock
);
3914 node
= root
->inode_tree
.rb_node
;
3918 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
3920 if (objectid
< entry
->vfs_inode
.i_ino
)
3921 node
= node
->rb_left
;
3922 else if (objectid
> entry
->vfs_inode
.i_ino
)
3923 node
= node
->rb_right
;
3929 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
3930 if (objectid
<= entry
->vfs_inode
.i_ino
) {
3934 prev
= rb_next(prev
);
3938 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
3939 objectid
= entry
->vfs_inode
.i_ino
+ 1;
3940 inode
= igrab(&entry
->vfs_inode
);
3942 spin_unlock(&root
->inode_lock
);
3943 if (atomic_read(&inode
->i_count
) > 1)
3944 d_prune_aliases(inode
);
3946 * btrfs_drop_inode will have it removed from
3947 * the inode cache when its usage count
3952 spin_lock(&root
->inode_lock
);
3956 if (cond_resched_lock(&root
->inode_lock
))
3959 node
= rb_next(node
);
3961 spin_unlock(&root
->inode_lock
);
3965 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
3967 struct btrfs_iget_args
*args
= p
;
3968 inode
->i_ino
= args
->ino
;
3969 BTRFS_I(inode
)->root
= args
->root
;
3970 btrfs_set_inode_space_info(args
->root
, inode
);
3974 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
3976 struct btrfs_iget_args
*args
= opaque
;
3977 return args
->ino
== inode
->i_ino
&&
3978 args
->root
== BTRFS_I(inode
)->root
;
3981 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
3983 struct btrfs_root
*root
)
3985 struct inode
*inode
;
3986 struct btrfs_iget_args args
;
3987 args
.ino
= objectid
;
3990 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
3991 btrfs_init_locked_inode
,
3996 /* Get an inode object given its location and corresponding root.
3997 * Returns in *is_new if the inode was read from disk
3999 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
4000 struct btrfs_root
*root
, int *new)
4002 struct inode
*inode
;
4004 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
4006 return ERR_PTR(-ENOMEM
);
4008 if (inode
->i_state
& I_NEW
) {
4009 BTRFS_I(inode
)->root
= root
;
4010 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
4011 btrfs_read_locked_inode(inode
);
4013 inode_tree_add(inode
);
4014 unlock_new_inode(inode
);
4022 static struct inode
*new_simple_dir(struct super_block
*s
,
4023 struct btrfs_key
*key
,
4024 struct btrfs_root
*root
)
4026 struct inode
*inode
= new_inode(s
);
4029 return ERR_PTR(-ENOMEM
);
4031 BTRFS_I(inode
)->root
= root
;
4032 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
4033 BTRFS_I(inode
)->dummy_inode
= 1;
4035 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
4036 inode
->i_op
= &simple_dir_inode_operations
;
4037 inode
->i_fop
= &simple_dir_operations
;
4038 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
4039 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4044 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
4046 struct inode
*inode
;
4047 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4048 struct btrfs_root
*sub_root
= root
;
4049 struct btrfs_key location
;
4053 dentry
->d_op
= &btrfs_dentry_operations
;
4055 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
4056 return ERR_PTR(-ENAMETOOLONG
);
4058 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
4061 return ERR_PTR(ret
);
4063 if (location
.objectid
== 0)
4066 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
4067 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
4071 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
4073 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
4074 ret
= fixup_tree_root_location(root
, dir
, dentry
,
4075 &location
, &sub_root
);
4078 inode
= ERR_PTR(ret
);
4080 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
4082 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
4084 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
4086 if (root
!= sub_root
) {
4087 down_read(&root
->fs_info
->cleanup_work_sem
);
4088 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
4089 btrfs_orphan_cleanup(sub_root
);
4090 up_read(&root
->fs_info
->cleanup_work_sem
);
4096 static int btrfs_dentry_delete(struct dentry
*dentry
)
4098 struct btrfs_root
*root
;
4100 if (!dentry
->d_inode
&& !IS_ROOT(dentry
))
4101 dentry
= dentry
->d_parent
;
4103 if (dentry
->d_inode
) {
4104 root
= BTRFS_I(dentry
->d_inode
)->root
;
4105 if (btrfs_root_refs(&root
->root_item
) == 0)
4111 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
4112 struct nameidata
*nd
)
4114 struct inode
*inode
;
4116 inode
= btrfs_lookup_dentry(dir
, dentry
);
4118 return ERR_CAST(inode
);
4120 return d_splice_alias(inode
, dentry
);
4123 static unsigned char btrfs_filetype_table
[] = {
4124 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
4127 static int btrfs_real_readdir(struct file
*filp
, void *dirent
,
4130 struct inode
*inode
= filp
->f_dentry
->d_inode
;
4131 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4132 struct btrfs_item
*item
;
4133 struct btrfs_dir_item
*di
;
4134 struct btrfs_key key
;
4135 struct btrfs_key found_key
;
4136 struct btrfs_path
*path
;
4139 struct extent_buffer
*leaf
;
4142 unsigned char d_type
;
4147 int key_type
= BTRFS_DIR_INDEX_KEY
;
4152 /* FIXME, use a real flag for deciding about the key type */
4153 if (root
->fs_info
->tree_root
== root
)
4154 key_type
= BTRFS_DIR_ITEM_KEY
;
4156 /* special case for "." */
4157 if (filp
->f_pos
== 0) {
4158 over
= filldir(dirent
, ".", 1,
4165 /* special case for .., just use the back ref */
4166 if (filp
->f_pos
== 1) {
4167 u64 pino
= parent_ino(filp
->f_path
.dentry
);
4168 over
= filldir(dirent
, "..", 2,
4174 path
= btrfs_alloc_path();
4177 btrfs_set_key_type(&key
, key_type
);
4178 key
.offset
= filp
->f_pos
;
4179 key
.objectid
= inode
->i_ino
;
4181 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4187 leaf
= path
->nodes
[0];
4188 nritems
= btrfs_header_nritems(leaf
);
4189 slot
= path
->slots
[0];
4190 if (advance
|| slot
>= nritems
) {
4191 if (slot
>= nritems
- 1) {
4192 ret
= btrfs_next_leaf(root
, path
);
4195 leaf
= path
->nodes
[0];
4196 nritems
= btrfs_header_nritems(leaf
);
4197 slot
= path
->slots
[0];
4205 item
= btrfs_item_nr(leaf
, slot
);
4206 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
4208 if (found_key
.objectid
!= key
.objectid
)
4210 if (btrfs_key_type(&found_key
) != key_type
)
4212 if (found_key
.offset
< filp
->f_pos
)
4215 filp
->f_pos
= found_key
.offset
;
4217 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
4219 di_total
= btrfs_item_size(leaf
, item
);
4221 while (di_cur
< di_total
) {
4222 struct btrfs_key location
;
4224 name_len
= btrfs_dir_name_len(leaf
, di
);
4225 if (name_len
<= sizeof(tmp_name
)) {
4226 name_ptr
= tmp_name
;
4228 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
4234 read_extent_buffer(leaf
, name_ptr
,
4235 (unsigned long)(di
+ 1), name_len
);
4237 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
4238 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
4240 /* is this a reference to our own snapshot? If so
4243 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
4244 location
.objectid
== root
->root_key
.objectid
) {
4248 over
= filldir(dirent
, name_ptr
, name_len
,
4249 found_key
.offset
, location
.objectid
,
4253 if (name_ptr
!= tmp_name
)
4258 di_len
= btrfs_dir_name_len(leaf
, di
) +
4259 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
4261 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
4265 /* Reached end of directory/root. Bump pos past the last item. */
4266 if (key_type
== BTRFS_DIR_INDEX_KEY
)
4268 * 32-bit glibc will use getdents64, but then strtol -
4269 * so the last number we can serve is this.
4271 filp
->f_pos
= 0x7fffffff;
4277 btrfs_free_path(path
);
4281 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
4283 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4284 struct btrfs_trans_handle
*trans
;
4287 if (BTRFS_I(inode
)->dummy_inode
)
4290 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
4291 trans
= btrfs_join_transaction(root
, 1);
4292 btrfs_set_trans_block_group(trans
, inode
);
4293 ret
= btrfs_commit_transaction(trans
, root
);
4299 * This is somewhat expensive, updating the tree every time the
4300 * inode changes. But, it is most likely to find the inode in cache.
4301 * FIXME, needs more benchmarking...there are no reasons other than performance
4302 * to keep or drop this code.
4304 void btrfs_dirty_inode(struct inode
*inode
)
4306 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4307 struct btrfs_trans_handle
*trans
;
4310 if (BTRFS_I(inode
)->dummy_inode
)
4313 trans
= btrfs_join_transaction(root
, 1);
4314 btrfs_set_trans_block_group(trans
, inode
);
4316 ret
= btrfs_update_inode(trans
, root
, inode
);
4317 if (ret
&& ret
== -ENOSPC
) {
4318 /* whoops, lets try again with the full transaction */
4319 btrfs_end_transaction(trans
, root
);
4320 trans
= btrfs_start_transaction(root
, 1);
4321 if (IS_ERR(trans
)) {
4322 if (printk_ratelimit()) {
4323 printk(KERN_ERR
"btrfs: fail to "
4324 "dirty inode %lu error %ld\n",
4325 inode
->i_ino
, PTR_ERR(trans
));
4329 btrfs_set_trans_block_group(trans
, inode
);
4331 ret
= btrfs_update_inode(trans
, root
, inode
);
4333 if (printk_ratelimit()) {
4334 printk(KERN_ERR
"btrfs: fail to "
4335 "dirty inode %lu error %d\n",
4340 btrfs_end_transaction(trans
, root
);
4344 * find the highest existing sequence number in a directory
4345 * and then set the in-memory index_cnt variable to reflect
4346 * free sequence numbers
4348 static int btrfs_set_inode_index_count(struct inode
*inode
)
4350 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4351 struct btrfs_key key
, found_key
;
4352 struct btrfs_path
*path
;
4353 struct extent_buffer
*leaf
;
4356 key
.objectid
= inode
->i_ino
;
4357 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
4358 key
.offset
= (u64
)-1;
4360 path
= btrfs_alloc_path();
4364 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4367 /* FIXME: we should be able to handle this */
4373 * MAGIC NUMBER EXPLANATION:
4374 * since we search a directory based on f_pos we have to start at 2
4375 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4376 * else has to start at 2
4378 if (path
->slots
[0] == 0) {
4379 BTRFS_I(inode
)->index_cnt
= 2;
4385 leaf
= path
->nodes
[0];
4386 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4388 if (found_key
.objectid
!= inode
->i_ino
||
4389 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
4390 BTRFS_I(inode
)->index_cnt
= 2;
4394 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
4396 btrfs_free_path(path
);
4401 * helper to find a free sequence number in a given directory. This current
4402 * code is very simple, later versions will do smarter things in the btree
4404 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
4408 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
4409 ret
= btrfs_set_inode_index_count(dir
);
4414 *index
= BTRFS_I(dir
)->index_cnt
;
4415 BTRFS_I(dir
)->index_cnt
++;
4420 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
4421 struct btrfs_root
*root
,
4423 const char *name
, int name_len
,
4424 u64 ref_objectid
, u64 objectid
,
4425 u64 alloc_hint
, int mode
, u64
*index
)
4427 struct inode
*inode
;
4428 struct btrfs_inode_item
*inode_item
;
4429 struct btrfs_key
*location
;
4430 struct btrfs_path
*path
;
4431 struct btrfs_inode_ref
*ref
;
4432 struct btrfs_key key
[2];
4438 path
= btrfs_alloc_path();
4441 inode
= new_inode(root
->fs_info
->sb
);
4443 return ERR_PTR(-ENOMEM
);
4446 ret
= btrfs_set_inode_index(dir
, index
);
4449 return ERR_PTR(ret
);
4453 * index_cnt is ignored for everything but a dir,
4454 * btrfs_get_inode_index_count has an explanation for the magic
4457 BTRFS_I(inode
)->index_cnt
= 2;
4458 BTRFS_I(inode
)->root
= root
;
4459 BTRFS_I(inode
)->generation
= trans
->transid
;
4460 btrfs_set_inode_space_info(root
, inode
);
4466 BTRFS_I(inode
)->block_group
=
4467 btrfs_find_block_group(root
, 0, alloc_hint
, owner
);
4469 key
[0].objectid
= objectid
;
4470 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
4473 key
[1].objectid
= objectid
;
4474 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
4475 key
[1].offset
= ref_objectid
;
4477 sizes
[0] = sizeof(struct btrfs_inode_item
);
4478 sizes
[1] = name_len
+ sizeof(*ref
);
4480 path
->leave_spinning
= 1;
4481 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
4485 inode_init_owner(inode
, dir
, mode
);
4486 inode
->i_ino
= objectid
;
4487 inode_set_bytes(inode
, 0);
4488 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4489 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4490 struct btrfs_inode_item
);
4491 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
4493 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
4494 struct btrfs_inode_ref
);
4495 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
4496 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
4497 ptr
= (unsigned long)(ref
+ 1);
4498 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
4500 btrfs_mark_buffer_dirty(path
->nodes
[0]);
4501 btrfs_free_path(path
);
4503 location
= &BTRFS_I(inode
)->location
;
4504 location
->objectid
= objectid
;
4505 location
->offset
= 0;
4506 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
4508 btrfs_inherit_iflags(inode
, dir
);
4510 if ((mode
& S_IFREG
)) {
4511 if (btrfs_test_opt(root
, NODATASUM
))
4512 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
4513 if (btrfs_test_opt(root
, NODATACOW
))
4514 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
;
4517 insert_inode_hash(inode
);
4518 inode_tree_add(inode
);
4522 BTRFS_I(dir
)->index_cnt
--;
4523 btrfs_free_path(path
);
4525 return ERR_PTR(ret
);
4528 static inline u8
btrfs_inode_type(struct inode
*inode
)
4530 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
4534 * utility function to add 'inode' into 'parent_inode' with
4535 * a give name and a given sequence number.
4536 * if 'add_backref' is true, also insert a backref from the
4537 * inode to the parent directory.
4539 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
4540 struct inode
*parent_inode
, struct inode
*inode
,
4541 const char *name
, int name_len
, int add_backref
, u64 index
)
4544 struct btrfs_key key
;
4545 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
4547 if (unlikely(inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4548 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
4550 key
.objectid
= inode
->i_ino
;
4551 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
4555 if (unlikely(inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4556 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
4557 key
.objectid
, root
->root_key
.objectid
,
4558 parent_inode
->i_ino
,
4559 index
, name
, name_len
);
4560 } else if (add_backref
) {
4561 ret
= btrfs_insert_inode_ref(trans
, root
,
4562 name
, name_len
, inode
->i_ino
,
4563 parent_inode
->i_ino
, index
);
4567 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
4568 parent_inode
->i_ino
, &key
,
4569 btrfs_inode_type(inode
), index
);
4572 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
4574 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
4575 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
4580 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
4581 struct dentry
*dentry
, struct inode
*inode
,
4582 int backref
, u64 index
)
4584 int err
= btrfs_add_link(trans
, dentry
->d_parent
->d_inode
,
4585 inode
, dentry
->d_name
.name
,
4586 dentry
->d_name
.len
, backref
, index
);
4588 d_instantiate(dentry
, inode
);
4596 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
4597 int mode
, dev_t rdev
)
4599 struct btrfs_trans_handle
*trans
;
4600 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4601 struct inode
*inode
= NULL
;
4605 unsigned long nr
= 0;
4608 if (!new_valid_dev(rdev
))
4611 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
4616 * 2 for inode item and ref
4618 * 1 for xattr if selinux is on
4620 trans
= btrfs_start_transaction(root
, 5);
4622 return PTR_ERR(trans
);
4624 btrfs_set_trans_block_group(trans
, dir
);
4626 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4628 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
4629 BTRFS_I(dir
)->block_group
, mode
, &index
);
4630 err
= PTR_ERR(inode
);
4634 err
= btrfs_init_inode_security(trans
, inode
, dir
);
4640 btrfs_set_trans_block_group(trans
, inode
);
4641 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0, index
);
4645 inode
->i_op
= &btrfs_special_inode_operations
;
4646 init_special_inode(inode
, inode
->i_mode
, rdev
);
4647 btrfs_update_inode(trans
, root
, inode
);
4649 btrfs_update_inode_block_group(trans
, inode
);
4650 btrfs_update_inode_block_group(trans
, dir
);
4652 nr
= trans
->blocks_used
;
4653 btrfs_end_transaction_throttle(trans
, root
);
4654 btrfs_btree_balance_dirty(root
, nr
);
4656 inode_dec_link_count(inode
);
4662 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
4663 int mode
, struct nameidata
*nd
)
4665 struct btrfs_trans_handle
*trans
;
4666 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4667 struct inode
*inode
= NULL
;
4670 unsigned long nr
= 0;
4674 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
4678 * 2 for inode item and ref
4680 * 1 for xattr if selinux is on
4682 trans
= btrfs_start_transaction(root
, 5);
4684 return PTR_ERR(trans
);
4686 btrfs_set_trans_block_group(trans
, dir
);
4688 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4690 dentry
->d_parent
->d_inode
->i_ino
,
4691 objectid
, BTRFS_I(dir
)->block_group
, mode
,
4693 err
= PTR_ERR(inode
);
4697 err
= btrfs_init_inode_security(trans
, inode
, dir
);
4703 btrfs_set_trans_block_group(trans
, inode
);
4704 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0, index
);
4708 inode
->i_mapping
->a_ops
= &btrfs_aops
;
4709 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
4710 inode
->i_fop
= &btrfs_file_operations
;
4711 inode
->i_op
= &btrfs_file_inode_operations
;
4712 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
4714 btrfs_update_inode_block_group(trans
, inode
);
4715 btrfs_update_inode_block_group(trans
, dir
);
4717 nr
= trans
->blocks_used
;
4718 btrfs_end_transaction_throttle(trans
, root
);
4720 inode_dec_link_count(inode
);
4723 btrfs_btree_balance_dirty(root
, nr
);
4727 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
4728 struct dentry
*dentry
)
4730 struct btrfs_trans_handle
*trans
;
4731 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4732 struct inode
*inode
= old_dentry
->d_inode
;
4734 unsigned long nr
= 0;
4738 if (inode
->i_nlink
== 0)
4741 /* do not allow sys_link's with other subvols of the same device */
4742 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
4745 btrfs_inc_nlink(inode
);
4747 err
= btrfs_set_inode_index(dir
, &index
);
4752 * 1 item for inode ref
4753 * 2 items for dir items
4755 trans
= btrfs_start_transaction(root
, 3);
4756 if (IS_ERR(trans
)) {
4757 err
= PTR_ERR(trans
);
4761 btrfs_set_trans_block_group(trans
, dir
);
4762 atomic_inc(&inode
->i_count
);
4764 err
= btrfs_add_nondir(trans
, dentry
, inode
, 1, index
);
4769 btrfs_update_inode_block_group(trans
, dir
);
4770 err
= btrfs_update_inode(trans
, root
, inode
);
4772 btrfs_log_new_name(trans
, inode
, NULL
, dentry
->d_parent
);
4775 nr
= trans
->blocks_used
;
4776 btrfs_end_transaction_throttle(trans
, root
);
4779 inode_dec_link_count(inode
);
4782 btrfs_btree_balance_dirty(root
, nr
);
4786 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
4788 struct inode
*inode
= NULL
;
4789 struct btrfs_trans_handle
*trans
;
4790 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4792 int drop_on_err
= 0;
4795 unsigned long nr
= 1;
4797 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
4802 * 2 items for inode and ref
4803 * 2 items for dir items
4804 * 1 for xattr if selinux is on
4806 trans
= btrfs_start_transaction(root
, 5);
4808 return PTR_ERR(trans
);
4809 btrfs_set_trans_block_group(trans
, dir
);
4811 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4813 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
4814 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
,
4816 if (IS_ERR(inode
)) {
4817 err
= PTR_ERR(inode
);
4823 err
= btrfs_init_inode_security(trans
, inode
, dir
);
4827 inode
->i_op
= &btrfs_dir_inode_operations
;
4828 inode
->i_fop
= &btrfs_dir_file_operations
;
4829 btrfs_set_trans_block_group(trans
, inode
);
4831 btrfs_i_size_write(inode
, 0);
4832 err
= btrfs_update_inode(trans
, root
, inode
);
4836 err
= btrfs_add_link(trans
, dentry
->d_parent
->d_inode
,
4837 inode
, dentry
->d_name
.name
,
4838 dentry
->d_name
.len
, 0, index
);
4842 d_instantiate(dentry
, inode
);
4844 btrfs_update_inode_block_group(trans
, inode
);
4845 btrfs_update_inode_block_group(trans
, dir
);
4848 nr
= trans
->blocks_used
;
4849 btrfs_end_transaction_throttle(trans
, root
);
4852 btrfs_btree_balance_dirty(root
, nr
);
4856 /* helper for btfs_get_extent. Given an existing extent in the tree,
4857 * and an extent that you want to insert, deal with overlap and insert
4858 * the new extent into the tree.
4860 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
4861 struct extent_map
*existing
,
4862 struct extent_map
*em
,
4863 u64 map_start
, u64 map_len
)
4867 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
4868 start_diff
= map_start
- em
->start
;
4869 em
->start
= map_start
;
4871 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
4872 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
4873 em
->block_start
+= start_diff
;
4874 em
->block_len
-= start_diff
;
4876 return add_extent_mapping(em_tree
, em
);
4879 static noinline
int uncompress_inline(struct btrfs_path
*path
,
4880 struct inode
*inode
, struct page
*page
,
4881 size_t pg_offset
, u64 extent_offset
,
4882 struct btrfs_file_extent_item
*item
)
4885 struct extent_buffer
*leaf
= path
->nodes
[0];
4888 unsigned long inline_size
;
4891 WARN_ON(pg_offset
!= 0);
4892 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
4893 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
4894 btrfs_item_nr(leaf
, path
->slots
[0]));
4895 tmp
= kmalloc(inline_size
, GFP_NOFS
);
4896 ptr
= btrfs_file_extent_inline_start(item
);
4898 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
4900 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
4901 ret
= btrfs_zlib_decompress(tmp
, page
, extent_offset
,
4902 inline_size
, max_size
);
4904 char *kaddr
= kmap_atomic(page
, KM_USER0
);
4905 unsigned long copy_size
= min_t(u64
,
4906 PAGE_CACHE_SIZE
- pg_offset
,
4907 max_size
- extent_offset
);
4908 memset(kaddr
+ pg_offset
, 0, copy_size
);
4909 kunmap_atomic(kaddr
, KM_USER0
);
4916 * a bit scary, this does extent mapping from logical file offset to the disk.
4917 * the ugly parts come from merging extents from the disk with the in-ram
4918 * representation. This gets more complex because of the data=ordered code,
4919 * where the in-ram extents might be locked pending data=ordered completion.
4921 * This also copies inline extents directly into the page.
4924 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
4925 size_t pg_offset
, u64 start
, u64 len
,
4931 u64 extent_start
= 0;
4933 u64 objectid
= inode
->i_ino
;
4935 struct btrfs_path
*path
= NULL
;
4936 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4937 struct btrfs_file_extent_item
*item
;
4938 struct extent_buffer
*leaf
;
4939 struct btrfs_key found_key
;
4940 struct extent_map
*em
= NULL
;
4941 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4942 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4943 struct btrfs_trans_handle
*trans
= NULL
;
4947 read_lock(&em_tree
->lock
);
4948 em
= lookup_extent_mapping(em_tree
, start
, len
);
4950 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4951 read_unlock(&em_tree
->lock
);
4954 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
4955 free_extent_map(em
);
4956 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
4957 free_extent_map(em
);
4961 em
= alloc_extent_map(GFP_NOFS
);
4966 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4967 em
->start
= EXTENT_MAP_HOLE
;
4968 em
->orig_start
= EXTENT_MAP_HOLE
;
4970 em
->block_len
= (u64
)-1;
4973 path
= btrfs_alloc_path();
4977 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
4978 objectid
, start
, trans
!= NULL
);
4985 if (path
->slots
[0] == 0)
4990 leaf
= path
->nodes
[0];
4991 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
4992 struct btrfs_file_extent_item
);
4993 /* are we inside the extent that was found? */
4994 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4995 found_type
= btrfs_key_type(&found_key
);
4996 if (found_key
.objectid
!= objectid
||
4997 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
5001 found_type
= btrfs_file_extent_type(leaf
, item
);
5002 extent_start
= found_key
.offset
;
5003 compressed
= btrfs_file_extent_compression(leaf
, item
);
5004 if (found_type
== BTRFS_FILE_EXTENT_REG
||
5005 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5006 extent_end
= extent_start
+
5007 btrfs_file_extent_num_bytes(leaf
, item
);
5008 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
5010 size
= btrfs_file_extent_inline_len(leaf
, item
);
5011 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
5012 ~((u64
)root
->sectorsize
- 1);
5015 if (start
>= extent_end
) {
5017 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
5018 ret
= btrfs_next_leaf(root
, path
);
5025 leaf
= path
->nodes
[0];
5027 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5028 if (found_key
.objectid
!= objectid
||
5029 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
5031 if (start
+ len
<= found_key
.offset
)
5034 em
->len
= found_key
.offset
- start
;
5038 if (found_type
== BTRFS_FILE_EXTENT_REG
||
5039 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5040 em
->start
= extent_start
;
5041 em
->len
= extent_end
- extent_start
;
5042 em
->orig_start
= extent_start
-
5043 btrfs_file_extent_offset(leaf
, item
);
5044 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
5046 em
->block_start
= EXTENT_MAP_HOLE
;
5050 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
5051 em
->block_start
= bytenr
;
5052 em
->block_len
= btrfs_file_extent_disk_num_bytes(leaf
,
5055 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
5056 em
->block_start
= bytenr
;
5057 em
->block_len
= em
->len
;
5058 if (found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
5059 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
5062 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
5066 size_t extent_offset
;
5069 em
->block_start
= EXTENT_MAP_INLINE
;
5070 if (!page
|| create
) {
5071 em
->start
= extent_start
;
5072 em
->len
= extent_end
- extent_start
;
5076 size
= btrfs_file_extent_inline_len(leaf
, item
);
5077 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
5078 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
5079 size
- extent_offset
);
5080 em
->start
= extent_start
+ extent_offset
;
5081 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
5082 ~((u64
)root
->sectorsize
- 1);
5083 em
->orig_start
= EXTENT_MAP_INLINE
;
5085 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
5086 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
5087 if (create
== 0 && !PageUptodate(page
)) {
5088 if (btrfs_file_extent_compression(leaf
, item
) ==
5089 BTRFS_COMPRESS_ZLIB
) {
5090 ret
= uncompress_inline(path
, inode
, page
,
5092 extent_offset
, item
);
5096 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
5098 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
5099 memset(map
+ pg_offset
+ copy_size
, 0,
5100 PAGE_CACHE_SIZE
- pg_offset
-
5105 flush_dcache_page(page
);
5106 } else if (create
&& PageUptodate(page
)) {
5110 free_extent_map(em
);
5112 btrfs_release_path(root
, path
);
5113 trans
= btrfs_join_transaction(root
, 1);
5117 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
5120 btrfs_mark_buffer_dirty(leaf
);
5122 set_extent_uptodate(io_tree
, em
->start
,
5123 extent_map_end(em
) - 1, GFP_NOFS
);
5126 printk(KERN_ERR
"btrfs unknown found_type %d\n", found_type
);
5133 em
->block_start
= EXTENT_MAP_HOLE
;
5134 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
5136 btrfs_release_path(root
, path
);
5137 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
5138 printk(KERN_ERR
"Btrfs: bad extent! em: [%llu %llu] passed "
5139 "[%llu %llu]\n", (unsigned long long)em
->start
,
5140 (unsigned long long)em
->len
,
5141 (unsigned long long)start
,
5142 (unsigned long long)len
);
5148 write_lock(&em_tree
->lock
);
5149 ret
= add_extent_mapping(em_tree
, em
);
5150 /* it is possible that someone inserted the extent into the tree
5151 * while we had the lock dropped. It is also possible that
5152 * an overlapping map exists in the tree
5154 if (ret
== -EEXIST
) {
5155 struct extent_map
*existing
;
5159 existing
= lookup_extent_mapping(em_tree
, start
, len
);
5160 if (existing
&& (existing
->start
> start
||
5161 existing
->start
+ existing
->len
<= start
)) {
5162 free_extent_map(existing
);
5166 existing
= lookup_extent_mapping(em_tree
, em
->start
,
5169 err
= merge_extent_mapping(em_tree
, existing
,
5172 free_extent_map(existing
);
5174 free_extent_map(em
);
5179 free_extent_map(em
);
5183 free_extent_map(em
);
5188 write_unlock(&em_tree
->lock
);
5191 btrfs_free_path(path
);
5193 ret
= btrfs_end_transaction(trans
, root
);
5198 free_extent_map(em
);
5199 return ERR_PTR(err
);
5204 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
5207 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5208 struct btrfs_trans_handle
*trans
;
5209 struct extent_map
*em
;
5210 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
5211 struct btrfs_key ins
;
5215 btrfs_drop_extent_cache(inode
, start
, start
+ len
- 1, 0);
5217 trans
= btrfs_join_transaction(root
, 0);
5219 return ERR_PTR(-ENOMEM
);
5221 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
5223 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
5224 ret
= btrfs_reserve_extent(trans
, root
, len
, root
->sectorsize
, 0,
5225 alloc_hint
, (u64
)-1, &ins
, 1);
5231 em
= alloc_extent_map(GFP_NOFS
);
5233 em
= ERR_PTR(-ENOMEM
);
5238 em
->orig_start
= em
->start
;
5239 em
->len
= ins
.offset
;
5241 em
->block_start
= ins
.objectid
;
5242 em
->block_len
= ins
.offset
;
5243 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5244 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
5247 write_lock(&em_tree
->lock
);
5248 ret
= add_extent_mapping(em_tree
, em
);
5249 write_unlock(&em_tree
->lock
);
5252 btrfs_drop_extent_cache(inode
, start
, start
+ em
->len
- 1, 0);
5255 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
5256 ins
.offset
, ins
.offset
, 0);
5258 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
5262 btrfs_end_transaction(trans
, root
);
5267 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5268 * block must be cow'd
5270 static noinline
int can_nocow_odirect(struct btrfs_trans_handle
*trans
,
5271 struct inode
*inode
, u64 offset
, u64 len
)
5273 struct btrfs_path
*path
;
5275 struct extent_buffer
*leaf
;
5276 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5277 struct btrfs_file_extent_item
*fi
;
5278 struct btrfs_key key
;
5286 path
= btrfs_alloc_path();
5290 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
5295 slot
= path
->slots
[0];
5298 /* can't find the item, must cow */
5305 leaf
= path
->nodes
[0];
5306 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
5307 if (key
.objectid
!= inode
->i_ino
||
5308 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
5309 /* not our file or wrong item type, must cow */
5313 if (key
.offset
> offset
) {
5314 /* Wrong offset, must cow */
5318 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
5319 found_type
= btrfs_file_extent_type(leaf
, fi
);
5320 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
5321 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
5322 /* not a regular extent, must cow */
5325 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
5326 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
5328 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
5329 if (extent_end
< offset
+ len
) {
5330 /* extent doesn't include our full range, must cow */
5334 if (btrfs_extent_readonly(root
, disk_bytenr
))
5338 * look for other files referencing this extent, if we
5339 * find any we must cow
5341 if (btrfs_cross_ref_exist(trans
, root
, inode
->i_ino
,
5342 key
.offset
- backref_offset
, disk_bytenr
))
5346 * adjust disk_bytenr and num_bytes to cover just the bytes
5347 * in this extent we are about to write. If there
5348 * are any csums in that range we have to cow in order
5349 * to keep the csums correct
5351 disk_bytenr
+= backref_offset
;
5352 disk_bytenr
+= offset
- key
.offset
;
5353 num_bytes
= min(offset
+ len
, extent_end
) - offset
;
5354 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
5357 * all of the above have passed, it is safe to overwrite this extent
5362 btrfs_free_path(path
);
5366 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
5367 struct buffer_head
*bh_result
, int create
)
5369 struct extent_map
*em
;
5370 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5371 u64 start
= iblock
<< inode
->i_blkbits
;
5372 u64 len
= bh_result
->b_size
;
5373 struct btrfs_trans_handle
*trans
;
5375 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
5380 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5381 * io. INLINE is special, and we could probably kludge it in here, but
5382 * it's still buffered so for safety lets just fall back to the generic
5385 * For COMPRESSED we _have_ to read the entire extent in so we can
5386 * decompress it, so there will be buffering required no matter what we
5387 * do, so go ahead and fallback to buffered.
5389 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5390 * to buffered IO. Don't blame me, this is the price we pay for using
5393 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
5394 em
->block_start
== EXTENT_MAP_INLINE
) {
5395 free_extent_map(em
);
5399 /* Just a good old fashioned hole, return */
5400 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
5401 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
5402 free_extent_map(em
);
5403 /* DIO will do one hole at a time, so just unlock a sector */
5404 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
,
5405 start
+ root
->sectorsize
- 1, GFP_NOFS
);
5410 * We don't allocate a new extent in the following cases
5412 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5414 * 2) The extent is marked as PREALLOC. We're good to go here and can
5415 * just use the extent.
5419 len
= em
->len
- (start
- em
->start
);
5423 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
5424 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
5425 em
->block_start
!= EXTENT_MAP_HOLE
)) {
5430 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
5431 type
= BTRFS_ORDERED_PREALLOC
;
5433 type
= BTRFS_ORDERED_NOCOW
;
5434 len
= min(len
, em
->len
- (start
- em
->start
));
5435 block_start
= em
->block_start
+ (start
- em
->start
);
5438 * we're not going to log anything, but we do need
5439 * to make sure the current transaction stays open
5440 * while we look for nocow cross refs
5442 trans
= btrfs_join_transaction(root
, 0);
5446 if (can_nocow_odirect(trans
, inode
, start
, len
) == 1) {
5447 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
5448 block_start
, len
, len
, type
);
5449 btrfs_end_transaction(trans
, root
);
5451 free_extent_map(em
);
5456 btrfs_end_transaction(trans
, root
);
5460 * this will cow the extent, reset the len in case we changed
5463 len
= bh_result
->b_size
;
5464 free_extent_map(em
);
5465 em
= btrfs_new_extent_direct(inode
, start
, len
);
5468 len
= min(len
, em
->len
- (start
- em
->start
));
5470 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
- 1,
5471 EXTENT_LOCKED
| EXTENT_DELALLOC
| EXTENT_DIRTY
, 1,
5474 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
5476 bh_result
->b_size
= len
;
5477 bh_result
->b_bdev
= em
->bdev
;
5478 set_buffer_mapped(bh_result
);
5479 if (create
&& !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
5480 set_buffer_new(bh_result
);
5482 free_extent_map(em
);
5487 struct btrfs_dio_private
{
5488 struct inode
*inode
;
5496 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
5498 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
5499 struct bio_vec
*bvec
= bio
->bi_io_vec
;
5500 struct btrfs_dio_private
*dip
= bio
->bi_private
;
5501 struct inode
*inode
= dip
->inode
;
5502 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5504 u32
*private = dip
->csums
;
5506 start
= dip
->logical_offset
;
5508 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
5509 struct page
*page
= bvec
->bv_page
;
5512 unsigned long flags
;
5514 local_irq_save(flags
);
5515 kaddr
= kmap_atomic(page
, KM_IRQ0
);
5516 csum
= btrfs_csum_data(root
, kaddr
+ bvec
->bv_offset
,
5517 csum
, bvec
->bv_len
);
5518 btrfs_csum_final(csum
, (char *)&csum
);
5519 kunmap_atomic(kaddr
, KM_IRQ0
);
5520 local_irq_restore(flags
);
5522 flush_dcache_page(bvec
->bv_page
);
5523 if (csum
!= *private) {
5524 printk(KERN_ERR
"btrfs csum failed ino %lu off"
5525 " %llu csum %u private %u\n",
5526 inode
->i_ino
, (unsigned long long)start
,
5532 start
+= bvec
->bv_len
;
5535 } while (bvec
<= bvec_end
);
5537 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
5538 dip
->logical_offset
+ dip
->bytes
- 1, GFP_NOFS
);
5539 bio
->bi_private
= dip
->private;
5543 dio_end_io(bio
, err
);
5546 static void btrfs_endio_direct_write(struct bio
*bio
, int err
)
5548 struct btrfs_dio_private
*dip
= bio
->bi_private
;
5549 struct inode
*inode
= dip
->inode
;
5550 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5551 struct btrfs_trans_handle
*trans
;
5552 struct btrfs_ordered_extent
*ordered
= NULL
;
5553 struct extent_state
*cached_state
= NULL
;
5559 ret
= btrfs_dec_test_ordered_pending(inode
, &ordered
,
5560 dip
->logical_offset
, dip
->bytes
);
5566 trans
= btrfs_join_transaction(root
, 1);
5571 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
5573 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
)) {
5574 ret
= btrfs_ordered_update_i_size(inode
, 0, ordered
);
5576 ret
= btrfs_update_inode(trans
, root
, inode
);
5581 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, ordered
->file_offset
,
5582 ordered
->file_offset
+ ordered
->len
- 1, 0,
5583 &cached_state
, GFP_NOFS
);
5585 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
)) {
5586 ret
= btrfs_mark_extent_written(trans
, inode
,
5587 ordered
->file_offset
,
5588 ordered
->file_offset
+
5595 ret
= insert_reserved_file_extent(trans
, inode
,
5596 ordered
->file_offset
,
5602 BTRFS_FILE_EXTENT_REG
);
5603 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
5604 ordered
->file_offset
, ordered
->len
);
5612 add_pending_csums(trans
, inode
, ordered
->file_offset
, &ordered
->list
);
5613 btrfs_ordered_update_i_size(inode
, 0, ordered
);
5614 btrfs_update_inode(trans
, root
, inode
);
5616 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, ordered
->file_offset
,
5617 ordered
->file_offset
+ ordered
->len
- 1,
5618 &cached_state
, GFP_NOFS
);
5620 btrfs_delalloc_release_metadata(inode
, ordered
->len
);
5621 btrfs_end_transaction(trans
, root
);
5622 btrfs_put_ordered_extent(ordered
);
5623 btrfs_put_ordered_extent(ordered
);
5625 bio
->bi_private
= dip
->private;
5629 dio_end_io(bio
, err
);
5632 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
5633 struct bio
*bio
, int mirror_num
,
5634 unsigned long bio_flags
, u64 offset
)
5637 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5638 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
5643 static void btrfs_submit_direct(int rw
, struct bio
*bio
, struct inode
*inode
,
5646 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5647 struct btrfs_dio_private
*dip
;
5648 struct bio_vec
*bvec
= bio
->bi_io_vec
;
5651 int write
= rw
& (1 << BIO_RW
);
5654 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
5656 dip
= kmalloc(sizeof(*dip
), GFP_NOFS
);
5664 dip
->csums
= kmalloc(sizeof(u32
) * bio
->bi_vcnt
, GFP_NOFS
);
5671 dip
->private = bio
->bi_private
;
5673 dip
->logical_offset
= file_offset
;
5675 start
= dip
->logical_offset
;
5678 dip
->bytes
+= bvec
->bv_len
;
5680 } while (bvec
<= (bio
->bi_io_vec
+ bio
->bi_vcnt
- 1));
5682 dip
->disk_bytenr
= (u64
)bio
->bi_sector
<< 9;
5683 bio
->bi_private
= dip
;
5686 bio
->bi_end_io
= btrfs_endio_direct_write
;
5688 bio
->bi_end_io
= btrfs_endio_direct_read
;
5690 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
5694 if (write
&& !skip_sum
) {
5695 ret
= btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
5696 inode
, rw
, bio
, 0, 0,
5697 dip
->logical_offset
,
5698 __btrfs_submit_bio_start_direct_io
,
5699 __btrfs_submit_bio_done
);
5703 } else if (!skip_sum
)
5704 btrfs_lookup_bio_sums_dio(root
, inode
, bio
,
5705 dip
->logical_offset
, dip
->csums
);
5707 ret
= btrfs_map_bio(root
, rw
, bio
, 0, 1);
5716 * If this is a write, we need to clean up the reserved space and kill
5717 * the ordered extent.
5720 struct btrfs_ordered_extent
*ordered
;
5721 ordered
= btrfs_lookup_ordered_extent(inode
,
5722 dip
->logical_offset
);
5723 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
5724 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
5725 btrfs_free_reserved_extent(root
, ordered
->start
,
5727 btrfs_put_ordered_extent(ordered
);
5728 btrfs_put_ordered_extent(ordered
);
5730 bio_endio(bio
, ret
);
5733 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
5734 const struct iovec
*iov
, loff_t offset
,
5735 unsigned long nr_segs
)
5740 unsigned blocksize_mask
= root
->sectorsize
- 1;
5741 ssize_t retval
= -EINVAL
;
5742 loff_t end
= offset
;
5744 if (offset
& blocksize_mask
)
5747 /* Check the memory alignment. Blocks cannot straddle pages */
5748 for (seg
= 0; seg
< nr_segs
; seg
++) {
5749 addr
= (unsigned long)iov
[seg
].iov_base
;
5750 size
= iov
[seg
].iov_len
;
5752 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
5759 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
5760 const struct iovec
*iov
, loff_t offset
,
5761 unsigned long nr_segs
)
5763 struct file
*file
= iocb
->ki_filp
;
5764 struct inode
*inode
= file
->f_mapping
->host
;
5765 struct btrfs_ordered_extent
*ordered
;
5766 struct extent_state
*cached_state
= NULL
;
5767 u64 lockstart
, lockend
;
5769 int writing
= rw
& WRITE
;
5771 size_t count
= iov_length(iov
, nr_segs
);
5773 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iov
,
5779 lockend
= offset
+ count
- 1;
5782 ret
= btrfs_delalloc_reserve_space(inode
, count
);
5788 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
5789 0, &cached_state
, GFP_NOFS
);
5791 * We're concerned with the entire range that we're going to be
5792 * doing DIO to, so we need to make sure theres no ordered
5793 * extents in this range.
5795 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
5796 lockend
- lockstart
+ 1);
5799 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
5800 &cached_state
, GFP_NOFS
);
5801 btrfs_start_ordered_extent(inode
, ordered
, 1);
5802 btrfs_put_ordered_extent(ordered
);
5807 * we don't use btrfs_set_extent_delalloc because we don't want
5808 * the dirty or uptodate bits
5811 write_bits
= EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
;
5812 ret
= set_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
5813 EXTENT_DELALLOC
, 0, NULL
, &cached_state
,
5816 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
5817 lockend
, EXTENT_LOCKED
| write_bits
,
5818 1, 0, &cached_state
, GFP_NOFS
);
5823 free_extent_state(cached_state
);
5824 cached_state
= NULL
;
5826 ret
= __blockdev_direct_IO(rw
, iocb
, inode
,
5827 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
5828 iov
, offset
, nr_segs
, btrfs_get_blocks_direct
, NULL
,
5829 btrfs_submit_direct
, 0);
5831 if (ret
< 0 && ret
!= -EIOCBQUEUED
) {
5832 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, offset
,
5833 offset
+ iov_length(iov
, nr_segs
) - 1,
5834 EXTENT_LOCKED
| write_bits
, 1, 0,
5835 &cached_state
, GFP_NOFS
);
5836 } else if (ret
>= 0 && ret
< iov_length(iov
, nr_segs
)) {
5838 * We're falling back to buffered, unlock the section we didn't
5841 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, offset
+ ret
,
5842 offset
+ iov_length(iov
, nr_segs
) - 1,
5843 EXTENT_LOCKED
| write_bits
, 1, 0,
5844 &cached_state
, GFP_NOFS
);
5847 free_extent_state(cached_state
);
5851 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
5852 __u64 start
, __u64 len
)
5854 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent
);
5857 int btrfs_readpage(struct file
*file
, struct page
*page
)
5859 struct extent_io_tree
*tree
;
5860 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
5861 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
5864 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
5866 struct extent_io_tree
*tree
;
5869 if (current
->flags
& PF_MEMALLOC
) {
5870 redirty_page_for_writepage(wbc
, page
);
5874 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
5875 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
5878 int btrfs_writepages(struct address_space
*mapping
,
5879 struct writeback_control
*wbc
)
5881 struct extent_io_tree
*tree
;
5883 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
5884 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
5888 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
5889 struct list_head
*pages
, unsigned nr_pages
)
5891 struct extent_io_tree
*tree
;
5892 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
5893 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
5896 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
5898 struct extent_io_tree
*tree
;
5899 struct extent_map_tree
*map
;
5902 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
5903 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
5904 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
5906 ClearPagePrivate(page
);
5907 set_page_private(page
, 0);
5908 page_cache_release(page
);
5913 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
5915 if (PageWriteback(page
) || PageDirty(page
))
5917 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
5920 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
5922 struct extent_io_tree
*tree
;
5923 struct btrfs_ordered_extent
*ordered
;
5924 struct extent_state
*cached_state
= NULL
;
5925 u64 page_start
= page_offset(page
);
5926 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
5930 * we have the page locked, so new writeback can't start,
5931 * and the dirty bit won't be cleared while we are here.
5933 * Wait for IO on this page so that we can safely clear
5934 * the PagePrivate2 bit and do ordered accounting
5936 wait_on_page_writeback(page
);
5938 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
5940 btrfs_releasepage(page
, GFP_NOFS
);
5943 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
,
5945 ordered
= btrfs_lookup_ordered_extent(page
->mapping
->host
,
5949 * IO on this page will never be started, so we need
5950 * to account for any ordered extents now
5952 clear_extent_bit(tree
, page_start
, page_end
,
5953 EXTENT_DIRTY
| EXTENT_DELALLOC
|
5954 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
, 1, 0,
5955 &cached_state
, GFP_NOFS
);
5957 * whoever cleared the private bit is responsible
5958 * for the finish_ordered_io
5960 if (TestClearPagePrivate2(page
)) {
5961 btrfs_finish_ordered_io(page
->mapping
->host
,
5962 page_start
, page_end
);
5964 btrfs_put_ordered_extent(ordered
);
5965 cached_state
= NULL
;
5966 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
,
5969 clear_extent_bit(tree
, page_start
, page_end
,
5970 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
5971 EXTENT_DO_ACCOUNTING
, 1, 1, &cached_state
, GFP_NOFS
);
5972 __btrfs_releasepage(page
, GFP_NOFS
);
5974 ClearPageChecked(page
);
5975 if (PagePrivate(page
)) {
5976 ClearPagePrivate(page
);
5977 set_page_private(page
, 0);
5978 page_cache_release(page
);
5983 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
5984 * called from a page fault handler when a page is first dirtied. Hence we must
5985 * be careful to check for EOF conditions here. We set the page up correctly
5986 * for a written page which means we get ENOSPC checking when writing into
5987 * holes and correct delalloc and unwritten extent mapping on filesystems that
5988 * support these features.
5990 * We are not allowed to take the i_mutex here so we have to play games to
5991 * protect against truncate races as the page could now be beyond EOF. Because
5992 * vmtruncate() writes the inode size before removing pages, once we have the
5993 * page lock we can determine safely if the page is beyond EOF. If it is not
5994 * beyond EOF, then the page is guaranteed safe against truncation until we
5997 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
5999 struct page
*page
= vmf
->page
;
6000 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
6001 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6002 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6003 struct btrfs_ordered_extent
*ordered
;
6004 struct extent_state
*cached_state
= NULL
;
6006 unsigned long zero_start
;
6012 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
6016 else /* -ENOSPC, -EIO, etc */
6017 ret
= VM_FAULT_SIGBUS
;
6021 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
6024 size
= i_size_read(inode
);
6025 page_start
= page_offset(page
);
6026 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6028 if ((page
->mapping
!= inode
->i_mapping
) ||
6029 (page_start
>= size
)) {
6030 /* page got truncated out from underneath us */
6033 wait_on_page_writeback(page
);
6035 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
,
6037 set_page_extent_mapped(page
);
6040 * we can't set the delalloc bits if there are pending ordered
6041 * extents. Drop our locks and wait for them to finish
6043 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
6045 unlock_extent_cached(io_tree
, page_start
, page_end
,
6046 &cached_state
, GFP_NOFS
);
6048 btrfs_start_ordered_extent(inode
, ordered
, 1);
6049 btrfs_put_ordered_extent(ordered
);
6054 * XXX - page_mkwrite gets called every time the page is dirtied, even
6055 * if it was already dirty, so for space accounting reasons we need to
6056 * clear any delalloc bits for the range we are fixing to save. There
6057 * is probably a better way to do this, but for now keep consistent with
6058 * prepare_pages in the normal write path.
6060 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
6061 EXTENT_DIRTY
| EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
,
6062 0, 0, &cached_state
, GFP_NOFS
);
6064 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
6067 unlock_extent_cached(io_tree
, page_start
, page_end
,
6068 &cached_state
, GFP_NOFS
);
6069 ret
= VM_FAULT_SIGBUS
;
6074 /* page is wholly or partially inside EOF */
6075 if (page_start
+ PAGE_CACHE_SIZE
> size
)
6076 zero_start
= size
& ~PAGE_CACHE_MASK
;
6078 zero_start
= PAGE_CACHE_SIZE
;
6080 if (zero_start
!= PAGE_CACHE_SIZE
) {
6082 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
6083 flush_dcache_page(page
);
6086 ClearPageChecked(page
);
6087 set_page_dirty(page
);
6088 SetPageUptodate(page
);
6090 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
6091 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
6093 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
6097 return VM_FAULT_LOCKED
;
6099 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
6104 static void btrfs_truncate(struct inode
*inode
)
6106 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6108 struct btrfs_trans_handle
*trans
;
6110 u64 mask
= root
->sectorsize
- 1;
6112 if (!S_ISREG(inode
->i_mode
)) {
6117 ret
= btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
6121 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
6122 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
6124 trans
= btrfs_start_transaction(root
, 0);
6125 BUG_ON(IS_ERR(trans
));
6126 btrfs_set_trans_block_group(trans
, inode
);
6127 trans
->block_rsv
= root
->orphan_block_rsv
;
6130 * setattr is responsible for setting the ordered_data_close flag,
6131 * but that is only tested during the last file release. That
6132 * could happen well after the next commit, leaving a great big
6133 * window where new writes may get lost if someone chooses to write
6134 * to this file after truncating to zero
6136 * The inode doesn't have any dirty data here, and so if we commit
6137 * this is a noop. If someone immediately starts writing to the inode
6138 * it is very likely we'll catch some of their writes in this
6139 * transaction, and the commit will find this file on the ordered
6140 * data list with good things to send down.
6142 * This is a best effort solution, there is still a window where
6143 * using truncate to replace the contents of the file will
6144 * end up with a zero length file after a crash.
6146 if (inode
->i_size
== 0 && BTRFS_I(inode
)->ordered_data_close
)
6147 btrfs_add_ordered_operation(trans
, root
, inode
);
6151 trans
= btrfs_start_transaction(root
, 0);
6152 BUG_ON(IS_ERR(trans
));
6153 btrfs_set_trans_block_group(trans
, inode
);
6154 trans
->block_rsv
= root
->orphan_block_rsv
;
6157 ret
= btrfs_block_rsv_check(trans
, root
,
6158 root
->orphan_block_rsv
, 0, 5);
6160 BUG_ON(ret
!= -EAGAIN
);
6161 ret
= btrfs_commit_transaction(trans
, root
);
6167 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
6169 BTRFS_EXTENT_DATA_KEY
);
6173 ret
= btrfs_update_inode(trans
, root
, inode
);
6176 nr
= trans
->blocks_used
;
6177 btrfs_end_transaction(trans
, root
);
6179 btrfs_btree_balance_dirty(root
, nr
);
6182 if (ret
== 0 && inode
->i_nlink
> 0) {
6183 ret
= btrfs_orphan_del(trans
, inode
);
6187 ret
= btrfs_update_inode(trans
, root
, inode
);
6190 nr
= trans
->blocks_used
;
6191 ret
= btrfs_end_transaction_throttle(trans
, root
);
6193 btrfs_btree_balance_dirty(root
, nr
);
6197 * create a new subvolume directory/inode (helper for the ioctl).
6199 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
6200 struct btrfs_root
*new_root
,
6201 u64 new_dirid
, u64 alloc_hint
)
6203 struct inode
*inode
;
6207 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2, new_dirid
,
6208 new_dirid
, alloc_hint
, S_IFDIR
| 0700, &index
);
6210 return PTR_ERR(inode
);
6211 inode
->i_op
= &btrfs_dir_inode_operations
;
6212 inode
->i_fop
= &btrfs_dir_file_operations
;
6215 btrfs_i_size_write(inode
, 0);
6217 err
= btrfs_update_inode(trans
, new_root
, inode
);
6224 /* helper function for file defrag and space balancing. This
6225 * forces readahead on a given range of bytes in an inode
6227 unsigned long btrfs_force_ra(struct address_space
*mapping
,
6228 struct file_ra_state
*ra
, struct file
*file
,
6229 pgoff_t offset
, pgoff_t last_index
)
6231 pgoff_t req_size
= last_index
- offset
+ 1;
6233 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
6234 return offset
+ req_size
;
6237 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
6239 struct btrfs_inode
*ei
;
6240 struct inode
*inode
;
6242 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
6247 ei
->space_info
= NULL
;
6251 ei
->last_sub_trans
= 0;
6252 ei
->logged_trans
= 0;
6253 ei
->delalloc_bytes
= 0;
6254 ei
->reserved_bytes
= 0;
6255 ei
->disk_i_size
= 0;
6257 ei
->index_cnt
= (u64
)-1;
6258 ei
->last_unlink_trans
= 0;
6260 spin_lock_init(&ei
->accounting_lock
);
6261 atomic_set(&ei
->outstanding_extents
, 0);
6262 ei
->reserved_extents
= 0;
6264 ei
->ordered_data_close
= 0;
6265 ei
->orphan_meta_reserved
= 0;
6266 ei
->dummy_inode
= 0;
6267 ei
->force_compress
= 0;
6269 inode
= &ei
->vfs_inode
;
6270 extent_map_tree_init(&ei
->extent_tree
, GFP_NOFS
);
6271 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
, GFP_NOFS
);
6272 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
, GFP_NOFS
);
6273 mutex_init(&ei
->log_mutex
);
6274 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
6275 INIT_LIST_HEAD(&ei
->i_orphan
);
6276 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
6277 INIT_LIST_HEAD(&ei
->ordered_operations
);
6278 RB_CLEAR_NODE(&ei
->rb_node
);
6283 void btrfs_destroy_inode(struct inode
*inode
)
6285 struct btrfs_ordered_extent
*ordered
;
6286 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6288 WARN_ON(!list_empty(&inode
->i_dentry
));
6289 WARN_ON(inode
->i_data
.nrpages
);
6290 WARN_ON(atomic_read(&BTRFS_I(inode
)->outstanding_extents
));
6291 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
6294 * This can happen where we create an inode, but somebody else also
6295 * created the same inode and we need to destroy the one we already
6302 * Make sure we're properly removed from the ordered operation
6306 if (!list_empty(&BTRFS_I(inode
)->ordered_operations
)) {
6307 spin_lock(&root
->fs_info
->ordered_extent_lock
);
6308 list_del_init(&BTRFS_I(inode
)->ordered_operations
);
6309 spin_unlock(&root
->fs_info
->ordered_extent_lock
);
6312 spin_lock(&root
->orphan_lock
);
6313 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
6314 printk(KERN_INFO
"BTRFS: inode %lu still on the orphan list\n",
6316 list_del_init(&BTRFS_I(inode
)->i_orphan
);
6318 spin_unlock(&root
->orphan_lock
);
6321 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
6325 printk(KERN_ERR
"btrfs found ordered "
6326 "extent %llu %llu on inode cleanup\n",
6327 (unsigned long long)ordered
->file_offset
,
6328 (unsigned long long)ordered
->len
);
6329 btrfs_remove_ordered_extent(inode
, ordered
);
6330 btrfs_put_ordered_extent(ordered
);
6331 btrfs_put_ordered_extent(ordered
);
6334 inode_tree_del(inode
);
6335 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
6337 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
6340 int btrfs_drop_inode(struct inode
*inode
)
6342 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6344 if (btrfs_root_refs(&root
->root_item
) == 0)
6347 return generic_drop_inode(inode
);
6350 static void init_once(void *foo
)
6352 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
6354 inode_init_once(&ei
->vfs_inode
);
6357 void btrfs_destroy_cachep(void)
6359 if (btrfs_inode_cachep
)
6360 kmem_cache_destroy(btrfs_inode_cachep
);
6361 if (btrfs_trans_handle_cachep
)
6362 kmem_cache_destroy(btrfs_trans_handle_cachep
);
6363 if (btrfs_transaction_cachep
)
6364 kmem_cache_destroy(btrfs_transaction_cachep
);
6365 if (btrfs_path_cachep
)
6366 kmem_cache_destroy(btrfs_path_cachep
);
6369 int btrfs_init_cachep(void)
6371 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode_cache",
6372 sizeof(struct btrfs_inode
), 0,
6373 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
6374 if (!btrfs_inode_cachep
)
6377 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle_cache",
6378 sizeof(struct btrfs_trans_handle
), 0,
6379 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6380 if (!btrfs_trans_handle_cachep
)
6383 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction_cache",
6384 sizeof(struct btrfs_transaction
), 0,
6385 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6386 if (!btrfs_transaction_cachep
)
6389 btrfs_path_cachep
= kmem_cache_create("btrfs_path_cache",
6390 sizeof(struct btrfs_path
), 0,
6391 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6392 if (!btrfs_path_cachep
)
6397 btrfs_destroy_cachep();
6401 static int btrfs_getattr(struct vfsmount
*mnt
,
6402 struct dentry
*dentry
, struct kstat
*stat
)
6404 struct inode
*inode
= dentry
->d_inode
;
6405 generic_fillattr(inode
, stat
);
6406 stat
->dev
= BTRFS_I(inode
)->root
->anon_super
.s_dev
;
6407 stat
->blksize
= PAGE_CACHE_SIZE
;
6408 stat
->blocks
= (inode_get_bytes(inode
) +
6409 BTRFS_I(inode
)->delalloc_bytes
) >> 9;
6413 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
6414 struct inode
*new_dir
, struct dentry
*new_dentry
)
6416 struct btrfs_trans_handle
*trans
;
6417 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
6418 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
6419 struct inode
*new_inode
= new_dentry
->d_inode
;
6420 struct inode
*old_inode
= old_dentry
->d_inode
;
6421 struct timespec ctime
= CURRENT_TIME
;
6426 if (new_dir
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
6429 /* we only allow rename subvolume link between subvolumes */
6430 if (old_inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
6433 if (old_inode
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
6434 (new_inode
&& new_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
))
6437 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
6438 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
6441 * we're using rename to replace one file with another.
6442 * and the replacement file is large. Start IO on it now so
6443 * we don't add too much work to the end of the transaction
6445 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
&&
6446 old_inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
6447 filemap_flush(old_inode
->i_mapping
);
6449 /* close the racy window with snapshot create/destroy ioctl */
6450 if (old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)
6451 down_read(&root
->fs_info
->subvol_sem
);
6453 * We want to reserve the absolute worst case amount of items. So if
6454 * both inodes are subvols and we need to unlink them then that would
6455 * require 4 item modifications, but if they are both normal inodes it
6456 * would require 5 item modifications, so we'll assume their normal
6457 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
6458 * should cover the worst case number of items we'll modify.
6460 trans
= btrfs_start_transaction(root
, 20);
6462 return PTR_ERR(trans
);
6464 btrfs_set_trans_block_group(trans
, new_dir
);
6467 btrfs_record_root_in_trans(trans
, dest
);
6469 ret
= btrfs_set_inode_index(new_dir
, &index
);
6473 if (unlikely(old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6474 /* force full log commit if subvolume involved. */
6475 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
6477 ret
= btrfs_insert_inode_ref(trans
, dest
,
6478 new_dentry
->d_name
.name
,
6479 new_dentry
->d_name
.len
,
6481 new_dir
->i_ino
, index
);
6485 * this is an ugly little race, but the rename is required
6486 * to make sure that if we crash, the inode is either at the
6487 * old name or the new one. pinning the log transaction lets
6488 * us make sure we don't allow a log commit to come in after
6489 * we unlink the name but before we add the new name back in.
6491 btrfs_pin_log_trans(root
);
6494 * make sure the inode gets flushed if it is replacing
6497 if (new_inode
&& new_inode
->i_size
&&
6498 old_inode
&& S_ISREG(old_inode
->i_mode
)) {
6499 btrfs_add_ordered_operation(trans
, root
, old_inode
);
6502 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
6503 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
6504 old_inode
->i_ctime
= ctime
;
6506 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
6507 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
6509 if (unlikely(old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6510 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
6511 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
6512 old_dentry
->d_name
.name
,
6513 old_dentry
->d_name
.len
);
6515 btrfs_inc_nlink(old_dentry
->d_inode
);
6516 ret
= btrfs_unlink_inode(trans
, root
, old_dir
,
6517 old_dentry
->d_inode
,
6518 old_dentry
->d_name
.name
,
6519 old_dentry
->d_name
.len
);
6524 new_inode
->i_ctime
= CURRENT_TIME
;
6525 if (unlikely(new_inode
->i_ino
==
6526 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
6527 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
6528 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
6530 new_dentry
->d_name
.name
,
6531 new_dentry
->d_name
.len
);
6532 BUG_ON(new_inode
->i_nlink
== 0);
6534 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
6535 new_dentry
->d_inode
,
6536 new_dentry
->d_name
.name
,
6537 new_dentry
->d_name
.len
);
6540 if (new_inode
->i_nlink
== 0) {
6541 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
6546 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
6547 new_dentry
->d_name
.name
,
6548 new_dentry
->d_name
.len
, 0, index
);
6551 if (old_inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
6552 btrfs_log_new_name(trans
, old_inode
, old_dir
,
6553 new_dentry
->d_parent
);
6554 btrfs_end_log_trans(root
);
6557 btrfs_end_transaction_throttle(trans
, root
);
6559 if (old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)
6560 up_read(&root
->fs_info
->subvol_sem
);
6566 * some fairly slow code that needs optimization. This walks the list
6567 * of all the inodes with pending delalloc and forces them to disk.
6569 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
6571 struct list_head
*head
= &root
->fs_info
->delalloc_inodes
;
6572 struct btrfs_inode
*binode
;
6573 struct inode
*inode
;
6575 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
6578 spin_lock(&root
->fs_info
->delalloc_lock
);
6579 while (!list_empty(head
)) {
6580 binode
= list_entry(head
->next
, struct btrfs_inode
,
6582 inode
= igrab(&binode
->vfs_inode
);
6584 list_del_init(&binode
->delalloc_inodes
);
6585 spin_unlock(&root
->fs_info
->delalloc_lock
);
6587 filemap_flush(inode
->i_mapping
);
6589 btrfs_add_delayed_iput(inode
);
6594 spin_lock(&root
->fs_info
->delalloc_lock
);
6596 spin_unlock(&root
->fs_info
->delalloc_lock
);
6598 /* the filemap_flush will queue IO into the worker threads, but
6599 * we have to make sure the IO is actually started and that
6600 * ordered extents get created before we return
6602 atomic_inc(&root
->fs_info
->async_submit_draining
);
6603 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
6604 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
6605 wait_event(root
->fs_info
->async_submit_wait
,
6606 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
6607 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
6609 atomic_dec(&root
->fs_info
->async_submit_draining
);
6613 int btrfs_start_one_delalloc_inode(struct btrfs_root
*root
, int delay_iput
)
6615 struct btrfs_inode
*binode
;
6616 struct inode
*inode
= NULL
;
6618 spin_lock(&root
->fs_info
->delalloc_lock
);
6619 while (!list_empty(&root
->fs_info
->delalloc_inodes
)) {
6620 binode
= list_entry(root
->fs_info
->delalloc_inodes
.next
,
6621 struct btrfs_inode
, delalloc_inodes
);
6622 inode
= igrab(&binode
->vfs_inode
);
6624 list_move_tail(&binode
->delalloc_inodes
,
6625 &root
->fs_info
->delalloc_inodes
);
6629 list_del_init(&binode
->delalloc_inodes
);
6630 cond_resched_lock(&root
->fs_info
->delalloc_lock
);
6632 spin_unlock(&root
->fs_info
->delalloc_lock
);
6635 write_inode_now(inode
, 0);
6637 btrfs_add_delayed_iput(inode
);
6645 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
6646 const char *symname
)
6648 struct btrfs_trans_handle
*trans
;
6649 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6650 struct btrfs_path
*path
;
6651 struct btrfs_key key
;
6652 struct inode
*inode
= NULL
;
6660 struct btrfs_file_extent_item
*ei
;
6661 struct extent_buffer
*leaf
;
6662 unsigned long nr
= 0;
6664 name_len
= strlen(symname
) + 1;
6665 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
6666 return -ENAMETOOLONG
;
6668 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
6672 * 2 items for inode item and ref
6673 * 2 items for dir items
6674 * 1 item for xattr if selinux is on
6676 trans
= btrfs_start_transaction(root
, 5);
6678 return PTR_ERR(trans
);
6680 btrfs_set_trans_block_group(trans
, dir
);
6682 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6684 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
6685 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
,
6687 err
= PTR_ERR(inode
);
6691 err
= btrfs_init_inode_security(trans
, inode
, dir
);
6697 btrfs_set_trans_block_group(trans
, inode
);
6698 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0, index
);
6702 inode
->i_mapping
->a_ops
= &btrfs_aops
;
6703 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
6704 inode
->i_fop
= &btrfs_file_operations
;
6705 inode
->i_op
= &btrfs_file_inode_operations
;
6706 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
6708 btrfs_update_inode_block_group(trans
, inode
);
6709 btrfs_update_inode_block_group(trans
, dir
);
6713 path
= btrfs_alloc_path();
6715 key
.objectid
= inode
->i_ino
;
6717 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
6718 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
6719 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
6725 leaf
= path
->nodes
[0];
6726 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
6727 struct btrfs_file_extent_item
);
6728 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
6729 btrfs_set_file_extent_type(leaf
, ei
,
6730 BTRFS_FILE_EXTENT_INLINE
);
6731 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
6732 btrfs_set_file_extent_compression(leaf
, ei
, 0);
6733 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
6734 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
6736 ptr
= btrfs_file_extent_inline_start(ei
);
6737 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
6738 btrfs_mark_buffer_dirty(leaf
);
6739 btrfs_free_path(path
);
6741 inode
->i_op
= &btrfs_symlink_inode_operations
;
6742 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
6743 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
6744 inode_set_bytes(inode
, name_len
);
6745 btrfs_i_size_write(inode
, name_len
- 1);
6746 err
= btrfs_update_inode(trans
, root
, inode
);
6751 nr
= trans
->blocks_used
;
6752 btrfs_end_transaction_throttle(trans
, root
);
6754 inode_dec_link_count(inode
);
6757 btrfs_btree_balance_dirty(root
, nr
);
6761 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
6762 u64 start
, u64 num_bytes
, u64 min_size
,
6763 loff_t actual_len
, u64
*alloc_hint
)
6765 struct btrfs_trans_handle
*trans
;
6766 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6767 struct btrfs_key ins
;
6768 u64 cur_offset
= start
;
6771 while (num_bytes
> 0) {
6772 trans
= btrfs_start_transaction(root
, 3);
6773 if (IS_ERR(trans
)) {
6774 ret
= PTR_ERR(trans
);
6778 ret
= btrfs_reserve_extent(trans
, root
, num_bytes
, min_size
,
6779 0, *alloc_hint
, (u64
)-1, &ins
, 1);
6781 btrfs_end_transaction(trans
, root
);
6785 ret
= insert_reserved_file_extent(trans
, inode
,
6786 cur_offset
, ins
.objectid
,
6787 ins
.offset
, ins
.offset
,
6788 ins
.offset
, 0, 0, 0,
6789 BTRFS_FILE_EXTENT_PREALLOC
);
6791 btrfs_drop_extent_cache(inode
, cur_offset
,
6792 cur_offset
+ ins
.offset
-1, 0);
6794 num_bytes
-= ins
.offset
;
6795 cur_offset
+= ins
.offset
;
6796 *alloc_hint
= ins
.objectid
+ ins
.offset
;
6798 inode
->i_ctime
= CURRENT_TIME
;
6799 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
6800 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
6801 (actual_len
> inode
->i_size
) &&
6802 (cur_offset
> inode
->i_size
)) {
6803 if (cur_offset
> actual_len
)
6804 i_size_write(inode
, actual_len
);
6806 i_size_write(inode
, cur_offset
);
6807 i_size_write(inode
, cur_offset
);
6808 btrfs_ordered_update_i_size(inode
, cur_offset
, NULL
);
6811 ret
= btrfs_update_inode(trans
, root
, inode
);
6814 btrfs_end_transaction(trans
, root
);
6819 static long btrfs_fallocate(struct inode
*inode
, int mode
,
6820 loff_t offset
, loff_t len
)
6822 struct extent_state
*cached_state
= NULL
;
6829 u64 mask
= BTRFS_I(inode
)->root
->sectorsize
- 1;
6830 struct extent_map
*em
;
6833 alloc_start
= offset
& ~mask
;
6834 alloc_end
= (offset
+ len
+ mask
) & ~mask
;
6837 * wait for ordered IO before we have any locks. We'll loop again
6838 * below with the locks held.
6840 btrfs_wait_ordered_range(inode
, alloc_start
, alloc_end
- alloc_start
);
6842 mutex_lock(&inode
->i_mutex
);
6843 if (alloc_start
> inode
->i_size
) {
6844 ret
= btrfs_cont_expand(inode
, alloc_start
);
6849 ret
= btrfs_check_data_free_space(inode
, alloc_end
- alloc_start
);
6853 locked_end
= alloc_end
- 1;
6855 struct btrfs_ordered_extent
*ordered
;
6857 /* the extent lock is ordered inside the running
6860 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, alloc_start
,
6861 locked_end
, 0, &cached_state
, GFP_NOFS
);
6862 ordered
= btrfs_lookup_first_ordered_extent(inode
,
6865 ordered
->file_offset
+ ordered
->len
> alloc_start
&&
6866 ordered
->file_offset
< alloc_end
) {
6867 btrfs_put_ordered_extent(ordered
);
6868 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
6869 alloc_start
, locked_end
,
6870 &cached_state
, GFP_NOFS
);
6872 * we can't wait on the range with the transaction
6873 * running or with the extent lock held
6875 btrfs_wait_ordered_range(inode
, alloc_start
,
6876 alloc_end
- alloc_start
);
6879 btrfs_put_ordered_extent(ordered
);
6884 cur_offset
= alloc_start
;
6886 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
6887 alloc_end
- cur_offset
, 0);
6888 BUG_ON(IS_ERR(em
) || !em
);
6889 last_byte
= min(extent_map_end(em
), alloc_end
);
6890 last_byte
= (last_byte
+ mask
) & ~mask
;
6891 if (em
->block_start
== EXTENT_MAP_HOLE
||
6892 (cur_offset
>= inode
->i_size
&&
6893 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
6894 ret
= btrfs_prealloc_file_range(inode
, mode
, cur_offset
,
6895 last_byte
- cur_offset
,
6896 1 << inode
->i_blkbits
,
6900 free_extent_map(em
);
6904 free_extent_map(em
);
6906 cur_offset
= last_byte
;
6907 if (cur_offset
>= alloc_end
) {
6912 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, alloc_start
, locked_end
,
6913 &cached_state
, GFP_NOFS
);
6915 btrfs_free_reserved_data_space(inode
, alloc_end
- alloc_start
);
6917 mutex_unlock(&inode
->i_mutex
);
6921 static int btrfs_set_page_dirty(struct page
*page
)
6923 return __set_page_dirty_nobuffers(page
);
6926 static int btrfs_permission(struct inode
*inode
, int mask
)
6928 if ((BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
) && (mask
& MAY_WRITE
))
6930 return generic_permission(inode
, mask
, btrfs_check_acl
);
6933 static const struct inode_operations btrfs_dir_inode_operations
= {
6934 .getattr
= btrfs_getattr
,
6935 .lookup
= btrfs_lookup
,
6936 .create
= btrfs_create
,
6937 .unlink
= btrfs_unlink
,
6939 .mkdir
= btrfs_mkdir
,
6940 .rmdir
= btrfs_rmdir
,
6941 .rename
= btrfs_rename
,
6942 .symlink
= btrfs_symlink
,
6943 .setattr
= btrfs_setattr
,
6944 .mknod
= btrfs_mknod
,
6945 .setxattr
= btrfs_setxattr
,
6946 .getxattr
= btrfs_getxattr
,
6947 .listxattr
= btrfs_listxattr
,
6948 .removexattr
= btrfs_removexattr
,
6949 .permission
= btrfs_permission
,
6951 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
6952 .lookup
= btrfs_lookup
,
6953 .permission
= btrfs_permission
,
6956 static const struct file_operations btrfs_dir_file_operations
= {
6957 .llseek
= generic_file_llseek
,
6958 .read
= generic_read_dir
,
6959 .readdir
= btrfs_real_readdir
,
6960 .unlocked_ioctl
= btrfs_ioctl
,
6961 #ifdef CONFIG_COMPAT
6962 .compat_ioctl
= btrfs_ioctl
,
6964 .release
= btrfs_release_file
,
6965 .fsync
= btrfs_sync_file
,
6968 static struct extent_io_ops btrfs_extent_io_ops
= {
6969 .fill_delalloc
= run_delalloc_range
,
6970 .submit_bio_hook
= btrfs_submit_bio_hook
,
6971 .merge_bio_hook
= btrfs_merge_bio_hook
,
6972 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
6973 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
6974 .writepage_start_hook
= btrfs_writepage_start_hook
,
6975 .readpage_io_failed_hook
= btrfs_io_failed_hook
,
6976 .set_bit_hook
= btrfs_set_bit_hook
,
6977 .clear_bit_hook
= btrfs_clear_bit_hook
,
6978 .merge_extent_hook
= btrfs_merge_extent_hook
,
6979 .split_extent_hook
= btrfs_split_extent_hook
,
6983 * btrfs doesn't support the bmap operation because swapfiles
6984 * use bmap to make a mapping of extents in the file. They assume
6985 * these extents won't change over the life of the file and they
6986 * use the bmap result to do IO directly to the drive.
6988 * the btrfs bmap call would return logical addresses that aren't
6989 * suitable for IO and they also will change frequently as COW
6990 * operations happen. So, swapfile + btrfs == corruption.
6992 * For now we're avoiding this by dropping bmap.
6994 static const struct address_space_operations btrfs_aops
= {
6995 .readpage
= btrfs_readpage
,
6996 .writepage
= btrfs_writepage
,
6997 .writepages
= btrfs_writepages
,
6998 .readpages
= btrfs_readpages
,
6999 .sync_page
= block_sync_page
,
7000 .direct_IO
= btrfs_direct_IO
,
7001 .invalidatepage
= btrfs_invalidatepage
,
7002 .releasepage
= btrfs_releasepage
,
7003 .set_page_dirty
= btrfs_set_page_dirty
,
7004 .error_remove_page
= generic_error_remove_page
,
7007 static const struct address_space_operations btrfs_symlink_aops
= {
7008 .readpage
= btrfs_readpage
,
7009 .writepage
= btrfs_writepage
,
7010 .invalidatepage
= btrfs_invalidatepage
,
7011 .releasepage
= btrfs_releasepage
,
7014 static const struct inode_operations btrfs_file_inode_operations
= {
7015 .truncate
= btrfs_truncate
,
7016 .getattr
= btrfs_getattr
,
7017 .setattr
= btrfs_setattr
,
7018 .setxattr
= btrfs_setxattr
,
7019 .getxattr
= btrfs_getxattr
,
7020 .listxattr
= btrfs_listxattr
,
7021 .removexattr
= btrfs_removexattr
,
7022 .permission
= btrfs_permission
,
7023 .fallocate
= btrfs_fallocate
,
7024 .fiemap
= btrfs_fiemap
,
7026 static const struct inode_operations btrfs_special_inode_operations
= {
7027 .getattr
= btrfs_getattr
,
7028 .setattr
= btrfs_setattr
,
7029 .permission
= btrfs_permission
,
7030 .setxattr
= btrfs_setxattr
,
7031 .getxattr
= btrfs_getxattr
,
7032 .listxattr
= btrfs_listxattr
,
7033 .removexattr
= btrfs_removexattr
,
7035 static const struct inode_operations btrfs_symlink_inode_operations
= {
7036 .readlink
= generic_readlink
,
7037 .follow_link
= page_follow_link_light
,
7038 .put_link
= page_put_link
,
7039 .permission
= btrfs_permission
,
7040 .setxattr
= btrfs_setxattr
,
7041 .getxattr
= btrfs_getxattr
,
7042 .listxattr
= btrfs_listxattr
,
7043 .removexattr
= btrfs_removexattr
,
7046 const struct dentry_operations btrfs_dentry_operations
= {
7047 .d_delete
= btrfs_dentry_delete
,