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 compress_type
= BTRFS_COMPRESS_NONE
;
127 if (compressed_size
&& compressed_pages
) {
128 compress_type
= root
->fs_info
->compress_type
;
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
);
162 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
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
,
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
;
267 struct list_head list
;
272 struct btrfs_root
*root
;
273 struct page
*locked_page
;
276 struct list_head extents
;
277 struct btrfs_work work
;
280 static noinline
int add_async_extent(struct async_cow
*cow
,
281 u64 start
, u64 ram_size
,
284 unsigned long nr_pages
,
287 struct async_extent
*async_extent
;
289 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
290 async_extent
->start
= start
;
291 async_extent
->ram_size
= ram_size
;
292 async_extent
->compressed_size
= compressed_size
;
293 async_extent
->pages
= pages
;
294 async_extent
->nr_pages
= nr_pages
;
295 async_extent
->compress_type
= compress_type
;
296 list_add_tail(&async_extent
->list
, &cow
->extents
);
301 * we create compressed extents in two phases. The first
302 * phase compresses a range of pages that have already been
303 * locked (both pages and state bits are locked).
305 * This is done inside an ordered work queue, and the compression
306 * is spread across many cpus. The actual IO submission is step
307 * two, and the ordered work queue takes care of making sure that
308 * happens in the same order things were put onto the queue by
309 * writepages and friends.
311 * If this code finds it can't get good compression, it puts an
312 * entry onto the work queue to write the uncompressed bytes. This
313 * makes sure that both compressed inodes and uncompressed inodes
314 * are written in the same order that pdflush sent them down.
316 static noinline
int compress_file_range(struct inode
*inode
,
317 struct page
*locked_page
,
319 struct async_cow
*async_cow
,
322 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
323 struct btrfs_trans_handle
*trans
;
325 u64 blocksize
= root
->sectorsize
;
327 u64 isize
= i_size_read(inode
);
329 struct page
**pages
= NULL
;
330 unsigned long nr_pages
;
331 unsigned long nr_pages_ret
= 0;
332 unsigned long total_compressed
= 0;
333 unsigned long total_in
= 0;
334 unsigned long max_compressed
= 128 * 1024;
335 unsigned long max_uncompressed
= 128 * 1024;
338 int compress_type
= root
->fs_info
->compress_type
;
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
);
378 * we do compression for mount -o compress and when the
379 * inode has not been flagged as nocompress. This flag can
380 * change at any time if we discover bad compression ratios.
382 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
) &&
383 (btrfs_test_opt(root
, COMPRESS
) ||
384 (BTRFS_I(inode
)->force_compress
))) {
386 pages
= kzalloc(sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
388 if (BTRFS_I(inode
)->force_compress
)
389 compress_type
= BTRFS_I(inode
)->force_compress
;
391 ret
= btrfs_compress_pages(compress_type
,
392 inode
->i_mapping
, start
,
393 total_compressed
, pages
,
394 nr_pages
, &nr_pages_ret
,
400 unsigned long offset
= total_compressed
&
401 (PAGE_CACHE_SIZE
- 1);
402 struct page
*page
= pages
[nr_pages_ret
- 1];
405 /* zero the tail end of the last page, we might be
406 * sending it down to disk
409 kaddr
= kmap_atomic(page
, KM_USER0
);
410 memset(kaddr
+ offset
, 0,
411 PAGE_CACHE_SIZE
- offset
);
412 kunmap_atomic(kaddr
, KM_USER0
);
418 trans
= btrfs_join_transaction(root
, 1);
419 BUG_ON(IS_ERR(trans
));
420 btrfs_set_trans_block_group(trans
, inode
);
421 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
423 /* lets try to make an inline extent */
424 if (ret
|| total_in
< (actual_end
- start
)) {
425 /* we didn't compress the entire range, try
426 * to make an uncompressed inline extent.
428 ret
= cow_file_range_inline(trans
, root
, inode
,
429 start
, end
, 0, NULL
);
431 /* try making a compressed inline extent */
432 ret
= cow_file_range_inline(trans
, root
, inode
,
434 total_compressed
, pages
);
438 * inline extent creation worked, we don't need
439 * to create any more async work items. Unlock
440 * and free up our temp pages.
442 extent_clear_unlock_delalloc(inode
,
443 &BTRFS_I(inode
)->io_tree
,
445 EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
446 EXTENT_CLEAR_DELALLOC
|
447 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
);
449 btrfs_end_transaction(trans
, root
);
452 btrfs_end_transaction(trans
, root
);
457 * we aren't doing an inline extent round the compressed size
458 * up to a block size boundary so the allocator does sane
461 total_compressed
= (total_compressed
+ blocksize
- 1) &
465 * one last check to make sure the compression is really a
466 * win, compare the page count read with the blocks on disk
468 total_in
= (total_in
+ PAGE_CACHE_SIZE
- 1) &
469 ~(PAGE_CACHE_SIZE
- 1);
470 if (total_compressed
>= total_in
) {
473 num_bytes
= total_in
;
476 if (!will_compress
&& pages
) {
478 * the compression code ran but failed to make things smaller,
479 * free any pages it allocated and our page pointer array
481 for (i
= 0; i
< nr_pages_ret
; i
++) {
482 WARN_ON(pages
[i
]->mapping
);
483 page_cache_release(pages
[i
]);
487 total_compressed
= 0;
490 /* flag the file so we don't compress in the future */
491 if (!btrfs_test_opt(root
, FORCE_COMPRESS
) &&
492 !(BTRFS_I(inode
)->force_compress
)) {
493 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
499 /* the async work queues will take care of doing actual
500 * allocation on disk for these compressed pages,
501 * and will submit them to the elevator.
503 add_async_extent(async_cow
, start
, num_bytes
,
504 total_compressed
, pages
, nr_pages_ret
,
507 if (start
+ num_bytes
< end
) {
514 cleanup_and_bail_uncompressed
:
516 * No compression, but we still need to write the pages in
517 * the file we've been given so far. redirty the locked
518 * page if it corresponds to our extent and set things up
519 * for the async work queue to run cow_file_range to do
520 * the normal delalloc dance
522 if (page_offset(locked_page
) >= start
&&
523 page_offset(locked_page
) <= end
) {
524 __set_page_dirty_nobuffers(locked_page
);
525 /* unlocked later on in the async handlers */
527 add_async_extent(async_cow
, start
, end
- start
+ 1,
528 0, NULL
, 0, BTRFS_COMPRESS_NONE
);
536 for (i
= 0; i
< nr_pages_ret
; i
++) {
537 WARN_ON(pages
[i
]->mapping
);
538 page_cache_release(pages
[i
]);
546 * phase two of compressed writeback. This is the ordered portion
547 * of the code, which only gets called in the order the work was
548 * queued. We walk all the async extents created by compress_file_range
549 * and send them down to the disk.
551 static noinline
int submit_compressed_extents(struct inode
*inode
,
552 struct async_cow
*async_cow
)
554 struct async_extent
*async_extent
;
556 struct btrfs_trans_handle
*trans
;
557 struct btrfs_key ins
;
558 struct extent_map
*em
;
559 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
560 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
561 struct extent_io_tree
*io_tree
;
564 if (list_empty(&async_cow
->extents
))
568 while (!list_empty(&async_cow
->extents
)) {
569 async_extent
= list_entry(async_cow
->extents
.next
,
570 struct async_extent
, list
);
571 list_del(&async_extent
->list
);
573 io_tree
= &BTRFS_I(inode
)->io_tree
;
576 /* did the compression code fall back to uncompressed IO? */
577 if (!async_extent
->pages
) {
578 int page_started
= 0;
579 unsigned long nr_written
= 0;
581 lock_extent(io_tree
, async_extent
->start
,
582 async_extent
->start
+
583 async_extent
->ram_size
- 1, GFP_NOFS
);
585 /* allocate blocks */
586 ret
= cow_file_range(inode
, async_cow
->locked_page
,
588 async_extent
->start
+
589 async_extent
->ram_size
- 1,
590 &page_started
, &nr_written
, 0);
593 * if page_started, cow_file_range inserted an
594 * inline extent and took care of all the unlocking
595 * and IO for us. Otherwise, we need to submit
596 * all those pages down to the drive.
598 if (!page_started
&& !ret
)
599 extent_write_locked_range(io_tree
,
600 inode
, async_extent
->start
,
601 async_extent
->start
+
602 async_extent
->ram_size
- 1,
610 lock_extent(io_tree
, async_extent
->start
,
611 async_extent
->start
+ async_extent
->ram_size
- 1,
614 trans
= btrfs_join_transaction(root
, 1);
615 BUG_ON(IS_ERR(trans
));
616 ret
= btrfs_reserve_extent(trans
, root
,
617 async_extent
->compressed_size
,
618 async_extent
->compressed_size
,
621 btrfs_end_transaction(trans
, root
);
625 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
626 WARN_ON(async_extent
->pages
[i
]->mapping
);
627 page_cache_release(async_extent
->pages
[i
]);
629 kfree(async_extent
->pages
);
630 async_extent
->nr_pages
= 0;
631 async_extent
->pages
= NULL
;
632 unlock_extent(io_tree
, async_extent
->start
,
633 async_extent
->start
+
634 async_extent
->ram_size
- 1, GFP_NOFS
);
639 * here we're doing allocation and writeback of the
642 btrfs_drop_extent_cache(inode
, async_extent
->start
,
643 async_extent
->start
+
644 async_extent
->ram_size
- 1, 0);
646 em
= alloc_extent_map(GFP_NOFS
);
647 em
->start
= async_extent
->start
;
648 em
->len
= async_extent
->ram_size
;
649 em
->orig_start
= em
->start
;
651 em
->block_start
= ins
.objectid
;
652 em
->block_len
= ins
.offset
;
653 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
654 em
->compress_type
= async_extent
->compress_type
;
655 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
656 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
659 write_lock(&em_tree
->lock
);
660 ret
= add_extent_mapping(em_tree
, em
);
661 write_unlock(&em_tree
->lock
);
662 if (ret
!= -EEXIST
) {
666 btrfs_drop_extent_cache(inode
, async_extent
->start
,
667 async_extent
->start
+
668 async_extent
->ram_size
- 1, 0);
671 ret
= btrfs_add_ordered_extent_compress(inode
,
674 async_extent
->ram_size
,
676 BTRFS_ORDERED_COMPRESSED
,
677 async_extent
->compress_type
);
681 * clear dirty, set writeback and unlock the pages.
683 extent_clear_unlock_delalloc(inode
,
684 &BTRFS_I(inode
)->io_tree
,
686 async_extent
->start
+
687 async_extent
->ram_size
- 1,
688 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
689 EXTENT_CLEAR_UNLOCK
|
690 EXTENT_CLEAR_DELALLOC
|
691 EXTENT_CLEAR_DIRTY
| EXTENT_SET_WRITEBACK
);
693 ret
= btrfs_submit_compressed_write(inode
,
695 async_extent
->ram_size
,
697 ins
.offset
, async_extent
->pages
,
698 async_extent
->nr_pages
);
701 alloc_hint
= ins
.objectid
+ ins
.offset
;
709 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
712 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
713 struct extent_map
*em
;
716 read_lock(&em_tree
->lock
);
717 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
720 * if block start isn't an actual block number then find the
721 * first block in this inode and use that as a hint. If that
722 * block is also bogus then just don't worry about it.
724 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
726 em
= search_extent_mapping(em_tree
, 0, 0);
727 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
728 alloc_hint
= em
->block_start
;
732 alloc_hint
= em
->block_start
;
736 read_unlock(&em_tree
->lock
);
742 * when extent_io.c finds a delayed allocation range in the file,
743 * the call backs end up in this code. The basic idea is to
744 * allocate extents on disk for the range, and create ordered data structs
745 * in ram to track those extents.
747 * locked_page is the page that writepage had locked already. We use
748 * it to make sure we don't do extra locks or unlocks.
750 * *page_started is set to one if we unlock locked_page and do everything
751 * required to start IO on it. It may be clean and already done with
754 static noinline
int cow_file_range(struct inode
*inode
,
755 struct page
*locked_page
,
756 u64 start
, u64 end
, int *page_started
,
757 unsigned long *nr_written
,
760 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
761 struct btrfs_trans_handle
*trans
;
764 unsigned long ram_size
;
767 u64 blocksize
= root
->sectorsize
;
768 struct btrfs_key ins
;
769 struct extent_map
*em
;
770 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
773 BUG_ON(root
== root
->fs_info
->tree_root
);
774 trans
= btrfs_join_transaction(root
, 1);
775 BUG_ON(IS_ERR(trans
));
776 btrfs_set_trans_block_group(trans
, inode
);
777 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
779 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
780 num_bytes
= max(blocksize
, num_bytes
);
781 disk_num_bytes
= num_bytes
;
785 /* lets try to make an inline extent */
786 ret
= cow_file_range_inline(trans
, root
, inode
,
787 start
, end
, 0, NULL
);
789 extent_clear_unlock_delalloc(inode
,
790 &BTRFS_I(inode
)->io_tree
,
792 EXTENT_CLEAR_UNLOCK_PAGE
|
793 EXTENT_CLEAR_UNLOCK
|
794 EXTENT_CLEAR_DELALLOC
|
796 EXTENT_SET_WRITEBACK
|
797 EXTENT_END_WRITEBACK
);
799 *nr_written
= *nr_written
+
800 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
807 BUG_ON(disk_num_bytes
>
808 btrfs_super_total_bytes(&root
->fs_info
->super_copy
));
810 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
811 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
813 while (disk_num_bytes
> 0) {
816 cur_alloc_size
= disk_num_bytes
;
817 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
818 root
->sectorsize
, 0, alloc_hint
,
822 em
= alloc_extent_map(GFP_NOFS
);
824 em
->orig_start
= em
->start
;
825 ram_size
= ins
.offset
;
826 em
->len
= ins
.offset
;
828 em
->block_start
= ins
.objectid
;
829 em
->block_len
= ins
.offset
;
830 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
831 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
834 write_lock(&em_tree
->lock
);
835 ret
= add_extent_mapping(em_tree
, em
);
836 write_unlock(&em_tree
->lock
);
837 if (ret
!= -EEXIST
) {
841 btrfs_drop_extent_cache(inode
, start
,
842 start
+ ram_size
- 1, 0);
845 cur_alloc_size
= ins
.offset
;
846 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
847 ram_size
, cur_alloc_size
, 0);
850 if (root
->root_key
.objectid
==
851 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
852 ret
= btrfs_reloc_clone_csums(inode
, start
,
857 if (disk_num_bytes
< cur_alloc_size
)
860 /* we're not doing compressed IO, don't unlock the first
861 * page (which the caller expects to stay locked), don't
862 * clear any dirty bits and don't set any writeback bits
864 * Do set the Private2 bit so we know this page was properly
865 * setup for writepage
867 op
= unlock
? EXTENT_CLEAR_UNLOCK_PAGE
: 0;
868 op
|= EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
871 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
872 start
, start
+ ram_size
- 1,
874 disk_num_bytes
-= cur_alloc_size
;
875 num_bytes
-= cur_alloc_size
;
876 alloc_hint
= ins
.objectid
+ ins
.offset
;
877 start
+= cur_alloc_size
;
881 btrfs_end_transaction(trans
, root
);
887 * work queue call back to started compression on a file and pages
889 static noinline
void async_cow_start(struct btrfs_work
*work
)
891 struct async_cow
*async_cow
;
893 async_cow
= container_of(work
, struct async_cow
, work
);
895 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
896 async_cow
->start
, async_cow
->end
, async_cow
,
899 async_cow
->inode
= NULL
;
903 * work queue call back to submit previously compressed pages
905 static noinline
void async_cow_submit(struct btrfs_work
*work
)
907 struct async_cow
*async_cow
;
908 struct btrfs_root
*root
;
909 unsigned long nr_pages
;
911 async_cow
= container_of(work
, struct async_cow
, work
);
913 root
= async_cow
->root
;
914 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
917 atomic_sub(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
919 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
921 waitqueue_active(&root
->fs_info
->async_submit_wait
))
922 wake_up(&root
->fs_info
->async_submit_wait
);
924 if (async_cow
->inode
)
925 submit_compressed_extents(async_cow
->inode
, async_cow
);
928 static noinline
void async_cow_free(struct btrfs_work
*work
)
930 struct async_cow
*async_cow
;
931 async_cow
= container_of(work
, struct async_cow
, work
);
935 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
936 u64 start
, u64 end
, int *page_started
,
937 unsigned long *nr_written
)
939 struct async_cow
*async_cow
;
940 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
941 unsigned long nr_pages
;
943 int limit
= 10 * 1024 * 1042;
945 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
946 1, 0, NULL
, GFP_NOFS
);
947 while (start
< end
) {
948 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
949 async_cow
->inode
= inode
;
950 async_cow
->root
= root
;
951 async_cow
->locked_page
= locked_page
;
952 async_cow
->start
= start
;
954 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
957 cur_end
= min(end
, start
+ 512 * 1024 - 1);
959 async_cow
->end
= cur_end
;
960 INIT_LIST_HEAD(&async_cow
->extents
);
962 async_cow
->work
.func
= async_cow_start
;
963 async_cow
->work
.ordered_func
= async_cow_submit
;
964 async_cow
->work
.ordered_free
= async_cow_free
;
965 async_cow
->work
.flags
= 0;
967 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
969 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
971 btrfs_queue_worker(&root
->fs_info
->delalloc_workers
,
974 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
975 wait_event(root
->fs_info
->async_submit_wait
,
976 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
980 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
981 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
982 wait_event(root
->fs_info
->async_submit_wait
,
983 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
987 *nr_written
+= nr_pages
;
994 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
995 u64 bytenr
, u64 num_bytes
)
998 struct btrfs_ordered_sum
*sums
;
1001 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
1002 bytenr
+ num_bytes
- 1, &list
);
1003 if (ret
== 0 && list_empty(&list
))
1006 while (!list_empty(&list
)) {
1007 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1008 list_del(&sums
->list
);
1015 * when nowcow writeback call back. This checks for snapshots or COW copies
1016 * of the extents that exist in the file, and COWs the file as required.
1018 * If no cow copies or snapshots exist, we write directly to the existing
1021 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1022 struct page
*locked_page
,
1023 u64 start
, u64 end
, int *page_started
, int force
,
1024 unsigned long *nr_written
)
1026 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1027 struct btrfs_trans_handle
*trans
;
1028 struct extent_buffer
*leaf
;
1029 struct btrfs_path
*path
;
1030 struct btrfs_file_extent_item
*fi
;
1031 struct btrfs_key found_key
;
1043 bool nolock
= false;
1045 path
= btrfs_alloc_path();
1047 if (root
== root
->fs_info
->tree_root
) {
1049 trans
= btrfs_join_transaction_nolock(root
, 1);
1051 trans
= btrfs_join_transaction(root
, 1);
1053 BUG_ON(IS_ERR(trans
));
1055 cow_start
= (u64
)-1;
1058 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
1061 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1062 leaf
= path
->nodes
[0];
1063 btrfs_item_key_to_cpu(leaf
, &found_key
,
1064 path
->slots
[0] - 1);
1065 if (found_key
.objectid
== inode
->i_ino
&&
1066 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1071 leaf
= path
->nodes
[0];
1072 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1073 ret
= btrfs_next_leaf(root
, path
);
1078 leaf
= path
->nodes
[0];
1084 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1086 if (found_key
.objectid
> inode
->i_ino
||
1087 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1088 found_key
.offset
> end
)
1091 if (found_key
.offset
> cur_offset
) {
1092 extent_end
= found_key
.offset
;
1097 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1098 struct btrfs_file_extent_item
);
1099 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1101 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1102 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1103 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1104 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1105 extent_end
= found_key
.offset
+
1106 btrfs_file_extent_num_bytes(leaf
, fi
);
1107 if (extent_end
<= start
) {
1111 if (disk_bytenr
== 0)
1113 if (btrfs_file_extent_compression(leaf
, fi
) ||
1114 btrfs_file_extent_encryption(leaf
, fi
) ||
1115 btrfs_file_extent_other_encoding(leaf
, fi
))
1117 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1119 if (btrfs_extent_readonly(root
, disk_bytenr
))
1121 if (btrfs_cross_ref_exist(trans
, root
, inode
->i_ino
,
1123 extent_offset
, disk_bytenr
))
1125 disk_bytenr
+= extent_offset
;
1126 disk_bytenr
+= cur_offset
- found_key
.offset
;
1127 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1129 * force cow if csum exists in the range.
1130 * this ensure that csum for a given extent are
1131 * either valid or do not exist.
1133 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1136 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1137 extent_end
= found_key
.offset
+
1138 btrfs_file_extent_inline_len(leaf
, fi
);
1139 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1144 if (extent_end
<= start
) {
1149 if (cow_start
== (u64
)-1)
1150 cow_start
= cur_offset
;
1151 cur_offset
= extent_end
;
1152 if (cur_offset
> end
)
1158 btrfs_release_path(root
, path
);
1159 if (cow_start
!= (u64
)-1) {
1160 ret
= cow_file_range(inode
, locked_page
, cow_start
,
1161 found_key
.offset
- 1, page_started
,
1164 cow_start
= (u64
)-1;
1167 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1168 struct extent_map
*em
;
1169 struct extent_map_tree
*em_tree
;
1170 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1171 em
= alloc_extent_map(GFP_NOFS
);
1172 em
->start
= cur_offset
;
1173 em
->orig_start
= em
->start
;
1174 em
->len
= num_bytes
;
1175 em
->block_len
= num_bytes
;
1176 em
->block_start
= disk_bytenr
;
1177 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1178 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1180 write_lock(&em_tree
->lock
);
1181 ret
= add_extent_mapping(em_tree
, em
);
1182 write_unlock(&em_tree
->lock
);
1183 if (ret
!= -EEXIST
) {
1184 free_extent_map(em
);
1187 btrfs_drop_extent_cache(inode
, em
->start
,
1188 em
->start
+ em
->len
- 1, 0);
1190 type
= BTRFS_ORDERED_PREALLOC
;
1192 type
= BTRFS_ORDERED_NOCOW
;
1195 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1196 num_bytes
, num_bytes
, type
);
1199 if (root
->root_key
.objectid
==
1200 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1201 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1206 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
1207 cur_offset
, cur_offset
+ num_bytes
- 1,
1208 locked_page
, EXTENT_CLEAR_UNLOCK_PAGE
|
1209 EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
1210 EXTENT_SET_PRIVATE2
);
1211 cur_offset
= extent_end
;
1212 if (cur_offset
> end
)
1215 btrfs_release_path(root
, path
);
1217 if (cur_offset
<= end
&& cow_start
== (u64
)-1)
1218 cow_start
= cur_offset
;
1219 if (cow_start
!= (u64
)-1) {
1220 ret
= cow_file_range(inode
, locked_page
, cow_start
, end
,
1221 page_started
, nr_written
, 1);
1226 ret
= btrfs_end_transaction_nolock(trans
, root
);
1229 ret
= btrfs_end_transaction(trans
, root
);
1232 btrfs_free_path(path
);
1237 * extent_io.c call back to do delayed allocation processing
1239 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1240 u64 start
, u64 end
, int *page_started
,
1241 unsigned long *nr_written
)
1244 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1246 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
)
1247 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1248 page_started
, 1, nr_written
);
1249 else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
)
1250 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1251 page_started
, 0, nr_written
);
1252 else if (!btrfs_test_opt(root
, COMPRESS
) &&
1253 !(BTRFS_I(inode
)->force_compress
))
1254 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1255 page_started
, nr_written
, 1);
1257 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1258 page_started
, nr_written
);
1262 static int btrfs_split_extent_hook(struct inode
*inode
,
1263 struct extent_state
*orig
, u64 split
)
1265 /* not delalloc, ignore it */
1266 if (!(orig
->state
& EXTENT_DELALLOC
))
1269 atomic_inc(&BTRFS_I(inode
)->outstanding_extents
);
1274 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1275 * extents so we can keep track of new extents that are just merged onto old
1276 * extents, such as when we are doing sequential writes, so we can properly
1277 * account for the metadata space we'll need.
1279 static int btrfs_merge_extent_hook(struct inode
*inode
,
1280 struct extent_state
*new,
1281 struct extent_state
*other
)
1283 /* not delalloc, ignore it */
1284 if (!(other
->state
& EXTENT_DELALLOC
))
1287 atomic_dec(&BTRFS_I(inode
)->outstanding_extents
);
1292 * extent_io.c set_bit_hook, used to track delayed allocation
1293 * bytes in this file, and to maintain the list of inodes that
1294 * have pending delalloc work to be done.
1296 static int btrfs_set_bit_hook(struct inode
*inode
,
1297 struct extent_state
*state
, int *bits
)
1301 * set_bit and clear bit hooks normally require _irqsave/restore
1302 * but in this case, we are only testeing for the DELALLOC
1303 * bit, which is only set or cleared with irqs on
1305 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1306 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1307 u64 len
= state
->end
+ 1 - state
->start
;
1308 int do_list
= (root
->root_key
.objectid
!=
1309 BTRFS_ROOT_TREE_OBJECTID
);
1311 if (*bits
& EXTENT_FIRST_DELALLOC
)
1312 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1314 atomic_inc(&BTRFS_I(inode
)->outstanding_extents
);
1316 spin_lock(&root
->fs_info
->delalloc_lock
);
1317 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1318 root
->fs_info
->delalloc_bytes
+= len
;
1319 if (do_list
&& list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1320 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1321 &root
->fs_info
->delalloc_inodes
);
1323 spin_unlock(&root
->fs_info
->delalloc_lock
);
1329 * extent_io.c clear_bit_hook, see set_bit_hook for why
1331 static int btrfs_clear_bit_hook(struct inode
*inode
,
1332 struct extent_state
*state
, int *bits
)
1335 * set_bit and clear bit hooks normally require _irqsave/restore
1336 * but in this case, we are only testeing for the DELALLOC
1337 * bit, which is only set or cleared with irqs on
1339 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1340 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1341 u64 len
= state
->end
+ 1 - state
->start
;
1342 int do_list
= (root
->root_key
.objectid
!=
1343 BTRFS_ROOT_TREE_OBJECTID
);
1345 if (*bits
& EXTENT_FIRST_DELALLOC
)
1346 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1347 else if (!(*bits
& EXTENT_DO_ACCOUNTING
))
1348 atomic_dec(&BTRFS_I(inode
)->outstanding_extents
);
1350 if (*bits
& EXTENT_DO_ACCOUNTING
)
1351 btrfs_delalloc_release_metadata(inode
, len
);
1353 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1355 btrfs_free_reserved_data_space(inode
, len
);
1357 spin_lock(&root
->fs_info
->delalloc_lock
);
1358 root
->fs_info
->delalloc_bytes
-= len
;
1359 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1361 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1362 !list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1363 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1365 spin_unlock(&root
->fs_info
->delalloc_lock
);
1371 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1372 * we don't create bios that span stripes or chunks
1374 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
1375 size_t size
, struct bio
*bio
,
1376 unsigned long bio_flags
)
1378 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1379 struct btrfs_mapping_tree
*map_tree
;
1380 u64 logical
= (u64
)bio
->bi_sector
<< 9;
1385 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1388 length
= bio
->bi_size
;
1389 map_tree
= &root
->fs_info
->mapping_tree
;
1390 map_length
= length
;
1391 ret
= btrfs_map_block(map_tree
, READ
, logical
,
1392 &map_length
, NULL
, 0);
1394 if (map_length
< length
+ size
)
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_start(struct inode
*inode
, int rw
,
1408 struct bio
*bio
, int mirror_num
,
1409 unsigned long bio_flags
,
1412 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1415 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1421 * in order to insert checksums into the metadata in large chunks,
1422 * we wait until bio submission time. All the pages in the bio are
1423 * checksummed and sums are attached onto the ordered extent record.
1425 * At IO completion time the cums attached on the ordered extent record
1426 * are inserted into the btree
1428 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1429 int mirror_num
, unsigned long bio_flags
,
1432 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1433 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1437 * extent_io.c submission hook. This does the right thing for csum calculation
1438 * on write, or reading the csums from the tree before a read
1440 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1441 int mirror_num
, unsigned long bio_flags
,
1444 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1448 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1450 if (root
== root
->fs_info
->tree_root
)
1451 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 2);
1453 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
1456 if (!(rw
& REQ_WRITE
)) {
1457 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1458 return btrfs_submit_compressed_read(inode
, bio
,
1459 mirror_num
, bio_flags
);
1460 } else if (!skip_sum
)
1461 btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1463 } else if (!skip_sum
) {
1464 /* csum items have already been cloned */
1465 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1467 /* we're doing a write, do the async checksumming */
1468 return btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1469 inode
, rw
, bio
, mirror_num
,
1470 bio_flags
, bio_offset
,
1471 __btrfs_submit_bio_start
,
1472 __btrfs_submit_bio_done
);
1476 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1480 * given a list of ordered sums record them in the inode. This happens
1481 * at IO completion time based on sums calculated at bio submission time.
1483 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1484 struct inode
*inode
, u64 file_offset
,
1485 struct list_head
*list
)
1487 struct btrfs_ordered_sum
*sum
;
1489 btrfs_set_trans_block_group(trans
, inode
);
1491 list_for_each_entry(sum
, list
, list
) {
1492 btrfs_csum_file_blocks(trans
,
1493 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1498 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1499 struct extent_state
**cached_state
)
1501 if ((end
& (PAGE_CACHE_SIZE
- 1)) == 0)
1503 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1504 cached_state
, GFP_NOFS
);
1507 /* see btrfs_writepage_start_hook for details on why this is required */
1508 struct btrfs_writepage_fixup
{
1510 struct btrfs_work work
;
1513 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1515 struct btrfs_writepage_fixup
*fixup
;
1516 struct btrfs_ordered_extent
*ordered
;
1517 struct extent_state
*cached_state
= NULL
;
1519 struct inode
*inode
;
1523 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1527 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1528 ClearPageChecked(page
);
1532 inode
= page
->mapping
->host
;
1533 page_start
= page_offset(page
);
1534 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1536 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1537 &cached_state
, GFP_NOFS
);
1539 /* already ordered? We're done */
1540 if (PagePrivate2(page
))
1543 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1545 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1546 page_end
, &cached_state
, GFP_NOFS
);
1548 btrfs_start_ordered_extent(inode
, ordered
, 1);
1553 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1554 ClearPageChecked(page
);
1556 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1557 &cached_state
, GFP_NOFS
);
1560 page_cache_release(page
);
1565 * There are a few paths in the higher layers of the kernel that directly
1566 * set the page dirty bit without asking the filesystem if it is a
1567 * good idea. This causes problems because we want to make sure COW
1568 * properly happens and the data=ordered rules are followed.
1570 * In our case any range that doesn't have the ORDERED bit set
1571 * hasn't been properly setup for IO. We kick off an async process
1572 * to fix it up. The async helper will wait for ordered extents, set
1573 * the delalloc bit and make it safe to write the page.
1575 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1577 struct inode
*inode
= page
->mapping
->host
;
1578 struct btrfs_writepage_fixup
*fixup
;
1579 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1581 /* this page is properly in the ordered list */
1582 if (TestClearPagePrivate2(page
))
1585 if (PageChecked(page
))
1588 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1592 SetPageChecked(page
);
1593 page_cache_get(page
);
1594 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
1596 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
1600 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1601 struct inode
*inode
, u64 file_pos
,
1602 u64 disk_bytenr
, u64 disk_num_bytes
,
1603 u64 num_bytes
, u64 ram_bytes
,
1604 u8 compression
, u8 encryption
,
1605 u16 other_encoding
, int extent_type
)
1607 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1608 struct btrfs_file_extent_item
*fi
;
1609 struct btrfs_path
*path
;
1610 struct extent_buffer
*leaf
;
1611 struct btrfs_key ins
;
1615 path
= btrfs_alloc_path();
1618 path
->leave_spinning
= 1;
1621 * we may be replacing one extent in the tree with another.
1622 * The new extent is pinned in the extent map, and we don't want
1623 * to drop it from the cache until it is completely in the btree.
1625 * So, tell btrfs_drop_extents to leave this extent in the cache.
1626 * the caller is expected to unpin it and allow it to be merged
1629 ret
= btrfs_drop_extents(trans
, inode
, file_pos
, file_pos
+ num_bytes
,
1633 ins
.objectid
= inode
->i_ino
;
1634 ins
.offset
= file_pos
;
1635 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1636 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
, sizeof(*fi
));
1638 leaf
= path
->nodes
[0];
1639 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1640 struct btrfs_file_extent_item
);
1641 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1642 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1643 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1644 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1645 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1646 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1647 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1648 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1649 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1650 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1652 btrfs_unlock_up_safe(path
, 1);
1653 btrfs_set_lock_blocking(leaf
);
1655 btrfs_mark_buffer_dirty(leaf
);
1657 inode_add_bytes(inode
, num_bytes
);
1659 ins
.objectid
= disk_bytenr
;
1660 ins
.offset
= disk_num_bytes
;
1661 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1662 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
1663 root
->root_key
.objectid
,
1664 inode
->i_ino
, file_pos
, &ins
);
1666 btrfs_free_path(path
);
1672 * helper function for btrfs_finish_ordered_io, this
1673 * just reads in some of the csum leaves to prime them into ram
1674 * before we start the transaction. It limits the amount of btree
1675 * reads required while inside the transaction.
1677 /* as ordered data IO finishes, this gets called so we can finish
1678 * an ordered extent if the range of bytes in the file it covers are
1681 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
)
1683 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1684 struct btrfs_trans_handle
*trans
= NULL
;
1685 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
1686 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1687 struct extent_state
*cached_state
= NULL
;
1688 int compress_type
= 0;
1690 bool nolock
= false;
1692 ret
= btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
1696 BUG_ON(!ordered_extent
);
1698 nolock
= (root
== root
->fs_info
->tree_root
);
1700 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
1701 BUG_ON(!list_empty(&ordered_extent
->list
));
1702 ret
= btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
1705 trans
= btrfs_join_transaction_nolock(root
, 1);
1707 trans
= btrfs_join_transaction(root
, 1);
1708 BUG_ON(IS_ERR(trans
));
1709 btrfs_set_trans_block_group(trans
, inode
);
1710 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1711 ret
= btrfs_update_inode(trans
, root
, inode
);
1717 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
1718 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
1719 0, &cached_state
, GFP_NOFS
);
1722 trans
= btrfs_join_transaction_nolock(root
, 1);
1724 trans
= btrfs_join_transaction(root
, 1);
1725 BUG_ON(IS_ERR(trans
));
1726 btrfs_set_trans_block_group(trans
, inode
);
1727 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1729 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
1730 compress_type
= ordered_extent
->compress_type
;
1731 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
1732 BUG_ON(compress_type
);
1733 ret
= btrfs_mark_extent_written(trans
, inode
,
1734 ordered_extent
->file_offset
,
1735 ordered_extent
->file_offset
+
1736 ordered_extent
->len
);
1739 BUG_ON(root
== root
->fs_info
->tree_root
);
1740 ret
= insert_reserved_file_extent(trans
, inode
,
1741 ordered_extent
->file_offset
,
1742 ordered_extent
->start
,
1743 ordered_extent
->disk_len
,
1744 ordered_extent
->len
,
1745 ordered_extent
->len
,
1746 compress_type
, 0, 0,
1747 BTRFS_FILE_EXTENT_REG
);
1748 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
1749 ordered_extent
->file_offset
,
1750 ordered_extent
->len
);
1753 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
1754 ordered_extent
->file_offset
+
1755 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
1757 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
1758 &ordered_extent
->list
);
1760 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
1761 ret
= btrfs_update_inode(trans
, root
, inode
);
1766 btrfs_end_transaction_nolock(trans
, root
);
1768 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
1770 btrfs_end_transaction(trans
, root
);
1774 btrfs_put_ordered_extent(ordered_extent
);
1775 /* once for the tree */
1776 btrfs_put_ordered_extent(ordered_extent
);
1781 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
1782 struct extent_state
*state
, int uptodate
)
1784 ClearPagePrivate2(page
);
1785 return btrfs_finish_ordered_io(page
->mapping
->host
, start
, end
);
1789 * When IO fails, either with EIO or csum verification fails, we
1790 * try other mirrors that might have a good copy of the data. This
1791 * io_failure_record is used to record state as we go through all the
1792 * mirrors. If another mirror has good data, the page is set up to date
1793 * and things continue. If a good mirror can't be found, the original
1794 * bio end_io callback is called to indicate things have failed.
1796 struct io_failure_record
{
1801 unsigned long bio_flags
;
1805 static int btrfs_io_failed_hook(struct bio
*failed_bio
,
1806 struct page
*page
, u64 start
, u64 end
,
1807 struct extent_state
*state
)
1809 struct io_failure_record
*failrec
= NULL
;
1811 struct extent_map
*em
;
1812 struct inode
*inode
= page
->mapping
->host
;
1813 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
1814 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1821 ret
= get_state_private(failure_tree
, start
, &private);
1823 failrec
= kmalloc(sizeof(*failrec
), GFP_NOFS
);
1826 failrec
->start
= start
;
1827 failrec
->len
= end
- start
+ 1;
1828 failrec
->last_mirror
= 0;
1829 failrec
->bio_flags
= 0;
1831 read_lock(&em_tree
->lock
);
1832 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
1833 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
1834 free_extent_map(em
);
1837 read_unlock(&em_tree
->lock
);
1839 if (!em
|| IS_ERR(em
)) {
1843 logical
= start
- em
->start
;
1844 logical
= em
->block_start
+ logical
;
1845 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
1846 logical
= em
->block_start
;
1847 failrec
->bio_flags
= EXTENT_BIO_COMPRESSED
;
1848 extent_set_compress_type(&failrec
->bio_flags
,
1851 failrec
->logical
= logical
;
1852 free_extent_map(em
);
1853 set_extent_bits(failure_tree
, start
, end
, EXTENT_LOCKED
|
1854 EXTENT_DIRTY
, GFP_NOFS
);
1855 set_state_private(failure_tree
, start
,
1856 (u64
)(unsigned long)failrec
);
1858 failrec
= (struct io_failure_record
*)(unsigned long)private;
1860 num_copies
= btrfs_num_copies(
1861 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
1862 failrec
->logical
, failrec
->len
);
1863 failrec
->last_mirror
++;
1865 spin_lock(&BTRFS_I(inode
)->io_tree
.lock
);
1866 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
1869 if (state
&& state
->start
!= failrec
->start
)
1871 spin_unlock(&BTRFS_I(inode
)->io_tree
.lock
);
1873 if (!state
|| failrec
->last_mirror
> num_copies
) {
1874 set_state_private(failure_tree
, failrec
->start
, 0);
1875 clear_extent_bits(failure_tree
, failrec
->start
,
1876 failrec
->start
+ failrec
->len
- 1,
1877 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
1881 bio
= bio_alloc(GFP_NOFS
, 1);
1882 bio
->bi_private
= state
;
1883 bio
->bi_end_io
= failed_bio
->bi_end_io
;
1884 bio
->bi_sector
= failrec
->logical
>> 9;
1885 bio
->bi_bdev
= failed_bio
->bi_bdev
;
1888 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
1889 if (failed_bio
->bi_rw
& REQ_WRITE
)
1894 BTRFS_I(inode
)->io_tree
.ops
->submit_bio_hook(inode
, rw
, bio
,
1895 failrec
->last_mirror
,
1896 failrec
->bio_flags
, 0);
1901 * each time an IO finishes, we do a fast check in the IO failure tree
1902 * to see if we need to process or clean up an io_failure_record
1904 static int btrfs_clean_io_failures(struct inode
*inode
, u64 start
)
1907 u64 private_failure
;
1908 struct io_failure_record
*failure
;
1912 if (count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
1913 (u64
)-1, 1, EXTENT_DIRTY
)) {
1914 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
,
1915 start
, &private_failure
);
1917 failure
= (struct io_failure_record
*)(unsigned long)
1919 set_state_private(&BTRFS_I(inode
)->io_failure_tree
,
1921 clear_extent_bits(&BTRFS_I(inode
)->io_failure_tree
,
1923 failure
->start
+ failure
->len
- 1,
1924 EXTENT_DIRTY
| EXTENT_LOCKED
,
1933 * when reads are done, we need to check csums to verify the data is correct
1934 * if there's a match, we allow the bio to finish. If not, we go through
1935 * the io_failure_record routines to find good copies
1937 static int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
1938 struct extent_state
*state
)
1940 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
1941 struct inode
*inode
= page
->mapping
->host
;
1942 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1944 u64
private = ~(u32
)0;
1946 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1949 if (PageChecked(page
)) {
1950 ClearPageChecked(page
);
1954 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
1957 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
1958 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
1959 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
1964 if (state
&& state
->start
== start
) {
1965 private = state
->private;
1968 ret
= get_state_private(io_tree
, start
, &private);
1970 kaddr
= kmap_atomic(page
, KM_USER0
);
1974 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
1975 btrfs_csum_final(csum
, (char *)&csum
);
1976 if (csum
!= private)
1979 kunmap_atomic(kaddr
, KM_USER0
);
1981 /* if the io failure tree for this inode is non-empty,
1982 * check to see if we've recovered from a failed IO
1984 btrfs_clean_io_failures(inode
, start
);
1988 if (printk_ratelimit()) {
1989 printk(KERN_INFO
"btrfs csum failed ino %lu off %llu csum %u "
1990 "private %llu\n", page
->mapping
->host
->i_ino
,
1991 (unsigned long long)start
, csum
,
1992 (unsigned long long)private);
1994 memset(kaddr
+ offset
, 1, end
- start
+ 1);
1995 flush_dcache_page(page
);
1996 kunmap_atomic(kaddr
, KM_USER0
);
2002 struct delayed_iput
{
2003 struct list_head list
;
2004 struct inode
*inode
;
2007 void btrfs_add_delayed_iput(struct inode
*inode
)
2009 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2010 struct delayed_iput
*delayed
;
2012 if (atomic_add_unless(&inode
->i_count
, -1, 1))
2015 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
2016 delayed
->inode
= inode
;
2018 spin_lock(&fs_info
->delayed_iput_lock
);
2019 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
2020 spin_unlock(&fs_info
->delayed_iput_lock
);
2023 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
2026 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2027 struct delayed_iput
*delayed
;
2030 spin_lock(&fs_info
->delayed_iput_lock
);
2031 empty
= list_empty(&fs_info
->delayed_iputs
);
2032 spin_unlock(&fs_info
->delayed_iput_lock
);
2036 down_read(&root
->fs_info
->cleanup_work_sem
);
2037 spin_lock(&fs_info
->delayed_iput_lock
);
2038 list_splice_init(&fs_info
->delayed_iputs
, &list
);
2039 spin_unlock(&fs_info
->delayed_iput_lock
);
2041 while (!list_empty(&list
)) {
2042 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
2043 list_del(&delayed
->list
);
2044 iput(delayed
->inode
);
2047 up_read(&root
->fs_info
->cleanup_work_sem
);
2051 * calculate extra metadata reservation when snapshotting a subvolume
2052 * contains orphan files.
2054 void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle
*trans
,
2055 struct btrfs_pending_snapshot
*pending
,
2056 u64
*bytes_to_reserve
)
2058 struct btrfs_root
*root
;
2059 struct btrfs_block_rsv
*block_rsv
;
2063 root
= pending
->root
;
2064 if (!root
->orphan_block_rsv
|| list_empty(&root
->orphan_list
))
2067 block_rsv
= root
->orphan_block_rsv
;
2069 /* orphan block reservation for the snapshot */
2070 num_bytes
= block_rsv
->size
;
2073 * after the snapshot is created, COWing tree blocks may use more
2074 * space than it frees. So we should make sure there is enough
2077 index
= trans
->transid
& 0x1;
2078 if (block_rsv
->reserved
+ block_rsv
->freed
[index
] < block_rsv
->size
) {
2079 num_bytes
+= block_rsv
->size
-
2080 (block_rsv
->reserved
+ block_rsv
->freed
[index
]);
2083 *bytes_to_reserve
+= num_bytes
;
2086 void btrfs_orphan_post_snapshot(struct btrfs_trans_handle
*trans
,
2087 struct btrfs_pending_snapshot
*pending
)
2089 struct btrfs_root
*root
= pending
->root
;
2090 struct btrfs_root
*snap
= pending
->snap
;
2091 struct btrfs_block_rsv
*block_rsv
;
2096 if (!root
->orphan_block_rsv
|| list_empty(&root
->orphan_list
))
2099 /* refill source subvolume's orphan block reservation */
2100 block_rsv
= root
->orphan_block_rsv
;
2101 index
= trans
->transid
& 0x1;
2102 if (block_rsv
->reserved
+ block_rsv
->freed
[index
] < block_rsv
->size
) {
2103 num_bytes
= block_rsv
->size
-
2104 (block_rsv
->reserved
+ block_rsv
->freed
[index
]);
2105 ret
= btrfs_block_rsv_migrate(&pending
->block_rsv
,
2106 root
->orphan_block_rsv
,
2111 /* setup orphan block reservation for the snapshot */
2112 block_rsv
= btrfs_alloc_block_rsv(snap
);
2115 btrfs_add_durable_block_rsv(root
->fs_info
, block_rsv
);
2116 snap
->orphan_block_rsv
= block_rsv
;
2118 num_bytes
= root
->orphan_block_rsv
->size
;
2119 ret
= btrfs_block_rsv_migrate(&pending
->block_rsv
,
2120 block_rsv
, num_bytes
);
2124 /* insert orphan item for the snapshot */
2125 WARN_ON(!root
->orphan_item_inserted
);
2126 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
2127 snap
->root_key
.objectid
);
2129 snap
->orphan_item_inserted
= 1;
2133 enum btrfs_orphan_cleanup_state
{
2134 ORPHAN_CLEANUP_STARTED
= 1,
2135 ORPHAN_CLEANUP_DONE
= 2,
2139 * This is called in transaction commmit time. If there are no orphan
2140 * files in the subvolume, it removes orphan item and frees block_rsv
2143 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
2144 struct btrfs_root
*root
)
2148 if (!list_empty(&root
->orphan_list
) ||
2149 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
2152 if (root
->orphan_item_inserted
&&
2153 btrfs_root_refs(&root
->root_item
) > 0) {
2154 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
2155 root
->root_key
.objectid
);
2157 root
->orphan_item_inserted
= 0;
2160 if (root
->orphan_block_rsv
) {
2161 WARN_ON(root
->orphan_block_rsv
->size
> 0);
2162 btrfs_free_block_rsv(root
, root
->orphan_block_rsv
);
2163 root
->orphan_block_rsv
= NULL
;
2168 * This creates an orphan entry for the given inode in case something goes
2169 * wrong in the middle of an unlink/truncate.
2171 * NOTE: caller of this function should reserve 5 units of metadata for
2174 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2176 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2177 struct btrfs_block_rsv
*block_rsv
= NULL
;
2182 if (!root
->orphan_block_rsv
) {
2183 block_rsv
= btrfs_alloc_block_rsv(root
);
2187 spin_lock(&root
->orphan_lock
);
2188 if (!root
->orphan_block_rsv
) {
2189 root
->orphan_block_rsv
= block_rsv
;
2190 } else if (block_rsv
) {
2191 btrfs_free_block_rsv(root
, block_rsv
);
2195 if (list_empty(&BTRFS_I(inode
)->i_orphan
)) {
2196 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
2199 * For proper ENOSPC handling, we should do orphan
2200 * cleanup when mounting. But this introduces backward
2201 * compatibility issue.
2203 if (!xchg(&root
->orphan_item_inserted
, 1))
2210 WARN_ON(!BTRFS_I(inode
)->orphan_meta_reserved
);
2213 if (!BTRFS_I(inode
)->orphan_meta_reserved
) {
2214 BTRFS_I(inode
)->orphan_meta_reserved
= 1;
2217 spin_unlock(&root
->orphan_lock
);
2220 btrfs_add_durable_block_rsv(root
->fs_info
, block_rsv
);
2222 /* grab metadata reservation from transaction handle */
2224 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
2228 /* insert an orphan item to track this unlinked/truncated file */
2230 ret
= btrfs_insert_orphan_item(trans
, root
, inode
->i_ino
);
2234 /* insert an orphan item to track subvolume contains orphan files */
2236 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
2237 root
->root_key
.objectid
);
2244 * We have done the truncate/delete so we can go ahead and remove the orphan
2245 * item for this particular inode.
2247 int btrfs_orphan_del(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2249 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2250 int delete_item
= 0;
2251 int release_rsv
= 0;
2254 spin_lock(&root
->orphan_lock
);
2255 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
2256 list_del_init(&BTRFS_I(inode
)->i_orphan
);
2260 if (BTRFS_I(inode
)->orphan_meta_reserved
) {
2261 BTRFS_I(inode
)->orphan_meta_reserved
= 0;
2264 spin_unlock(&root
->orphan_lock
);
2266 if (trans
&& delete_item
) {
2267 ret
= btrfs_del_orphan_item(trans
, root
, inode
->i_ino
);
2272 btrfs_orphan_release_metadata(inode
);
2278 * this cleans up any orphans that may be left on the list from the last use
2281 void btrfs_orphan_cleanup(struct btrfs_root
*root
)
2283 struct btrfs_path
*path
;
2284 struct extent_buffer
*leaf
;
2285 struct btrfs_key key
, found_key
;
2286 struct btrfs_trans_handle
*trans
;
2287 struct inode
*inode
;
2288 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
2290 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
2293 path
= btrfs_alloc_path();
2297 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
2298 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
2299 key
.offset
= (u64
)-1;
2302 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2304 printk(KERN_ERR
"Error searching slot for orphan: %d"
2310 * if ret == 0 means we found what we were searching for, which
2311 * is weird, but possible, so only screw with path if we didnt
2312 * find the key and see if we have stuff that matches
2315 if (path
->slots
[0] == 0)
2320 /* pull out the item */
2321 leaf
= path
->nodes
[0];
2322 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2324 /* make sure the item matches what we want */
2325 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
2327 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
2330 /* release the path since we're done with it */
2331 btrfs_release_path(root
, path
);
2334 * this is where we are basically btrfs_lookup, without the
2335 * crossing root thing. we store the inode number in the
2336 * offset of the orphan item.
2338 found_key
.objectid
= found_key
.offset
;
2339 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
2340 found_key
.offset
= 0;
2341 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
2342 BUG_ON(IS_ERR(inode
));
2345 * add this inode to the orphan list so btrfs_orphan_del does
2346 * the proper thing when we hit it
2348 spin_lock(&root
->orphan_lock
);
2349 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
2350 spin_unlock(&root
->orphan_lock
);
2353 * if this is a bad inode, means we actually succeeded in
2354 * removing the inode, but not the orphan record, which means
2355 * we need to manually delete the orphan since iput will just
2356 * do a destroy_inode
2358 if (is_bad_inode(inode
)) {
2359 trans
= btrfs_start_transaction(root
, 0);
2360 BUG_ON(IS_ERR(trans
));
2361 btrfs_orphan_del(trans
, inode
);
2362 btrfs_end_transaction(trans
, root
);
2367 /* if we have links, this was a truncate, lets do that */
2368 if (inode
->i_nlink
) {
2370 btrfs_truncate(inode
);
2375 /* this will do delete_inode and everything for us */
2378 btrfs_free_path(path
);
2380 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
2382 if (root
->orphan_block_rsv
)
2383 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
2386 if (root
->orphan_block_rsv
|| root
->orphan_item_inserted
) {
2387 trans
= btrfs_join_transaction(root
, 1);
2388 BUG_ON(IS_ERR(trans
));
2389 btrfs_end_transaction(trans
, root
);
2393 printk(KERN_INFO
"btrfs: unlinked %d orphans\n", nr_unlink
);
2395 printk(KERN_INFO
"btrfs: truncated %d orphans\n", nr_truncate
);
2399 * very simple check to peek ahead in the leaf looking for xattrs. If we
2400 * don't find any xattrs, we know there can't be any acls.
2402 * slot is the slot the inode is in, objectid is the objectid of the inode
2404 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
2405 int slot
, u64 objectid
)
2407 u32 nritems
= btrfs_header_nritems(leaf
);
2408 struct btrfs_key found_key
;
2412 while (slot
< nritems
) {
2413 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2415 /* we found a different objectid, there must not be acls */
2416 if (found_key
.objectid
!= objectid
)
2419 /* we found an xattr, assume we've got an acl */
2420 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
)
2424 * we found a key greater than an xattr key, there can't
2425 * be any acls later on
2427 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
2434 * it goes inode, inode backrefs, xattrs, extents,
2435 * so if there are a ton of hard links to an inode there can
2436 * be a lot of backrefs. Don't waste time searching too hard,
2437 * this is just an optimization
2442 /* we hit the end of the leaf before we found an xattr or
2443 * something larger than an xattr. We have to assume the inode
2450 * read an inode from the btree into the in-memory inode
2452 static void btrfs_read_locked_inode(struct inode
*inode
)
2454 struct btrfs_path
*path
;
2455 struct extent_buffer
*leaf
;
2456 struct btrfs_inode_item
*inode_item
;
2457 struct btrfs_timespec
*tspec
;
2458 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2459 struct btrfs_key location
;
2461 u64 alloc_group_block
;
2465 path
= btrfs_alloc_path();
2467 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
2469 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
2473 leaf
= path
->nodes
[0];
2474 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2475 struct btrfs_inode_item
);
2477 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
2478 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
2479 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
2480 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
2481 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
2483 tspec
= btrfs_inode_atime(inode_item
);
2484 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2485 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2487 tspec
= btrfs_inode_mtime(inode_item
);
2488 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2489 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2491 tspec
= btrfs_inode_ctime(inode_item
);
2492 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2493 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2495 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
2496 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
2497 BTRFS_I(inode
)->sequence
= btrfs_inode_sequence(leaf
, inode_item
);
2498 inode
->i_generation
= BTRFS_I(inode
)->generation
;
2500 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
2502 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
2503 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
2505 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
2508 * try to precache a NULL acl entry for files that don't have
2509 * any xattrs or acls
2511 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0], inode
->i_ino
);
2513 cache_no_acl(inode
);
2515 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
, 0,
2516 alloc_group_block
, 0);
2517 btrfs_free_path(path
);
2520 switch (inode
->i_mode
& S_IFMT
) {
2522 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2523 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2524 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2525 inode
->i_fop
= &btrfs_file_operations
;
2526 inode
->i_op
= &btrfs_file_inode_operations
;
2529 inode
->i_fop
= &btrfs_dir_file_operations
;
2530 if (root
== root
->fs_info
->tree_root
)
2531 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
2533 inode
->i_op
= &btrfs_dir_inode_operations
;
2536 inode
->i_op
= &btrfs_symlink_inode_operations
;
2537 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2538 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2541 inode
->i_op
= &btrfs_special_inode_operations
;
2542 init_special_inode(inode
, inode
->i_mode
, rdev
);
2546 btrfs_update_iflags(inode
);
2550 btrfs_free_path(path
);
2551 make_bad_inode(inode
);
2555 * given a leaf and an inode, copy the inode fields into the leaf
2557 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
2558 struct extent_buffer
*leaf
,
2559 struct btrfs_inode_item
*item
,
2560 struct inode
*inode
)
2562 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
2563 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
2564 btrfs_set_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
);
2565 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
2566 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
2568 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
2569 inode
->i_atime
.tv_sec
);
2570 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
2571 inode
->i_atime
.tv_nsec
);
2573 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
2574 inode
->i_mtime
.tv_sec
);
2575 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
2576 inode
->i_mtime
.tv_nsec
);
2578 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
2579 inode
->i_ctime
.tv_sec
);
2580 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
2581 inode
->i_ctime
.tv_nsec
);
2583 btrfs_set_inode_nbytes(leaf
, item
, inode_get_bytes(inode
));
2584 btrfs_set_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
);
2585 btrfs_set_inode_sequence(leaf
, item
, BTRFS_I(inode
)->sequence
);
2586 btrfs_set_inode_transid(leaf
, item
, trans
->transid
);
2587 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
2588 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
2589 btrfs_set_inode_block_group(leaf
, item
, BTRFS_I(inode
)->block_group
);
2593 * copy everything in the in-memory inode into the btree.
2595 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
2596 struct btrfs_root
*root
, struct inode
*inode
)
2598 struct btrfs_inode_item
*inode_item
;
2599 struct btrfs_path
*path
;
2600 struct extent_buffer
*leaf
;
2603 path
= btrfs_alloc_path();
2605 path
->leave_spinning
= 1;
2606 ret
= btrfs_lookup_inode(trans
, root
, path
,
2607 &BTRFS_I(inode
)->location
, 1);
2614 btrfs_unlock_up_safe(path
, 1);
2615 leaf
= path
->nodes
[0];
2616 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2617 struct btrfs_inode_item
);
2619 fill_inode_item(trans
, leaf
, inode_item
, inode
);
2620 btrfs_mark_buffer_dirty(leaf
);
2621 btrfs_set_inode_last_trans(trans
, inode
);
2624 btrfs_free_path(path
);
2630 * unlink helper that gets used here in inode.c and in the tree logging
2631 * recovery code. It remove a link in a directory with a given name, and
2632 * also drops the back refs in the inode to the directory
2634 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
2635 struct btrfs_root
*root
,
2636 struct inode
*dir
, struct inode
*inode
,
2637 const char *name
, int name_len
)
2639 struct btrfs_path
*path
;
2641 struct extent_buffer
*leaf
;
2642 struct btrfs_dir_item
*di
;
2643 struct btrfs_key key
;
2646 path
= btrfs_alloc_path();
2652 path
->leave_spinning
= 1;
2653 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
2654 name
, name_len
, -1);
2663 leaf
= path
->nodes
[0];
2664 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2665 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2668 btrfs_release_path(root
, path
);
2670 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
2672 dir
->i_ino
, &index
);
2674 printk(KERN_INFO
"btrfs failed to delete reference to %.*s, "
2675 "inode %lu parent %lu\n", name_len
, name
,
2676 inode
->i_ino
, dir
->i_ino
);
2680 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
2681 index
, name
, name_len
, -1);
2690 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2691 btrfs_release_path(root
, path
);
2693 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
2695 BUG_ON(ret
!= 0 && ret
!= -ENOENT
);
2697 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
2702 btrfs_free_path(path
);
2706 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
2707 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
2708 btrfs_update_inode(trans
, root
, dir
);
2709 btrfs_drop_nlink(inode
);
2710 ret
= btrfs_update_inode(trans
, root
, inode
);
2715 /* helper to check if there is any shared block in the path */
2716 static int check_path_shared(struct btrfs_root
*root
,
2717 struct btrfs_path
*path
)
2719 struct extent_buffer
*eb
;
2723 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
2726 if (!path
->nodes
[level
])
2728 eb
= path
->nodes
[level
];
2729 if (!btrfs_block_can_be_shared(root
, eb
))
2731 ret
= btrfs_lookup_extent_info(NULL
, root
, eb
->start
, eb
->len
,
2740 * helper to start transaction for unlink and rmdir.
2742 * unlink and rmdir are special in btrfs, they do not always free space.
2743 * so in enospc case, we should make sure they will free space before
2744 * allowing them to use the global metadata reservation.
2746 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
,
2747 struct dentry
*dentry
)
2749 struct btrfs_trans_handle
*trans
;
2750 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2751 struct btrfs_path
*path
;
2752 struct btrfs_inode_ref
*ref
;
2753 struct btrfs_dir_item
*di
;
2754 struct inode
*inode
= dentry
->d_inode
;
2760 trans
= btrfs_start_transaction(root
, 10);
2761 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
2764 if (inode
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
2765 return ERR_PTR(-ENOSPC
);
2767 /* check if there is someone else holds reference */
2768 if (S_ISDIR(inode
->i_mode
) && atomic_read(&inode
->i_count
) > 1)
2769 return ERR_PTR(-ENOSPC
);
2771 if (atomic_read(&inode
->i_count
) > 2)
2772 return ERR_PTR(-ENOSPC
);
2774 if (xchg(&root
->fs_info
->enospc_unlink
, 1))
2775 return ERR_PTR(-ENOSPC
);
2777 path
= btrfs_alloc_path();
2779 root
->fs_info
->enospc_unlink
= 0;
2780 return ERR_PTR(-ENOMEM
);
2783 trans
= btrfs_start_transaction(root
, 0);
2784 if (IS_ERR(trans
)) {
2785 btrfs_free_path(path
);
2786 root
->fs_info
->enospc_unlink
= 0;
2790 path
->skip_locking
= 1;
2791 path
->search_commit_root
= 1;
2793 ret
= btrfs_lookup_inode(trans
, root
, path
,
2794 &BTRFS_I(dir
)->location
, 0);
2800 if (check_path_shared(root
, path
))
2805 btrfs_release_path(root
, path
);
2807 ret
= btrfs_lookup_inode(trans
, root
, path
,
2808 &BTRFS_I(inode
)->location
, 0);
2814 if (check_path_shared(root
, path
))
2819 btrfs_release_path(root
, path
);
2821 if (ret
== 0 && S_ISREG(inode
->i_mode
)) {
2822 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
2823 inode
->i_ino
, (u64
)-1, 0);
2829 if (check_path_shared(root
, path
))
2831 btrfs_release_path(root
, path
);
2839 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
2840 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
2846 if (check_path_shared(root
, path
))
2852 btrfs_release_path(root
, path
);
2854 ref
= btrfs_lookup_inode_ref(trans
, root
, path
,
2855 dentry
->d_name
.name
, dentry
->d_name
.len
,
2856 inode
->i_ino
, dir
->i_ino
, 0);
2862 if (check_path_shared(root
, path
))
2864 index
= btrfs_inode_ref_index(path
->nodes
[0], ref
);
2865 btrfs_release_path(root
, path
);
2867 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
, index
,
2868 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
2873 BUG_ON(ret
== -ENOENT
);
2874 if (check_path_shared(root
, path
))
2879 btrfs_free_path(path
);
2881 btrfs_end_transaction(trans
, root
);
2882 root
->fs_info
->enospc_unlink
= 0;
2883 return ERR_PTR(err
);
2886 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
2890 static void __unlink_end_trans(struct btrfs_trans_handle
*trans
,
2891 struct btrfs_root
*root
)
2893 if (trans
->block_rsv
== &root
->fs_info
->global_block_rsv
) {
2894 BUG_ON(!root
->fs_info
->enospc_unlink
);
2895 root
->fs_info
->enospc_unlink
= 0;
2897 btrfs_end_transaction_throttle(trans
, root
);
2900 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
2902 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2903 struct btrfs_trans_handle
*trans
;
2904 struct inode
*inode
= dentry
->d_inode
;
2906 unsigned long nr
= 0;
2908 trans
= __unlink_start_trans(dir
, dentry
);
2910 return PTR_ERR(trans
);
2912 btrfs_set_trans_block_group(trans
, dir
);
2914 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
2916 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
2917 dentry
->d_name
.name
, dentry
->d_name
.len
);
2920 if (inode
->i_nlink
== 0) {
2921 ret
= btrfs_orphan_add(trans
, inode
);
2925 nr
= trans
->blocks_used
;
2926 __unlink_end_trans(trans
, root
);
2927 btrfs_btree_balance_dirty(root
, nr
);
2931 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
2932 struct btrfs_root
*root
,
2933 struct inode
*dir
, u64 objectid
,
2934 const char *name
, int name_len
)
2936 struct btrfs_path
*path
;
2937 struct extent_buffer
*leaf
;
2938 struct btrfs_dir_item
*di
;
2939 struct btrfs_key key
;
2943 path
= btrfs_alloc_path();
2947 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
2948 name
, name_len
, -1);
2949 BUG_ON(!di
|| IS_ERR(di
));
2951 leaf
= path
->nodes
[0];
2952 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2953 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
2954 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2956 btrfs_release_path(root
, path
);
2958 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
2959 objectid
, root
->root_key
.objectid
,
2960 dir
->i_ino
, &index
, name
, name_len
);
2962 BUG_ON(ret
!= -ENOENT
);
2963 di
= btrfs_search_dir_index_item(root
, path
, dir
->i_ino
,
2965 BUG_ON(!di
|| IS_ERR(di
));
2967 leaf
= path
->nodes
[0];
2968 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2969 btrfs_release_path(root
, path
);
2973 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
2974 index
, name
, name_len
, -1);
2975 BUG_ON(!di
|| IS_ERR(di
));
2977 leaf
= path
->nodes
[0];
2978 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2979 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
2980 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2982 btrfs_release_path(root
, path
);
2984 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
2985 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
2986 ret
= btrfs_update_inode(trans
, root
, dir
);
2989 btrfs_free_path(path
);
2993 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2995 struct inode
*inode
= dentry
->d_inode
;
2997 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2998 struct btrfs_trans_handle
*trans
;
2999 unsigned long nr
= 0;
3001 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
||
3002 inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)
3005 trans
= __unlink_start_trans(dir
, dentry
);
3007 return PTR_ERR(trans
);
3009 btrfs_set_trans_block_group(trans
, dir
);
3011 if (unlikely(inode
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
3012 err
= btrfs_unlink_subvol(trans
, root
, dir
,
3013 BTRFS_I(inode
)->location
.objectid
,
3014 dentry
->d_name
.name
,
3015 dentry
->d_name
.len
);
3019 err
= btrfs_orphan_add(trans
, inode
);
3023 /* now the directory is empty */
3024 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3025 dentry
->d_name
.name
, dentry
->d_name
.len
);
3027 btrfs_i_size_write(inode
, 0);
3029 nr
= trans
->blocks_used
;
3030 __unlink_end_trans(trans
, root
);
3031 btrfs_btree_balance_dirty(root
, nr
);
3038 * when truncating bytes in a file, it is possible to avoid reading
3039 * the leaves that contain only checksum items. This can be the
3040 * majority of the IO required to delete a large file, but it must
3041 * be done carefully.
3043 * The keys in the level just above the leaves are checked to make sure
3044 * the lowest key in a given leaf is a csum key, and starts at an offset
3045 * after the new size.
3047 * Then the key for the next leaf is checked to make sure it also has
3048 * a checksum item for the same file. If it does, we know our target leaf
3049 * contains only checksum items, and it can be safely freed without reading
3052 * This is just an optimization targeted at large files. It may do
3053 * nothing. It will return 0 unless things went badly.
3055 static noinline
int drop_csum_leaves(struct btrfs_trans_handle
*trans
,
3056 struct btrfs_root
*root
,
3057 struct btrfs_path
*path
,
3058 struct inode
*inode
, u64 new_size
)
3060 struct btrfs_key key
;
3063 struct btrfs_key found_key
;
3064 struct btrfs_key other_key
;
3065 struct btrfs_leaf_ref
*ref
;
3069 path
->lowest_level
= 1;
3070 key
.objectid
= inode
->i_ino
;
3071 key
.type
= BTRFS_CSUM_ITEM_KEY
;
3072 key
.offset
= new_size
;
3074 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3078 if (path
->nodes
[1] == NULL
) {
3083 btrfs_node_key_to_cpu(path
->nodes
[1], &found_key
, path
->slots
[1]);
3084 nritems
= btrfs_header_nritems(path
->nodes
[1]);
3089 if (path
->slots
[1] >= nritems
)
3092 /* did we find a key greater than anything we want to delete? */
3093 if (found_key
.objectid
> inode
->i_ino
||
3094 (found_key
.objectid
== inode
->i_ino
&& found_key
.type
> key
.type
))
3097 /* we check the next key in the node to make sure the leave contains
3098 * only checksum items. This comparison doesn't work if our
3099 * leaf is the last one in the node
3101 if (path
->slots
[1] + 1 >= nritems
) {
3103 /* search forward from the last key in the node, this
3104 * will bring us into the next node in the tree
3106 btrfs_node_key_to_cpu(path
->nodes
[1], &found_key
, nritems
- 1);
3108 /* unlikely, but we inc below, so check to be safe */
3109 if (found_key
.offset
== (u64
)-1)
3112 /* search_forward needs a path with locks held, do the
3113 * search again for the original key. It is possible
3114 * this will race with a balance and return a path that
3115 * we could modify, but this drop is just an optimization
3116 * and is allowed to miss some leaves.
3118 btrfs_release_path(root
, path
);
3121 /* setup a max key for search_forward */
3122 other_key
.offset
= (u64
)-1;
3123 other_key
.type
= key
.type
;
3124 other_key
.objectid
= key
.objectid
;
3126 path
->keep_locks
= 1;
3127 ret
= btrfs_search_forward(root
, &found_key
, &other_key
,
3129 path
->keep_locks
= 0;
3130 if (ret
|| found_key
.objectid
!= key
.objectid
||
3131 found_key
.type
!= key
.type
) {
3136 key
.offset
= found_key
.offset
;
3137 btrfs_release_path(root
, path
);
3142 /* we know there's one more slot after us in the tree,
3143 * read that key so we can verify it is also a checksum item
3145 btrfs_node_key_to_cpu(path
->nodes
[1], &other_key
, path
->slots
[1] + 1);
3147 if (found_key
.objectid
< inode
->i_ino
)
3150 if (found_key
.type
!= key
.type
|| found_key
.offset
< new_size
)
3154 * if the key for the next leaf isn't a csum key from this objectid,
3155 * we can't be sure there aren't good items inside this leaf.
3158 if (other_key
.objectid
!= inode
->i_ino
|| other_key
.type
!= key
.type
)
3161 leaf_start
= btrfs_node_blockptr(path
->nodes
[1], path
->slots
[1]);
3162 leaf_gen
= btrfs_node_ptr_generation(path
->nodes
[1], path
->slots
[1]);
3164 * it is safe to delete this leaf, it contains only
3165 * csum items from this inode at an offset >= new_size
3167 ret
= btrfs_del_leaf(trans
, root
, path
, leaf_start
);
3170 if (root
->ref_cows
&& leaf_gen
< trans
->transid
) {
3171 ref
= btrfs_alloc_leaf_ref(root
, 0);
3173 ref
->root_gen
= root
->root_key
.offset
;
3174 ref
->bytenr
= leaf_start
;
3176 ref
->generation
= leaf_gen
;
3179 btrfs_sort_leaf_ref(ref
);
3181 ret
= btrfs_add_leaf_ref(root
, ref
, 0);
3183 btrfs_free_leaf_ref(root
, ref
);
3189 btrfs_release_path(root
, path
);
3191 if (other_key
.objectid
== inode
->i_ino
&&
3192 other_key
.type
== key
.type
&& other_key
.offset
> key
.offset
) {
3193 key
.offset
= other_key
.offset
;
3199 /* fixup any changes we've made to the path */
3200 path
->lowest_level
= 0;
3201 path
->keep_locks
= 0;
3202 btrfs_release_path(root
, path
);
3209 * this can truncate away extent items, csum items and directory items.
3210 * It starts at a high offset and removes keys until it can't find
3211 * any higher than new_size
3213 * csum items that cross the new i_size are truncated to the new size
3216 * min_type is the minimum key type to truncate down to. If set to 0, this
3217 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3219 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
3220 struct btrfs_root
*root
,
3221 struct inode
*inode
,
3222 u64 new_size
, u32 min_type
)
3224 struct btrfs_path
*path
;
3225 struct extent_buffer
*leaf
;
3226 struct btrfs_file_extent_item
*fi
;
3227 struct btrfs_key key
;
3228 struct btrfs_key found_key
;
3229 u64 extent_start
= 0;
3230 u64 extent_num_bytes
= 0;
3231 u64 extent_offset
= 0;
3233 u64 mask
= root
->sectorsize
- 1;
3234 u32 found_type
= (u8
)-1;
3237 int pending_del_nr
= 0;
3238 int pending_del_slot
= 0;
3239 int extent_type
= -1;
3244 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
3246 if (root
->ref_cows
|| root
== root
->fs_info
->tree_root
)
3247 btrfs_drop_extent_cache(inode
, new_size
& (~mask
), (u64
)-1, 0);
3249 path
= btrfs_alloc_path();
3253 key
.objectid
= inode
->i_ino
;
3254 key
.offset
= (u64
)-1;
3258 path
->leave_spinning
= 1;
3259 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3266 /* there are no items in the tree for us to truncate, we're
3269 if (path
->slots
[0] == 0)
3276 leaf
= path
->nodes
[0];
3277 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3278 found_type
= btrfs_key_type(&found_key
);
3281 if (found_key
.objectid
!= inode
->i_ino
)
3284 if (found_type
< min_type
)
3287 item_end
= found_key
.offset
;
3288 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
3289 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3290 struct btrfs_file_extent_item
);
3291 extent_type
= btrfs_file_extent_type(leaf
, fi
);
3292 encoding
= btrfs_file_extent_compression(leaf
, fi
);
3293 encoding
|= btrfs_file_extent_encryption(leaf
, fi
);
3294 encoding
|= btrfs_file_extent_other_encoding(leaf
, fi
);
3296 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3298 btrfs_file_extent_num_bytes(leaf
, fi
);
3299 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3300 item_end
+= btrfs_file_extent_inline_len(leaf
,
3305 if (found_type
> min_type
) {
3308 if (item_end
< new_size
)
3310 if (found_key
.offset
>= new_size
)
3316 /* FIXME, shrink the extent if the ref count is only 1 */
3317 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
3320 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3322 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
3323 if (!del_item
&& !encoding
) {
3324 u64 orig_num_bytes
=
3325 btrfs_file_extent_num_bytes(leaf
, fi
);
3326 extent_num_bytes
= new_size
-
3327 found_key
.offset
+ root
->sectorsize
- 1;
3328 extent_num_bytes
= extent_num_bytes
&
3329 ~((u64
)root
->sectorsize
- 1);
3330 btrfs_set_file_extent_num_bytes(leaf
, fi
,
3332 num_dec
= (orig_num_bytes
-
3334 if (root
->ref_cows
&& extent_start
!= 0)
3335 inode_sub_bytes(inode
, num_dec
);
3336 btrfs_mark_buffer_dirty(leaf
);
3339 btrfs_file_extent_disk_num_bytes(leaf
,
3341 extent_offset
= found_key
.offset
-
3342 btrfs_file_extent_offset(leaf
, fi
);
3344 /* FIXME blocksize != 4096 */
3345 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
3346 if (extent_start
!= 0) {
3349 inode_sub_bytes(inode
, num_dec
);
3352 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3354 * we can't truncate inline items that have had
3358 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
3359 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
3360 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
3361 u32 size
= new_size
- found_key
.offset
;
3363 if (root
->ref_cows
) {
3364 inode_sub_bytes(inode
, item_end
+ 1 -
3368 btrfs_file_extent_calc_inline_size(size
);
3369 ret
= btrfs_truncate_item(trans
, root
, path
,
3372 } else if (root
->ref_cows
) {
3373 inode_sub_bytes(inode
, item_end
+ 1 -
3379 if (!pending_del_nr
) {
3380 /* no pending yet, add ourselves */
3381 pending_del_slot
= path
->slots
[0];
3383 } else if (pending_del_nr
&&
3384 path
->slots
[0] + 1 == pending_del_slot
) {
3385 /* hop on the pending chunk */
3387 pending_del_slot
= path
->slots
[0];
3394 if (found_extent
&& (root
->ref_cows
||
3395 root
== root
->fs_info
->tree_root
)) {
3396 btrfs_set_path_blocking(path
);
3397 ret
= btrfs_free_extent(trans
, root
, extent_start
,
3398 extent_num_bytes
, 0,
3399 btrfs_header_owner(leaf
),
3400 inode
->i_ino
, extent_offset
);
3404 if (found_type
== BTRFS_INODE_ITEM_KEY
)
3407 if (path
->slots
[0] == 0 ||
3408 path
->slots
[0] != pending_del_slot
) {
3409 if (root
->ref_cows
) {
3413 if (pending_del_nr
) {
3414 ret
= btrfs_del_items(trans
, root
, path
,
3420 btrfs_release_path(root
, path
);
3427 if (pending_del_nr
) {
3428 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
3432 btrfs_free_path(path
);
3437 * taken from block_truncate_page, but does cow as it zeros out
3438 * any bytes left in the last page in the file.
3440 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
3442 struct inode
*inode
= mapping
->host
;
3443 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3444 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3445 struct btrfs_ordered_extent
*ordered
;
3446 struct extent_state
*cached_state
= NULL
;
3448 u32 blocksize
= root
->sectorsize
;
3449 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
3450 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
3456 if ((offset
& (blocksize
- 1)) == 0)
3458 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
3464 page
= grab_cache_page(mapping
, index
);
3466 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
3470 page_start
= page_offset(page
);
3471 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3473 if (!PageUptodate(page
)) {
3474 ret
= btrfs_readpage(NULL
, page
);
3476 if (page
->mapping
!= mapping
) {
3478 page_cache_release(page
);
3481 if (!PageUptodate(page
)) {
3486 wait_on_page_writeback(page
);
3488 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
,
3490 set_page_extent_mapped(page
);
3492 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
3494 unlock_extent_cached(io_tree
, page_start
, page_end
,
3495 &cached_state
, GFP_NOFS
);
3497 page_cache_release(page
);
3498 btrfs_start_ordered_extent(inode
, ordered
, 1);
3499 btrfs_put_ordered_extent(ordered
);
3503 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
3504 EXTENT_DIRTY
| EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
,
3505 0, 0, &cached_state
, GFP_NOFS
);
3507 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
3510 unlock_extent_cached(io_tree
, page_start
, page_end
,
3511 &cached_state
, GFP_NOFS
);
3516 if (offset
!= PAGE_CACHE_SIZE
) {
3518 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
3519 flush_dcache_page(page
);
3522 ClearPageChecked(page
);
3523 set_page_dirty(page
);
3524 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
3529 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
3531 page_cache_release(page
);
3536 int btrfs_cont_expand(struct inode
*inode
, loff_t size
)
3538 struct btrfs_trans_handle
*trans
;
3539 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3540 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3541 struct extent_map
*em
= NULL
;
3542 struct extent_state
*cached_state
= NULL
;
3543 u64 mask
= root
->sectorsize
- 1;
3544 u64 hole_start
= (inode
->i_size
+ mask
) & ~mask
;
3545 u64 block_end
= (size
+ mask
) & ~mask
;
3551 if (size
<= hole_start
)
3555 struct btrfs_ordered_extent
*ordered
;
3556 btrfs_wait_ordered_range(inode
, hole_start
,
3557 block_end
- hole_start
);
3558 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
3559 &cached_state
, GFP_NOFS
);
3560 ordered
= btrfs_lookup_ordered_extent(inode
, hole_start
);
3563 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
3564 &cached_state
, GFP_NOFS
);
3565 btrfs_put_ordered_extent(ordered
);
3568 cur_offset
= hole_start
;
3570 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
3571 block_end
- cur_offset
, 0);
3572 BUG_ON(IS_ERR(em
) || !em
);
3573 last_byte
= min(extent_map_end(em
), block_end
);
3574 last_byte
= (last_byte
+ mask
) & ~mask
;
3575 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
3577 hole_size
= last_byte
- cur_offset
;
3579 trans
= btrfs_start_transaction(root
, 2);
3580 if (IS_ERR(trans
)) {
3581 err
= PTR_ERR(trans
);
3584 btrfs_set_trans_block_group(trans
, inode
);
3586 err
= btrfs_drop_extents(trans
, inode
, cur_offset
,
3587 cur_offset
+ hole_size
,
3591 err
= btrfs_insert_file_extent(trans
, root
,
3592 inode
->i_ino
, cur_offset
, 0,
3593 0, hole_size
, 0, hole_size
,
3597 btrfs_drop_extent_cache(inode
, hole_start
,
3600 btrfs_end_transaction(trans
, root
);
3602 free_extent_map(em
);
3604 cur_offset
= last_byte
;
3605 if (cur_offset
>= block_end
)
3609 free_extent_map(em
);
3610 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
3615 static int btrfs_setattr_size(struct inode
*inode
, struct iattr
*attr
)
3617 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3618 struct btrfs_trans_handle
*trans
;
3622 if (attr
->ia_size
== inode
->i_size
)
3625 if (attr
->ia_size
> inode
->i_size
) {
3626 unsigned long limit
;
3627 limit
= current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
3628 if (attr
->ia_size
> inode
->i_sb
->s_maxbytes
)
3630 if (limit
!= RLIM_INFINITY
&& attr
->ia_size
> limit
) {
3631 send_sig(SIGXFSZ
, current
, 0);
3636 trans
= btrfs_start_transaction(root
, 5);
3638 return PTR_ERR(trans
);
3640 btrfs_set_trans_block_group(trans
, inode
);
3642 ret
= btrfs_orphan_add(trans
, inode
);
3645 nr
= trans
->blocks_used
;
3646 btrfs_end_transaction(trans
, root
);
3647 btrfs_btree_balance_dirty(root
, nr
);
3649 if (attr
->ia_size
> inode
->i_size
) {
3650 ret
= btrfs_cont_expand(inode
, attr
->ia_size
);
3652 btrfs_truncate(inode
);
3656 i_size_write(inode
, attr
->ia_size
);
3657 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
3659 trans
= btrfs_start_transaction(root
, 0);
3660 BUG_ON(IS_ERR(trans
));
3661 btrfs_set_trans_block_group(trans
, inode
);
3662 trans
->block_rsv
= root
->orphan_block_rsv
;
3663 BUG_ON(!trans
->block_rsv
);
3665 ret
= btrfs_update_inode(trans
, root
, inode
);
3667 if (inode
->i_nlink
> 0) {
3668 ret
= btrfs_orphan_del(trans
, inode
);
3671 nr
= trans
->blocks_used
;
3672 btrfs_end_transaction(trans
, root
);
3673 btrfs_btree_balance_dirty(root
, nr
);
3678 * We're truncating a file that used to have good data down to
3679 * zero. Make sure it gets into the ordered flush list so that
3680 * any new writes get down to disk quickly.
3682 if (attr
->ia_size
== 0)
3683 BTRFS_I(inode
)->ordered_data_close
= 1;
3685 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3686 ret
= vmtruncate(inode
, attr
->ia_size
);
3692 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
3694 struct inode
*inode
= dentry
->d_inode
;
3695 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3698 if (btrfs_root_readonly(root
))
3701 err
= inode_change_ok(inode
, attr
);
3705 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
3706 err
= btrfs_setattr_size(inode
, attr
);
3711 if (attr
->ia_valid
) {
3712 setattr_copy(inode
, attr
);
3713 mark_inode_dirty(inode
);
3715 if (attr
->ia_valid
& ATTR_MODE
)
3716 err
= btrfs_acl_chmod(inode
);
3722 void btrfs_evict_inode(struct inode
*inode
)
3724 struct btrfs_trans_handle
*trans
;
3725 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3729 truncate_inode_pages(&inode
->i_data
, 0);
3730 if (inode
->i_nlink
&& (btrfs_root_refs(&root
->root_item
) != 0 ||
3731 root
== root
->fs_info
->tree_root
))
3734 if (is_bad_inode(inode
)) {
3735 btrfs_orphan_del(NULL
, inode
);
3738 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
3739 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
3741 if (root
->fs_info
->log_root_recovering
) {
3742 BUG_ON(!list_empty(&BTRFS_I(inode
)->i_orphan
));
3746 if (inode
->i_nlink
> 0) {
3747 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0);
3751 btrfs_i_size_write(inode
, 0);
3754 trans
= btrfs_start_transaction(root
, 0);
3755 BUG_ON(IS_ERR(trans
));
3756 btrfs_set_trans_block_group(trans
, inode
);
3757 trans
->block_rsv
= root
->orphan_block_rsv
;
3759 ret
= btrfs_block_rsv_check(trans
, root
,
3760 root
->orphan_block_rsv
, 0, 5);
3762 BUG_ON(ret
!= -EAGAIN
);
3763 ret
= btrfs_commit_transaction(trans
, root
);
3768 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
3772 nr
= trans
->blocks_used
;
3773 btrfs_end_transaction(trans
, root
);
3775 btrfs_btree_balance_dirty(root
, nr
);
3780 ret
= btrfs_orphan_del(trans
, inode
);
3784 nr
= trans
->blocks_used
;
3785 btrfs_end_transaction(trans
, root
);
3786 btrfs_btree_balance_dirty(root
, nr
);
3788 end_writeback(inode
);
3793 * this returns the key found in the dir entry in the location pointer.
3794 * If no dir entries were found, location->objectid is 0.
3796 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
3797 struct btrfs_key
*location
)
3799 const char *name
= dentry
->d_name
.name
;
3800 int namelen
= dentry
->d_name
.len
;
3801 struct btrfs_dir_item
*di
;
3802 struct btrfs_path
*path
;
3803 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3806 path
= btrfs_alloc_path();
3809 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
3814 if (!di
|| IS_ERR(di
))
3817 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
3819 btrfs_free_path(path
);
3822 location
->objectid
= 0;
3827 * when we hit a tree root in a directory, the btrfs part of the inode
3828 * needs to be changed to reflect the root directory of the tree root. This
3829 * is kind of like crossing a mount point.
3831 static int fixup_tree_root_location(struct btrfs_root
*root
,
3833 struct dentry
*dentry
,
3834 struct btrfs_key
*location
,
3835 struct btrfs_root
**sub_root
)
3837 struct btrfs_path
*path
;
3838 struct btrfs_root
*new_root
;
3839 struct btrfs_root_ref
*ref
;
3840 struct extent_buffer
*leaf
;
3844 path
= btrfs_alloc_path();
3851 ret
= btrfs_find_root_ref(root
->fs_info
->tree_root
, path
,
3852 BTRFS_I(dir
)->root
->root_key
.objectid
,
3853 location
->objectid
);
3860 leaf
= path
->nodes
[0];
3861 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
3862 if (btrfs_root_ref_dirid(leaf
, ref
) != dir
->i_ino
||
3863 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
3866 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
3867 (unsigned long)(ref
+ 1),
3868 dentry
->d_name
.len
);
3872 btrfs_release_path(root
->fs_info
->tree_root
, path
);
3874 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
3875 if (IS_ERR(new_root
)) {
3876 err
= PTR_ERR(new_root
);
3880 if (btrfs_root_refs(&new_root
->root_item
) == 0) {
3885 *sub_root
= new_root
;
3886 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
3887 location
->type
= BTRFS_INODE_ITEM_KEY
;
3888 location
->offset
= 0;
3891 btrfs_free_path(path
);
3895 static void inode_tree_add(struct inode
*inode
)
3897 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3898 struct btrfs_inode
*entry
;
3900 struct rb_node
*parent
;
3902 p
= &root
->inode_tree
.rb_node
;
3905 if (hlist_unhashed(&inode
->i_hash
))
3908 spin_lock(&root
->inode_lock
);
3911 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
3913 if (inode
->i_ino
< entry
->vfs_inode
.i_ino
)
3914 p
= &parent
->rb_left
;
3915 else if (inode
->i_ino
> entry
->vfs_inode
.i_ino
)
3916 p
= &parent
->rb_right
;
3918 WARN_ON(!(entry
->vfs_inode
.i_state
&
3919 (I_WILL_FREE
| I_FREEING
)));
3920 rb_erase(parent
, &root
->inode_tree
);
3921 RB_CLEAR_NODE(parent
);
3922 spin_unlock(&root
->inode_lock
);
3926 rb_link_node(&BTRFS_I(inode
)->rb_node
, parent
, p
);
3927 rb_insert_color(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
3928 spin_unlock(&root
->inode_lock
);
3931 static void inode_tree_del(struct inode
*inode
)
3933 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3936 spin_lock(&root
->inode_lock
);
3937 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
3938 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
3939 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
3940 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
3942 spin_unlock(&root
->inode_lock
);
3945 * Free space cache has inodes in the tree root, but the tree root has a
3946 * root_refs of 0, so this could end up dropping the tree root as a
3947 * snapshot, so we need the extra !root->fs_info->tree_root check to
3948 * make sure we don't drop it.
3950 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0 &&
3951 root
!= root
->fs_info
->tree_root
) {
3952 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
3953 spin_lock(&root
->inode_lock
);
3954 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
3955 spin_unlock(&root
->inode_lock
);
3957 btrfs_add_dead_root(root
);
3961 int btrfs_invalidate_inodes(struct btrfs_root
*root
)
3963 struct rb_node
*node
;
3964 struct rb_node
*prev
;
3965 struct btrfs_inode
*entry
;
3966 struct inode
*inode
;
3969 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
3971 spin_lock(&root
->inode_lock
);
3973 node
= root
->inode_tree
.rb_node
;
3977 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
3979 if (objectid
< entry
->vfs_inode
.i_ino
)
3980 node
= node
->rb_left
;
3981 else if (objectid
> entry
->vfs_inode
.i_ino
)
3982 node
= node
->rb_right
;
3988 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
3989 if (objectid
<= entry
->vfs_inode
.i_ino
) {
3993 prev
= rb_next(prev
);
3997 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
3998 objectid
= entry
->vfs_inode
.i_ino
+ 1;
3999 inode
= igrab(&entry
->vfs_inode
);
4001 spin_unlock(&root
->inode_lock
);
4002 if (atomic_read(&inode
->i_count
) > 1)
4003 d_prune_aliases(inode
);
4005 * btrfs_drop_inode will have it removed from
4006 * the inode cache when its usage count
4011 spin_lock(&root
->inode_lock
);
4015 if (cond_resched_lock(&root
->inode_lock
))
4018 node
= rb_next(node
);
4020 spin_unlock(&root
->inode_lock
);
4024 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
4026 struct btrfs_iget_args
*args
= p
;
4027 inode
->i_ino
= args
->ino
;
4028 BTRFS_I(inode
)->root
= args
->root
;
4029 btrfs_set_inode_space_info(args
->root
, inode
);
4033 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
4035 struct btrfs_iget_args
*args
= opaque
;
4036 return args
->ino
== inode
->i_ino
&&
4037 args
->root
== BTRFS_I(inode
)->root
;
4040 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
4042 struct btrfs_root
*root
)
4044 struct inode
*inode
;
4045 struct btrfs_iget_args args
;
4046 args
.ino
= objectid
;
4049 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
4050 btrfs_init_locked_inode
,
4055 /* Get an inode object given its location and corresponding root.
4056 * Returns in *is_new if the inode was read from disk
4058 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
4059 struct btrfs_root
*root
, int *new)
4061 struct inode
*inode
;
4063 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
4065 return ERR_PTR(-ENOMEM
);
4067 if (inode
->i_state
& I_NEW
) {
4068 BTRFS_I(inode
)->root
= root
;
4069 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
4070 btrfs_read_locked_inode(inode
);
4072 inode_tree_add(inode
);
4073 unlock_new_inode(inode
);
4081 static struct inode
*new_simple_dir(struct super_block
*s
,
4082 struct btrfs_key
*key
,
4083 struct btrfs_root
*root
)
4085 struct inode
*inode
= new_inode(s
);
4088 return ERR_PTR(-ENOMEM
);
4090 BTRFS_I(inode
)->root
= root
;
4091 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
4092 BTRFS_I(inode
)->dummy_inode
= 1;
4094 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
4095 inode
->i_op
= &simple_dir_inode_operations
;
4096 inode
->i_fop
= &simple_dir_operations
;
4097 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
4098 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4103 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
4105 struct inode
*inode
;
4106 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4107 struct btrfs_root
*sub_root
= root
;
4108 struct btrfs_key location
;
4112 dentry
->d_op
= &btrfs_dentry_operations
;
4114 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
4115 return ERR_PTR(-ENAMETOOLONG
);
4117 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
4120 return ERR_PTR(ret
);
4122 if (location
.objectid
== 0)
4125 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
4126 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
4130 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
4132 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
4133 ret
= fixup_tree_root_location(root
, dir
, dentry
,
4134 &location
, &sub_root
);
4137 inode
= ERR_PTR(ret
);
4139 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
4141 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
4143 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
4145 if (!IS_ERR(inode
) && root
!= sub_root
) {
4146 down_read(&root
->fs_info
->cleanup_work_sem
);
4147 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
4148 btrfs_orphan_cleanup(sub_root
);
4149 up_read(&root
->fs_info
->cleanup_work_sem
);
4155 static int btrfs_dentry_delete(struct dentry
*dentry
)
4157 struct btrfs_root
*root
;
4159 if (!dentry
->d_inode
&& !IS_ROOT(dentry
))
4160 dentry
= dentry
->d_parent
;
4162 if (dentry
->d_inode
) {
4163 root
= BTRFS_I(dentry
->d_inode
)->root
;
4164 if (btrfs_root_refs(&root
->root_item
) == 0)
4170 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
4171 struct nameidata
*nd
)
4173 struct inode
*inode
;
4175 inode
= btrfs_lookup_dentry(dir
, dentry
);
4177 return ERR_CAST(inode
);
4179 return d_splice_alias(inode
, dentry
);
4182 static unsigned char btrfs_filetype_table
[] = {
4183 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
4186 static int btrfs_real_readdir(struct file
*filp
, void *dirent
,
4189 struct inode
*inode
= filp
->f_dentry
->d_inode
;
4190 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4191 struct btrfs_item
*item
;
4192 struct btrfs_dir_item
*di
;
4193 struct btrfs_key key
;
4194 struct btrfs_key found_key
;
4195 struct btrfs_path
*path
;
4198 struct extent_buffer
*leaf
;
4201 unsigned char d_type
;
4206 int key_type
= BTRFS_DIR_INDEX_KEY
;
4211 /* FIXME, use a real flag for deciding about the key type */
4212 if (root
->fs_info
->tree_root
== root
)
4213 key_type
= BTRFS_DIR_ITEM_KEY
;
4215 /* special case for "." */
4216 if (filp
->f_pos
== 0) {
4217 over
= filldir(dirent
, ".", 1,
4224 /* special case for .., just use the back ref */
4225 if (filp
->f_pos
== 1) {
4226 u64 pino
= parent_ino(filp
->f_path
.dentry
);
4227 over
= filldir(dirent
, "..", 2,
4233 path
= btrfs_alloc_path();
4236 btrfs_set_key_type(&key
, key_type
);
4237 key
.offset
= filp
->f_pos
;
4238 key
.objectid
= inode
->i_ino
;
4240 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4246 leaf
= path
->nodes
[0];
4247 nritems
= btrfs_header_nritems(leaf
);
4248 slot
= path
->slots
[0];
4249 if (advance
|| slot
>= nritems
) {
4250 if (slot
>= nritems
- 1) {
4251 ret
= btrfs_next_leaf(root
, path
);
4254 leaf
= path
->nodes
[0];
4255 nritems
= btrfs_header_nritems(leaf
);
4256 slot
= path
->slots
[0];
4264 item
= btrfs_item_nr(leaf
, slot
);
4265 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
4267 if (found_key
.objectid
!= key
.objectid
)
4269 if (btrfs_key_type(&found_key
) != key_type
)
4271 if (found_key
.offset
< filp
->f_pos
)
4274 filp
->f_pos
= found_key
.offset
;
4276 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
4278 di_total
= btrfs_item_size(leaf
, item
);
4280 while (di_cur
< di_total
) {
4281 struct btrfs_key location
;
4283 name_len
= btrfs_dir_name_len(leaf
, di
);
4284 if (name_len
<= sizeof(tmp_name
)) {
4285 name_ptr
= tmp_name
;
4287 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
4293 read_extent_buffer(leaf
, name_ptr
,
4294 (unsigned long)(di
+ 1), name_len
);
4296 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
4297 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
4299 /* is this a reference to our own snapshot? If so
4302 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
4303 location
.objectid
== root
->root_key
.objectid
) {
4307 over
= filldir(dirent
, name_ptr
, name_len
,
4308 found_key
.offset
, location
.objectid
,
4312 if (name_ptr
!= tmp_name
)
4317 di_len
= btrfs_dir_name_len(leaf
, di
) +
4318 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
4320 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
4324 /* Reached end of directory/root. Bump pos past the last item. */
4325 if (key_type
== BTRFS_DIR_INDEX_KEY
)
4327 * 32-bit glibc will use getdents64, but then strtol -
4328 * so the last number we can serve is this.
4330 filp
->f_pos
= 0x7fffffff;
4336 btrfs_free_path(path
);
4340 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
4342 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4343 struct btrfs_trans_handle
*trans
;
4345 bool nolock
= false;
4347 if (BTRFS_I(inode
)->dummy_inode
)
4351 nolock
= (root
->fs_info
->closing
&& root
== root
->fs_info
->tree_root
);
4353 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
4355 trans
= btrfs_join_transaction_nolock(root
, 1);
4357 trans
= btrfs_join_transaction(root
, 1);
4359 return PTR_ERR(trans
);
4360 btrfs_set_trans_block_group(trans
, inode
);
4362 ret
= btrfs_end_transaction_nolock(trans
, root
);
4364 ret
= btrfs_commit_transaction(trans
, root
);
4370 * This is somewhat expensive, updating the tree every time the
4371 * inode changes. But, it is most likely to find the inode in cache.
4372 * FIXME, needs more benchmarking...there are no reasons other than performance
4373 * to keep or drop this code.
4375 void btrfs_dirty_inode(struct inode
*inode
)
4377 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4378 struct btrfs_trans_handle
*trans
;
4381 if (BTRFS_I(inode
)->dummy_inode
)
4384 trans
= btrfs_join_transaction(root
, 1);
4385 BUG_ON(IS_ERR(trans
));
4386 btrfs_set_trans_block_group(trans
, inode
);
4388 ret
= btrfs_update_inode(trans
, root
, inode
);
4389 if (ret
&& ret
== -ENOSPC
) {
4390 /* whoops, lets try again with the full transaction */
4391 btrfs_end_transaction(trans
, root
);
4392 trans
= btrfs_start_transaction(root
, 1);
4393 if (IS_ERR(trans
)) {
4394 if (printk_ratelimit()) {
4395 printk(KERN_ERR
"btrfs: fail to "
4396 "dirty inode %lu error %ld\n",
4397 inode
->i_ino
, PTR_ERR(trans
));
4401 btrfs_set_trans_block_group(trans
, inode
);
4403 ret
= btrfs_update_inode(trans
, root
, inode
);
4405 if (printk_ratelimit()) {
4406 printk(KERN_ERR
"btrfs: fail to "
4407 "dirty inode %lu error %d\n",
4412 btrfs_end_transaction(trans
, root
);
4416 * find the highest existing sequence number in a directory
4417 * and then set the in-memory index_cnt variable to reflect
4418 * free sequence numbers
4420 static int btrfs_set_inode_index_count(struct inode
*inode
)
4422 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4423 struct btrfs_key key
, found_key
;
4424 struct btrfs_path
*path
;
4425 struct extent_buffer
*leaf
;
4428 key
.objectid
= inode
->i_ino
;
4429 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
4430 key
.offset
= (u64
)-1;
4432 path
= btrfs_alloc_path();
4436 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4439 /* FIXME: we should be able to handle this */
4445 * MAGIC NUMBER EXPLANATION:
4446 * since we search a directory based on f_pos we have to start at 2
4447 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4448 * else has to start at 2
4450 if (path
->slots
[0] == 0) {
4451 BTRFS_I(inode
)->index_cnt
= 2;
4457 leaf
= path
->nodes
[0];
4458 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4460 if (found_key
.objectid
!= inode
->i_ino
||
4461 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
4462 BTRFS_I(inode
)->index_cnt
= 2;
4466 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
4468 btrfs_free_path(path
);
4473 * helper to find a free sequence number in a given directory. This current
4474 * code is very simple, later versions will do smarter things in the btree
4476 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
4480 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
4481 ret
= btrfs_set_inode_index_count(dir
);
4486 *index
= BTRFS_I(dir
)->index_cnt
;
4487 BTRFS_I(dir
)->index_cnt
++;
4492 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
4493 struct btrfs_root
*root
,
4495 const char *name
, int name_len
,
4496 u64 ref_objectid
, u64 objectid
,
4497 u64 alloc_hint
, int mode
, u64
*index
)
4499 struct inode
*inode
;
4500 struct btrfs_inode_item
*inode_item
;
4501 struct btrfs_key
*location
;
4502 struct btrfs_path
*path
;
4503 struct btrfs_inode_ref
*ref
;
4504 struct btrfs_key key
[2];
4510 path
= btrfs_alloc_path();
4513 inode
= new_inode(root
->fs_info
->sb
);
4515 return ERR_PTR(-ENOMEM
);
4518 ret
= btrfs_set_inode_index(dir
, index
);
4521 return ERR_PTR(ret
);
4525 * index_cnt is ignored for everything but a dir,
4526 * btrfs_get_inode_index_count has an explanation for the magic
4529 BTRFS_I(inode
)->index_cnt
= 2;
4530 BTRFS_I(inode
)->root
= root
;
4531 BTRFS_I(inode
)->generation
= trans
->transid
;
4532 inode
->i_generation
= BTRFS_I(inode
)->generation
;
4533 btrfs_set_inode_space_info(root
, inode
);
4539 BTRFS_I(inode
)->block_group
=
4540 btrfs_find_block_group(root
, 0, alloc_hint
, owner
);
4542 key
[0].objectid
= objectid
;
4543 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
4546 key
[1].objectid
= objectid
;
4547 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
4548 key
[1].offset
= ref_objectid
;
4550 sizes
[0] = sizeof(struct btrfs_inode_item
);
4551 sizes
[1] = name_len
+ sizeof(*ref
);
4553 path
->leave_spinning
= 1;
4554 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
4558 inode_init_owner(inode
, dir
, mode
);
4559 inode
->i_ino
= objectid
;
4560 inode_set_bytes(inode
, 0);
4561 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4562 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4563 struct btrfs_inode_item
);
4564 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
4566 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
4567 struct btrfs_inode_ref
);
4568 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
4569 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
4570 ptr
= (unsigned long)(ref
+ 1);
4571 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
4573 btrfs_mark_buffer_dirty(path
->nodes
[0]);
4574 btrfs_free_path(path
);
4576 location
= &BTRFS_I(inode
)->location
;
4577 location
->objectid
= objectid
;
4578 location
->offset
= 0;
4579 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
4581 btrfs_inherit_iflags(inode
, dir
);
4583 if ((mode
& S_IFREG
)) {
4584 if (btrfs_test_opt(root
, NODATASUM
))
4585 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
4586 if (btrfs_test_opt(root
, NODATACOW
))
4587 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
;
4590 insert_inode_hash(inode
);
4591 inode_tree_add(inode
);
4595 BTRFS_I(dir
)->index_cnt
--;
4596 btrfs_free_path(path
);
4598 return ERR_PTR(ret
);
4601 static inline u8
btrfs_inode_type(struct inode
*inode
)
4603 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
4607 * utility function to add 'inode' into 'parent_inode' with
4608 * a give name and a given sequence number.
4609 * if 'add_backref' is true, also insert a backref from the
4610 * inode to the parent directory.
4612 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
4613 struct inode
*parent_inode
, struct inode
*inode
,
4614 const char *name
, int name_len
, int add_backref
, u64 index
)
4617 struct btrfs_key key
;
4618 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
4620 if (unlikely(inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4621 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
4623 key
.objectid
= inode
->i_ino
;
4624 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
4628 if (unlikely(inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4629 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
4630 key
.objectid
, root
->root_key
.objectid
,
4631 parent_inode
->i_ino
,
4632 index
, name
, name_len
);
4633 } else if (add_backref
) {
4634 ret
= btrfs_insert_inode_ref(trans
, root
,
4635 name
, name_len
, inode
->i_ino
,
4636 parent_inode
->i_ino
, index
);
4640 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
4641 parent_inode
->i_ino
, &key
,
4642 btrfs_inode_type(inode
), index
);
4645 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
4647 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
4648 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
4653 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
4654 struct inode
*dir
, struct dentry
*dentry
,
4655 struct inode
*inode
, int backref
, u64 index
)
4657 int err
= btrfs_add_link(trans
, dir
, inode
,
4658 dentry
->d_name
.name
, dentry
->d_name
.len
,
4661 d_instantiate(dentry
, inode
);
4669 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
4670 int mode
, dev_t rdev
)
4672 struct btrfs_trans_handle
*trans
;
4673 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4674 struct inode
*inode
= NULL
;
4678 unsigned long nr
= 0;
4681 if (!new_valid_dev(rdev
))
4684 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
4689 * 2 for inode item and ref
4691 * 1 for xattr if selinux is on
4693 trans
= btrfs_start_transaction(root
, 5);
4695 return PTR_ERR(trans
);
4697 btrfs_set_trans_block_group(trans
, dir
);
4699 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4700 dentry
->d_name
.len
, dir
->i_ino
, objectid
,
4701 BTRFS_I(dir
)->block_group
, mode
, &index
);
4702 err
= PTR_ERR(inode
);
4706 err
= btrfs_init_inode_security(trans
, inode
, dir
);
4712 btrfs_set_trans_block_group(trans
, inode
);
4713 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
4717 inode
->i_op
= &btrfs_special_inode_operations
;
4718 init_special_inode(inode
, inode
->i_mode
, rdev
);
4719 btrfs_update_inode(trans
, root
, inode
);
4721 btrfs_update_inode_block_group(trans
, inode
);
4722 btrfs_update_inode_block_group(trans
, dir
);
4724 nr
= trans
->blocks_used
;
4725 btrfs_end_transaction_throttle(trans
, root
);
4726 btrfs_btree_balance_dirty(root
, nr
);
4728 inode_dec_link_count(inode
);
4734 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
4735 int mode
, struct nameidata
*nd
)
4737 struct btrfs_trans_handle
*trans
;
4738 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4739 struct inode
*inode
= NULL
;
4742 unsigned long nr
= 0;
4746 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
4750 * 2 for inode item and ref
4752 * 1 for xattr if selinux is on
4754 trans
= btrfs_start_transaction(root
, 5);
4756 return PTR_ERR(trans
);
4758 btrfs_set_trans_block_group(trans
, dir
);
4760 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4761 dentry
->d_name
.len
, dir
->i_ino
, objectid
,
4762 BTRFS_I(dir
)->block_group
, mode
, &index
);
4763 err
= PTR_ERR(inode
);
4767 err
= btrfs_init_inode_security(trans
, inode
, dir
);
4773 btrfs_set_trans_block_group(trans
, inode
);
4774 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
4778 inode
->i_mapping
->a_ops
= &btrfs_aops
;
4779 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
4780 inode
->i_fop
= &btrfs_file_operations
;
4781 inode
->i_op
= &btrfs_file_inode_operations
;
4782 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
4784 btrfs_update_inode_block_group(trans
, inode
);
4785 btrfs_update_inode_block_group(trans
, dir
);
4787 nr
= trans
->blocks_used
;
4788 btrfs_end_transaction_throttle(trans
, root
);
4790 inode_dec_link_count(inode
);
4793 btrfs_btree_balance_dirty(root
, nr
);
4797 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
4798 struct dentry
*dentry
)
4800 struct btrfs_trans_handle
*trans
;
4801 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4802 struct inode
*inode
= old_dentry
->d_inode
;
4804 unsigned long nr
= 0;
4808 if (inode
->i_nlink
== 0)
4811 /* do not allow sys_link's with other subvols of the same device */
4812 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
4815 btrfs_inc_nlink(inode
);
4816 inode
->i_ctime
= CURRENT_TIME
;
4818 err
= btrfs_set_inode_index(dir
, &index
);
4823 * 1 item for inode ref
4824 * 2 items for dir items
4826 trans
= btrfs_start_transaction(root
, 3);
4827 if (IS_ERR(trans
)) {
4828 err
= PTR_ERR(trans
);
4832 btrfs_set_trans_block_group(trans
, dir
);
4833 atomic_inc(&inode
->i_count
);
4835 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
4840 struct dentry
*parent
= dget_parent(dentry
);
4841 btrfs_update_inode_block_group(trans
, dir
);
4842 err
= btrfs_update_inode(trans
, root
, inode
);
4844 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
4848 nr
= trans
->blocks_used
;
4849 btrfs_end_transaction_throttle(trans
, root
);
4852 inode_dec_link_count(inode
);
4855 btrfs_btree_balance_dirty(root
, nr
);
4859 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
4861 struct inode
*inode
= NULL
;
4862 struct btrfs_trans_handle
*trans
;
4863 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4865 int drop_on_err
= 0;
4868 unsigned long nr
= 1;
4870 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
4875 * 2 items for inode and ref
4876 * 2 items for dir items
4877 * 1 for xattr if selinux is on
4879 trans
= btrfs_start_transaction(root
, 5);
4881 return PTR_ERR(trans
);
4882 btrfs_set_trans_block_group(trans
, dir
);
4884 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4885 dentry
->d_name
.len
, dir
->i_ino
, objectid
,
4886 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
,
4888 if (IS_ERR(inode
)) {
4889 err
= PTR_ERR(inode
);
4895 err
= btrfs_init_inode_security(trans
, inode
, dir
);
4899 inode
->i_op
= &btrfs_dir_inode_operations
;
4900 inode
->i_fop
= &btrfs_dir_file_operations
;
4901 btrfs_set_trans_block_group(trans
, inode
);
4903 btrfs_i_size_write(inode
, 0);
4904 err
= btrfs_update_inode(trans
, root
, inode
);
4908 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
4909 dentry
->d_name
.len
, 0, index
);
4913 d_instantiate(dentry
, inode
);
4915 btrfs_update_inode_block_group(trans
, inode
);
4916 btrfs_update_inode_block_group(trans
, dir
);
4919 nr
= trans
->blocks_used
;
4920 btrfs_end_transaction_throttle(trans
, root
);
4923 btrfs_btree_balance_dirty(root
, nr
);
4927 /* helper for btfs_get_extent. Given an existing extent in the tree,
4928 * and an extent that you want to insert, deal with overlap and insert
4929 * the new extent into the tree.
4931 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
4932 struct extent_map
*existing
,
4933 struct extent_map
*em
,
4934 u64 map_start
, u64 map_len
)
4938 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
4939 start_diff
= map_start
- em
->start
;
4940 em
->start
= map_start
;
4942 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
4943 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
4944 em
->block_start
+= start_diff
;
4945 em
->block_len
-= start_diff
;
4947 return add_extent_mapping(em_tree
, em
);
4950 static noinline
int uncompress_inline(struct btrfs_path
*path
,
4951 struct inode
*inode
, struct page
*page
,
4952 size_t pg_offset
, u64 extent_offset
,
4953 struct btrfs_file_extent_item
*item
)
4956 struct extent_buffer
*leaf
= path
->nodes
[0];
4959 unsigned long inline_size
;
4963 WARN_ON(pg_offset
!= 0);
4964 compress_type
= btrfs_file_extent_compression(leaf
, item
);
4965 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
4966 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
4967 btrfs_item_nr(leaf
, path
->slots
[0]));
4968 tmp
= kmalloc(inline_size
, GFP_NOFS
);
4969 ptr
= btrfs_file_extent_inline_start(item
);
4971 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
4973 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
4974 ret
= btrfs_decompress(compress_type
, tmp
, page
,
4975 extent_offset
, inline_size
, max_size
);
4977 char *kaddr
= kmap_atomic(page
, KM_USER0
);
4978 unsigned long copy_size
= min_t(u64
,
4979 PAGE_CACHE_SIZE
- pg_offset
,
4980 max_size
- extent_offset
);
4981 memset(kaddr
+ pg_offset
, 0, copy_size
);
4982 kunmap_atomic(kaddr
, KM_USER0
);
4989 * a bit scary, this does extent mapping from logical file offset to the disk.
4990 * the ugly parts come from merging extents from the disk with the in-ram
4991 * representation. This gets more complex because of the data=ordered code,
4992 * where the in-ram extents might be locked pending data=ordered completion.
4994 * This also copies inline extents directly into the page.
4997 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
4998 size_t pg_offset
, u64 start
, u64 len
,
5004 u64 extent_start
= 0;
5006 u64 objectid
= inode
->i_ino
;
5008 struct btrfs_path
*path
= NULL
;
5009 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5010 struct btrfs_file_extent_item
*item
;
5011 struct extent_buffer
*leaf
;
5012 struct btrfs_key found_key
;
5013 struct extent_map
*em
= NULL
;
5014 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
5015 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
5016 struct btrfs_trans_handle
*trans
= NULL
;
5020 read_lock(&em_tree
->lock
);
5021 em
= lookup_extent_mapping(em_tree
, start
, len
);
5023 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5024 read_unlock(&em_tree
->lock
);
5027 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
5028 free_extent_map(em
);
5029 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
5030 free_extent_map(em
);
5034 em
= alloc_extent_map(GFP_NOFS
);
5039 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5040 em
->start
= EXTENT_MAP_HOLE
;
5041 em
->orig_start
= EXTENT_MAP_HOLE
;
5043 em
->block_len
= (u64
)-1;
5046 path
= btrfs_alloc_path();
5050 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
5051 objectid
, start
, trans
!= NULL
);
5058 if (path
->slots
[0] == 0)
5063 leaf
= path
->nodes
[0];
5064 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5065 struct btrfs_file_extent_item
);
5066 /* are we inside the extent that was found? */
5067 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5068 found_type
= btrfs_key_type(&found_key
);
5069 if (found_key
.objectid
!= objectid
||
5070 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
5074 found_type
= btrfs_file_extent_type(leaf
, item
);
5075 extent_start
= found_key
.offset
;
5076 compress_type
= btrfs_file_extent_compression(leaf
, item
);
5077 if (found_type
== BTRFS_FILE_EXTENT_REG
||
5078 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5079 extent_end
= extent_start
+
5080 btrfs_file_extent_num_bytes(leaf
, item
);
5081 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
5083 size
= btrfs_file_extent_inline_len(leaf
, item
);
5084 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
5085 ~((u64
)root
->sectorsize
- 1);
5088 if (start
>= extent_end
) {
5090 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
5091 ret
= btrfs_next_leaf(root
, path
);
5098 leaf
= path
->nodes
[0];
5100 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5101 if (found_key
.objectid
!= objectid
||
5102 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
5104 if (start
+ len
<= found_key
.offset
)
5107 em
->len
= found_key
.offset
- start
;
5111 if (found_type
== BTRFS_FILE_EXTENT_REG
||
5112 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5113 em
->start
= extent_start
;
5114 em
->len
= extent_end
- extent_start
;
5115 em
->orig_start
= extent_start
-
5116 btrfs_file_extent_offset(leaf
, item
);
5117 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
5119 em
->block_start
= EXTENT_MAP_HOLE
;
5122 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
5123 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
5124 em
->compress_type
= compress_type
;
5125 em
->block_start
= bytenr
;
5126 em
->block_len
= btrfs_file_extent_disk_num_bytes(leaf
,
5129 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
5130 em
->block_start
= bytenr
;
5131 em
->block_len
= em
->len
;
5132 if (found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
5133 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
5136 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
5140 size_t extent_offset
;
5143 em
->block_start
= EXTENT_MAP_INLINE
;
5144 if (!page
|| create
) {
5145 em
->start
= extent_start
;
5146 em
->len
= extent_end
- extent_start
;
5150 size
= btrfs_file_extent_inline_len(leaf
, item
);
5151 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
5152 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
5153 size
- extent_offset
);
5154 em
->start
= extent_start
+ extent_offset
;
5155 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
5156 ~((u64
)root
->sectorsize
- 1);
5157 em
->orig_start
= EXTENT_MAP_INLINE
;
5158 if (compress_type
) {
5159 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
5160 em
->compress_type
= compress_type
;
5162 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
5163 if (create
== 0 && !PageUptodate(page
)) {
5164 if (btrfs_file_extent_compression(leaf
, item
) !=
5165 BTRFS_COMPRESS_NONE
) {
5166 ret
= uncompress_inline(path
, inode
, page
,
5168 extent_offset
, item
);
5172 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
5174 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
5175 memset(map
+ pg_offset
+ copy_size
, 0,
5176 PAGE_CACHE_SIZE
- pg_offset
-
5181 flush_dcache_page(page
);
5182 } else if (create
&& PageUptodate(page
)) {
5186 free_extent_map(em
);
5188 btrfs_release_path(root
, path
);
5189 trans
= btrfs_join_transaction(root
, 1);
5191 return ERR_CAST(trans
);
5195 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
5198 btrfs_mark_buffer_dirty(leaf
);
5200 set_extent_uptodate(io_tree
, em
->start
,
5201 extent_map_end(em
) - 1, GFP_NOFS
);
5204 printk(KERN_ERR
"btrfs unknown found_type %d\n", found_type
);
5211 em
->block_start
= EXTENT_MAP_HOLE
;
5212 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
5214 btrfs_release_path(root
, path
);
5215 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
5216 printk(KERN_ERR
"Btrfs: bad extent! em: [%llu %llu] passed "
5217 "[%llu %llu]\n", (unsigned long long)em
->start
,
5218 (unsigned long long)em
->len
,
5219 (unsigned long long)start
,
5220 (unsigned long long)len
);
5226 write_lock(&em_tree
->lock
);
5227 ret
= add_extent_mapping(em_tree
, em
);
5228 /* it is possible that someone inserted the extent into the tree
5229 * while we had the lock dropped. It is also possible that
5230 * an overlapping map exists in the tree
5232 if (ret
== -EEXIST
) {
5233 struct extent_map
*existing
;
5237 existing
= lookup_extent_mapping(em_tree
, start
, len
);
5238 if (existing
&& (existing
->start
> start
||
5239 existing
->start
+ existing
->len
<= start
)) {
5240 free_extent_map(existing
);
5244 existing
= lookup_extent_mapping(em_tree
, em
->start
,
5247 err
= merge_extent_mapping(em_tree
, existing
,
5250 free_extent_map(existing
);
5252 free_extent_map(em
);
5257 free_extent_map(em
);
5261 free_extent_map(em
);
5266 write_unlock(&em_tree
->lock
);
5269 btrfs_free_path(path
);
5271 ret
= btrfs_end_transaction(trans
, root
);
5276 free_extent_map(em
);
5277 return ERR_PTR(err
);
5282 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
5285 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5286 struct btrfs_trans_handle
*trans
;
5287 struct extent_map
*em
;
5288 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
5289 struct btrfs_key ins
;
5293 btrfs_drop_extent_cache(inode
, start
, start
+ len
- 1, 0);
5295 trans
= btrfs_join_transaction(root
, 0);
5297 return ERR_CAST(trans
);
5299 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
5301 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
5302 ret
= btrfs_reserve_extent(trans
, root
, len
, root
->sectorsize
, 0,
5303 alloc_hint
, (u64
)-1, &ins
, 1);
5309 em
= alloc_extent_map(GFP_NOFS
);
5311 em
= ERR_PTR(-ENOMEM
);
5316 em
->orig_start
= em
->start
;
5317 em
->len
= ins
.offset
;
5319 em
->block_start
= ins
.objectid
;
5320 em
->block_len
= ins
.offset
;
5321 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5322 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
5325 write_lock(&em_tree
->lock
);
5326 ret
= add_extent_mapping(em_tree
, em
);
5327 write_unlock(&em_tree
->lock
);
5330 btrfs_drop_extent_cache(inode
, start
, start
+ em
->len
- 1, 0);
5333 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
5334 ins
.offset
, ins
.offset
, 0);
5336 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
5340 btrfs_end_transaction(trans
, root
);
5345 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5346 * block must be cow'd
5348 static noinline
int can_nocow_odirect(struct btrfs_trans_handle
*trans
,
5349 struct inode
*inode
, u64 offset
, u64 len
)
5351 struct btrfs_path
*path
;
5353 struct extent_buffer
*leaf
;
5354 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5355 struct btrfs_file_extent_item
*fi
;
5356 struct btrfs_key key
;
5364 path
= btrfs_alloc_path();
5368 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
5373 slot
= path
->slots
[0];
5376 /* can't find the item, must cow */
5383 leaf
= path
->nodes
[0];
5384 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
5385 if (key
.objectid
!= inode
->i_ino
||
5386 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
5387 /* not our file or wrong item type, must cow */
5391 if (key
.offset
> offset
) {
5392 /* Wrong offset, must cow */
5396 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
5397 found_type
= btrfs_file_extent_type(leaf
, fi
);
5398 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
5399 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
5400 /* not a regular extent, must cow */
5403 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
5404 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
5406 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
5407 if (extent_end
< offset
+ len
) {
5408 /* extent doesn't include our full range, must cow */
5412 if (btrfs_extent_readonly(root
, disk_bytenr
))
5416 * look for other files referencing this extent, if we
5417 * find any we must cow
5419 if (btrfs_cross_ref_exist(trans
, root
, inode
->i_ino
,
5420 key
.offset
- backref_offset
, disk_bytenr
))
5424 * adjust disk_bytenr and num_bytes to cover just the bytes
5425 * in this extent we are about to write. If there
5426 * are any csums in that range we have to cow in order
5427 * to keep the csums correct
5429 disk_bytenr
+= backref_offset
;
5430 disk_bytenr
+= offset
- key
.offset
;
5431 num_bytes
= min(offset
+ len
, extent_end
) - offset
;
5432 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
5435 * all of the above have passed, it is safe to overwrite this extent
5440 btrfs_free_path(path
);
5444 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
5445 struct buffer_head
*bh_result
, int create
)
5447 struct extent_map
*em
;
5448 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5449 u64 start
= iblock
<< inode
->i_blkbits
;
5450 u64 len
= bh_result
->b_size
;
5451 struct btrfs_trans_handle
*trans
;
5453 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
5458 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5459 * io. INLINE is special, and we could probably kludge it in here, but
5460 * it's still buffered so for safety lets just fall back to the generic
5463 * For COMPRESSED we _have_ to read the entire extent in so we can
5464 * decompress it, so there will be buffering required no matter what we
5465 * do, so go ahead and fallback to buffered.
5467 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5468 * to buffered IO. Don't blame me, this is the price we pay for using
5471 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
5472 em
->block_start
== EXTENT_MAP_INLINE
) {
5473 free_extent_map(em
);
5477 /* Just a good old fashioned hole, return */
5478 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
5479 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
5480 free_extent_map(em
);
5481 /* DIO will do one hole at a time, so just unlock a sector */
5482 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
,
5483 start
+ root
->sectorsize
- 1, GFP_NOFS
);
5488 * We don't allocate a new extent in the following cases
5490 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5492 * 2) The extent is marked as PREALLOC. We're good to go here and can
5493 * just use the extent.
5497 len
= em
->len
- (start
- em
->start
);
5501 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
5502 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
5503 em
->block_start
!= EXTENT_MAP_HOLE
)) {
5508 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
5509 type
= BTRFS_ORDERED_PREALLOC
;
5511 type
= BTRFS_ORDERED_NOCOW
;
5512 len
= min(len
, em
->len
- (start
- em
->start
));
5513 block_start
= em
->block_start
+ (start
- em
->start
);
5516 * we're not going to log anything, but we do need
5517 * to make sure the current transaction stays open
5518 * while we look for nocow cross refs
5520 trans
= btrfs_join_transaction(root
, 0);
5524 if (can_nocow_odirect(trans
, inode
, start
, len
) == 1) {
5525 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
5526 block_start
, len
, len
, type
);
5527 btrfs_end_transaction(trans
, root
);
5529 free_extent_map(em
);
5534 btrfs_end_transaction(trans
, root
);
5538 * this will cow the extent, reset the len in case we changed
5541 len
= bh_result
->b_size
;
5542 free_extent_map(em
);
5543 em
= btrfs_new_extent_direct(inode
, start
, len
);
5546 len
= min(len
, em
->len
- (start
- em
->start
));
5548 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
- 1,
5549 EXTENT_LOCKED
| EXTENT_DELALLOC
| EXTENT_DIRTY
, 1,
5552 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
5554 bh_result
->b_size
= len
;
5555 bh_result
->b_bdev
= em
->bdev
;
5556 set_buffer_mapped(bh_result
);
5557 if (create
&& !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
5558 set_buffer_new(bh_result
);
5560 free_extent_map(em
);
5565 struct btrfs_dio_private
{
5566 struct inode
*inode
;
5573 /* number of bios pending for this dio */
5574 atomic_t pending_bios
;
5579 struct bio
*orig_bio
;
5582 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
5584 struct btrfs_dio_private
*dip
= bio
->bi_private
;
5585 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
5586 struct bio_vec
*bvec
= bio
->bi_io_vec
;
5587 struct inode
*inode
= dip
->inode
;
5588 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5590 u32
*private = dip
->csums
;
5592 start
= dip
->logical_offset
;
5594 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
5595 struct page
*page
= bvec
->bv_page
;
5598 unsigned long flags
;
5600 local_irq_save(flags
);
5601 kaddr
= kmap_atomic(page
, KM_IRQ0
);
5602 csum
= btrfs_csum_data(root
, kaddr
+ bvec
->bv_offset
,
5603 csum
, bvec
->bv_len
);
5604 btrfs_csum_final(csum
, (char *)&csum
);
5605 kunmap_atomic(kaddr
, KM_IRQ0
);
5606 local_irq_restore(flags
);
5608 flush_dcache_page(bvec
->bv_page
);
5609 if (csum
!= *private) {
5610 printk(KERN_ERR
"btrfs csum failed ino %lu off"
5611 " %llu csum %u private %u\n",
5612 inode
->i_ino
, (unsigned long long)start
,
5618 start
+= bvec
->bv_len
;
5621 } while (bvec
<= bvec_end
);
5623 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
5624 dip
->logical_offset
+ dip
->bytes
- 1, GFP_NOFS
);
5625 bio
->bi_private
= dip
->private;
5629 dio_end_io(bio
, err
);
5632 static void btrfs_endio_direct_write(struct bio
*bio
, int err
)
5634 struct btrfs_dio_private
*dip
= bio
->bi_private
;
5635 struct inode
*inode
= dip
->inode
;
5636 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5637 struct btrfs_trans_handle
*trans
;
5638 struct btrfs_ordered_extent
*ordered
= NULL
;
5639 struct extent_state
*cached_state
= NULL
;
5640 u64 ordered_offset
= dip
->logical_offset
;
5641 u64 ordered_bytes
= dip
->bytes
;
5647 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
5655 trans
= btrfs_join_transaction(root
, 1);
5656 if (IS_ERR(trans
)) {
5660 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
5662 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
)) {
5663 ret
= btrfs_ordered_update_i_size(inode
, 0, ordered
);
5665 ret
= btrfs_update_inode(trans
, root
, inode
);
5670 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, ordered
->file_offset
,
5671 ordered
->file_offset
+ ordered
->len
- 1, 0,
5672 &cached_state
, GFP_NOFS
);
5674 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
)) {
5675 ret
= btrfs_mark_extent_written(trans
, inode
,
5676 ordered
->file_offset
,
5677 ordered
->file_offset
+
5684 ret
= insert_reserved_file_extent(trans
, inode
,
5685 ordered
->file_offset
,
5691 BTRFS_FILE_EXTENT_REG
);
5692 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
5693 ordered
->file_offset
, ordered
->len
);
5701 add_pending_csums(trans
, inode
, ordered
->file_offset
, &ordered
->list
);
5702 btrfs_ordered_update_i_size(inode
, 0, ordered
);
5703 btrfs_update_inode(trans
, root
, inode
);
5705 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, ordered
->file_offset
,
5706 ordered
->file_offset
+ ordered
->len
- 1,
5707 &cached_state
, GFP_NOFS
);
5709 btrfs_delalloc_release_metadata(inode
, ordered
->len
);
5710 btrfs_end_transaction(trans
, root
);
5711 ordered_offset
= ordered
->file_offset
+ ordered
->len
;
5712 btrfs_put_ordered_extent(ordered
);
5713 btrfs_put_ordered_extent(ordered
);
5717 * our bio might span multiple ordered extents. If we haven't
5718 * completed the accounting for the whole dio, go back and try again
5720 if (ordered_offset
< dip
->logical_offset
+ dip
->bytes
) {
5721 ordered_bytes
= dip
->logical_offset
+ dip
->bytes
-
5726 bio
->bi_private
= dip
->private;
5730 dio_end_io(bio
, err
);
5733 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
5734 struct bio
*bio
, int mirror_num
,
5735 unsigned long bio_flags
, u64 offset
)
5738 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5739 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
5744 static void btrfs_end_dio_bio(struct bio
*bio
, int err
)
5746 struct btrfs_dio_private
*dip
= bio
->bi_private
;
5749 printk(KERN_ERR
"btrfs direct IO failed ino %lu rw %lu "
5750 "sector %#Lx len %u err no %d\n",
5751 dip
->inode
->i_ino
, bio
->bi_rw
,
5752 (unsigned long long)bio
->bi_sector
, bio
->bi_size
, err
);
5756 * before atomic variable goto zero, we must make sure
5757 * dip->errors is perceived to be set.
5759 smp_mb__before_atomic_dec();
5762 /* if there are more bios still pending for this dio, just exit */
5763 if (!atomic_dec_and_test(&dip
->pending_bios
))
5767 bio_io_error(dip
->orig_bio
);
5769 set_bit(BIO_UPTODATE
, &dip
->orig_bio
->bi_flags
);
5770 bio_endio(dip
->orig_bio
, 0);
5776 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
5777 u64 first_sector
, gfp_t gfp_flags
)
5779 int nr_vecs
= bio_get_nr_vecs(bdev
);
5780 return btrfs_bio_alloc(bdev
, first_sector
, nr_vecs
, gfp_flags
);
5783 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
5784 int rw
, u64 file_offset
, int skip_sum
,
5787 int write
= rw
& REQ_WRITE
;
5788 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5792 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
5796 if (write
&& !skip_sum
) {
5797 ret
= btrfs_wq_submit_bio(root
->fs_info
,
5798 inode
, rw
, bio
, 0, 0,
5800 __btrfs_submit_bio_start_direct_io
,
5801 __btrfs_submit_bio_done
);
5803 } else if (!skip_sum
)
5804 btrfs_lookup_bio_sums_dio(root
, inode
, bio
,
5805 file_offset
, csums
);
5807 ret
= btrfs_map_bio(root
, rw
, bio
, 0, 1);
5813 static int btrfs_submit_direct_hook(int rw
, struct btrfs_dio_private
*dip
,
5816 struct inode
*inode
= dip
->inode
;
5817 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5818 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
5820 struct bio
*orig_bio
= dip
->orig_bio
;
5821 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
5822 u64 start_sector
= orig_bio
->bi_sector
;
5823 u64 file_offset
= dip
->logical_offset
;
5827 u32
*csums
= dip
->csums
;
5830 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
5833 bio
->bi_private
= dip
;
5834 bio
->bi_end_io
= btrfs_end_dio_bio
;
5835 atomic_inc(&dip
->pending_bios
);
5837 map_length
= orig_bio
->bi_size
;
5838 ret
= btrfs_map_block(map_tree
, READ
, start_sector
<< 9,
5839 &map_length
, NULL
, 0);
5845 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
5846 if (unlikely(map_length
< submit_len
+ bvec
->bv_len
||
5847 bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
5848 bvec
->bv_offset
) < bvec
->bv_len
)) {
5850 * inc the count before we submit the bio so
5851 * we know the end IO handler won't happen before
5852 * we inc the count. Otherwise, the dip might get freed
5853 * before we're done setting it up
5855 atomic_inc(&dip
->pending_bios
);
5856 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
,
5857 file_offset
, skip_sum
,
5861 atomic_dec(&dip
->pending_bios
);
5866 csums
= csums
+ nr_pages
;
5867 start_sector
+= submit_len
>> 9;
5868 file_offset
+= submit_len
;
5873 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
5874 start_sector
, GFP_NOFS
);
5877 bio
->bi_private
= dip
;
5878 bio
->bi_end_io
= btrfs_end_dio_bio
;
5880 map_length
= orig_bio
->bi_size
;
5881 ret
= btrfs_map_block(map_tree
, READ
, start_sector
<< 9,
5882 &map_length
, NULL
, 0);
5888 submit_len
+= bvec
->bv_len
;
5894 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
, file_offset
, skip_sum
,
5903 * before atomic variable goto zero, we must
5904 * make sure dip->errors is perceived to be set.
5906 smp_mb__before_atomic_dec();
5907 if (atomic_dec_and_test(&dip
->pending_bios
))
5908 bio_io_error(dip
->orig_bio
);
5910 /* bio_end_io() will handle error, so we needn't return it */
5914 static void btrfs_submit_direct(int rw
, struct bio
*bio
, struct inode
*inode
,
5917 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5918 struct btrfs_dio_private
*dip
;
5919 struct bio_vec
*bvec
= bio
->bi_io_vec
;
5921 int write
= rw
& REQ_WRITE
;
5924 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
5926 dip
= kmalloc(sizeof(*dip
), GFP_NOFS
);
5934 dip
->csums
= kmalloc(sizeof(u32
) * bio
->bi_vcnt
, GFP_NOFS
);
5941 dip
->private = bio
->bi_private
;
5943 dip
->logical_offset
= file_offset
;
5947 dip
->bytes
+= bvec
->bv_len
;
5949 } while (bvec
<= (bio
->bi_io_vec
+ bio
->bi_vcnt
- 1));
5951 dip
->disk_bytenr
= (u64
)bio
->bi_sector
<< 9;
5952 bio
->bi_private
= dip
;
5954 dip
->orig_bio
= bio
;
5955 atomic_set(&dip
->pending_bios
, 0);
5958 bio
->bi_end_io
= btrfs_endio_direct_write
;
5960 bio
->bi_end_io
= btrfs_endio_direct_read
;
5962 ret
= btrfs_submit_direct_hook(rw
, dip
, skip_sum
);
5967 * If this is a write, we need to clean up the reserved space and kill
5968 * the ordered extent.
5971 struct btrfs_ordered_extent
*ordered
;
5972 ordered
= btrfs_lookup_ordered_extent(inode
, file_offset
);
5973 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
5974 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
5975 btrfs_free_reserved_extent(root
, ordered
->start
,
5977 btrfs_put_ordered_extent(ordered
);
5978 btrfs_put_ordered_extent(ordered
);
5980 bio_endio(bio
, ret
);
5983 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
5984 const struct iovec
*iov
, loff_t offset
,
5985 unsigned long nr_segs
)
5990 unsigned blocksize_mask
= root
->sectorsize
- 1;
5991 ssize_t retval
= -EINVAL
;
5992 loff_t end
= offset
;
5994 if (offset
& blocksize_mask
)
5997 /* Check the memory alignment. Blocks cannot straddle pages */
5998 for (seg
= 0; seg
< nr_segs
; seg
++) {
5999 addr
= (unsigned long)iov
[seg
].iov_base
;
6000 size
= iov
[seg
].iov_len
;
6002 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
6009 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
6010 const struct iovec
*iov
, loff_t offset
,
6011 unsigned long nr_segs
)
6013 struct file
*file
= iocb
->ki_filp
;
6014 struct inode
*inode
= file
->f_mapping
->host
;
6015 struct btrfs_ordered_extent
*ordered
;
6016 struct extent_state
*cached_state
= NULL
;
6017 u64 lockstart
, lockend
;
6019 int writing
= rw
& WRITE
;
6021 size_t count
= iov_length(iov
, nr_segs
);
6023 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iov
,
6029 lockend
= offset
+ count
- 1;
6032 ret
= btrfs_delalloc_reserve_space(inode
, count
);
6038 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6039 0, &cached_state
, GFP_NOFS
);
6041 * We're concerned with the entire range that we're going to be
6042 * doing DIO to, so we need to make sure theres no ordered
6043 * extents in this range.
6045 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
6046 lockend
- lockstart
+ 1);
6049 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6050 &cached_state
, GFP_NOFS
);
6051 btrfs_start_ordered_extent(inode
, ordered
, 1);
6052 btrfs_put_ordered_extent(ordered
);
6057 * we don't use btrfs_set_extent_delalloc because we don't want
6058 * the dirty or uptodate bits
6061 write_bits
= EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
;
6062 ret
= set_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6063 EXTENT_DELALLOC
, 0, NULL
, &cached_state
,
6066 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6067 lockend
, EXTENT_LOCKED
| write_bits
,
6068 1, 0, &cached_state
, GFP_NOFS
);
6073 free_extent_state(cached_state
);
6074 cached_state
= NULL
;
6076 ret
= __blockdev_direct_IO(rw
, iocb
, inode
,
6077 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
6078 iov
, offset
, nr_segs
, btrfs_get_blocks_direct
, NULL
,
6079 btrfs_submit_direct
, 0);
6081 if (ret
< 0 && ret
!= -EIOCBQUEUED
) {
6082 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, offset
,
6083 offset
+ iov_length(iov
, nr_segs
) - 1,
6084 EXTENT_LOCKED
| write_bits
, 1, 0,
6085 &cached_state
, GFP_NOFS
);
6086 } else if (ret
>= 0 && ret
< iov_length(iov
, nr_segs
)) {
6088 * We're falling back to buffered, unlock the section we didn't
6091 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, offset
+ ret
,
6092 offset
+ iov_length(iov
, nr_segs
) - 1,
6093 EXTENT_LOCKED
| write_bits
, 1, 0,
6094 &cached_state
, GFP_NOFS
);
6097 free_extent_state(cached_state
);
6101 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
6102 __u64 start
, __u64 len
)
6104 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent
);
6107 int btrfs_readpage(struct file
*file
, struct page
*page
)
6109 struct extent_io_tree
*tree
;
6110 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6111 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
6114 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
6116 struct extent_io_tree
*tree
;
6119 if (current
->flags
& PF_MEMALLOC
) {
6120 redirty_page_for_writepage(wbc
, page
);
6124 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6125 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
6128 int btrfs_writepages(struct address_space
*mapping
,
6129 struct writeback_control
*wbc
)
6131 struct extent_io_tree
*tree
;
6133 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
6134 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
6138 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
6139 struct list_head
*pages
, unsigned nr_pages
)
6141 struct extent_io_tree
*tree
;
6142 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
6143 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
6146 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
6148 struct extent_io_tree
*tree
;
6149 struct extent_map_tree
*map
;
6152 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6153 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
6154 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
6156 ClearPagePrivate(page
);
6157 set_page_private(page
, 0);
6158 page_cache_release(page
);
6163 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
6165 if (PageWriteback(page
) || PageDirty(page
))
6167 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
6170 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
6172 struct extent_io_tree
*tree
;
6173 struct btrfs_ordered_extent
*ordered
;
6174 struct extent_state
*cached_state
= NULL
;
6175 u64 page_start
= page_offset(page
);
6176 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6180 * we have the page locked, so new writeback can't start,
6181 * and the dirty bit won't be cleared while we are here.
6183 * Wait for IO on this page so that we can safely clear
6184 * the PagePrivate2 bit and do ordered accounting
6186 wait_on_page_writeback(page
);
6188 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6190 btrfs_releasepage(page
, GFP_NOFS
);
6193 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
,
6195 ordered
= btrfs_lookup_ordered_extent(page
->mapping
->host
,
6199 * IO on this page will never be started, so we need
6200 * to account for any ordered extents now
6202 clear_extent_bit(tree
, page_start
, page_end
,
6203 EXTENT_DIRTY
| EXTENT_DELALLOC
|
6204 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
, 1, 0,
6205 &cached_state
, GFP_NOFS
);
6207 * whoever cleared the private bit is responsible
6208 * for the finish_ordered_io
6210 if (TestClearPagePrivate2(page
)) {
6211 btrfs_finish_ordered_io(page
->mapping
->host
,
6212 page_start
, page_end
);
6214 btrfs_put_ordered_extent(ordered
);
6215 cached_state
= NULL
;
6216 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
,
6219 clear_extent_bit(tree
, page_start
, page_end
,
6220 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
6221 EXTENT_DO_ACCOUNTING
, 1, 1, &cached_state
, GFP_NOFS
);
6222 __btrfs_releasepage(page
, GFP_NOFS
);
6224 ClearPageChecked(page
);
6225 if (PagePrivate(page
)) {
6226 ClearPagePrivate(page
);
6227 set_page_private(page
, 0);
6228 page_cache_release(page
);
6233 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6234 * called from a page fault handler when a page is first dirtied. Hence we must
6235 * be careful to check for EOF conditions here. We set the page up correctly
6236 * for a written page which means we get ENOSPC checking when writing into
6237 * holes and correct delalloc and unwritten extent mapping on filesystems that
6238 * support these features.
6240 * We are not allowed to take the i_mutex here so we have to play games to
6241 * protect against truncate races as the page could now be beyond EOF. Because
6242 * vmtruncate() writes the inode size before removing pages, once we have the
6243 * page lock we can determine safely if the page is beyond EOF. If it is not
6244 * beyond EOF, then the page is guaranteed safe against truncation until we
6247 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
6249 struct page
*page
= vmf
->page
;
6250 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
6251 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6252 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6253 struct btrfs_ordered_extent
*ordered
;
6254 struct extent_state
*cached_state
= NULL
;
6256 unsigned long zero_start
;
6262 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
6266 else /* -ENOSPC, -EIO, etc */
6267 ret
= VM_FAULT_SIGBUS
;
6271 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
6274 size
= i_size_read(inode
);
6275 page_start
= page_offset(page
);
6276 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6278 if ((page
->mapping
!= inode
->i_mapping
) ||
6279 (page_start
>= size
)) {
6280 /* page got truncated out from underneath us */
6283 wait_on_page_writeback(page
);
6285 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
,
6287 set_page_extent_mapped(page
);
6290 * we can't set the delalloc bits if there are pending ordered
6291 * extents. Drop our locks and wait for them to finish
6293 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
6295 unlock_extent_cached(io_tree
, page_start
, page_end
,
6296 &cached_state
, GFP_NOFS
);
6298 btrfs_start_ordered_extent(inode
, ordered
, 1);
6299 btrfs_put_ordered_extent(ordered
);
6304 * XXX - page_mkwrite gets called every time the page is dirtied, even
6305 * if it was already dirty, so for space accounting reasons we need to
6306 * clear any delalloc bits for the range we are fixing to save. There
6307 * is probably a better way to do this, but for now keep consistent with
6308 * prepare_pages in the normal write path.
6310 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
6311 EXTENT_DIRTY
| EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
,
6312 0, 0, &cached_state
, GFP_NOFS
);
6314 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
6317 unlock_extent_cached(io_tree
, page_start
, page_end
,
6318 &cached_state
, GFP_NOFS
);
6319 ret
= VM_FAULT_SIGBUS
;
6324 /* page is wholly or partially inside EOF */
6325 if (page_start
+ PAGE_CACHE_SIZE
> size
)
6326 zero_start
= size
& ~PAGE_CACHE_MASK
;
6328 zero_start
= PAGE_CACHE_SIZE
;
6330 if (zero_start
!= PAGE_CACHE_SIZE
) {
6332 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
6333 flush_dcache_page(page
);
6336 ClearPageChecked(page
);
6337 set_page_dirty(page
);
6338 SetPageUptodate(page
);
6340 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
6341 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
6343 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
6347 return VM_FAULT_LOCKED
;
6349 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
6354 static void btrfs_truncate(struct inode
*inode
)
6356 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6358 struct btrfs_trans_handle
*trans
;
6360 u64 mask
= root
->sectorsize
- 1;
6362 if (!S_ISREG(inode
->i_mode
)) {
6367 ret
= btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
6371 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
6372 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
6374 trans
= btrfs_start_transaction(root
, 0);
6375 BUG_ON(IS_ERR(trans
));
6376 btrfs_set_trans_block_group(trans
, inode
);
6377 trans
->block_rsv
= root
->orphan_block_rsv
;
6380 * setattr is responsible for setting the ordered_data_close flag,
6381 * but that is only tested during the last file release. That
6382 * could happen well after the next commit, leaving a great big
6383 * window where new writes may get lost if someone chooses to write
6384 * to this file after truncating to zero
6386 * The inode doesn't have any dirty data here, and so if we commit
6387 * this is a noop. If someone immediately starts writing to the inode
6388 * it is very likely we'll catch some of their writes in this
6389 * transaction, and the commit will find this file on the ordered
6390 * data list with good things to send down.
6392 * This is a best effort solution, there is still a window where
6393 * using truncate to replace the contents of the file will
6394 * end up with a zero length file after a crash.
6396 if (inode
->i_size
== 0 && BTRFS_I(inode
)->ordered_data_close
)
6397 btrfs_add_ordered_operation(trans
, root
, inode
);
6401 trans
= btrfs_start_transaction(root
, 0);
6402 BUG_ON(IS_ERR(trans
));
6403 btrfs_set_trans_block_group(trans
, inode
);
6404 trans
->block_rsv
= root
->orphan_block_rsv
;
6407 ret
= btrfs_block_rsv_check(trans
, root
,
6408 root
->orphan_block_rsv
, 0, 5);
6410 BUG_ON(ret
!= -EAGAIN
);
6411 ret
= btrfs_commit_transaction(trans
, root
);
6417 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
6419 BTRFS_EXTENT_DATA_KEY
);
6423 ret
= btrfs_update_inode(trans
, root
, inode
);
6426 nr
= trans
->blocks_used
;
6427 btrfs_end_transaction(trans
, root
);
6429 btrfs_btree_balance_dirty(root
, nr
);
6432 if (ret
== 0 && inode
->i_nlink
> 0) {
6433 ret
= btrfs_orphan_del(trans
, inode
);
6437 ret
= btrfs_update_inode(trans
, root
, inode
);
6440 nr
= trans
->blocks_used
;
6441 ret
= btrfs_end_transaction_throttle(trans
, root
);
6443 btrfs_btree_balance_dirty(root
, nr
);
6447 * create a new subvolume directory/inode (helper for the ioctl).
6449 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
6450 struct btrfs_root
*new_root
,
6451 u64 new_dirid
, u64 alloc_hint
)
6453 struct inode
*inode
;
6457 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2, new_dirid
,
6458 new_dirid
, alloc_hint
, S_IFDIR
| 0700, &index
);
6460 return PTR_ERR(inode
);
6461 inode
->i_op
= &btrfs_dir_inode_operations
;
6462 inode
->i_fop
= &btrfs_dir_file_operations
;
6465 btrfs_i_size_write(inode
, 0);
6467 err
= btrfs_update_inode(trans
, new_root
, inode
);
6474 /* helper function for file defrag and space balancing. This
6475 * forces readahead on a given range of bytes in an inode
6477 unsigned long btrfs_force_ra(struct address_space
*mapping
,
6478 struct file_ra_state
*ra
, struct file
*file
,
6479 pgoff_t offset
, pgoff_t last_index
)
6481 pgoff_t req_size
= last_index
- offset
+ 1;
6483 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
6484 return offset
+ req_size
;
6487 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
6489 struct btrfs_inode
*ei
;
6490 struct inode
*inode
;
6492 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
6497 ei
->space_info
= NULL
;
6501 ei
->last_sub_trans
= 0;
6502 ei
->logged_trans
= 0;
6503 ei
->delalloc_bytes
= 0;
6504 ei
->reserved_bytes
= 0;
6505 ei
->disk_i_size
= 0;
6507 ei
->index_cnt
= (u64
)-1;
6508 ei
->last_unlink_trans
= 0;
6510 spin_lock_init(&ei
->accounting_lock
);
6511 atomic_set(&ei
->outstanding_extents
, 0);
6512 ei
->reserved_extents
= 0;
6514 ei
->ordered_data_close
= 0;
6515 ei
->orphan_meta_reserved
= 0;
6516 ei
->dummy_inode
= 0;
6517 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
6519 inode
= &ei
->vfs_inode
;
6520 extent_map_tree_init(&ei
->extent_tree
, GFP_NOFS
);
6521 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
, GFP_NOFS
);
6522 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
, GFP_NOFS
);
6523 mutex_init(&ei
->log_mutex
);
6524 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
6525 INIT_LIST_HEAD(&ei
->i_orphan
);
6526 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
6527 INIT_LIST_HEAD(&ei
->ordered_operations
);
6528 RB_CLEAR_NODE(&ei
->rb_node
);
6533 void btrfs_destroy_inode(struct inode
*inode
)
6535 struct btrfs_ordered_extent
*ordered
;
6536 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6538 WARN_ON(!list_empty(&inode
->i_dentry
));
6539 WARN_ON(inode
->i_data
.nrpages
);
6540 WARN_ON(atomic_read(&BTRFS_I(inode
)->outstanding_extents
));
6541 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
6544 * This can happen where we create an inode, but somebody else also
6545 * created the same inode and we need to destroy the one we already
6552 * Make sure we're properly removed from the ordered operation
6556 if (!list_empty(&BTRFS_I(inode
)->ordered_operations
)) {
6557 spin_lock(&root
->fs_info
->ordered_extent_lock
);
6558 list_del_init(&BTRFS_I(inode
)->ordered_operations
);
6559 spin_unlock(&root
->fs_info
->ordered_extent_lock
);
6562 if (root
== root
->fs_info
->tree_root
) {
6563 struct btrfs_block_group_cache
*block_group
;
6565 block_group
= btrfs_lookup_block_group(root
->fs_info
,
6566 BTRFS_I(inode
)->block_group
);
6567 if (block_group
&& block_group
->inode
== inode
) {
6568 spin_lock(&block_group
->lock
);
6569 block_group
->inode
= NULL
;
6570 spin_unlock(&block_group
->lock
);
6571 btrfs_put_block_group(block_group
);
6572 } else if (block_group
) {
6573 btrfs_put_block_group(block_group
);
6577 spin_lock(&root
->orphan_lock
);
6578 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
6579 printk(KERN_INFO
"BTRFS: inode %lu still on the orphan list\n",
6581 list_del_init(&BTRFS_I(inode
)->i_orphan
);
6583 spin_unlock(&root
->orphan_lock
);
6586 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
6590 printk(KERN_ERR
"btrfs found ordered "
6591 "extent %llu %llu on inode cleanup\n",
6592 (unsigned long long)ordered
->file_offset
,
6593 (unsigned long long)ordered
->len
);
6594 btrfs_remove_ordered_extent(inode
, ordered
);
6595 btrfs_put_ordered_extent(ordered
);
6596 btrfs_put_ordered_extent(ordered
);
6599 inode_tree_del(inode
);
6600 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
6602 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
6605 int btrfs_drop_inode(struct inode
*inode
)
6607 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6609 if (btrfs_root_refs(&root
->root_item
) == 0 &&
6610 root
!= root
->fs_info
->tree_root
)
6613 return generic_drop_inode(inode
);
6616 static void init_once(void *foo
)
6618 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
6620 inode_init_once(&ei
->vfs_inode
);
6623 void btrfs_destroy_cachep(void)
6625 if (btrfs_inode_cachep
)
6626 kmem_cache_destroy(btrfs_inode_cachep
);
6627 if (btrfs_trans_handle_cachep
)
6628 kmem_cache_destroy(btrfs_trans_handle_cachep
);
6629 if (btrfs_transaction_cachep
)
6630 kmem_cache_destroy(btrfs_transaction_cachep
);
6631 if (btrfs_path_cachep
)
6632 kmem_cache_destroy(btrfs_path_cachep
);
6635 int btrfs_init_cachep(void)
6637 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode_cache",
6638 sizeof(struct btrfs_inode
), 0,
6639 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
6640 if (!btrfs_inode_cachep
)
6643 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle_cache",
6644 sizeof(struct btrfs_trans_handle
), 0,
6645 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6646 if (!btrfs_trans_handle_cachep
)
6649 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction_cache",
6650 sizeof(struct btrfs_transaction
), 0,
6651 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6652 if (!btrfs_transaction_cachep
)
6655 btrfs_path_cachep
= kmem_cache_create("btrfs_path_cache",
6656 sizeof(struct btrfs_path
), 0,
6657 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6658 if (!btrfs_path_cachep
)
6663 btrfs_destroy_cachep();
6667 static int btrfs_getattr(struct vfsmount
*mnt
,
6668 struct dentry
*dentry
, struct kstat
*stat
)
6670 struct inode
*inode
= dentry
->d_inode
;
6671 generic_fillattr(inode
, stat
);
6672 stat
->dev
= BTRFS_I(inode
)->root
->anon_super
.s_dev
;
6673 stat
->blksize
= PAGE_CACHE_SIZE
;
6674 stat
->blocks
= (inode_get_bytes(inode
) +
6675 BTRFS_I(inode
)->delalloc_bytes
) >> 9;
6679 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
6680 struct inode
*new_dir
, struct dentry
*new_dentry
)
6682 struct btrfs_trans_handle
*trans
;
6683 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
6684 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
6685 struct inode
*new_inode
= new_dentry
->d_inode
;
6686 struct inode
*old_inode
= old_dentry
->d_inode
;
6687 struct timespec ctime
= CURRENT_TIME
;
6692 if (new_dir
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
6695 /* we only allow rename subvolume link between subvolumes */
6696 if (old_inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
6699 if (old_inode
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
6700 (new_inode
&& new_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
))
6703 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
6704 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
6707 * we're using rename to replace one file with another.
6708 * and the replacement file is large. Start IO on it now so
6709 * we don't add too much work to the end of the transaction
6711 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
&&
6712 old_inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
6713 filemap_flush(old_inode
->i_mapping
);
6715 /* close the racy window with snapshot create/destroy ioctl */
6716 if (old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)
6717 down_read(&root
->fs_info
->subvol_sem
);
6719 * We want to reserve the absolute worst case amount of items. So if
6720 * both inodes are subvols and we need to unlink them then that would
6721 * require 4 item modifications, but if they are both normal inodes it
6722 * would require 5 item modifications, so we'll assume their normal
6723 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
6724 * should cover the worst case number of items we'll modify.
6726 trans
= btrfs_start_transaction(root
, 20);
6728 return PTR_ERR(trans
);
6730 btrfs_set_trans_block_group(trans
, new_dir
);
6733 btrfs_record_root_in_trans(trans
, dest
);
6735 ret
= btrfs_set_inode_index(new_dir
, &index
);
6739 if (unlikely(old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6740 /* force full log commit if subvolume involved. */
6741 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
6743 ret
= btrfs_insert_inode_ref(trans
, dest
,
6744 new_dentry
->d_name
.name
,
6745 new_dentry
->d_name
.len
,
6747 new_dir
->i_ino
, index
);
6751 * this is an ugly little race, but the rename is required
6752 * to make sure that if we crash, the inode is either at the
6753 * old name or the new one. pinning the log transaction lets
6754 * us make sure we don't allow a log commit to come in after
6755 * we unlink the name but before we add the new name back in.
6757 btrfs_pin_log_trans(root
);
6760 * make sure the inode gets flushed if it is replacing
6763 if (new_inode
&& new_inode
->i_size
&&
6764 old_inode
&& S_ISREG(old_inode
->i_mode
)) {
6765 btrfs_add_ordered_operation(trans
, root
, old_inode
);
6768 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
6769 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
6770 old_inode
->i_ctime
= ctime
;
6772 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
6773 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
6775 if (unlikely(old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6776 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
6777 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
6778 old_dentry
->d_name
.name
,
6779 old_dentry
->d_name
.len
);
6781 btrfs_inc_nlink(old_dentry
->d_inode
);
6782 ret
= btrfs_unlink_inode(trans
, root
, old_dir
,
6783 old_dentry
->d_inode
,
6784 old_dentry
->d_name
.name
,
6785 old_dentry
->d_name
.len
);
6790 new_inode
->i_ctime
= CURRENT_TIME
;
6791 if (unlikely(new_inode
->i_ino
==
6792 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
6793 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
6794 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
6796 new_dentry
->d_name
.name
,
6797 new_dentry
->d_name
.len
);
6798 BUG_ON(new_inode
->i_nlink
== 0);
6800 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
6801 new_dentry
->d_inode
,
6802 new_dentry
->d_name
.name
,
6803 new_dentry
->d_name
.len
);
6806 if (new_inode
->i_nlink
== 0) {
6807 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
6812 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
6813 new_dentry
->d_name
.name
,
6814 new_dentry
->d_name
.len
, 0, index
);
6817 if (old_inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
6818 struct dentry
*parent
= dget_parent(new_dentry
);
6819 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
6821 btrfs_end_log_trans(root
);
6824 btrfs_end_transaction_throttle(trans
, root
);
6826 if (old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)
6827 up_read(&root
->fs_info
->subvol_sem
);
6833 * some fairly slow code that needs optimization. This walks the list
6834 * of all the inodes with pending delalloc and forces them to disk.
6836 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
6838 struct list_head
*head
= &root
->fs_info
->delalloc_inodes
;
6839 struct btrfs_inode
*binode
;
6840 struct inode
*inode
;
6842 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
6845 spin_lock(&root
->fs_info
->delalloc_lock
);
6846 while (!list_empty(head
)) {
6847 binode
= list_entry(head
->next
, struct btrfs_inode
,
6849 inode
= igrab(&binode
->vfs_inode
);
6851 list_del_init(&binode
->delalloc_inodes
);
6852 spin_unlock(&root
->fs_info
->delalloc_lock
);
6854 filemap_flush(inode
->i_mapping
);
6856 btrfs_add_delayed_iput(inode
);
6861 spin_lock(&root
->fs_info
->delalloc_lock
);
6863 spin_unlock(&root
->fs_info
->delalloc_lock
);
6865 /* the filemap_flush will queue IO into the worker threads, but
6866 * we have to make sure the IO is actually started and that
6867 * ordered extents get created before we return
6869 atomic_inc(&root
->fs_info
->async_submit_draining
);
6870 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
6871 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
6872 wait_event(root
->fs_info
->async_submit_wait
,
6873 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
6874 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
6876 atomic_dec(&root
->fs_info
->async_submit_draining
);
6880 int btrfs_start_one_delalloc_inode(struct btrfs_root
*root
, int delay_iput
,
6883 struct btrfs_inode
*binode
;
6884 struct inode
*inode
= NULL
;
6886 spin_lock(&root
->fs_info
->delalloc_lock
);
6887 while (!list_empty(&root
->fs_info
->delalloc_inodes
)) {
6888 binode
= list_entry(root
->fs_info
->delalloc_inodes
.next
,
6889 struct btrfs_inode
, delalloc_inodes
);
6890 inode
= igrab(&binode
->vfs_inode
);
6892 list_move_tail(&binode
->delalloc_inodes
,
6893 &root
->fs_info
->delalloc_inodes
);
6897 list_del_init(&binode
->delalloc_inodes
);
6898 cond_resched_lock(&root
->fs_info
->delalloc_lock
);
6900 spin_unlock(&root
->fs_info
->delalloc_lock
);
6904 filemap_write_and_wait(inode
->i_mapping
);
6906 * We have to do this because compression doesn't
6907 * actually set PG_writeback until it submits the pages
6908 * for IO, which happens in an async thread, so we could
6909 * race and not actually wait for any writeback pages
6910 * because they've not been submitted yet. Technically
6911 * this could still be the case for the ordered stuff
6912 * since the async thread may not have started to do its
6913 * work yet. If this becomes the case then we need to
6914 * figure out a way to make sure that in writepage we
6915 * wait for any async pages to be submitted before
6916 * returning so that fdatawait does what its supposed to
6919 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
6921 filemap_flush(inode
->i_mapping
);
6924 btrfs_add_delayed_iput(inode
);
6932 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
6933 const char *symname
)
6935 struct btrfs_trans_handle
*trans
;
6936 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6937 struct btrfs_path
*path
;
6938 struct btrfs_key key
;
6939 struct inode
*inode
= NULL
;
6947 struct btrfs_file_extent_item
*ei
;
6948 struct extent_buffer
*leaf
;
6949 unsigned long nr
= 0;
6951 name_len
= strlen(symname
) + 1;
6952 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
6953 return -ENAMETOOLONG
;
6955 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
6959 * 2 items for inode item and ref
6960 * 2 items for dir items
6961 * 1 item for xattr if selinux is on
6963 trans
= btrfs_start_transaction(root
, 5);
6965 return PTR_ERR(trans
);
6967 btrfs_set_trans_block_group(trans
, dir
);
6969 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6970 dentry
->d_name
.len
, dir
->i_ino
, objectid
,
6971 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
,
6973 err
= PTR_ERR(inode
);
6977 err
= btrfs_init_inode_security(trans
, inode
, dir
);
6983 btrfs_set_trans_block_group(trans
, inode
);
6984 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
6988 inode
->i_mapping
->a_ops
= &btrfs_aops
;
6989 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
6990 inode
->i_fop
= &btrfs_file_operations
;
6991 inode
->i_op
= &btrfs_file_inode_operations
;
6992 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
6994 btrfs_update_inode_block_group(trans
, inode
);
6995 btrfs_update_inode_block_group(trans
, dir
);
6999 path
= btrfs_alloc_path();
7001 key
.objectid
= inode
->i_ino
;
7003 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
7004 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
7005 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
7011 leaf
= path
->nodes
[0];
7012 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
7013 struct btrfs_file_extent_item
);
7014 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
7015 btrfs_set_file_extent_type(leaf
, ei
,
7016 BTRFS_FILE_EXTENT_INLINE
);
7017 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
7018 btrfs_set_file_extent_compression(leaf
, ei
, 0);
7019 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
7020 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
7022 ptr
= btrfs_file_extent_inline_start(ei
);
7023 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
7024 btrfs_mark_buffer_dirty(leaf
);
7025 btrfs_free_path(path
);
7027 inode
->i_op
= &btrfs_symlink_inode_operations
;
7028 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
7029 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
7030 inode_set_bytes(inode
, name_len
);
7031 btrfs_i_size_write(inode
, name_len
- 1);
7032 err
= btrfs_update_inode(trans
, root
, inode
);
7037 nr
= trans
->blocks_used
;
7038 btrfs_end_transaction_throttle(trans
, root
);
7040 inode_dec_link_count(inode
);
7043 btrfs_btree_balance_dirty(root
, nr
);
7047 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
7048 u64 start
, u64 num_bytes
, u64 min_size
,
7049 loff_t actual_len
, u64
*alloc_hint
,
7050 struct btrfs_trans_handle
*trans
)
7052 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7053 struct btrfs_key ins
;
7054 u64 cur_offset
= start
;
7057 bool own_trans
= true;
7061 while (num_bytes
> 0) {
7063 trans
= btrfs_start_transaction(root
, 3);
7064 if (IS_ERR(trans
)) {
7065 ret
= PTR_ERR(trans
);
7070 ret
= btrfs_reserve_extent(trans
, root
, num_bytes
, min_size
,
7071 0, *alloc_hint
, (u64
)-1, &ins
, 1);
7074 btrfs_end_transaction(trans
, root
);
7078 ret
= insert_reserved_file_extent(trans
, inode
,
7079 cur_offset
, ins
.objectid
,
7080 ins
.offset
, ins
.offset
,
7081 ins
.offset
, 0, 0, 0,
7082 BTRFS_FILE_EXTENT_PREALLOC
);
7084 btrfs_drop_extent_cache(inode
, cur_offset
,
7085 cur_offset
+ ins
.offset
-1, 0);
7087 num_bytes
-= ins
.offset
;
7088 cur_offset
+= ins
.offset
;
7089 *alloc_hint
= ins
.objectid
+ ins
.offset
;
7091 inode
->i_ctime
= CURRENT_TIME
;
7092 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
7093 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
7094 (actual_len
> inode
->i_size
) &&
7095 (cur_offset
> inode
->i_size
)) {
7096 if (cur_offset
> actual_len
)
7097 i_size
= actual_len
;
7099 i_size
= cur_offset
;
7100 i_size_write(inode
, i_size
);
7101 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
7104 ret
= btrfs_update_inode(trans
, root
, inode
);
7108 btrfs_end_transaction(trans
, root
);
7113 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
7114 u64 start
, u64 num_bytes
, u64 min_size
,
7115 loff_t actual_len
, u64
*alloc_hint
)
7117 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
7118 min_size
, actual_len
, alloc_hint
,
7122 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
7123 struct btrfs_trans_handle
*trans
, int mode
,
7124 u64 start
, u64 num_bytes
, u64 min_size
,
7125 loff_t actual_len
, u64
*alloc_hint
)
7127 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
7128 min_size
, actual_len
, alloc_hint
, trans
);
7131 static long btrfs_fallocate(struct inode
*inode
, int mode
,
7132 loff_t offset
, loff_t len
)
7134 struct extent_state
*cached_state
= NULL
;
7141 u64 mask
= BTRFS_I(inode
)->root
->sectorsize
- 1;
7142 struct extent_map
*em
;
7145 alloc_start
= offset
& ~mask
;
7146 alloc_end
= (offset
+ len
+ mask
) & ~mask
;
7149 * wait for ordered IO before we have any locks. We'll loop again
7150 * below with the locks held.
7152 btrfs_wait_ordered_range(inode
, alloc_start
, alloc_end
- alloc_start
);
7154 mutex_lock(&inode
->i_mutex
);
7155 ret
= inode_newsize_ok(inode
, alloc_end
);
7159 if (alloc_start
> inode
->i_size
) {
7160 ret
= btrfs_cont_expand(inode
, alloc_start
);
7165 ret
= btrfs_check_data_free_space(inode
, alloc_end
- alloc_start
);
7169 locked_end
= alloc_end
- 1;
7171 struct btrfs_ordered_extent
*ordered
;
7173 /* the extent lock is ordered inside the running
7176 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, alloc_start
,
7177 locked_end
, 0, &cached_state
, GFP_NOFS
);
7178 ordered
= btrfs_lookup_first_ordered_extent(inode
,
7181 ordered
->file_offset
+ ordered
->len
> alloc_start
&&
7182 ordered
->file_offset
< alloc_end
) {
7183 btrfs_put_ordered_extent(ordered
);
7184 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
7185 alloc_start
, locked_end
,
7186 &cached_state
, GFP_NOFS
);
7188 * we can't wait on the range with the transaction
7189 * running or with the extent lock held
7191 btrfs_wait_ordered_range(inode
, alloc_start
,
7192 alloc_end
- alloc_start
);
7195 btrfs_put_ordered_extent(ordered
);
7200 cur_offset
= alloc_start
;
7202 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
7203 alloc_end
- cur_offset
, 0);
7204 BUG_ON(IS_ERR(em
) || !em
);
7205 last_byte
= min(extent_map_end(em
), alloc_end
);
7206 last_byte
= (last_byte
+ mask
) & ~mask
;
7207 if (em
->block_start
== EXTENT_MAP_HOLE
||
7208 (cur_offset
>= inode
->i_size
&&
7209 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
7210 ret
= btrfs_prealloc_file_range(inode
, mode
, cur_offset
,
7211 last_byte
- cur_offset
,
7212 1 << inode
->i_blkbits
,
7216 free_extent_map(em
);
7220 free_extent_map(em
);
7222 cur_offset
= last_byte
;
7223 if (cur_offset
>= alloc_end
) {
7228 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, alloc_start
, locked_end
,
7229 &cached_state
, GFP_NOFS
);
7231 btrfs_free_reserved_data_space(inode
, alloc_end
- alloc_start
);
7233 mutex_unlock(&inode
->i_mutex
);
7237 static int btrfs_set_page_dirty(struct page
*page
)
7239 return __set_page_dirty_nobuffers(page
);
7242 static int btrfs_permission(struct inode
*inode
, int mask
)
7244 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7246 if (btrfs_root_readonly(root
) && (mask
& MAY_WRITE
))
7248 if ((BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
) && (mask
& MAY_WRITE
))
7250 return generic_permission(inode
, mask
, btrfs_check_acl
);
7253 static const struct inode_operations btrfs_dir_inode_operations
= {
7254 .getattr
= btrfs_getattr
,
7255 .lookup
= btrfs_lookup
,
7256 .create
= btrfs_create
,
7257 .unlink
= btrfs_unlink
,
7259 .mkdir
= btrfs_mkdir
,
7260 .rmdir
= btrfs_rmdir
,
7261 .rename
= btrfs_rename
,
7262 .symlink
= btrfs_symlink
,
7263 .setattr
= btrfs_setattr
,
7264 .mknod
= btrfs_mknod
,
7265 .setxattr
= btrfs_setxattr
,
7266 .getxattr
= btrfs_getxattr
,
7267 .listxattr
= btrfs_listxattr
,
7268 .removexattr
= btrfs_removexattr
,
7269 .permission
= btrfs_permission
,
7271 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
7272 .lookup
= btrfs_lookup
,
7273 .permission
= btrfs_permission
,
7276 static const struct file_operations btrfs_dir_file_operations
= {
7277 .llseek
= generic_file_llseek
,
7278 .read
= generic_read_dir
,
7279 .readdir
= btrfs_real_readdir
,
7280 .unlocked_ioctl
= btrfs_ioctl
,
7281 #ifdef CONFIG_COMPAT
7282 .compat_ioctl
= btrfs_ioctl
,
7284 .release
= btrfs_release_file
,
7285 .fsync
= btrfs_sync_file
,
7288 static struct extent_io_ops btrfs_extent_io_ops
= {
7289 .fill_delalloc
= run_delalloc_range
,
7290 .submit_bio_hook
= btrfs_submit_bio_hook
,
7291 .merge_bio_hook
= btrfs_merge_bio_hook
,
7292 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
7293 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
7294 .writepage_start_hook
= btrfs_writepage_start_hook
,
7295 .readpage_io_failed_hook
= btrfs_io_failed_hook
,
7296 .set_bit_hook
= btrfs_set_bit_hook
,
7297 .clear_bit_hook
= btrfs_clear_bit_hook
,
7298 .merge_extent_hook
= btrfs_merge_extent_hook
,
7299 .split_extent_hook
= btrfs_split_extent_hook
,
7303 * btrfs doesn't support the bmap operation because swapfiles
7304 * use bmap to make a mapping of extents in the file. They assume
7305 * these extents won't change over the life of the file and they
7306 * use the bmap result to do IO directly to the drive.
7308 * the btrfs bmap call would return logical addresses that aren't
7309 * suitable for IO and they also will change frequently as COW
7310 * operations happen. So, swapfile + btrfs == corruption.
7312 * For now we're avoiding this by dropping bmap.
7314 static const struct address_space_operations btrfs_aops
= {
7315 .readpage
= btrfs_readpage
,
7316 .writepage
= btrfs_writepage
,
7317 .writepages
= btrfs_writepages
,
7318 .readpages
= btrfs_readpages
,
7319 .sync_page
= block_sync_page
,
7320 .direct_IO
= btrfs_direct_IO
,
7321 .invalidatepage
= btrfs_invalidatepage
,
7322 .releasepage
= btrfs_releasepage
,
7323 .set_page_dirty
= btrfs_set_page_dirty
,
7324 .error_remove_page
= generic_error_remove_page
,
7327 static const struct address_space_operations btrfs_symlink_aops
= {
7328 .readpage
= btrfs_readpage
,
7329 .writepage
= btrfs_writepage
,
7330 .invalidatepage
= btrfs_invalidatepage
,
7331 .releasepage
= btrfs_releasepage
,
7334 static const struct inode_operations btrfs_file_inode_operations
= {
7335 .truncate
= btrfs_truncate
,
7336 .getattr
= btrfs_getattr
,
7337 .setattr
= btrfs_setattr
,
7338 .setxattr
= btrfs_setxattr
,
7339 .getxattr
= btrfs_getxattr
,
7340 .listxattr
= btrfs_listxattr
,
7341 .removexattr
= btrfs_removexattr
,
7342 .permission
= btrfs_permission
,
7343 .fallocate
= btrfs_fallocate
,
7344 .fiemap
= btrfs_fiemap
,
7346 static const struct inode_operations btrfs_special_inode_operations
= {
7347 .getattr
= btrfs_getattr
,
7348 .setattr
= btrfs_setattr
,
7349 .permission
= btrfs_permission
,
7350 .setxattr
= btrfs_setxattr
,
7351 .getxattr
= btrfs_getxattr
,
7352 .listxattr
= btrfs_listxattr
,
7353 .removexattr
= btrfs_removexattr
,
7355 static const struct inode_operations btrfs_symlink_inode_operations
= {
7356 .readlink
= generic_readlink
,
7357 .follow_link
= page_follow_link_light
,
7358 .put_link
= page_put_link
,
7359 .getattr
= btrfs_getattr
,
7360 .permission
= btrfs_permission
,
7361 .setxattr
= btrfs_setxattr
,
7362 .getxattr
= btrfs_getxattr
,
7363 .listxattr
= btrfs_listxattr
,
7364 .removexattr
= btrfs_removexattr
,
7367 const struct dentry_operations btrfs_dentry_operations
= {
7368 .d_delete
= btrfs_dentry_delete
,