2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/statfs.h>
34 #include <linux/compat.h>
35 #include <linux/bit_spinlock.h>
36 #include <linux/xattr.h>
37 #include <linux/posix_acl.h>
38 #include <linux/falloc.h>
39 #include <linux/slab.h>
43 #include "transaction.h"
44 #include "btrfs_inode.h"
46 #include "print-tree.h"
48 #include "ordered-data.h"
51 #include "compression.h"
54 struct btrfs_iget_args
{
56 struct btrfs_root
*root
;
59 static const struct inode_operations btrfs_dir_inode_operations
;
60 static const struct inode_operations btrfs_symlink_inode_operations
;
61 static const struct inode_operations btrfs_dir_ro_inode_operations
;
62 static const struct inode_operations btrfs_special_inode_operations
;
63 static const struct inode_operations btrfs_file_inode_operations
;
64 static const struct address_space_operations btrfs_aops
;
65 static const struct address_space_operations btrfs_symlink_aops
;
66 static const struct file_operations btrfs_dir_file_operations
;
67 static struct extent_io_ops btrfs_extent_io_ops
;
69 static struct kmem_cache
*btrfs_inode_cachep
;
70 struct kmem_cache
*btrfs_trans_handle_cachep
;
71 struct kmem_cache
*btrfs_transaction_cachep
;
72 struct kmem_cache
*btrfs_path_cachep
;
75 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
76 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
77 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
78 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
79 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
80 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
81 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
82 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
85 static void btrfs_truncate(struct inode
*inode
);
86 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
);
87 static noinline
int cow_file_range(struct inode
*inode
,
88 struct page
*locked_page
,
89 u64 start
, u64 end
, int *page_started
,
90 unsigned long *nr_written
, int unlock
);
92 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
93 struct inode
*inode
, struct inode
*dir
,
94 const struct qstr
*qstr
)
98 err
= btrfs_init_acl(trans
, inode
, dir
);
100 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
105 * this does all the hard work for inserting an inline extent into
106 * the btree. The caller should have done a btrfs_drop_extents so that
107 * no overlapping inline items exist in the btree
109 static noinline
int insert_inline_extent(struct btrfs_trans_handle
*trans
,
110 struct btrfs_root
*root
, struct inode
*inode
,
111 u64 start
, size_t size
, size_t compressed_size
,
112 struct page
**compressed_pages
)
114 struct btrfs_key key
;
115 struct btrfs_path
*path
;
116 struct extent_buffer
*leaf
;
117 struct page
*page
= NULL
;
120 struct btrfs_file_extent_item
*ei
;
123 size_t cur_size
= size
;
125 unsigned long offset
;
126 int compress_type
= BTRFS_COMPRESS_NONE
;
128 if (compressed_size
&& compressed_pages
) {
129 compress_type
= root
->fs_info
->compress_type
;
130 cur_size
= compressed_size
;
133 path
= btrfs_alloc_path();
137 path
->leave_spinning
= 1;
138 btrfs_set_trans_block_group(trans
, inode
);
140 key
.objectid
= inode
->i_ino
;
142 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
143 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
145 inode_add_bytes(inode
, size
);
146 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
153 leaf
= path
->nodes
[0];
154 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
155 struct btrfs_file_extent_item
);
156 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
157 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
158 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
159 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
160 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
161 ptr
= btrfs_file_extent_inline_start(ei
);
163 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
166 while (compressed_size
> 0) {
167 cpage
= compressed_pages
[i
];
168 cur_size
= min_t(unsigned long, compressed_size
,
171 kaddr
= kmap_atomic(cpage
, KM_USER0
);
172 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
173 kunmap_atomic(kaddr
, KM_USER0
);
177 compressed_size
-= cur_size
;
179 btrfs_set_file_extent_compression(leaf
, ei
,
182 page
= find_get_page(inode
->i_mapping
,
183 start
>> PAGE_CACHE_SHIFT
);
184 btrfs_set_file_extent_compression(leaf
, ei
, 0);
185 kaddr
= kmap_atomic(page
, KM_USER0
);
186 offset
= start
& (PAGE_CACHE_SIZE
- 1);
187 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
188 kunmap_atomic(kaddr
, KM_USER0
);
189 page_cache_release(page
);
191 btrfs_mark_buffer_dirty(leaf
);
192 btrfs_free_path(path
);
195 * we're an inline extent, so nobody can
196 * extend the file past i_size without locking
197 * a page we already have locked.
199 * We must do any isize and inode updates
200 * before we unlock the pages. Otherwise we
201 * could end up racing with unlink.
203 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
204 btrfs_update_inode(trans
, root
, inode
);
208 btrfs_free_path(path
);
214 * conditionally insert an inline extent into the file. This
215 * does the checks required to make sure the data is small enough
216 * to fit as an inline extent.
218 static noinline
int cow_file_range_inline(struct btrfs_trans_handle
*trans
,
219 struct btrfs_root
*root
,
220 struct inode
*inode
, u64 start
, u64 end
,
221 size_t compressed_size
,
222 struct page
**compressed_pages
)
224 u64 isize
= i_size_read(inode
);
225 u64 actual_end
= min(end
+ 1, isize
);
226 u64 inline_len
= actual_end
- start
;
227 u64 aligned_end
= (end
+ root
->sectorsize
- 1) &
228 ~((u64
)root
->sectorsize
- 1);
230 u64 data_len
= inline_len
;
234 data_len
= compressed_size
;
237 actual_end
>= PAGE_CACHE_SIZE
||
238 data_len
>= BTRFS_MAX_INLINE_DATA_SIZE(root
) ||
240 (actual_end
& (root
->sectorsize
- 1)) == 0) ||
242 data_len
> root
->fs_info
->max_inline
) {
246 ret
= btrfs_drop_extents(trans
, inode
, start
, aligned_end
,
250 if (isize
> actual_end
)
251 inline_len
= min_t(u64
, isize
, actual_end
);
252 ret
= insert_inline_extent(trans
, root
, inode
, start
,
253 inline_len
, compressed_size
,
256 btrfs_delalloc_release_metadata(inode
, end
+ 1 - start
);
257 btrfs_drop_extent_cache(inode
, start
, aligned_end
- 1, 0);
261 struct async_extent
{
266 unsigned long nr_pages
;
268 struct list_head list
;
273 struct btrfs_root
*root
;
274 struct page
*locked_page
;
277 struct list_head extents
;
278 struct btrfs_work work
;
281 static noinline
int add_async_extent(struct async_cow
*cow
,
282 u64 start
, u64 ram_size
,
285 unsigned long nr_pages
,
288 struct async_extent
*async_extent
;
290 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
291 async_extent
->start
= start
;
292 async_extent
->ram_size
= ram_size
;
293 async_extent
->compressed_size
= compressed_size
;
294 async_extent
->pages
= pages
;
295 async_extent
->nr_pages
= nr_pages
;
296 async_extent
->compress_type
= compress_type
;
297 list_add_tail(&async_extent
->list
, &cow
->extents
);
302 * we create compressed extents in two phases. The first
303 * phase compresses a range of pages that have already been
304 * locked (both pages and state bits are locked).
306 * This is done inside an ordered work queue, and the compression
307 * is spread across many cpus. The actual IO submission is step
308 * two, and the ordered work queue takes care of making sure that
309 * happens in the same order things were put onto the queue by
310 * writepages and friends.
312 * If this code finds it can't get good compression, it puts an
313 * entry onto the work queue to write the uncompressed bytes. This
314 * makes sure that both compressed inodes and uncompressed inodes
315 * are written in the same order that pdflush sent them down.
317 static noinline
int compress_file_range(struct inode
*inode
,
318 struct page
*locked_page
,
320 struct async_cow
*async_cow
,
323 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
324 struct btrfs_trans_handle
*trans
;
326 u64 blocksize
= root
->sectorsize
;
328 u64 isize
= i_size_read(inode
);
330 struct page
**pages
= NULL
;
331 unsigned long nr_pages
;
332 unsigned long nr_pages_ret
= 0;
333 unsigned long total_compressed
= 0;
334 unsigned long total_in
= 0;
335 unsigned long max_compressed
= 128 * 1024;
336 unsigned long max_uncompressed
= 128 * 1024;
339 int compress_type
= root
->fs_info
->compress_type
;
341 actual_end
= min_t(u64
, isize
, end
+ 1);
344 nr_pages
= (end
>> PAGE_CACHE_SHIFT
) - (start
>> PAGE_CACHE_SHIFT
) + 1;
345 nr_pages
= min(nr_pages
, (128 * 1024UL) / PAGE_CACHE_SIZE
);
348 * we don't want to send crud past the end of i_size through
349 * compression, that's just a waste of CPU time. So, if the
350 * end of the file is before the start of our current
351 * requested range of bytes, we bail out to the uncompressed
352 * cleanup code that can deal with all of this.
354 * It isn't really the fastest way to fix things, but this is a
355 * very uncommon corner.
357 if (actual_end
<= start
)
358 goto cleanup_and_bail_uncompressed
;
360 total_compressed
= actual_end
- start
;
362 /* we want to make sure that amount of ram required to uncompress
363 * an extent is reasonable, so we limit the total size in ram
364 * of a compressed extent to 128k. This is a crucial number
365 * because it also controls how easily we can spread reads across
366 * cpus for decompression.
368 * We also want to make sure the amount of IO required to do
369 * a random read is reasonably small, so we limit the size of
370 * a compressed extent to 128k.
372 total_compressed
= min(total_compressed
, max_uncompressed
);
373 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
374 num_bytes
= max(blocksize
, num_bytes
);
379 * we do compression for mount -o compress and when the
380 * inode has not been flagged as nocompress. This flag can
381 * change at any time if we discover bad compression ratios.
383 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
) &&
384 (btrfs_test_opt(root
, COMPRESS
) ||
385 (BTRFS_I(inode
)->force_compress
))) {
387 pages
= kzalloc(sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
389 if (BTRFS_I(inode
)->force_compress
)
390 compress_type
= BTRFS_I(inode
)->force_compress
;
392 ret
= btrfs_compress_pages(compress_type
,
393 inode
->i_mapping
, start
,
394 total_compressed
, pages
,
395 nr_pages
, &nr_pages_ret
,
401 unsigned long offset
= total_compressed
&
402 (PAGE_CACHE_SIZE
- 1);
403 struct page
*page
= pages
[nr_pages_ret
- 1];
406 /* zero the tail end of the last page, we might be
407 * sending it down to disk
410 kaddr
= kmap_atomic(page
, KM_USER0
);
411 memset(kaddr
+ offset
, 0,
412 PAGE_CACHE_SIZE
- offset
);
413 kunmap_atomic(kaddr
, KM_USER0
);
419 trans
= btrfs_join_transaction(root
, 1);
420 BUG_ON(IS_ERR(trans
));
421 btrfs_set_trans_block_group(trans
, inode
);
422 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
424 /* lets try to make an inline extent */
425 if (ret
|| total_in
< (actual_end
- start
)) {
426 /* we didn't compress the entire range, try
427 * to make an uncompressed inline extent.
429 ret
= cow_file_range_inline(trans
, root
, inode
,
430 start
, end
, 0, NULL
);
432 /* try making a compressed inline extent */
433 ret
= cow_file_range_inline(trans
, root
, inode
,
435 total_compressed
, pages
);
439 * inline extent creation worked, we don't need
440 * to create any more async work items. Unlock
441 * and free up our temp pages.
443 extent_clear_unlock_delalloc(inode
,
444 &BTRFS_I(inode
)->io_tree
,
446 EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
447 EXTENT_CLEAR_DELALLOC
|
448 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
);
450 btrfs_end_transaction(trans
, root
);
453 btrfs_end_transaction(trans
, root
);
458 * we aren't doing an inline extent round the compressed size
459 * up to a block size boundary so the allocator does sane
462 total_compressed
= (total_compressed
+ blocksize
- 1) &
466 * one last check to make sure the compression is really a
467 * win, compare the page count read with the blocks on disk
469 total_in
= (total_in
+ PAGE_CACHE_SIZE
- 1) &
470 ~(PAGE_CACHE_SIZE
- 1);
471 if (total_compressed
>= total_in
) {
474 num_bytes
= total_in
;
477 if (!will_compress
&& pages
) {
479 * the compression code ran but failed to make things smaller,
480 * free any pages it allocated and our page pointer array
482 for (i
= 0; i
< nr_pages_ret
; i
++) {
483 WARN_ON(pages
[i
]->mapping
);
484 page_cache_release(pages
[i
]);
488 total_compressed
= 0;
491 /* flag the file so we don't compress in the future */
492 if (!btrfs_test_opt(root
, FORCE_COMPRESS
) &&
493 !(BTRFS_I(inode
)->force_compress
)) {
494 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
500 /* the async work queues will take care of doing actual
501 * allocation on disk for these compressed pages,
502 * and will submit them to the elevator.
504 add_async_extent(async_cow
, start
, num_bytes
,
505 total_compressed
, pages
, nr_pages_ret
,
508 if (start
+ num_bytes
< end
) {
515 cleanup_and_bail_uncompressed
:
517 * No compression, but we still need to write the pages in
518 * the file we've been given so far. redirty the locked
519 * page if it corresponds to our extent and set things up
520 * for the async work queue to run cow_file_range to do
521 * the normal delalloc dance
523 if (page_offset(locked_page
) >= start
&&
524 page_offset(locked_page
) <= end
) {
525 __set_page_dirty_nobuffers(locked_page
);
526 /* unlocked later on in the async handlers */
528 add_async_extent(async_cow
, start
, end
- start
+ 1,
529 0, NULL
, 0, BTRFS_COMPRESS_NONE
);
537 for (i
= 0; i
< nr_pages_ret
; i
++) {
538 WARN_ON(pages
[i
]->mapping
);
539 page_cache_release(pages
[i
]);
547 * phase two of compressed writeback. This is the ordered portion
548 * of the code, which only gets called in the order the work was
549 * queued. We walk all the async extents created by compress_file_range
550 * and send them down to the disk.
552 static noinline
int submit_compressed_extents(struct inode
*inode
,
553 struct async_cow
*async_cow
)
555 struct async_extent
*async_extent
;
557 struct btrfs_trans_handle
*trans
;
558 struct btrfs_key ins
;
559 struct extent_map
*em
;
560 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
561 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
562 struct extent_io_tree
*io_tree
;
565 if (list_empty(&async_cow
->extents
))
569 while (!list_empty(&async_cow
->extents
)) {
570 async_extent
= list_entry(async_cow
->extents
.next
,
571 struct async_extent
, list
);
572 list_del(&async_extent
->list
);
574 io_tree
= &BTRFS_I(inode
)->io_tree
;
577 /* did the compression code fall back to uncompressed IO? */
578 if (!async_extent
->pages
) {
579 int page_started
= 0;
580 unsigned long nr_written
= 0;
582 lock_extent(io_tree
, async_extent
->start
,
583 async_extent
->start
+
584 async_extent
->ram_size
- 1, GFP_NOFS
);
586 /* allocate blocks */
587 ret
= cow_file_range(inode
, async_cow
->locked_page
,
589 async_extent
->start
+
590 async_extent
->ram_size
- 1,
591 &page_started
, &nr_written
, 0);
594 * if page_started, cow_file_range inserted an
595 * inline extent and took care of all the unlocking
596 * and IO for us. Otherwise, we need to submit
597 * all those pages down to the drive.
599 if (!page_started
&& !ret
)
600 extent_write_locked_range(io_tree
,
601 inode
, async_extent
->start
,
602 async_extent
->start
+
603 async_extent
->ram_size
- 1,
611 lock_extent(io_tree
, async_extent
->start
,
612 async_extent
->start
+ async_extent
->ram_size
- 1,
615 trans
= btrfs_join_transaction(root
, 1);
616 BUG_ON(IS_ERR(trans
));
617 ret
= btrfs_reserve_extent(trans
, root
,
618 async_extent
->compressed_size
,
619 async_extent
->compressed_size
,
622 btrfs_end_transaction(trans
, root
);
626 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
627 WARN_ON(async_extent
->pages
[i
]->mapping
);
628 page_cache_release(async_extent
->pages
[i
]);
630 kfree(async_extent
->pages
);
631 async_extent
->nr_pages
= 0;
632 async_extent
->pages
= NULL
;
633 unlock_extent(io_tree
, async_extent
->start
,
634 async_extent
->start
+
635 async_extent
->ram_size
- 1, GFP_NOFS
);
640 * here we're doing allocation and writeback of the
643 btrfs_drop_extent_cache(inode
, async_extent
->start
,
644 async_extent
->start
+
645 async_extent
->ram_size
- 1, 0);
647 em
= alloc_extent_map(GFP_NOFS
);
649 em
->start
= async_extent
->start
;
650 em
->len
= async_extent
->ram_size
;
651 em
->orig_start
= em
->start
;
653 em
->block_start
= ins
.objectid
;
654 em
->block_len
= ins
.offset
;
655 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
656 em
->compress_type
= async_extent
->compress_type
;
657 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
658 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
661 write_lock(&em_tree
->lock
);
662 ret
= add_extent_mapping(em_tree
, em
);
663 write_unlock(&em_tree
->lock
);
664 if (ret
!= -EEXIST
) {
668 btrfs_drop_extent_cache(inode
, async_extent
->start
,
669 async_extent
->start
+
670 async_extent
->ram_size
- 1, 0);
673 ret
= btrfs_add_ordered_extent_compress(inode
,
676 async_extent
->ram_size
,
678 BTRFS_ORDERED_COMPRESSED
,
679 async_extent
->compress_type
);
683 * clear dirty, set writeback and unlock the pages.
685 extent_clear_unlock_delalloc(inode
,
686 &BTRFS_I(inode
)->io_tree
,
688 async_extent
->start
+
689 async_extent
->ram_size
- 1,
690 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
691 EXTENT_CLEAR_UNLOCK
|
692 EXTENT_CLEAR_DELALLOC
|
693 EXTENT_CLEAR_DIRTY
| EXTENT_SET_WRITEBACK
);
695 ret
= btrfs_submit_compressed_write(inode
,
697 async_extent
->ram_size
,
699 ins
.offset
, async_extent
->pages
,
700 async_extent
->nr_pages
);
703 alloc_hint
= ins
.objectid
+ ins
.offset
;
711 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
714 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
715 struct extent_map
*em
;
718 read_lock(&em_tree
->lock
);
719 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
722 * if block start isn't an actual block number then find the
723 * first block in this inode and use that as a hint. If that
724 * block is also bogus then just don't worry about it.
726 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
728 em
= search_extent_mapping(em_tree
, 0, 0);
729 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
730 alloc_hint
= em
->block_start
;
734 alloc_hint
= em
->block_start
;
738 read_unlock(&em_tree
->lock
);
744 * when extent_io.c finds a delayed allocation range in the file,
745 * the call backs end up in this code. The basic idea is to
746 * allocate extents on disk for the range, and create ordered data structs
747 * in ram to track those extents.
749 * locked_page is the page that writepage had locked already. We use
750 * it to make sure we don't do extra locks or unlocks.
752 * *page_started is set to one if we unlock locked_page and do everything
753 * required to start IO on it. It may be clean and already done with
756 static noinline
int cow_file_range(struct inode
*inode
,
757 struct page
*locked_page
,
758 u64 start
, u64 end
, int *page_started
,
759 unsigned long *nr_written
,
762 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
763 struct btrfs_trans_handle
*trans
;
766 unsigned long ram_size
;
769 u64 blocksize
= root
->sectorsize
;
770 struct btrfs_key ins
;
771 struct extent_map
*em
;
772 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
775 BUG_ON(root
== root
->fs_info
->tree_root
);
776 trans
= btrfs_join_transaction(root
, 1);
777 BUG_ON(IS_ERR(trans
));
778 btrfs_set_trans_block_group(trans
, inode
);
779 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
781 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
782 num_bytes
= max(blocksize
, num_bytes
);
783 disk_num_bytes
= num_bytes
;
787 /* lets try to make an inline extent */
788 ret
= cow_file_range_inline(trans
, root
, inode
,
789 start
, end
, 0, NULL
);
791 extent_clear_unlock_delalloc(inode
,
792 &BTRFS_I(inode
)->io_tree
,
794 EXTENT_CLEAR_UNLOCK_PAGE
|
795 EXTENT_CLEAR_UNLOCK
|
796 EXTENT_CLEAR_DELALLOC
|
798 EXTENT_SET_WRITEBACK
|
799 EXTENT_END_WRITEBACK
);
801 *nr_written
= *nr_written
+
802 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
809 BUG_ON(disk_num_bytes
>
810 btrfs_super_total_bytes(&root
->fs_info
->super_copy
));
812 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
813 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
815 while (disk_num_bytes
> 0) {
818 cur_alloc_size
= disk_num_bytes
;
819 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
820 root
->sectorsize
, 0, alloc_hint
,
824 em
= alloc_extent_map(GFP_NOFS
);
827 em
->orig_start
= em
->start
;
828 ram_size
= ins
.offset
;
829 em
->len
= ins
.offset
;
831 em
->block_start
= ins
.objectid
;
832 em
->block_len
= ins
.offset
;
833 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
834 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
837 write_lock(&em_tree
->lock
);
838 ret
= add_extent_mapping(em_tree
, em
);
839 write_unlock(&em_tree
->lock
);
840 if (ret
!= -EEXIST
) {
844 btrfs_drop_extent_cache(inode
, start
,
845 start
+ ram_size
- 1, 0);
848 cur_alloc_size
= ins
.offset
;
849 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
850 ram_size
, cur_alloc_size
, 0);
853 if (root
->root_key
.objectid
==
854 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
855 ret
= btrfs_reloc_clone_csums(inode
, start
,
860 if (disk_num_bytes
< cur_alloc_size
)
863 /* we're not doing compressed IO, don't unlock the first
864 * page (which the caller expects to stay locked), don't
865 * clear any dirty bits and don't set any writeback bits
867 * Do set the Private2 bit so we know this page was properly
868 * setup for writepage
870 op
= unlock
? EXTENT_CLEAR_UNLOCK_PAGE
: 0;
871 op
|= EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
874 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
875 start
, start
+ ram_size
- 1,
877 disk_num_bytes
-= cur_alloc_size
;
878 num_bytes
-= cur_alloc_size
;
879 alloc_hint
= ins
.objectid
+ ins
.offset
;
880 start
+= cur_alloc_size
;
884 btrfs_end_transaction(trans
, root
);
890 * work queue call back to started compression on a file and pages
892 static noinline
void async_cow_start(struct btrfs_work
*work
)
894 struct async_cow
*async_cow
;
896 async_cow
= container_of(work
, struct async_cow
, work
);
898 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
899 async_cow
->start
, async_cow
->end
, async_cow
,
902 async_cow
->inode
= NULL
;
906 * work queue call back to submit previously compressed pages
908 static noinline
void async_cow_submit(struct btrfs_work
*work
)
910 struct async_cow
*async_cow
;
911 struct btrfs_root
*root
;
912 unsigned long nr_pages
;
914 async_cow
= container_of(work
, struct async_cow
, work
);
916 root
= async_cow
->root
;
917 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
920 atomic_sub(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
922 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
924 waitqueue_active(&root
->fs_info
->async_submit_wait
))
925 wake_up(&root
->fs_info
->async_submit_wait
);
927 if (async_cow
->inode
)
928 submit_compressed_extents(async_cow
->inode
, async_cow
);
931 static noinline
void async_cow_free(struct btrfs_work
*work
)
933 struct async_cow
*async_cow
;
934 async_cow
= container_of(work
, struct async_cow
, work
);
938 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
939 u64 start
, u64 end
, int *page_started
,
940 unsigned long *nr_written
)
942 struct async_cow
*async_cow
;
943 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
944 unsigned long nr_pages
;
946 int limit
= 10 * 1024 * 1042;
948 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
949 1, 0, NULL
, GFP_NOFS
);
950 while (start
< end
) {
951 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
952 async_cow
->inode
= inode
;
953 async_cow
->root
= root
;
954 async_cow
->locked_page
= locked_page
;
955 async_cow
->start
= start
;
957 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
960 cur_end
= min(end
, start
+ 512 * 1024 - 1);
962 async_cow
->end
= cur_end
;
963 INIT_LIST_HEAD(&async_cow
->extents
);
965 async_cow
->work
.func
= async_cow_start
;
966 async_cow
->work
.ordered_func
= async_cow_submit
;
967 async_cow
->work
.ordered_free
= async_cow_free
;
968 async_cow
->work
.flags
= 0;
970 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
972 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
974 btrfs_queue_worker(&root
->fs_info
->delalloc_workers
,
977 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
978 wait_event(root
->fs_info
->async_submit_wait
,
979 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
983 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
984 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
985 wait_event(root
->fs_info
->async_submit_wait
,
986 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
990 *nr_written
+= nr_pages
;
997 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
998 u64 bytenr
, u64 num_bytes
)
1001 struct btrfs_ordered_sum
*sums
;
1004 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
1005 bytenr
+ num_bytes
- 1, &list
);
1006 if (ret
== 0 && list_empty(&list
))
1009 while (!list_empty(&list
)) {
1010 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1011 list_del(&sums
->list
);
1018 * when nowcow writeback call back. This checks for snapshots or COW copies
1019 * of the extents that exist in the file, and COWs the file as required.
1021 * If no cow copies or snapshots exist, we write directly to the existing
1024 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1025 struct page
*locked_page
,
1026 u64 start
, u64 end
, int *page_started
, int force
,
1027 unsigned long *nr_written
)
1029 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1030 struct btrfs_trans_handle
*trans
;
1031 struct extent_buffer
*leaf
;
1032 struct btrfs_path
*path
;
1033 struct btrfs_file_extent_item
*fi
;
1034 struct btrfs_key found_key
;
1046 bool nolock
= false;
1048 path
= btrfs_alloc_path();
1050 if (root
== root
->fs_info
->tree_root
) {
1052 trans
= btrfs_join_transaction_nolock(root
, 1);
1054 trans
= btrfs_join_transaction(root
, 1);
1056 BUG_ON(IS_ERR(trans
));
1058 cow_start
= (u64
)-1;
1061 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
1064 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1065 leaf
= path
->nodes
[0];
1066 btrfs_item_key_to_cpu(leaf
, &found_key
,
1067 path
->slots
[0] - 1);
1068 if (found_key
.objectid
== inode
->i_ino
&&
1069 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1074 leaf
= path
->nodes
[0];
1075 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1076 ret
= btrfs_next_leaf(root
, path
);
1081 leaf
= path
->nodes
[0];
1087 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1089 if (found_key
.objectid
> inode
->i_ino
||
1090 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1091 found_key
.offset
> end
)
1094 if (found_key
.offset
> cur_offset
) {
1095 extent_end
= found_key
.offset
;
1100 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1101 struct btrfs_file_extent_item
);
1102 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1104 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1105 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1106 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1107 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1108 extent_end
= found_key
.offset
+
1109 btrfs_file_extent_num_bytes(leaf
, fi
);
1110 if (extent_end
<= start
) {
1114 if (disk_bytenr
== 0)
1116 if (btrfs_file_extent_compression(leaf
, fi
) ||
1117 btrfs_file_extent_encryption(leaf
, fi
) ||
1118 btrfs_file_extent_other_encoding(leaf
, fi
))
1120 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1122 if (btrfs_extent_readonly(root
, disk_bytenr
))
1124 if (btrfs_cross_ref_exist(trans
, root
, inode
->i_ino
,
1126 extent_offset
, disk_bytenr
))
1128 disk_bytenr
+= extent_offset
;
1129 disk_bytenr
+= cur_offset
- found_key
.offset
;
1130 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1132 * force cow if csum exists in the range.
1133 * this ensure that csum for a given extent are
1134 * either valid or do not exist.
1136 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1139 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1140 extent_end
= found_key
.offset
+
1141 btrfs_file_extent_inline_len(leaf
, fi
);
1142 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1147 if (extent_end
<= start
) {
1152 if (cow_start
== (u64
)-1)
1153 cow_start
= cur_offset
;
1154 cur_offset
= extent_end
;
1155 if (cur_offset
> end
)
1161 btrfs_release_path(root
, path
);
1162 if (cow_start
!= (u64
)-1) {
1163 ret
= cow_file_range(inode
, locked_page
, cow_start
,
1164 found_key
.offset
- 1, page_started
,
1167 cow_start
= (u64
)-1;
1170 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1171 struct extent_map
*em
;
1172 struct extent_map_tree
*em_tree
;
1173 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1174 em
= alloc_extent_map(GFP_NOFS
);
1176 em
->start
= cur_offset
;
1177 em
->orig_start
= em
->start
;
1178 em
->len
= num_bytes
;
1179 em
->block_len
= num_bytes
;
1180 em
->block_start
= disk_bytenr
;
1181 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1182 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1184 write_lock(&em_tree
->lock
);
1185 ret
= add_extent_mapping(em_tree
, em
);
1186 write_unlock(&em_tree
->lock
);
1187 if (ret
!= -EEXIST
) {
1188 free_extent_map(em
);
1191 btrfs_drop_extent_cache(inode
, em
->start
,
1192 em
->start
+ em
->len
- 1, 0);
1194 type
= BTRFS_ORDERED_PREALLOC
;
1196 type
= BTRFS_ORDERED_NOCOW
;
1199 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1200 num_bytes
, num_bytes
, type
);
1203 if (root
->root_key
.objectid
==
1204 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1205 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1210 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
1211 cur_offset
, cur_offset
+ num_bytes
- 1,
1212 locked_page
, EXTENT_CLEAR_UNLOCK_PAGE
|
1213 EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
1214 EXTENT_SET_PRIVATE2
);
1215 cur_offset
= extent_end
;
1216 if (cur_offset
> end
)
1219 btrfs_release_path(root
, path
);
1221 if (cur_offset
<= end
&& cow_start
== (u64
)-1)
1222 cow_start
= cur_offset
;
1223 if (cow_start
!= (u64
)-1) {
1224 ret
= cow_file_range(inode
, locked_page
, cow_start
, end
,
1225 page_started
, nr_written
, 1);
1230 ret
= btrfs_end_transaction_nolock(trans
, root
);
1233 ret
= btrfs_end_transaction(trans
, root
);
1236 btrfs_free_path(path
);
1241 * extent_io.c call back to do delayed allocation processing
1243 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1244 u64 start
, u64 end
, int *page_started
,
1245 unsigned long *nr_written
)
1248 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1250 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
)
1251 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1252 page_started
, 1, nr_written
);
1253 else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
)
1254 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1255 page_started
, 0, nr_written
);
1256 else if (!btrfs_test_opt(root
, COMPRESS
) &&
1257 !(BTRFS_I(inode
)->force_compress
))
1258 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1259 page_started
, nr_written
, 1);
1261 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1262 page_started
, nr_written
);
1266 static int btrfs_split_extent_hook(struct inode
*inode
,
1267 struct extent_state
*orig
, u64 split
)
1269 /* not delalloc, ignore it */
1270 if (!(orig
->state
& EXTENT_DELALLOC
))
1273 atomic_inc(&BTRFS_I(inode
)->outstanding_extents
);
1278 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1279 * extents so we can keep track of new extents that are just merged onto old
1280 * extents, such as when we are doing sequential writes, so we can properly
1281 * account for the metadata space we'll need.
1283 static int btrfs_merge_extent_hook(struct inode
*inode
,
1284 struct extent_state
*new,
1285 struct extent_state
*other
)
1287 /* not delalloc, ignore it */
1288 if (!(other
->state
& EXTENT_DELALLOC
))
1291 atomic_dec(&BTRFS_I(inode
)->outstanding_extents
);
1296 * extent_io.c set_bit_hook, used to track delayed allocation
1297 * bytes in this file, and to maintain the list of inodes that
1298 * have pending delalloc work to be done.
1300 static int btrfs_set_bit_hook(struct inode
*inode
,
1301 struct extent_state
*state
, int *bits
)
1305 * set_bit and clear bit hooks normally require _irqsave/restore
1306 * but in this case, we are only testeing for the DELALLOC
1307 * bit, which is only set or cleared with irqs on
1309 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1310 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1311 u64 len
= state
->end
+ 1 - state
->start
;
1312 int do_list
= (root
->root_key
.objectid
!=
1313 BTRFS_ROOT_TREE_OBJECTID
);
1315 if (*bits
& EXTENT_FIRST_DELALLOC
)
1316 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1318 atomic_inc(&BTRFS_I(inode
)->outstanding_extents
);
1320 spin_lock(&root
->fs_info
->delalloc_lock
);
1321 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1322 root
->fs_info
->delalloc_bytes
+= len
;
1323 if (do_list
&& list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1324 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1325 &root
->fs_info
->delalloc_inodes
);
1327 spin_unlock(&root
->fs_info
->delalloc_lock
);
1333 * extent_io.c clear_bit_hook, see set_bit_hook for why
1335 static int btrfs_clear_bit_hook(struct inode
*inode
,
1336 struct extent_state
*state
, int *bits
)
1339 * set_bit and clear bit hooks normally require _irqsave/restore
1340 * but in this case, we are only testeing for the DELALLOC
1341 * bit, which is only set or cleared with irqs on
1343 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1344 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1345 u64 len
= state
->end
+ 1 - state
->start
;
1346 int do_list
= (root
->root_key
.objectid
!=
1347 BTRFS_ROOT_TREE_OBJECTID
);
1349 if (*bits
& EXTENT_FIRST_DELALLOC
)
1350 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1351 else if (!(*bits
& EXTENT_DO_ACCOUNTING
))
1352 atomic_dec(&BTRFS_I(inode
)->outstanding_extents
);
1354 if (*bits
& EXTENT_DO_ACCOUNTING
)
1355 btrfs_delalloc_release_metadata(inode
, len
);
1357 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1359 btrfs_free_reserved_data_space(inode
, len
);
1361 spin_lock(&root
->fs_info
->delalloc_lock
);
1362 root
->fs_info
->delalloc_bytes
-= len
;
1363 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1365 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1366 !list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1367 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1369 spin_unlock(&root
->fs_info
->delalloc_lock
);
1375 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1376 * we don't create bios that span stripes or chunks
1378 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
1379 size_t size
, struct bio
*bio
,
1380 unsigned long bio_flags
)
1382 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1383 struct btrfs_mapping_tree
*map_tree
;
1384 u64 logical
= (u64
)bio
->bi_sector
<< 9;
1389 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1392 length
= bio
->bi_size
;
1393 map_tree
= &root
->fs_info
->mapping_tree
;
1394 map_length
= length
;
1395 ret
= btrfs_map_block(map_tree
, READ
, logical
,
1396 &map_length
, NULL
, 0);
1398 if (map_length
< length
+ size
)
1404 * in order to insert checksums into the metadata in large chunks,
1405 * we wait until bio submission time. All the pages in the bio are
1406 * checksummed and sums are attached onto the ordered extent record.
1408 * At IO completion time the cums attached on the ordered extent record
1409 * are inserted into the btree
1411 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1412 struct bio
*bio
, int mirror_num
,
1413 unsigned long bio_flags
,
1416 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1419 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1425 * in order to insert checksums into the metadata in large chunks,
1426 * we wait until bio submission time. All the pages in the bio are
1427 * checksummed and sums are attached onto the ordered extent record.
1429 * At IO completion time the cums attached on the ordered extent record
1430 * are inserted into the btree
1432 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1433 int mirror_num
, unsigned long bio_flags
,
1436 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1437 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1441 * extent_io.c submission hook. This does the right thing for csum calculation
1442 * on write, or reading the csums from the tree before a read
1444 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1445 int mirror_num
, unsigned long bio_flags
,
1448 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1452 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1454 if (root
== root
->fs_info
->tree_root
)
1455 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 2);
1457 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
1460 if (!(rw
& REQ_WRITE
)) {
1461 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1462 return btrfs_submit_compressed_read(inode
, bio
,
1463 mirror_num
, bio_flags
);
1464 } else if (!skip_sum
)
1465 btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1467 } else if (!skip_sum
) {
1468 /* csum items have already been cloned */
1469 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1471 /* we're doing a write, do the async checksumming */
1472 return btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1473 inode
, rw
, bio
, mirror_num
,
1474 bio_flags
, bio_offset
,
1475 __btrfs_submit_bio_start
,
1476 __btrfs_submit_bio_done
);
1480 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1484 * given a list of ordered sums record them in the inode. This happens
1485 * at IO completion time based on sums calculated at bio submission time.
1487 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1488 struct inode
*inode
, u64 file_offset
,
1489 struct list_head
*list
)
1491 struct btrfs_ordered_sum
*sum
;
1493 btrfs_set_trans_block_group(trans
, inode
);
1495 list_for_each_entry(sum
, list
, list
) {
1496 btrfs_csum_file_blocks(trans
,
1497 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1502 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1503 struct extent_state
**cached_state
)
1505 if ((end
& (PAGE_CACHE_SIZE
- 1)) == 0)
1507 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1508 cached_state
, GFP_NOFS
);
1511 /* see btrfs_writepage_start_hook for details on why this is required */
1512 struct btrfs_writepage_fixup
{
1514 struct btrfs_work work
;
1517 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1519 struct btrfs_writepage_fixup
*fixup
;
1520 struct btrfs_ordered_extent
*ordered
;
1521 struct extent_state
*cached_state
= NULL
;
1523 struct inode
*inode
;
1527 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1531 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1532 ClearPageChecked(page
);
1536 inode
= page
->mapping
->host
;
1537 page_start
= page_offset(page
);
1538 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1540 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1541 &cached_state
, GFP_NOFS
);
1543 /* already ordered? We're done */
1544 if (PagePrivate2(page
))
1547 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1549 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1550 page_end
, &cached_state
, GFP_NOFS
);
1552 btrfs_start_ordered_extent(inode
, ordered
, 1);
1557 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1558 ClearPageChecked(page
);
1560 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1561 &cached_state
, GFP_NOFS
);
1564 page_cache_release(page
);
1569 * There are a few paths in the higher layers of the kernel that directly
1570 * set the page dirty bit without asking the filesystem if it is a
1571 * good idea. This causes problems because we want to make sure COW
1572 * properly happens and the data=ordered rules are followed.
1574 * In our case any range that doesn't have the ORDERED bit set
1575 * hasn't been properly setup for IO. We kick off an async process
1576 * to fix it up. The async helper will wait for ordered extents, set
1577 * the delalloc bit and make it safe to write the page.
1579 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1581 struct inode
*inode
= page
->mapping
->host
;
1582 struct btrfs_writepage_fixup
*fixup
;
1583 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1585 /* this page is properly in the ordered list */
1586 if (TestClearPagePrivate2(page
))
1589 if (PageChecked(page
))
1592 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1596 SetPageChecked(page
);
1597 page_cache_get(page
);
1598 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
1600 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
1604 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1605 struct inode
*inode
, u64 file_pos
,
1606 u64 disk_bytenr
, u64 disk_num_bytes
,
1607 u64 num_bytes
, u64 ram_bytes
,
1608 u8 compression
, u8 encryption
,
1609 u16 other_encoding
, int extent_type
)
1611 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1612 struct btrfs_file_extent_item
*fi
;
1613 struct btrfs_path
*path
;
1614 struct extent_buffer
*leaf
;
1615 struct btrfs_key ins
;
1619 path
= btrfs_alloc_path();
1622 path
->leave_spinning
= 1;
1625 * we may be replacing one extent in the tree with another.
1626 * The new extent is pinned in the extent map, and we don't want
1627 * to drop it from the cache until it is completely in the btree.
1629 * So, tell btrfs_drop_extents to leave this extent in the cache.
1630 * the caller is expected to unpin it and allow it to be merged
1633 ret
= btrfs_drop_extents(trans
, inode
, file_pos
, file_pos
+ num_bytes
,
1637 ins
.objectid
= inode
->i_ino
;
1638 ins
.offset
= file_pos
;
1639 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1640 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
, sizeof(*fi
));
1642 leaf
= path
->nodes
[0];
1643 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1644 struct btrfs_file_extent_item
);
1645 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1646 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1647 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1648 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1649 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1650 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1651 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1652 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1653 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1654 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1656 btrfs_unlock_up_safe(path
, 1);
1657 btrfs_set_lock_blocking(leaf
);
1659 btrfs_mark_buffer_dirty(leaf
);
1661 inode_add_bytes(inode
, num_bytes
);
1663 ins
.objectid
= disk_bytenr
;
1664 ins
.offset
= disk_num_bytes
;
1665 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1666 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
1667 root
->root_key
.objectid
,
1668 inode
->i_ino
, file_pos
, &ins
);
1670 btrfs_free_path(path
);
1676 * helper function for btrfs_finish_ordered_io, this
1677 * just reads in some of the csum leaves to prime them into ram
1678 * before we start the transaction. It limits the amount of btree
1679 * reads required while inside the transaction.
1681 /* as ordered data IO finishes, this gets called so we can finish
1682 * an ordered extent if the range of bytes in the file it covers are
1685 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
)
1687 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1688 struct btrfs_trans_handle
*trans
= NULL
;
1689 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
1690 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1691 struct extent_state
*cached_state
= NULL
;
1692 int compress_type
= 0;
1694 bool nolock
= false;
1696 ret
= btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
1700 BUG_ON(!ordered_extent
);
1702 nolock
= (root
== root
->fs_info
->tree_root
);
1704 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
1705 BUG_ON(!list_empty(&ordered_extent
->list
));
1706 ret
= btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
1709 trans
= btrfs_join_transaction_nolock(root
, 1);
1711 trans
= btrfs_join_transaction(root
, 1);
1712 BUG_ON(IS_ERR(trans
));
1713 btrfs_set_trans_block_group(trans
, inode
);
1714 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1715 ret
= btrfs_update_inode(trans
, root
, inode
);
1721 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
1722 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
1723 0, &cached_state
, GFP_NOFS
);
1726 trans
= btrfs_join_transaction_nolock(root
, 1);
1728 trans
= btrfs_join_transaction(root
, 1);
1729 BUG_ON(IS_ERR(trans
));
1730 btrfs_set_trans_block_group(trans
, inode
);
1731 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1733 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
1734 compress_type
= ordered_extent
->compress_type
;
1735 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
1736 BUG_ON(compress_type
);
1737 ret
= btrfs_mark_extent_written(trans
, inode
,
1738 ordered_extent
->file_offset
,
1739 ordered_extent
->file_offset
+
1740 ordered_extent
->len
);
1743 BUG_ON(root
== root
->fs_info
->tree_root
);
1744 ret
= insert_reserved_file_extent(trans
, inode
,
1745 ordered_extent
->file_offset
,
1746 ordered_extent
->start
,
1747 ordered_extent
->disk_len
,
1748 ordered_extent
->len
,
1749 ordered_extent
->len
,
1750 compress_type
, 0, 0,
1751 BTRFS_FILE_EXTENT_REG
);
1752 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
1753 ordered_extent
->file_offset
,
1754 ordered_extent
->len
);
1757 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
1758 ordered_extent
->file_offset
+
1759 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
1761 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
1762 &ordered_extent
->list
);
1764 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
1765 ret
= btrfs_update_inode(trans
, root
, inode
);
1770 btrfs_end_transaction_nolock(trans
, root
);
1772 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
1774 btrfs_end_transaction(trans
, root
);
1778 btrfs_put_ordered_extent(ordered_extent
);
1779 /* once for the tree */
1780 btrfs_put_ordered_extent(ordered_extent
);
1785 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
1786 struct extent_state
*state
, int uptodate
)
1788 ClearPagePrivate2(page
);
1789 return btrfs_finish_ordered_io(page
->mapping
->host
, start
, end
);
1793 * When IO fails, either with EIO or csum verification fails, we
1794 * try other mirrors that might have a good copy of the data. This
1795 * io_failure_record is used to record state as we go through all the
1796 * mirrors. If another mirror has good data, the page is set up to date
1797 * and things continue. If a good mirror can't be found, the original
1798 * bio end_io callback is called to indicate things have failed.
1800 struct io_failure_record
{
1805 unsigned long bio_flags
;
1809 static int btrfs_io_failed_hook(struct bio
*failed_bio
,
1810 struct page
*page
, u64 start
, u64 end
,
1811 struct extent_state
*state
)
1813 struct io_failure_record
*failrec
= NULL
;
1815 struct extent_map
*em
;
1816 struct inode
*inode
= page
->mapping
->host
;
1817 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
1818 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1825 ret
= get_state_private(failure_tree
, start
, &private);
1827 failrec
= kmalloc(sizeof(*failrec
), GFP_NOFS
);
1830 failrec
->start
= start
;
1831 failrec
->len
= end
- start
+ 1;
1832 failrec
->last_mirror
= 0;
1833 failrec
->bio_flags
= 0;
1835 read_lock(&em_tree
->lock
);
1836 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
1837 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
1838 free_extent_map(em
);
1841 read_unlock(&em_tree
->lock
);
1843 if (!em
|| IS_ERR(em
)) {
1847 logical
= start
- em
->start
;
1848 logical
= em
->block_start
+ logical
;
1849 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
1850 logical
= em
->block_start
;
1851 failrec
->bio_flags
= EXTENT_BIO_COMPRESSED
;
1852 extent_set_compress_type(&failrec
->bio_flags
,
1855 failrec
->logical
= logical
;
1856 free_extent_map(em
);
1857 set_extent_bits(failure_tree
, start
, end
, EXTENT_LOCKED
|
1858 EXTENT_DIRTY
, GFP_NOFS
);
1859 set_state_private(failure_tree
, start
,
1860 (u64
)(unsigned long)failrec
);
1862 failrec
= (struct io_failure_record
*)(unsigned long)private;
1864 num_copies
= btrfs_num_copies(
1865 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
1866 failrec
->logical
, failrec
->len
);
1867 failrec
->last_mirror
++;
1869 spin_lock(&BTRFS_I(inode
)->io_tree
.lock
);
1870 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
1873 if (state
&& state
->start
!= failrec
->start
)
1875 spin_unlock(&BTRFS_I(inode
)->io_tree
.lock
);
1877 if (!state
|| failrec
->last_mirror
> num_copies
) {
1878 set_state_private(failure_tree
, failrec
->start
, 0);
1879 clear_extent_bits(failure_tree
, failrec
->start
,
1880 failrec
->start
+ failrec
->len
- 1,
1881 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
1885 bio
= bio_alloc(GFP_NOFS
, 1);
1886 bio
->bi_private
= state
;
1887 bio
->bi_end_io
= failed_bio
->bi_end_io
;
1888 bio
->bi_sector
= failrec
->logical
>> 9;
1889 bio
->bi_bdev
= failed_bio
->bi_bdev
;
1892 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
1893 if (failed_bio
->bi_rw
& REQ_WRITE
)
1898 BTRFS_I(inode
)->io_tree
.ops
->submit_bio_hook(inode
, rw
, bio
,
1899 failrec
->last_mirror
,
1900 failrec
->bio_flags
, 0);
1905 * each time an IO finishes, we do a fast check in the IO failure tree
1906 * to see if we need to process or clean up an io_failure_record
1908 static int btrfs_clean_io_failures(struct inode
*inode
, u64 start
)
1911 u64 private_failure
;
1912 struct io_failure_record
*failure
;
1916 if (count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
1917 (u64
)-1, 1, EXTENT_DIRTY
, 0)) {
1918 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
,
1919 start
, &private_failure
);
1921 failure
= (struct io_failure_record
*)(unsigned long)
1923 set_state_private(&BTRFS_I(inode
)->io_failure_tree
,
1925 clear_extent_bits(&BTRFS_I(inode
)->io_failure_tree
,
1927 failure
->start
+ failure
->len
- 1,
1928 EXTENT_DIRTY
| EXTENT_LOCKED
,
1937 * when reads are done, we need to check csums to verify the data is correct
1938 * if there's a match, we allow the bio to finish. If not, we go through
1939 * the io_failure_record routines to find good copies
1941 static int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
1942 struct extent_state
*state
)
1944 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
1945 struct inode
*inode
= page
->mapping
->host
;
1946 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1948 u64
private = ~(u32
)0;
1950 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1953 if (PageChecked(page
)) {
1954 ClearPageChecked(page
);
1958 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
1961 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
1962 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
1963 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
1968 if (state
&& state
->start
== start
) {
1969 private = state
->private;
1972 ret
= get_state_private(io_tree
, start
, &private);
1974 kaddr
= kmap_atomic(page
, KM_USER0
);
1978 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
1979 btrfs_csum_final(csum
, (char *)&csum
);
1980 if (csum
!= private)
1983 kunmap_atomic(kaddr
, KM_USER0
);
1985 /* if the io failure tree for this inode is non-empty,
1986 * check to see if we've recovered from a failed IO
1988 btrfs_clean_io_failures(inode
, start
);
1992 if (printk_ratelimit()) {
1993 printk(KERN_INFO
"btrfs csum failed ino %lu off %llu csum %u "
1994 "private %llu\n", page
->mapping
->host
->i_ino
,
1995 (unsigned long long)start
, csum
,
1996 (unsigned long long)private);
1998 memset(kaddr
+ offset
, 1, end
- start
+ 1);
1999 flush_dcache_page(page
);
2000 kunmap_atomic(kaddr
, KM_USER0
);
2006 struct delayed_iput
{
2007 struct list_head list
;
2008 struct inode
*inode
;
2011 void btrfs_add_delayed_iput(struct inode
*inode
)
2013 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2014 struct delayed_iput
*delayed
;
2016 if (atomic_add_unless(&inode
->i_count
, -1, 1))
2019 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
2020 delayed
->inode
= inode
;
2022 spin_lock(&fs_info
->delayed_iput_lock
);
2023 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
2024 spin_unlock(&fs_info
->delayed_iput_lock
);
2027 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
2030 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2031 struct delayed_iput
*delayed
;
2034 spin_lock(&fs_info
->delayed_iput_lock
);
2035 empty
= list_empty(&fs_info
->delayed_iputs
);
2036 spin_unlock(&fs_info
->delayed_iput_lock
);
2040 down_read(&root
->fs_info
->cleanup_work_sem
);
2041 spin_lock(&fs_info
->delayed_iput_lock
);
2042 list_splice_init(&fs_info
->delayed_iputs
, &list
);
2043 spin_unlock(&fs_info
->delayed_iput_lock
);
2045 while (!list_empty(&list
)) {
2046 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
2047 list_del(&delayed
->list
);
2048 iput(delayed
->inode
);
2051 up_read(&root
->fs_info
->cleanup_work_sem
);
2055 * calculate extra metadata reservation when snapshotting a subvolume
2056 * contains orphan files.
2058 void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle
*trans
,
2059 struct btrfs_pending_snapshot
*pending
,
2060 u64
*bytes_to_reserve
)
2062 struct btrfs_root
*root
;
2063 struct btrfs_block_rsv
*block_rsv
;
2067 root
= pending
->root
;
2068 if (!root
->orphan_block_rsv
|| list_empty(&root
->orphan_list
))
2071 block_rsv
= root
->orphan_block_rsv
;
2073 /* orphan block reservation for the snapshot */
2074 num_bytes
= block_rsv
->size
;
2077 * after the snapshot is created, COWing tree blocks may use more
2078 * space than it frees. So we should make sure there is enough
2081 index
= trans
->transid
& 0x1;
2082 if (block_rsv
->reserved
+ block_rsv
->freed
[index
] < block_rsv
->size
) {
2083 num_bytes
+= block_rsv
->size
-
2084 (block_rsv
->reserved
+ block_rsv
->freed
[index
]);
2087 *bytes_to_reserve
+= num_bytes
;
2090 void btrfs_orphan_post_snapshot(struct btrfs_trans_handle
*trans
,
2091 struct btrfs_pending_snapshot
*pending
)
2093 struct btrfs_root
*root
= pending
->root
;
2094 struct btrfs_root
*snap
= pending
->snap
;
2095 struct btrfs_block_rsv
*block_rsv
;
2100 if (!root
->orphan_block_rsv
|| list_empty(&root
->orphan_list
))
2103 /* refill source subvolume's orphan block reservation */
2104 block_rsv
= root
->orphan_block_rsv
;
2105 index
= trans
->transid
& 0x1;
2106 if (block_rsv
->reserved
+ block_rsv
->freed
[index
] < block_rsv
->size
) {
2107 num_bytes
= block_rsv
->size
-
2108 (block_rsv
->reserved
+ block_rsv
->freed
[index
]);
2109 ret
= btrfs_block_rsv_migrate(&pending
->block_rsv
,
2110 root
->orphan_block_rsv
,
2115 /* setup orphan block reservation for the snapshot */
2116 block_rsv
= btrfs_alloc_block_rsv(snap
);
2119 btrfs_add_durable_block_rsv(root
->fs_info
, block_rsv
);
2120 snap
->orphan_block_rsv
= block_rsv
;
2122 num_bytes
= root
->orphan_block_rsv
->size
;
2123 ret
= btrfs_block_rsv_migrate(&pending
->block_rsv
,
2124 block_rsv
, num_bytes
);
2128 /* insert orphan item for the snapshot */
2129 WARN_ON(!root
->orphan_item_inserted
);
2130 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
2131 snap
->root_key
.objectid
);
2133 snap
->orphan_item_inserted
= 1;
2137 enum btrfs_orphan_cleanup_state
{
2138 ORPHAN_CLEANUP_STARTED
= 1,
2139 ORPHAN_CLEANUP_DONE
= 2,
2143 * This is called in transaction commmit time. If there are no orphan
2144 * files in the subvolume, it removes orphan item and frees block_rsv
2147 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
2148 struct btrfs_root
*root
)
2152 if (!list_empty(&root
->orphan_list
) ||
2153 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
2156 if (root
->orphan_item_inserted
&&
2157 btrfs_root_refs(&root
->root_item
) > 0) {
2158 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
2159 root
->root_key
.objectid
);
2161 root
->orphan_item_inserted
= 0;
2164 if (root
->orphan_block_rsv
) {
2165 WARN_ON(root
->orphan_block_rsv
->size
> 0);
2166 btrfs_free_block_rsv(root
, root
->orphan_block_rsv
);
2167 root
->orphan_block_rsv
= NULL
;
2172 * This creates an orphan entry for the given inode in case something goes
2173 * wrong in the middle of an unlink/truncate.
2175 * NOTE: caller of this function should reserve 5 units of metadata for
2178 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2180 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2181 struct btrfs_block_rsv
*block_rsv
= NULL
;
2186 if (!root
->orphan_block_rsv
) {
2187 block_rsv
= btrfs_alloc_block_rsv(root
);
2191 spin_lock(&root
->orphan_lock
);
2192 if (!root
->orphan_block_rsv
) {
2193 root
->orphan_block_rsv
= block_rsv
;
2194 } else if (block_rsv
) {
2195 btrfs_free_block_rsv(root
, block_rsv
);
2199 if (list_empty(&BTRFS_I(inode
)->i_orphan
)) {
2200 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
2203 * For proper ENOSPC handling, we should do orphan
2204 * cleanup when mounting. But this introduces backward
2205 * compatibility issue.
2207 if (!xchg(&root
->orphan_item_inserted
, 1))
2214 WARN_ON(!BTRFS_I(inode
)->orphan_meta_reserved
);
2217 if (!BTRFS_I(inode
)->orphan_meta_reserved
) {
2218 BTRFS_I(inode
)->orphan_meta_reserved
= 1;
2221 spin_unlock(&root
->orphan_lock
);
2224 btrfs_add_durable_block_rsv(root
->fs_info
, block_rsv
);
2226 /* grab metadata reservation from transaction handle */
2228 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
2232 /* insert an orphan item to track this unlinked/truncated file */
2234 ret
= btrfs_insert_orphan_item(trans
, root
, inode
->i_ino
);
2238 /* insert an orphan item to track subvolume contains orphan files */
2240 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
2241 root
->root_key
.objectid
);
2248 * We have done the truncate/delete so we can go ahead and remove the orphan
2249 * item for this particular inode.
2251 int btrfs_orphan_del(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2253 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2254 int delete_item
= 0;
2255 int release_rsv
= 0;
2258 spin_lock(&root
->orphan_lock
);
2259 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
2260 list_del_init(&BTRFS_I(inode
)->i_orphan
);
2264 if (BTRFS_I(inode
)->orphan_meta_reserved
) {
2265 BTRFS_I(inode
)->orphan_meta_reserved
= 0;
2268 spin_unlock(&root
->orphan_lock
);
2270 if (trans
&& delete_item
) {
2271 ret
= btrfs_del_orphan_item(trans
, root
, inode
->i_ino
);
2276 btrfs_orphan_release_metadata(inode
);
2282 * this cleans up any orphans that may be left on the list from the last use
2285 void btrfs_orphan_cleanup(struct btrfs_root
*root
)
2287 struct btrfs_path
*path
;
2288 struct extent_buffer
*leaf
;
2289 struct btrfs_key key
, found_key
;
2290 struct btrfs_trans_handle
*trans
;
2291 struct inode
*inode
;
2292 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
2294 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
2297 path
= btrfs_alloc_path();
2301 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
2302 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
2303 key
.offset
= (u64
)-1;
2306 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2308 printk(KERN_ERR
"Error searching slot for orphan: %d"
2314 * if ret == 0 means we found what we were searching for, which
2315 * is weird, but possible, so only screw with path if we didnt
2316 * find the key and see if we have stuff that matches
2319 if (path
->slots
[0] == 0)
2324 /* pull out the item */
2325 leaf
= path
->nodes
[0];
2326 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2328 /* make sure the item matches what we want */
2329 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
2331 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
2334 /* release the path since we're done with it */
2335 btrfs_release_path(root
, path
);
2338 * this is where we are basically btrfs_lookup, without the
2339 * crossing root thing. we store the inode number in the
2340 * offset of the orphan item.
2342 found_key
.objectid
= found_key
.offset
;
2343 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
2344 found_key
.offset
= 0;
2345 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
2346 BUG_ON(IS_ERR(inode
));
2349 * add this inode to the orphan list so btrfs_orphan_del does
2350 * the proper thing when we hit it
2352 spin_lock(&root
->orphan_lock
);
2353 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
2354 spin_unlock(&root
->orphan_lock
);
2357 * if this is a bad inode, means we actually succeeded in
2358 * removing the inode, but not the orphan record, which means
2359 * we need to manually delete the orphan since iput will just
2360 * do a destroy_inode
2362 if (is_bad_inode(inode
)) {
2363 trans
= btrfs_start_transaction(root
, 0);
2364 BUG_ON(IS_ERR(trans
));
2365 btrfs_orphan_del(trans
, inode
);
2366 btrfs_end_transaction(trans
, root
);
2371 /* if we have links, this was a truncate, lets do that */
2372 if (inode
->i_nlink
) {
2374 btrfs_truncate(inode
);
2379 /* this will do delete_inode and everything for us */
2382 btrfs_free_path(path
);
2384 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
2386 if (root
->orphan_block_rsv
)
2387 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
2390 if (root
->orphan_block_rsv
|| root
->orphan_item_inserted
) {
2391 trans
= btrfs_join_transaction(root
, 1);
2392 BUG_ON(IS_ERR(trans
));
2393 btrfs_end_transaction(trans
, root
);
2397 printk(KERN_INFO
"btrfs: unlinked %d orphans\n", nr_unlink
);
2399 printk(KERN_INFO
"btrfs: truncated %d orphans\n", nr_truncate
);
2403 * very simple check to peek ahead in the leaf looking for xattrs. If we
2404 * don't find any xattrs, we know there can't be any acls.
2406 * slot is the slot the inode is in, objectid is the objectid of the inode
2408 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
2409 int slot
, u64 objectid
)
2411 u32 nritems
= btrfs_header_nritems(leaf
);
2412 struct btrfs_key found_key
;
2416 while (slot
< nritems
) {
2417 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2419 /* we found a different objectid, there must not be acls */
2420 if (found_key
.objectid
!= objectid
)
2423 /* we found an xattr, assume we've got an acl */
2424 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
)
2428 * we found a key greater than an xattr key, there can't
2429 * be any acls later on
2431 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
2438 * it goes inode, inode backrefs, xattrs, extents,
2439 * so if there are a ton of hard links to an inode there can
2440 * be a lot of backrefs. Don't waste time searching too hard,
2441 * this is just an optimization
2446 /* we hit the end of the leaf before we found an xattr or
2447 * something larger than an xattr. We have to assume the inode
2454 * read an inode from the btree into the in-memory inode
2456 static void btrfs_read_locked_inode(struct inode
*inode
)
2458 struct btrfs_path
*path
;
2459 struct extent_buffer
*leaf
;
2460 struct btrfs_inode_item
*inode_item
;
2461 struct btrfs_timespec
*tspec
;
2462 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2463 struct btrfs_key location
;
2465 u64 alloc_group_block
;
2469 path
= btrfs_alloc_path();
2471 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
2473 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
2477 leaf
= path
->nodes
[0];
2478 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2479 struct btrfs_inode_item
);
2481 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
2482 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
2483 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
2484 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
2485 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
2487 tspec
= btrfs_inode_atime(inode_item
);
2488 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2489 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2491 tspec
= btrfs_inode_mtime(inode_item
);
2492 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2493 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2495 tspec
= btrfs_inode_ctime(inode_item
);
2496 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2497 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2499 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
2500 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
2501 BTRFS_I(inode
)->sequence
= btrfs_inode_sequence(leaf
, inode_item
);
2502 inode
->i_generation
= BTRFS_I(inode
)->generation
;
2504 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
2506 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
2507 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
2509 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
2512 * try to precache a NULL acl entry for files that don't have
2513 * any xattrs or acls
2515 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0], inode
->i_ino
);
2517 cache_no_acl(inode
);
2519 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
, 0,
2520 alloc_group_block
, 0);
2521 btrfs_free_path(path
);
2524 switch (inode
->i_mode
& S_IFMT
) {
2526 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2527 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2528 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2529 inode
->i_fop
= &btrfs_file_operations
;
2530 inode
->i_op
= &btrfs_file_inode_operations
;
2533 inode
->i_fop
= &btrfs_dir_file_operations
;
2534 if (root
== root
->fs_info
->tree_root
)
2535 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
2537 inode
->i_op
= &btrfs_dir_inode_operations
;
2540 inode
->i_op
= &btrfs_symlink_inode_operations
;
2541 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2542 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2545 inode
->i_op
= &btrfs_special_inode_operations
;
2546 init_special_inode(inode
, inode
->i_mode
, rdev
);
2550 btrfs_update_iflags(inode
);
2554 btrfs_free_path(path
);
2555 make_bad_inode(inode
);
2559 * given a leaf and an inode, copy the inode fields into the leaf
2561 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
2562 struct extent_buffer
*leaf
,
2563 struct btrfs_inode_item
*item
,
2564 struct inode
*inode
)
2566 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
2567 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
2568 btrfs_set_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
);
2569 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
2570 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
2572 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
2573 inode
->i_atime
.tv_sec
);
2574 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
2575 inode
->i_atime
.tv_nsec
);
2577 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
2578 inode
->i_mtime
.tv_sec
);
2579 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
2580 inode
->i_mtime
.tv_nsec
);
2582 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
2583 inode
->i_ctime
.tv_sec
);
2584 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
2585 inode
->i_ctime
.tv_nsec
);
2587 btrfs_set_inode_nbytes(leaf
, item
, inode_get_bytes(inode
));
2588 btrfs_set_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
);
2589 btrfs_set_inode_sequence(leaf
, item
, BTRFS_I(inode
)->sequence
);
2590 btrfs_set_inode_transid(leaf
, item
, trans
->transid
);
2591 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
2592 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
2593 btrfs_set_inode_block_group(leaf
, item
, BTRFS_I(inode
)->block_group
);
2597 * copy everything in the in-memory inode into the btree.
2599 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
2600 struct btrfs_root
*root
, struct inode
*inode
)
2602 struct btrfs_inode_item
*inode_item
;
2603 struct btrfs_path
*path
;
2604 struct extent_buffer
*leaf
;
2607 path
= btrfs_alloc_path();
2609 path
->leave_spinning
= 1;
2610 ret
= btrfs_lookup_inode(trans
, root
, path
,
2611 &BTRFS_I(inode
)->location
, 1);
2618 btrfs_unlock_up_safe(path
, 1);
2619 leaf
= path
->nodes
[0];
2620 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2621 struct btrfs_inode_item
);
2623 fill_inode_item(trans
, leaf
, inode_item
, inode
);
2624 btrfs_mark_buffer_dirty(leaf
);
2625 btrfs_set_inode_last_trans(trans
, inode
);
2628 btrfs_free_path(path
);
2634 * unlink helper that gets used here in inode.c and in the tree logging
2635 * recovery code. It remove a link in a directory with a given name, and
2636 * also drops the back refs in the inode to the directory
2638 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
2639 struct btrfs_root
*root
,
2640 struct inode
*dir
, struct inode
*inode
,
2641 const char *name
, int name_len
)
2643 struct btrfs_path
*path
;
2645 struct extent_buffer
*leaf
;
2646 struct btrfs_dir_item
*di
;
2647 struct btrfs_key key
;
2650 path
= btrfs_alloc_path();
2656 path
->leave_spinning
= 1;
2657 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
2658 name
, name_len
, -1);
2667 leaf
= path
->nodes
[0];
2668 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2669 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2672 btrfs_release_path(root
, path
);
2674 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
2676 dir
->i_ino
, &index
);
2678 printk(KERN_INFO
"btrfs failed to delete reference to %.*s, "
2679 "inode %lu parent %lu\n", name_len
, name
,
2680 inode
->i_ino
, dir
->i_ino
);
2684 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
2685 index
, name
, name_len
, -1);
2694 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2695 btrfs_release_path(root
, path
);
2697 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
2699 BUG_ON(ret
!= 0 && ret
!= -ENOENT
);
2701 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
2706 btrfs_free_path(path
);
2710 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
2711 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
2712 btrfs_update_inode(trans
, root
, dir
);
2713 btrfs_drop_nlink(inode
);
2714 ret
= btrfs_update_inode(trans
, root
, inode
);
2719 /* helper to check if there is any shared block in the path */
2720 static int check_path_shared(struct btrfs_root
*root
,
2721 struct btrfs_path
*path
)
2723 struct extent_buffer
*eb
;
2727 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
2730 if (!path
->nodes
[level
])
2732 eb
= path
->nodes
[level
];
2733 if (!btrfs_block_can_be_shared(root
, eb
))
2735 ret
= btrfs_lookup_extent_info(NULL
, root
, eb
->start
, eb
->len
,
2744 * helper to start transaction for unlink and rmdir.
2746 * unlink and rmdir are special in btrfs, they do not always free space.
2747 * so in enospc case, we should make sure they will free space before
2748 * allowing them to use the global metadata reservation.
2750 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
,
2751 struct dentry
*dentry
)
2753 struct btrfs_trans_handle
*trans
;
2754 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2755 struct btrfs_path
*path
;
2756 struct btrfs_inode_ref
*ref
;
2757 struct btrfs_dir_item
*di
;
2758 struct inode
*inode
= dentry
->d_inode
;
2764 trans
= btrfs_start_transaction(root
, 10);
2765 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
2768 if (inode
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
2769 return ERR_PTR(-ENOSPC
);
2771 /* check if there is someone else holds reference */
2772 if (S_ISDIR(inode
->i_mode
) && atomic_read(&inode
->i_count
) > 1)
2773 return ERR_PTR(-ENOSPC
);
2775 if (atomic_read(&inode
->i_count
) > 2)
2776 return ERR_PTR(-ENOSPC
);
2778 if (xchg(&root
->fs_info
->enospc_unlink
, 1))
2779 return ERR_PTR(-ENOSPC
);
2781 path
= btrfs_alloc_path();
2783 root
->fs_info
->enospc_unlink
= 0;
2784 return ERR_PTR(-ENOMEM
);
2787 trans
= btrfs_start_transaction(root
, 0);
2788 if (IS_ERR(trans
)) {
2789 btrfs_free_path(path
);
2790 root
->fs_info
->enospc_unlink
= 0;
2794 path
->skip_locking
= 1;
2795 path
->search_commit_root
= 1;
2797 ret
= btrfs_lookup_inode(trans
, root
, path
,
2798 &BTRFS_I(dir
)->location
, 0);
2804 if (check_path_shared(root
, path
))
2809 btrfs_release_path(root
, path
);
2811 ret
= btrfs_lookup_inode(trans
, root
, path
,
2812 &BTRFS_I(inode
)->location
, 0);
2818 if (check_path_shared(root
, path
))
2823 btrfs_release_path(root
, path
);
2825 if (ret
== 0 && S_ISREG(inode
->i_mode
)) {
2826 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
2827 inode
->i_ino
, (u64
)-1, 0);
2833 if (check_path_shared(root
, path
))
2835 btrfs_release_path(root
, path
);
2843 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
2844 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
2850 if (check_path_shared(root
, path
))
2856 btrfs_release_path(root
, path
);
2858 ref
= btrfs_lookup_inode_ref(trans
, root
, path
,
2859 dentry
->d_name
.name
, dentry
->d_name
.len
,
2860 inode
->i_ino
, dir
->i_ino
, 0);
2866 if (check_path_shared(root
, path
))
2868 index
= btrfs_inode_ref_index(path
->nodes
[0], ref
);
2869 btrfs_release_path(root
, path
);
2871 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
, index
,
2872 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
2877 BUG_ON(ret
== -ENOENT
);
2878 if (check_path_shared(root
, path
))
2883 btrfs_free_path(path
);
2885 btrfs_end_transaction(trans
, root
);
2886 root
->fs_info
->enospc_unlink
= 0;
2887 return ERR_PTR(err
);
2890 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
2894 static void __unlink_end_trans(struct btrfs_trans_handle
*trans
,
2895 struct btrfs_root
*root
)
2897 if (trans
->block_rsv
== &root
->fs_info
->global_block_rsv
) {
2898 BUG_ON(!root
->fs_info
->enospc_unlink
);
2899 root
->fs_info
->enospc_unlink
= 0;
2901 btrfs_end_transaction_throttle(trans
, root
);
2904 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
2906 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2907 struct btrfs_trans_handle
*trans
;
2908 struct inode
*inode
= dentry
->d_inode
;
2910 unsigned long nr
= 0;
2912 trans
= __unlink_start_trans(dir
, dentry
);
2914 return PTR_ERR(trans
);
2916 btrfs_set_trans_block_group(trans
, dir
);
2918 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
2920 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
2921 dentry
->d_name
.name
, dentry
->d_name
.len
);
2924 if (inode
->i_nlink
== 0) {
2925 ret
= btrfs_orphan_add(trans
, inode
);
2929 nr
= trans
->blocks_used
;
2930 __unlink_end_trans(trans
, root
);
2931 btrfs_btree_balance_dirty(root
, nr
);
2935 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
2936 struct btrfs_root
*root
,
2937 struct inode
*dir
, u64 objectid
,
2938 const char *name
, int name_len
)
2940 struct btrfs_path
*path
;
2941 struct extent_buffer
*leaf
;
2942 struct btrfs_dir_item
*di
;
2943 struct btrfs_key key
;
2947 path
= btrfs_alloc_path();
2951 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
2952 name
, name_len
, -1);
2953 BUG_ON(!di
|| IS_ERR(di
));
2955 leaf
= path
->nodes
[0];
2956 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2957 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
2958 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2960 btrfs_release_path(root
, path
);
2962 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
2963 objectid
, root
->root_key
.objectid
,
2964 dir
->i_ino
, &index
, name
, name_len
);
2966 BUG_ON(ret
!= -ENOENT
);
2967 di
= btrfs_search_dir_index_item(root
, path
, dir
->i_ino
,
2969 BUG_ON(!di
|| IS_ERR(di
));
2971 leaf
= path
->nodes
[0];
2972 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2973 btrfs_release_path(root
, path
);
2977 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
2978 index
, name
, name_len
, -1);
2979 BUG_ON(!di
|| IS_ERR(di
));
2981 leaf
= path
->nodes
[0];
2982 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2983 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
2984 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2986 btrfs_release_path(root
, path
);
2988 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
2989 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
2990 ret
= btrfs_update_inode(trans
, root
, dir
);
2993 btrfs_free_path(path
);
2997 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2999 struct inode
*inode
= dentry
->d_inode
;
3001 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3002 struct btrfs_trans_handle
*trans
;
3003 unsigned long nr
= 0;
3005 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
||
3006 inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)
3009 trans
= __unlink_start_trans(dir
, dentry
);
3011 return PTR_ERR(trans
);
3013 btrfs_set_trans_block_group(trans
, dir
);
3015 if (unlikely(inode
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
3016 err
= btrfs_unlink_subvol(trans
, root
, dir
,
3017 BTRFS_I(inode
)->location
.objectid
,
3018 dentry
->d_name
.name
,
3019 dentry
->d_name
.len
);
3023 err
= btrfs_orphan_add(trans
, inode
);
3027 /* now the directory is empty */
3028 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3029 dentry
->d_name
.name
, dentry
->d_name
.len
);
3031 btrfs_i_size_write(inode
, 0);
3033 nr
= trans
->blocks_used
;
3034 __unlink_end_trans(trans
, root
);
3035 btrfs_btree_balance_dirty(root
, nr
);
3042 * when truncating bytes in a file, it is possible to avoid reading
3043 * the leaves that contain only checksum items. This can be the
3044 * majority of the IO required to delete a large file, but it must
3045 * be done carefully.
3047 * The keys in the level just above the leaves are checked to make sure
3048 * the lowest key in a given leaf is a csum key, and starts at an offset
3049 * after the new size.
3051 * Then the key for the next leaf is checked to make sure it also has
3052 * a checksum item for the same file. If it does, we know our target leaf
3053 * contains only checksum items, and it can be safely freed without reading
3056 * This is just an optimization targeted at large files. It may do
3057 * nothing. It will return 0 unless things went badly.
3059 static noinline
int drop_csum_leaves(struct btrfs_trans_handle
*trans
,
3060 struct btrfs_root
*root
,
3061 struct btrfs_path
*path
,
3062 struct inode
*inode
, u64 new_size
)
3064 struct btrfs_key key
;
3067 struct btrfs_key found_key
;
3068 struct btrfs_key other_key
;
3069 struct btrfs_leaf_ref
*ref
;
3073 path
->lowest_level
= 1;
3074 key
.objectid
= inode
->i_ino
;
3075 key
.type
= BTRFS_CSUM_ITEM_KEY
;
3076 key
.offset
= new_size
;
3078 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3082 if (path
->nodes
[1] == NULL
) {
3087 btrfs_node_key_to_cpu(path
->nodes
[1], &found_key
, path
->slots
[1]);
3088 nritems
= btrfs_header_nritems(path
->nodes
[1]);
3093 if (path
->slots
[1] >= nritems
)
3096 /* did we find a key greater than anything we want to delete? */
3097 if (found_key
.objectid
> inode
->i_ino
||
3098 (found_key
.objectid
== inode
->i_ino
&& found_key
.type
> key
.type
))
3101 /* we check the next key in the node to make sure the leave contains
3102 * only checksum items. This comparison doesn't work if our
3103 * leaf is the last one in the node
3105 if (path
->slots
[1] + 1 >= nritems
) {
3107 /* search forward from the last key in the node, this
3108 * will bring us into the next node in the tree
3110 btrfs_node_key_to_cpu(path
->nodes
[1], &found_key
, nritems
- 1);
3112 /* unlikely, but we inc below, so check to be safe */
3113 if (found_key
.offset
== (u64
)-1)
3116 /* search_forward needs a path with locks held, do the
3117 * search again for the original key. It is possible
3118 * this will race with a balance and return a path that
3119 * we could modify, but this drop is just an optimization
3120 * and is allowed to miss some leaves.
3122 btrfs_release_path(root
, path
);
3125 /* setup a max key for search_forward */
3126 other_key
.offset
= (u64
)-1;
3127 other_key
.type
= key
.type
;
3128 other_key
.objectid
= key
.objectid
;
3130 path
->keep_locks
= 1;
3131 ret
= btrfs_search_forward(root
, &found_key
, &other_key
,
3133 path
->keep_locks
= 0;
3134 if (ret
|| found_key
.objectid
!= key
.objectid
||
3135 found_key
.type
!= key
.type
) {
3140 key
.offset
= found_key
.offset
;
3141 btrfs_release_path(root
, path
);
3146 /* we know there's one more slot after us in the tree,
3147 * read that key so we can verify it is also a checksum item
3149 btrfs_node_key_to_cpu(path
->nodes
[1], &other_key
, path
->slots
[1] + 1);
3151 if (found_key
.objectid
< inode
->i_ino
)
3154 if (found_key
.type
!= key
.type
|| found_key
.offset
< new_size
)
3158 * if the key for the next leaf isn't a csum key from this objectid,
3159 * we can't be sure there aren't good items inside this leaf.
3162 if (other_key
.objectid
!= inode
->i_ino
|| other_key
.type
!= key
.type
)
3165 leaf_start
= btrfs_node_blockptr(path
->nodes
[1], path
->slots
[1]);
3166 leaf_gen
= btrfs_node_ptr_generation(path
->nodes
[1], path
->slots
[1]);
3168 * it is safe to delete this leaf, it contains only
3169 * csum items from this inode at an offset >= new_size
3171 ret
= btrfs_del_leaf(trans
, root
, path
, leaf_start
);
3174 if (root
->ref_cows
&& leaf_gen
< trans
->transid
) {
3175 ref
= btrfs_alloc_leaf_ref(root
, 0);
3177 ref
->root_gen
= root
->root_key
.offset
;
3178 ref
->bytenr
= leaf_start
;
3180 ref
->generation
= leaf_gen
;
3183 btrfs_sort_leaf_ref(ref
);
3185 ret
= btrfs_add_leaf_ref(root
, ref
, 0);
3187 btrfs_free_leaf_ref(root
, ref
);
3193 btrfs_release_path(root
, path
);
3195 if (other_key
.objectid
== inode
->i_ino
&&
3196 other_key
.type
== key
.type
&& other_key
.offset
> key
.offset
) {
3197 key
.offset
= other_key
.offset
;
3203 /* fixup any changes we've made to the path */
3204 path
->lowest_level
= 0;
3205 path
->keep_locks
= 0;
3206 btrfs_release_path(root
, path
);
3213 * this can truncate away extent items, csum items and directory items.
3214 * It starts at a high offset and removes keys until it can't find
3215 * any higher than new_size
3217 * csum items that cross the new i_size are truncated to the new size
3220 * min_type is the minimum key type to truncate down to. If set to 0, this
3221 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3223 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
3224 struct btrfs_root
*root
,
3225 struct inode
*inode
,
3226 u64 new_size
, u32 min_type
)
3228 struct btrfs_path
*path
;
3229 struct extent_buffer
*leaf
;
3230 struct btrfs_file_extent_item
*fi
;
3231 struct btrfs_key key
;
3232 struct btrfs_key found_key
;
3233 u64 extent_start
= 0;
3234 u64 extent_num_bytes
= 0;
3235 u64 extent_offset
= 0;
3237 u64 mask
= root
->sectorsize
- 1;
3238 u32 found_type
= (u8
)-1;
3241 int pending_del_nr
= 0;
3242 int pending_del_slot
= 0;
3243 int extent_type
= -1;
3248 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
3250 if (root
->ref_cows
|| root
== root
->fs_info
->tree_root
)
3251 btrfs_drop_extent_cache(inode
, new_size
& (~mask
), (u64
)-1, 0);
3253 path
= btrfs_alloc_path();
3257 key
.objectid
= inode
->i_ino
;
3258 key
.offset
= (u64
)-1;
3262 path
->leave_spinning
= 1;
3263 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3270 /* there are no items in the tree for us to truncate, we're
3273 if (path
->slots
[0] == 0)
3280 leaf
= path
->nodes
[0];
3281 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3282 found_type
= btrfs_key_type(&found_key
);
3285 if (found_key
.objectid
!= inode
->i_ino
)
3288 if (found_type
< min_type
)
3291 item_end
= found_key
.offset
;
3292 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
3293 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3294 struct btrfs_file_extent_item
);
3295 extent_type
= btrfs_file_extent_type(leaf
, fi
);
3296 encoding
= btrfs_file_extent_compression(leaf
, fi
);
3297 encoding
|= btrfs_file_extent_encryption(leaf
, fi
);
3298 encoding
|= btrfs_file_extent_other_encoding(leaf
, fi
);
3300 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3302 btrfs_file_extent_num_bytes(leaf
, fi
);
3303 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3304 item_end
+= btrfs_file_extent_inline_len(leaf
,
3309 if (found_type
> min_type
) {
3312 if (item_end
< new_size
)
3314 if (found_key
.offset
>= new_size
)
3320 /* FIXME, shrink the extent if the ref count is only 1 */
3321 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
3324 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3326 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
3327 if (!del_item
&& !encoding
) {
3328 u64 orig_num_bytes
=
3329 btrfs_file_extent_num_bytes(leaf
, fi
);
3330 extent_num_bytes
= new_size
-
3331 found_key
.offset
+ root
->sectorsize
- 1;
3332 extent_num_bytes
= extent_num_bytes
&
3333 ~((u64
)root
->sectorsize
- 1);
3334 btrfs_set_file_extent_num_bytes(leaf
, fi
,
3336 num_dec
= (orig_num_bytes
-
3338 if (root
->ref_cows
&& extent_start
!= 0)
3339 inode_sub_bytes(inode
, num_dec
);
3340 btrfs_mark_buffer_dirty(leaf
);
3343 btrfs_file_extent_disk_num_bytes(leaf
,
3345 extent_offset
= found_key
.offset
-
3346 btrfs_file_extent_offset(leaf
, fi
);
3348 /* FIXME blocksize != 4096 */
3349 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
3350 if (extent_start
!= 0) {
3353 inode_sub_bytes(inode
, num_dec
);
3356 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3358 * we can't truncate inline items that have had
3362 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
3363 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
3364 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
3365 u32 size
= new_size
- found_key
.offset
;
3367 if (root
->ref_cows
) {
3368 inode_sub_bytes(inode
, item_end
+ 1 -
3372 btrfs_file_extent_calc_inline_size(size
);
3373 ret
= btrfs_truncate_item(trans
, root
, path
,
3376 } else if (root
->ref_cows
) {
3377 inode_sub_bytes(inode
, item_end
+ 1 -
3383 if (!pending_del_nr
) {
3384 /* no pending yet, add ourselves */
3385 pending_del_slot
= path
->slots
[0];
3387 } else if (pending_del_nr
&&
3388 path
->slots
[0] + 1 == pending_del_slot
) {
3389 /* hop on the pending chunk */
3391 pending_del_slot
= path
->slots
[0];
3398 if (found_extent
&& (root
->ref_cows
||
3399 root
== root
->fs_info
->tree_root
)) {
3400 btrfs_set_path_blocking(path
);
3401 ret
= btrfs_free_extent(trans
, root
, extent_start
,
3402 extent_num_bytes
, 0,
3403 btrfs_header_owner(leaf
),
3404 inode
->i_ino
, extent_offset
);
3408 if (found_type
== BTRFS_INODE_ITEM_KEY
)
3411 if (path
->slots
[0] == 0 ||
3412 path
->slots
[0] != pending_del_slot
) {
3413 if (root
->ref_cows
) {
3417 if (pending_del_nr
) {
3418 ret
= btrfs_del_items(trans
, root
, path
,
3424 btrfs_release_path(root
, path
);
3431 if (pending_del_nr
) {
3432 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
3436 btrfs_free_path(path
);
3441 * taken from block_truncate_page, but does cow as it zeros out
3442 * any bytes left in the last page in the file.
3444 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
3446 struct inode
*inode
= mapping
->host
;
3447 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3448 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3449 struct btrfs_ordered_extent
*ordered
;
3450 struct extent_state
*cached_state
= NULL
;
3452 u32 blocksize
= root
->sectorsize
;
3453 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
3454 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
3460 if ((offset
& (blocksize
- 1)) == 0)
3462 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
3468 page
= grab_cache_page(mapping
, index
);
3470 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
3474 page_start
= page_offset(page
);
3475 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3477 if (!PageUptodate(page
)) {
3478 ret
= btrfs_readpage(NULL
, page
);
3480 if (page
->mapping
!= mapping
) {
3482 page_cache_release(page
);
3485 if (!PageUptodate(page
)) {
3490 wait_on_page_writeback(page
);
3492 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
,
3494 set_page_extent_mapped(page
);
3496 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
3498 unlock_extent_cached(io_tree
, page_start
, page_end
,
3499 &cached_state
, GFP_NOFS
);
3501 page_cache_release(page
);
3502 btrfs_start_ordered_extent(inode
, ordered
, 1);
3503 btrfs_put_ordered_extent(ordered
);
3507 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
3508 EXTENT_DIRTY
| EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
,
3509 0, 0, &cached_state
, GFP_NOFS
);
3511 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
3514 unlock_extent_cached(io_tree
, page_start
, page_end
,
3515 &cached_state
, GFP_NOFS
);
3520 if (offset
!= PAGE_CACHE_SIZE
) {
3522 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
3523 flush_dcache_page(page
);
3526 ClearPageChecked(page
);
3527 set_page_dirty(page
);
3528 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
3533 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
3535 page_cache_release(page
);
3540 int btrfs_cont_expand(struct inode
*inode
, loff_t size
)
3542 struct btrfs_trans_handle
*trans
;
3543 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3544 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3545 struct extent_map
*em
= NULL
;
3546 struct extent_state
*cached_state
= NULL
;
3547 u64 mask
= root
->sectorsize
- 1;
3548 u64 hole_start
= (inode
->i_size
+ mask
) & ~mask
;
3549 u64 block_end
= (size
+ mask
) & ~mask
;
3555 if (size
<= hole_start
)
3559 struct btrfs_ordered_extent
*ordered
;
3560 btrfs_wait_ordered_range(inode
, hole_start
,
3561 block_end
- hole_start
);
3562 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
3563 &cached_state
, GFP_NOFS
);
3564 ordered
= btrfs_lookup_ordered_extent(inode
, hole_start
);
3567 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
3568 &cached_state
, GFP_NOFS
);
3569 btrfs_put_ordered_extent(ordered
);
3572 cur_offset
= hole_start
;
3574 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
3575 block_end
- cur_offset
, 0);
3576 BUG_ON(IS_ERR(em
) || !em
);
3577 last_byte
= min(extent_map_end(em
), block_end
);
3578 last_byte
= (last_byte
+ mask
) & ~mask
;
3579 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
3581 hole_size
= last_byte
- cur_offset
;
3583 trans
= btrfs_start_transaction(root
, 2);
3584 if (IS_ERR(trans
)) {
3585 err
= PTR_ERR(trans
);
3588 btrfs_set_trans_block_group(trans
, inode
);
3590 err
= btrfs_drop_extents(trans
, inode
, cur_offset
,
3591 cur_offset
+ hole_size
,
3595 err
= btrfs_insert_file_extent(trans
, root
,
3596 inode
->i_ino
, cur_offset
, 0,
3597 0, hole_size
, 0, hole_size
,
3601 btrfs_drop_extent_cache(inode
, hole_start
,
3604 btrfs_end_transaction(trans
, root
);
3606 free_extent_map(em
);
3608 cur_offset
= last_byte
;
3609 if (cur_offset
>= block_end
)
3613 free_extent_map(em
);
3614 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
3619 static int btrfs_setattr_size(struct inode
*inode
, struct iattr
*attr
)
3621 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3622 struct btrfs_trans_handle
*trans
;
3626 if (attr
->ia_size
== inode
->i_size
)
3629 if (attr
->ia_size
> inode
->i_size
) {
3630 unsigned long limit
;
3631 limit
= current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
3632 if (attr
->ia_size
> inode
->i_sb
->s_maxbytes
)
3634 if (limit
!= RLIM_INFINITY
&& attr
->ia_size
> limit
) {
3635 send_sig(SIGXFSZ
, current
, 0);
3640 trans
= btrfs_start_transaction(root
, 5);
3642 return PTR_ERR(trans
);
3644 btrfs_set_trans_block_group(trans
, inode
);
3646 ret
= btrfs_orphan_add(trans
, inode
);
3649 nr
= trans
->blocks_used
;
3650 btrfs_end_transaction(trans
, root
);
3651 btrfs_btree_balance_dirty(root
, nr
);
3653 if (attr
->ia_size
> inode
->i_size
) {
3654 ret
= btrfs_cont_expand(inode
, attr
->ia_size
);
3656 btrfs_truncate(inode
);
3660 i_size_write(inode
, attr
->ia_size
);
3661 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
3663 trans
= btrfs_start_transaction(root
, 0);
3664 BUG_ON(IS_ERR(trans
));
3665 btrfs_set_trans_block_group(trans
, inode
);
3666 trans
->block_rsv
= root
->orphan_block_rsv
;
3667 BUG_ON(!trans
->block_rsv
);
3669 ret
= btrfs_update_inode(trans
, root
, inode
);
3671 if (inode
->i_nlink
> 0) {
3672 ret
= btrfs_orphan_del(trans
, inode
);
3675 nr
= trans
->blocks_used
;
3676 btrfs_end_transaction(trans
, root
);
3677 btrfs_btree_balance_dirty(root
, nr
);
3682 * We're truncating a file that used to have good data down to
3683 * zero. Make sure it gets into the ordered flush list so that
3684 * any new writes get down to disk quickly.
3686 if (attr
->ia_size
== 0)
3687 BTRFS_I(inode
)->ordered_data_close
= 1;
3689 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3690 ret
= vmtruncate(inode
, attr
->ia_size
);
3696 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
3698 struct inode
*inode
= dentry
->d_inode
;
3699 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3702 if (btrfs_root_readonly(root
))
3705 err
= inode_change_ok(inode
, attr
);
3709 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
3710 err
= btrfs_setattr_size(inode
, attr
);
3715 if (attr
->ia_valid
) {
3716 setattr_copy(inode
, attr
);
3717 mark_inode_dirty(inode
);
3719 if (attr
->ia_valid
& ATTR_MODE
)
3720 err
= btrfs_acl_chmod(inode
);
3726 void btrfs_evict_inode(struct inode
*inode
)
3728 struct btrfs_trans_handle
*trans
;
3729 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3733 truncate_inode_pages(&inode
->i_data
, 0);
3734 if (inode
->i_nlink
&& (btrfs_root_refs(&root
->root_item
) != 0 ||
3735 root
== root
->fs_info
->tree_root
))
3738 if (is_bad_inode(inode
)) {
3739 btrfs_orphan_del(NULL
, inode
);
3742 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
3743 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
3745 if (root
->fs_info
->log_root_recovering
) {
3746 BUG_ON(!list_empty(&BTRFS_I(inode
)->i_orphan
));
3750 if (inode
->i_nlink
> 0) {
3751 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0);
3755 btrfs_i_size_write(inode
, 0);
3758 trans
= btrfs_start_transaction(root
, 0);
3759 BUG_ON(IS_ERR(trans
));
3760 btrfs_set_trans_block_group(trans
, inode
);
3761 trans
->block_rsv
= root
->orphan_block_rsv
;
3763 ret
= btrfs_block_rsv_check(trans
, root
,
3764 root
->orphan_block_rsv
, 0, 5);
3766 BUG_ON(ret
!= -EAGAIN
);
3767 ret
= btrfs_commit_transaction(trans
, root
);
3772 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
3776 nr
= trans
->blocks_used
;
3777 btrfs_end_transaction(trans
, root
);
3779 btrfs_btree_balance_dirty(root
, nr
);
3784 ret
= btrfs_orphan_del(trans
, inode
);
3788 nr
= trans
->blocks_used
;
3789 btrfs_end_transaction(trans
, root
);
3790 btrfs_btree_balance_dirty(root
, nr
);
3792 end_writeback(inode
);
3797 * this returns the key found in the dir entry in the location pointer.
3798 * If no dir entries were found, location->objectid is 0.
3800 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
3801 struct btrfs_key
*location
)
3803 const char *name
= dentry
->d_name
.name
;
3804 int namelen
= dentry
->d_name
.len
;
3805 struct btrfs_dir_item
*di
;
3806 struct btrfs_path
*path
;
3807 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3810 path
= btrfs_alloc_path();
3813 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
3818 if (!di
|| IS_ERR(di
))
3821 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
3823 btrfs_free_path(path
);
3826 location
->objectid
= 0;
3831 * when we hit a tree root in a directory, the btrfs part of the inode
3832 * needs to be changed to reflect the root directory of the tree root. This
3833 * is kind of like crossing a mount point.
3835 static int fixup_tree_root_location(struct btrfs_root
*root
,
3837 struct dentry
*dentry
,
3838 struct btrfs_key
*location
,
3839 struct btrfs_root
**sub_root
)
3841 struct btrfs_path
*path
;
3842 struct btrfs_root
*new_root
;
3843 struct btrfs_root_ref
*ref
;
3844 struct extent_buffer
*leaf
;
3848 path
= btrfs_alloc_path();
3855 ret
= btrfs_find_root_ref(root
->fs_info
->tree_root
, path
,
3856 BTRFS_I(dir
)->root
->root_key
.objectid
,
3857 location
->objectid
);
3864 leaf
= path
->nodes
[0];
3865 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
3866 if (btrfs_root_ref_dirid(leaf
, ref
) != dir
->i_ino
||
3867 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
3870 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
3871 (unsigned long)(ref
+ 1),
3872 dentry
->d_name
.len
);
3876 btrfs_release_path(root
->fs_info
->tree_root
, path
);
3878 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
3879 if (IS_ERR(new_root
)) {
3880 err
= PTR_ERR(new_root
);
3884 if (btrfs_root_refs(&new_root
->root_item
) == 0) {
3889 *sub_root
= new_root
;
3890 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
3891 location
->type
= BTRFS_INODE_ITEM_KEY
;
3892 location
->offset
= 0;
3895 btrfs_free_path(path
);
3899 static void inode_tree_add(struct inode
*inode
)
3901 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3902 struct btrfs_inode
*entry
;
3904 struct rb_node
*parent
;
3906 p
= &root
->inode_tree
.rb_node
;
3909 if (inode_unhashed(inode
))
3912 spin_lock(&root
->inode_lock
);
3915 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
3917 if (inode
->i_ino
< entry
->vfs_inode
.i_ino
)
3918 p
= &parent
->rb_left
;
3919 else if (inode
->i_ino
> entry
->vfs_inode
.i_ino
)
3920 p
= &parent
->rb_right
;
3922 WARN_ON(!(entry
->vfs_inode
.i_state
&
3923 (I_WILL_FREE
| I_FREEING
)));
3924 rb_erase(parent
, &root
->inode_tree
);
3925 RB_CLEAR_NODE(parent
);
3926 spin_unlock(&root
->inode_lock
);
3930 rb_link_node(&BTRFS_I(inode
)->rb_node
, parent
, p
);
3931 rb_insert_color(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
3932 spin_unlock(&root
->inode_lock
);
3935 static void inode_tree_del(struct inode
*inode
)
3937 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3940 spin_lock(&root
->inode_lock
);
3941 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
3942 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
3943 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
3944 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
3946 spin_unlock(&root
->inode_lock
);
3949 * Free space cache has inodes in the tree root, but the tree root has a
3950 * root_refs of 0, so this could end up dropping the tree root as a
3951 * snapshot, so we need the extra !root->fs_info->tree_root check to
3952 * make sure we don't drop it.
3954 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0 &&
3955 root
!= root
->fs_info
->tree_root
) {
3956 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
3957 spin_lock(&root
->inode_lock
);
3958 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
3959 spin_unlock(&root
->inode_lock
);
3961 btrfs_add_dead_root(root
);
3965 int btrfs_invalidate_inodes(struct btrfs_root
*root
)
3967 struct rb_node
*node
;
3968 struct rb_node
*prev
;
3969 struct btrfs_inode
*entry
;
3970 struct inode
*inode
;
3973 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
3975 spin_lock(&root
->inode_lock
);
3977 node
= root
->inode_tree
.rb_node
;
3981 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
3983 if (objectid
< entry
->vfs_inode
.i_ino
)
3984 node
= node
->rb_left
;
3985 else if (objectid
> entry
->vfs_inode
.i_ino
)
3986 node
= node
->rb_right
;
3992 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
3993 if (objectid
<= entry
->vfs_inode
.i_ino
) {
3997 prev
= rb_next(prev
);
4001 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4002 objectid
= entry
->vfs_inode
.i_ino
+ 1;
4003 inode
= igrab(&entry
->vfs_inode
);
4005 spin_unlock(&root
->inode_lock
);
4006 if (atomic_read(&inode
->i_count
) > 1)
4007 d_prune_aliases(inode
);
4009 * btrfs_drop_inode will have it removed from
4010 * the inode cache when its usage count
4015 spin_lock(&root
->inode_lock
);
4019 if (cond_resched_lock(&root
->inode_lock
))
4022 node
= rb_next(node
);
4024 spin_unlock(&root
->inode_lock
);
4028 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
4030 struct btrfs_iget_args
*args
= p
;
4031 inode
->i_ino
= args
->ino
;
4032 BTRFS_I(inode
)->root
= args
->root
;
4033 btrfs_set_inode_space_info(args
->root
, inode
);
4037 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
4039 struct btrfs_iget_args
*args
= opaque
;
4040 return args
->ino
== inode
->i_ino
&&
4041 args
->root
== BTRFS_I(inode
)->root
;
4044 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
4046 struct btrfs_root
*root
)
4048 struct inode
*inode
;
4049 struct btrfs_iget_args args
;
4050 args
.ino
= objectid
;
4053 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
4054 btrfs_init_locked_inode
,
4059 /* Get an inode object given its location and corresponding root.
4060 * Returns in *is_new if the inode was read from disk
4062 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
4063 struct btrfs_root
*root
, int *new)
4065 struct inode
*inode
;
4067 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
4069 return ERR_PTR(-ENOMEM
);
4071 if (inode
->i_state
& I_NEW
) {
4072 BTRFS_I(inode
)->root
= root
;
4073 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
4074 btrfs_read_locked_inode(inode
);
4076 inode_tree_add(inode
);
4077 unlock_new_inode(inode
);
4085 static struct inode
*new_simple_dir(struct super_block
*s
,
4086 struct btrfs_key
*key
,
4087 struct btrfs_root
*root
)
4089 struct inode
*inode
= new_inode(s
);
4092 return ERR_PTR(-ENOMEM
);
4094 BTRFS_I(inode
)->root
= root
;
4095 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
4096 BTRFS_I(inode
)->dummy_inode
= 1;
4098 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
4099 inode
->i_op
= &simple_dir_inode_operations
;
4100 inode
->i_fop
= &simple_dir_operations
;
4101 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
4102 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4107 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
4109 struct inode
*inode
;
4110 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4111 struct btrfs_root
*sub_root
= root
;
4112 struct btrfs_key location
;
4116 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
4117 return ERR_PTR(-ENAMETOOLONG
);
4119 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
4122 return ERR_PTR(ret
);
4124 if (location
.objectid
== 0)
4127 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
4128 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
4132 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
4134 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
4135 ret
= fixup_tree_root_location(root
, dir
, dentry
,
4136 &location
, &sub_root
);
4139 inode
= ERR_PTR(ret
);
4141 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
4143 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
4145 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
4147 if (!IS_ERR(inode
) && root
!= sub_root
) {
4148 down_read(&root
->fs_info
->cleanup_work_sem
);
4149 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
4150 btrfs_orphan_cleanup(sub_root
);
4151 up_read(&root
->fs_info
->cleanup_work_sem
);
4157 static int btrfs_dentry_delete(const struct dentry
*dentry
)
4159 struct btrfs_root
*root
;
4161 if (!dentry
->d_inode
&& !IS_ROOT(dentry
))
4162 dentry
= dentry
->d_parent
;
4164 if (dentry
->d_inode
) {
4165 root
= BTRFS_I(dentry
->d_inode
)->root
;
4166 if (btrfs_root_refs(&root
->root_item
) == 0)
4172 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
4173 struct nameidata
*nd
)
4175 struct inode
*inode
;
4177 inode
= btrfs_lookup_dentry(dir
, dentry
);
4179 return ERR_CAST(inode
);
4181 return d_splice_alias(inode
, dentry
);
4184 static unsigned char btrfs_filetype_table
[] = {
4185 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
4188 static int btrfs_real_readdir(struct file
*filp
, void *dirent
,
4191 struct inode
*inode
= filp
->f_dentry
->d_inode
;
4192 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4193 struct btrfs_item
*item
;
4194 struct btrfs_dir_item
*di
;
4195 struct btrfs_key key
;
4196 struct btrfs_key found_key
;
4197 struct btrfs_path
*path
;
4200 struct extent_buffer
*leaf
;
4203 unsigned char d_type
;
4208 int key_type
= BTRFS_DIR_INDEX_KEY
;
4213 /* FIXME, use a real flag for deciding about the key type */
4214 if (root
->fs_info
->tree_root
== root
)
4215 key_type
= BTRFS_DIR_ITEM_KEY
;
4217 /* special case for "." */
4218 if (filp
->f_pos
== 0) {
4219 over
= filldir(dirent
, ".", 1,
4226 /* special case for .., just use the back ref */
4227 if (filp
->f_pos
== 1) {
4228 u64 pino
= parent_ino(filp
->f_path
.dentry
);
4229 over
= filldir(dirent
, "..", 2,
4235 path
= btrfs_alloc_path();
4238 btrfs_set_key_type(&key
, key_type
);
4239 key
.offset
= filp
->f_pos
;
4240 key
.objectid
= inode
->i_ino
;
4242 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4248 leaf
= path
->nodes
[0];
4249 nritems
= btrfs_header_nritems(leaf
);
4250 slot
= path
->slots
[0];
4251 if (advance
|| slot
>= nritems
) {
4252 if (slot
>= nritems
- 1) {
4253 ret
= btrfs_next_leaf(root
, path
);
4256 leaf
= path
->nodes
[0];
4257 nritems
= btrfs_header_nritems(leaf
);
4258 slot
= path
->slots
[0];
4266 item
= btrfs_item_nr(leaf
, slot
);
4267 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
4269 if (found_key
.objectid
!= key
.objectid
)
4271 if (btrfs_key_type(&found_key
) != key_type
)
4273 if (found_key
.offset
< filp
->f_pos
)
4276 filp
->f_pos
= found_key
.offset
;
4278 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
4280 di_total
= btrfs_item_size(leaf
, item
);
4282 while (di_cur
< di_total
) {
4283 struct btrfs_key location
;
4285 name_len
= btrfs_dir_name_len(leaf
, di
);
4286 if (name_len
<= sizeof(tmp_name
)) {
4287 name_ptr
= tmp_name
;
4289 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
4295 read_extent_buffer(leaf
, name_ptr
,
4296 (unsigned long)(di
+ 1), name_len
);
4298 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
4299 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
4301 /* is this a reference to our own snapshot? If so
4304 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
4305 location
.objectid
== root
->root_key
.objectid
) {
4309 over
= filldir(dirent
, name_ptr
, name_len
,
4310 found_key
.offset
, location
.objectid
,
4314 if (name_ptr
!= tmp_name
)
4319 di_len
= btrfs_dir_name_len(leaf
, di
) +
4320 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
4322 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
4326 /* Reached end of directory/root. Bump pos past the last item. */
4327 if (key_type
== BTRFS_DIR_INDEX_KEY
)
4329 * 32-bit glibc will use getdents64, but then strtol -
4330 * so the last number we can serve is this.
4332 filp
->f_pos
= 0x7fffffff;
4338 btrfs_free_path(path
);
4342 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
4344 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4345 struct btrfs_trans_handle
*trans
;
4347 bool nolock
= false;
4349 if (BTRFS_I(inode
)->dummy_inode
)
4353 nolock
= (root
->fs_info
->closing
&& root
== root
->fs_info
->tree_root
);
4355 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
4357 trans
= btrfs_join_transaction_nolock(root
, 1);
4359 trans
= btrfs_join_transaction(root
, 1);
4361 return PTR_ERR(trans
);
4362 btrfs_set_trans_block_group(trans
, inode
);
4364 ret
= btrfs_end_transaction_nolock(trans
, root
);
4366 ret
= btrfs_commit_transaction(trans
, root
);
4372 * This is somewhat expensive, updating the tree every time the
4373 * inode changes. But, it is most likely to find the inode in cache.
4374 * FIXME, needs more benchmarking...there are no reasons other than performance
4375 * to keep or drop this code.
4377 void btrfs_dirty_inode(struct inode
*inode
)
4379 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4380 struct btrfs_trans_handle
*trans
;
4383 if (BTRFS_I(inode
)->dummy_inode
)
4386 trans
= btrfs_join_transaction(root
, 1);
4387 BUG_ON(IS_ERR(trans
));
4388 btrfs_set_trans_block_group(trans
, inode
);
4390 ret
= btrfs_update_inode(trans
, root
, inode
);
4391 if (ret
&& ret
== -ENOSPC
) {
4392 /* whoops, lets try again with the full transaction */
4393 btrfs_end_transaction(trans
, root
);
4394 trans
= btrfs_start_transaction(root
, 1);
4395 if (IS_ERR(trans
)) {
4396 if (printk_ratelimit()) {
4397 printk(KERN_ERR
"btrfs: fail to "
4398 "dirty inode %lu error %ld\n",
4399 inode
->i_ino
, PTR_ERR(trans
));
4403 btrfs_set_trans_block_group(trans
, inode
);
4405 ret
= btrfs_update_inode(trans
, root
, inode
);
4407 if (printk_ratelimit()) {
4408 printk(KERN_ERR
"btrfs: fail to "
4409 "dirty inode %lu error %d\n",
4414 btrfs_end_transaction(trans
, root
);
4418 * find the highest existing sequence number in a directory
4419 * and then set the in-memory index_cnt variable to reflect
4420 * free sequence numbers
4422 static int btrfs_set_inode_index_count(struct inode
*inode
)
4424 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4425 struct btrfs_key key
, found_key
;
4426 struct btrfs_path
*path
;
4427 struct extent_buffer
*leaf
;
4430 key
.objectid
= inode
->i_ino
;
4431 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
4432 key
.offset
= (u64
)-1;
4434 path
= btrfs_alloc_path();
4438 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4441 /* FIXME: we should be able to handle this */
4447 * MAGIC NUMBER EXPLANATION:
4448 * since we search a directory based on f_pos we have to start at 2
4449 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4450 * else has to start at 2
4452 if (path
->slots
[0] == 0) {
4453 BTRFS_I(inode
)->index_cnt
= 2;
4459 leaf
= path
->nodes
[0];
4460 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4462 if (found_key
.objectid
!= inode
->i_ino
||
4463 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
4464 BTRFS_I(inode
)->index_cnt
= 2;
4468 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
4470 btrfs_free_path(path
);
4475 * helper to find a free sequence number in a given directory. This current
4476 * code is very simple, later versions will do smarter things in the btree
4478 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
4482 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
4483 ret
= btrfs_set_inode_index_count(dir
);
4488 *index
= BTRFS_I(dir
)->index_cnt
;
4489 BTRFS_I(dir
)->index_cnt
++;
4494 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
4495 struct btrfs_root
*root
,
4497 const char *name
, int name_len
,
4498 u64 ref_objectid
, u64 objectid
,
4499 u64 alloc_hint
, int mode
, u64
*index
)
4501 struct inode
*inode
;
4502 struct btrfs_inode_item
*inode_item
;
4503 struct btrfs_key
*location
;
4504 struct btrfs_path
*path
;
4505 struct btrfs_inode_ref
*ref
;
4506 struct btrfs_key key
[2];
4512 path
= btrfs_alloc_path();
4515 inode
= new_inode(root
->fs_info
->sb
);
4517 return ERR_PTR(-ENOMEM
);
4520 ret
= btrfs_set_inode_index(dir
, index
);
4523 return ERR_PTR(ret
);
4527 * index_cnt is ignored for everything but a dir,
4528 * btrfs_get_inode_index_count has an explanation for the magic
4531 BTRFS_I(inode
)->index_cnt
= 2;
4532 BTRFS_I(inode
)->root
= root
;
4533 BTRFS_I(inode
)->generation
= trans
->transid
;
4534 inode
->i_generation
= BTRFS_I(inode
)->generation
;
4535 btrfs_set_inode_space_info(root
, inode
);
4541 BTRFS_I(inode
)->block_group
=
4542 btrfs_find_block_group(root
, 0, alloc_hint
, owner
);
4544 key
[0].objectid
= objectid
;
4545 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
4548 key
[1].objectid
= objectid
;
4549 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
4550 key
[1].offset
= ref_objectid
;
4552 sizes
[0] = sizeof(struct btrfs_inode_item
);
4553 sizes
[1] = name_len
+ sizeof(*ref
);
4555 path
->leave_spinning
= 1;
4556 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
4560 inode_init_owner(inode
, dir
, mode
);
4561 inode
->i_ino
= objectid
;
4562 inode_set_bytes(inode
, 0);
4563 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4564 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4565 struct btrfs_inode_item
);
4566 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
4568 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
4569 struct btrfs_inode_ref
);
4570 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
4571 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
4572 ptr
= (unsigned long)(ref
+ 1);
4573 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
4575 btrfs_mark_buffer_dirty(path
->nodes
[0]);
4576 btrfs_free_path(path
);
4578 location
= &BTRFS_I(inode
)->location
;
4579 location
->objectid
= objectid
;
4580 location
->offset
= 0;
4581 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
4583 btrfs_inherit_iflags(inode
, dir
);
4585 if ((mode
& S_IFREG
)) {
4586 if (btrfs_test_opt(root
, NODATASUM
))
4587 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
4588 if (btrfs_test_opt(root
, NODATACOW
))
4589 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
;
4592 insert_inode_hash(inode
);
4593 inode_tree_add(inode
);
4597 BTRFS_I(dir
)->index_cnt
--;
4598 btrfs_free_path(path
);
4600 return ERR_PTR(ret
);
4603 static inline u8
btrfs_inode_type(struct inode
*inode
)
4605 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
4609 * utility function to add 'inode' into 'parent_inode' with
4610 * a give name and a given sequence number.
4611 * if 'add_backref' is true, also insert a backref from the
4612 * inode to the parent directory.
4614 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
4615 struct inode
*parent_inode
, struct inode
*inode
,
4616 const char *name
, int name_len
, int add_backref
, u64 index
)
4619 struct btrfs_key key
;
4620 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
4622 if (unlikely(inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4623 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
4625 key
.objectid
= inode
->i_ino
;
4626 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
4630 if (unlikely(inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4631 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
4632 key
.objectid
, root
->root_key
.objectid
,
4633 parent_inode
->i_ino
,
4634 index
, name
, name_len
);
4635 } else if (add_backref
) {
4636 ret
= btrfs_insert_inode_ref(trans
, root
,
4637 name
, name_len
, inode
->i_ino
,
4638 parent_inode
->i_ino
, index
);
4642 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
4643 parent_inode
->i_ino
, &key
,
4644 btrfs_inode_type(inode
), index
);
4647 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
4649 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
4650 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
4655 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
4656 struct inode
*dir
, struct dentry
*dentry
,
4657 struct inode
*inode
, int backref
, u64 index
)
4659 int err
= btrfs_add_link(trans
, dir
, inode
,
4660 dentry
->d_name
.name
, dentry
->d_name
.len
,
4663 d_instantiate(dentry
, inode
);
4671 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
4672 int mode
, dev_t rdev
)
4674 struct btrfs_trans_handle
*trans
;
4675 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4676 struct inode
*inode
= NULL
;
4680 unsigned long nr
= 0;
4683 if (!new_valid_dev(rdev
))
4686 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
4691 * 2 for inode item and ref
4693 * 1 for xattr if selinux is on
4695 trans
= btrfs_start_transaction(root
, 5);
4697 return PTR_ERR(trans
);
4699 btrfs_set_trans_block_group(trans
, dir
);
4701 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4702 dentry
->d_name
.len
, dir
->i_ino
, objectid
,
4703 BTRFS_I(dir
)->block_group
, mode
, &index
);
4704 err
= PTR_ERR(inode
);
4708 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
4714 btrfs_set_trans_block_group(trans
, inode
);
4715 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
4719 inode
->i_op
= &btrfs_special_inode_operations
;
4720 init_special_inode(inode
, inode
->i_mode
, rdev
);
4721 btrfs_update_inode(trans
, root
, inode
);
4723 btrfs_update_inode_block_group(trans
, inode
);
4724 btrfs_update_inode_block_group(trans
, dir
);
4726 nr
= trans
->blocks_used
;
4727 btrfs_end_transaction_throttle(trans
, root
);
4728 btrfs_btree_balance_dirty(root
, nr
);
4730 inode_dec_link_count(inode
);
4736 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
4737 int mode
, struct nameidata
*nd
)
4739 struct btrfs_trans_handle
*trans
;
4740 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4741 struct inode
*inode
= NULL
;
4744 unsigned long nr
= 0;
4748 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
4752 * 2 for inode item and ref
4754 * 1 for xattr if selinux is on
4756 trans
= btrfs_start_transaction(root
, 5);
4758 return PTR_ERR(trans
);
4760 btrfs_set_trans_block_group(trans
, dir
);
4762 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4763 dentry
->d_name
.len
, dir
->i_ino
, objectid
,
4764 BTRFS_I(dir
)->block_group
, mode
, &index
);
4765 err
= PTR_ERR(inode
);
4769 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
4775 btrfs_set_trans_block_group(trans
, inode
);
4776 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
4780 inode
->i_mapping
->a_ops
= &btrfs_aops
;
4781 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
4782 inode
->i_fop
= &btrfs_file_operations
;
4783 inode
->i_op
= &btrfs_file_inode_operations
;
4784 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
4786 btrfs_update_inode_block_group(trans
, inode
);
4787 btrfs_update_inode_block_group(trans
, dir
);
4789 nr
= trans
->blocks_used
;
4790 btrfs_end_transaction_throttle(trans
, root
);
4792 inode_dec_link_count(inode
);
4795 btrfs_btree_balance_dirty(root
, nr
);
4799 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
4800 struct dentry
*dentry
)
4802 struct btrfs_trans_handle
*trans
;
4803 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4804 struct inode
*inode
= old_dentry
->d_inode
;
4806 unsigned long nr
= 0;
4810 /* do not allow sys_link's with other subvols of the same device */
4811 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
4814 btrfs_inc_nlink(inode
);
4815 inode
->i_ctime
= CURRENT_TIME
;
4817 err
= btrfs_set_inode_index(dir
, &index
);
4822 * 2 items for inode and inode ref
4823 * 2 items for dir items
4824 * 1 item for parent inode
4826 trans
= btrfs_start_transaction(root
, 5);
4827 if (IS_ERR(trans
)) {
4828 err
= PTR_ERR(trans
);
4832 btrfs_set_trans_block_group(trans
, dir
);
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
, &dentry
->d_name
);
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 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
5283 size_t pg_offset
, u64 start
, u64 len
,
5286 struct extent_map
*em
;
5287 struct extent_map
*hole_em
= NULL
;
5288 u64 range_start
= start
;
5294 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
5299 * if our em maps to a hole, there might
5300 * actually be delalloc bytes behind it
5302 if (em
->block_start
!= EXTENT_MAP_HOLE
)
5308 /* check to see if we've wrapped (len == -1 or similar) */
5317 /* ok, we didn't find anything, lets look for delalloc */
5318 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
5319 end
, len
, EXTENT_DELALLOC
, 1);
5320 found_end
= range_start
+ found
;
5321 if (found_end
< range_start
)
5322 found_end
= (u64
)-1;
5325 * we didn't find anything useful, return
5326 * the original results from get_extent()
5328 if (range_start
> end
|| found_end
<= start
) {
5334 /* adjust the range_start to make sure it doesn't
5335 * go backwards from the start they passed in
5337 range_start
= max(start
,range_start
);
5338 found
= found_end
- range_start
;
5341 u64 hole_start
= start
;
5344 em
= alloc_extent_map(GFP_NOFS
);
5350 * when btrfs_get_extent can't find anything it
5351 * returns one huge hole
5353 * make sure what it found really fits our range, and
5354 * adjust to make sure it is based on the start from
5358 u64 calc_end
= extent_map_end(hole_em
);
5360 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
5361 free_extent_map(hole_em
);
5364 hole_start
= max(hole_em
->start
, start
);
5365 hole_len
= calc_end
- hole_start
;
5369 if (hole_em
&& range_start
> hole_start
) {
5370 /* our hole starts before our delalloc, so we
5371 * have to return just the parts of the hole
5372 * that go until the delalloc starts
5374 em
->len
= min(hole_len
,
5375 range_start
- hole_start
);
5376 em
->start
= hole_start
;
5377 em
->orig_start
= hole_start
;
5379 * don't adjust block start at all,
5380 * it is fixed at EXTENT_MAP_HOLE
5382 em
->block_start
= hole_em
->block_start
;
5383 em
->block_len
= hole_len
;
5385 em
->start
= range_start
;
5387 em
->orig_start
= range_start
;
5388 em
->block_start
= EXTENT_MAP_DELALLOC
;
5389 em
->block_len
= found
;
5391 } else if (hole_em
) {
5396 free_extent_map(hole_em
);
5398 free_extent_map(em
);
5399 return ERR_PTR(err
);
5404 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
5407 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5408 struct btrfs_trans_handle
*trans
;
5409 struct extent_map
*em
;
5410 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
5411 struct btrfs_key ins
;
5415 btrfs_drop_extent_cache(inode
, start
, start
+ len
- 1, 0);
5417 trans
= btrfs_join_transaction(root
, 0);
5419 return ERR_CAST(trans
);
5421 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
5423 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
5424 ret
= btrfs_reserve_extent(trans
, root
, len
, root
->sectorsize
, 0,
5425 alloc_hint
, (u64
)-1, &ins
, 1);
5431 em
= alloc_extent_map(GFP_NOFS
);
5433 em
= ERR_PTR(-ENOMEM
);
5438 em
->orig_start
= em
->start
;
5439 em
->len
= ins
.offset
;
5441 em
->block_start
= ins
.objectid
;
5442 em
->block_len
= ins
.offset
;
5443 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5444 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
5447 write_lock(&em_tree
->lock
);
5448 ret
= add_extent_mapping(em_tree
, em
);
5449 write_unlock(&em_tree
->lock
);
5452 btrfs_drop_extent_cache(inode
, start
, start
+ em
->len
- 1, 0);
5455 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
5456 ins
.offset
, ins
.offset
, 0);
5458 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
5462 btrfs_end_transaction(trans
, root
);
5467 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5468 * block must be cow'd
5470 static noinline
int can_nocow_odirect(struct btrfs_trans_handle
*trans
,
5471 struct inode
*inode
, u64 offset
, u64 len
)
5473 struct btrfs_path
*path
;
5475 struct extent_buffer
*leaf
;
5476 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5477 struct btrfs_file_extent_item
*fi
;
5478 struct btrfs_key key
;
5486 path
= btrfs_alloc_path();
5490 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
5495 slot
= path
->slots
[0];
5498 /* can't find the item, must cow */
5505 leaf
= path
->nodes
[0];
5506 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
5507 if (key
.objectid
!= inode
->i_ino
||
5508 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
5509 /* not our file or wrong item type, must cow */
5513 if (key
.offset
> offset
) {
5514 /* Wrong offset, must cow */
5518 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
5519 found_type
= btrfs_file_extent_type(leaf
, fi
);
5520 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
5521 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
5522 /* not a regular extent, must cow */
5525 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
5526 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
5528 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
5529 if (extent_end
< offset
+ len
) {
5530 /* extent doesn't include our full range, must cow */
5534 if (btrfs_extent_readonly(root
, disk_bytenr
))
5538 * look for other files referencing this extent, if we
5539 * find any we must cow
5541 if (btrfs_cross_ref_exist(trans
, root
, inode
->i_ino
,
5542 key
.offset
- backref_offset
, disk_bytenr
))
5546 * adjust disk_bytenr and num_bytes to cover just the bytes
5547 * in this extent we are about to write. If there
5548 * are any csums in that range we have to cow in order
5549 * to keep the csums correct
5551 disk_bytenr
+= backref_offset
;
5552 disk_bytenr
+= offset
- key
.offset
;
5553 num_bytes
= min(offset
+ len
, extent_end
) - offset
;
5554 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
5557 * all of the above have passed, it is safe to overwrite this extent
5562 btrfs_free_path(path
);
5566 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
5567 struct buffer_head
*bh_result
, int create
)
5569 struct extent_map
*em
;
5570 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5571 u64 start
= iblock
<< inode
->i_blkbits
;
5572 u64 len
= bh_result
->b_size
;
5573 struct btrfs_trans_handle
*trans
;
5575 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
5580 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5581 * io. INLINE is special, and we could probably kludge it in here, but
5582 * it's still buffered so for safety lets just fall back to the generic
5585 * For COMPRESSED we _have_ to read the entire extent in so we can
5586 * decompress it, so there will be buffering required no matter what we
5587 * do, so go ahead and fallback to buffered.
5589 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5590 * to buffered IO. Don't blame me, this is the price we pay for using
5593 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
5594 em
->block_start
== EXTENT_MAP_INLINE
) {
5595 free_extent_map(em
);
5599 /* Just a good old fashioned hole, return */
5600 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
5601 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
5602 free_extent_map(em
);
5603 /* DIO will do one hole at a time, so just unlock a sector */
5604 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
,
5605 start
+ root
->sectorsize
- 1, GFP_NOFS
);
5610 * We don't allocate a new extent in the following cases
5612 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5614 * 2) The extent is marked as PREALLOC. We're good to go here and can
5615 * just use the extent.
5619 len
= em
->len
- (start
- em
->start
);
5623 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
5624 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
5625 em
->block_start
!= EXTENT_MAP_HOLE
)) {
5630 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
5631 type
= BTRFS_ORDERED_PREALLOC
;
5633 type
= BTRFS_ORDERED_NOCOW
;
5634 len
= min(len
, em
->len
- (start
- em
->start
));
5635 block_start
= em
->block_start
+ (start
- em
->start
);
5638 * we're not going to log anything, but we do need
5639 * to make sure the current transaction stays open
5640 * while we look for nocow cross refs
5642 trans
= btrfs_join_transaction(root
, 0);
5646 if (can_nocow_odirect(trans
, inode
, start
, len
) == 1) {
5647 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
5648 block_start
, len
, len
, type
);
5649 btrfs_end_transaction(trans
, root
);
5651 free_extent_map(em
);
5656 btrfs_end_transaction(trans
, root
);
5660 * this will cow the extent, reset the len in case we changed
5663 len
= bh_result
->b_size
;
5664 free_extent_map(em
);
5665 em
= btrfs_new_extent_direct(inode
, start
, len
);
5668 len
= min(len
, em
->len
- (start
- em
->start
));
5670 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
- 1,
5671 EXTENT_LOCKED
| EXTENT_DELALLOC
| EXTENT_DIRTY
, 1,
5674 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
5676 bh_result
->b_size
= len
;
5677 bh_result
->b_bdev
= em
->bdev
;
5678 set_buffer_mapped(bh_result
);
5679 if (create
&& !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
5680 set_buffer_new(bh_result
);
5682 free_extent_map(em
);
5687 struct btrfs_dio_private
{
5688 struct inode
*inode
;
5695 /* number of bios pending for this dio */
5696 atomic_t pending_bios
;
5701 struct bio
*orig_bio
;
5704 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
5706 struct btrfs_dio_private
*dip
= bio
->bi_private
;
5707 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
5708 struct bio_vec
*bvec
= bio
->bi_io_vec
;
5709 struct inode
*inode
= dip
->inode
;
5710 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5712 u32
*private = dip
->csums
;
5714 start
= dip
->logical_offset
;
5716 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
5717 struct page
*page
= bvec
->bv_page
;
5720 unsigned long flags
;
5722 local_irq_save(flags
);
5723 kaddr
= kmap_atomic(page
, KM_IRQ0
);
5724 csum
= btrfs_csum_data(root
, kaddr
+ bvec
->bv_offset
,
5725 csum
, bvec
->bv_len
);
5726 btrfs_csum_final(csum
, (char *)&csum
);
5727 kunmap_atomic(kaddr
, KM_IRQ0
);
5728 local_irq_restore(flags
);
5730 flush_dcache_page(bvec
->bv_page
);
5731 if (csum
!= *private) {
5732 printk(KERN_ERR
"btrfs csum failed ino %lu off"
5733 " %llu csum %u private %u\n",
5734 inode
->i_ino
, (unsigned long long)start
,
5740 start
+= bvec
->bv_len
;
5743 } while (bvec
<= bvec_end
);
5745 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
5746 dip
->logical_offset
+ dip
->bytes
- 1, GFP_NOFS
);
5747 bio
->bi_private
= dip
->private;
5751 dio_end_io(bio
, err
);
5754 static void btrfs_endio_direct_write(struct bio
*bio
, int err
)
5756 struct btrfs_dio_private
*dip
= bio
->bi_private
;
5757 struct inode
*inode
= dip
->inode
;
5758 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5759 struct btrfs_trans_handle
*trans
;
5760 struct btrfs_ordered_extent
*ordered
= NULL
;
5761 struct extent_state
*cached_state
= NULL
;
5762 u64 ordered_offset
= dip
->logical_offset
;
5763 u64 ordered_bytes
= dip
->bytes
;
5769 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
5777 trans
= btrfs_join_transaction(root
, 1);
5778 if (IS_ERR(trans
)) {
5782 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
5784 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
)) {
5785 ret
= btrfs_ordered_update_i_size(inode
, 0, ordered
);
5787 ret
= btrfs_update_inode(trans
, root
, inode
);
5792 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, ordered
->file_offset
,
5793 ordered
->file_offset
+ ordered
->len
- 1, 0,
5794 &cached_state
, GFP_NOFS
);
5796 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
)) {
5797 ret
= btrfs_mark_extent_written(trans
, inode
,
5798 ordered
->file_offset
,
5799 ordered
->file_offset
+
5806 ret
= insert_reserved_file_extent(trans
, inode
,
5807 ordered
->file_offset
,
5813 BTRFS_FILE_EXTENT_REG
);
5814 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
5815 ordered
->file_offset
, ordered
->len
);
5823 add_pending_csums(trans
, inode
, ordered
->file_offset
, &ordered
->list
);
5824 btrfs_ordered_update_i_size(inode
, 0, ordered
);
5825 btrfs_update_inode(trans
, root
, inode
);
5827 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, ordered
->file_offset
,
5828 ordered
->file_offset
+ ordered
->len
- 1,
5829 &cached_state
, GFP_NOFS
);
5831 btrfs_delalloc_release_metadata(inode
, ordered
->len
);
5832 btrfs_end_transaction(trans
, root
);
5833 ordered_offset
= ordered
->file_offset
+ ordered
->len
;
5834 btrfs_put_ordered_extent(ordered
);
5835 btrfs_put_ordered_extent(ordered
);
5839 * our bio might span multiple ordered extents. If we haven't
5840 * completed the accounting for the whole dio, go back and try again
5842 if (ordered_offset
< dip
->logical_offset
+ dip
->bytes
) {
5843 ordered_bytes
= dip
->logical_offset
+ dip
->bytes
-
5848 bio
->bi_private
= dip
->private;
5852 dio_end_io(bio
, err
);
5855 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
5856 struct bio
*bio
, int mirror_num
,
5857 unsigned long bio_flags
, u64 offset
)
5860 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5861 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
5866 static void btrfs_end_dio_bio(struct bio
*bio
, int err
)
5868 struct btrfs_dio_private
*dip
= bio
->bi_private
;
5871 printk(KERN_ERR
"btrfs direct IO failed ino %lu rw %lu "
5872 "sector %#Lx len %u err no %d\n",
5873 dip
->inode
->i_ino
, bio
->bi_rw
,
5874 (unsigned long long)bio
->bi_sector
, bio
->bi_size
, err
);
5878 * before atomic variable goto zero, we must make sure
5879 * dip->errors is perceived to be set.
5881 smp_mb__before_atomic_dec();
5884 /* if there are more bios still pending for this dio, just exit */
5885 if (!atomic_dec_and_test(&dip
->pending_bios
))
5889 bio_io_error(dip
->orig_bio
);
5891 set_bit(BIO_UPTODATE
, &dip
->orig_bio
->bi_flags
);
5892 bio_endio(dip
->orig_bio
, 0);
5898 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
5899 u64 first_sector
, gfp_t gfp_flags
)
5901 int nr_vecs
= bio_get_nr_vecs(bdev
);
5902 return btrfs_bio_alloc(bdev
, first_sector
, nr_vecs
, gfp_flags
);
5905 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
5906 int rw
, u64 file_offset
, int skip_sum
,
5909 int write
= rw
& REQ_WRITE
;
5910 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5914 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
5918 if (write
&& !skip_sum
) {
5919 ret
= btrfs_wq_submit_bio(root
->fs_info
,
5920 inode
, rw
, bio
, 0, 0,
5922 __btrfs_submit_bio_start_direct_io
,
5923 __btrfs_submit_bio_done
);
5925 } else if (!skip_sum
)
5926 btrfs_lookup_bio_sums_dio(root
, inode
, bio
,
5927 file_offset
, csums
);
5929 ret
= btrfs_map_bio(root
, rw
, bio
, 0, 1);
5935 static int btrfs_submit_direct_hook(int rw
, struct btrfs_dio_private
*dip
,
5938 struct inode
*inode
= dip
->inode
;
5939 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5940 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
5942 struct bio
*orig_bio
= dip
->orig_bio
;
5943 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
5944 u64 start_sector
= orig_bio
->bi_sector
;
5945 u64 file_offset
= dip
->logical_offset
;
5949 u32
*csums
= dip
->csums
;
5952 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
5955 bio
->bi_private
= dip
;
5956 bio
->bi_end_io
= btrfs_end_dio_bio
;
5957 atomic_inc(&dip
->pending_bios
);
5959 map_length
= orig_bio
->bi_size
;
5960 ret
= btrfs_map_block(map_tree
, READ
, start_sector
<< 9,
5961 &map_length
, NULL
, 0);
5967 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
5968 if (unlikely(map_length
< submit_len
+ bvec
->bv_len
||
5969 bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
5970 bvec
->bv_offset
) < bvec
->bv_len
)) {
5972 * inc the count before we submit the bio so
5973 * we know the end IO handler won't happen before
5974 * we inc the count. Otherwise, the dip might get freed
5975 * before we're done setting it up
5977 atomic_inc(&dip
->pending_bios
);
5978 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
,
5979 file_offset
, skip_sum
,
5983 atomic_dec(&dip
->pending_bios
);
5988 csums
= csums
+ nr_pages
;
5989 start_sector
+= submit_len
>> 9;
5990 file_offset
+= submit_len
;
5995 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
5996 start_sector
, GFP_NOFS
);
5999 bio
->bi_private
= dip
;
6000 bio
->bi_end_io
= btrfs_end_dio_bio
;
6002 map_length
= orig_bio
->bi_size
;
6003 ret
= btrfs_map_block(map_tree
, READ
, start_sector
<< 9,
6004 &map_length
, NULL
, 0);
6010 submit_len
+= bvec
->bv_len
;
6016 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
, file_offset
, skip_sum
,
6025 * before atomic variable goto zero, we must
6026 * make sure dip->errors is perceived to be set.
6028 smp_mb__before_atomic_dec();
6029 if (atomic_dec_and_test(&dip
->pending_bios
))
6030 bio_io_error(dip
->orig_bio
);
6032 /* bio_end_io() will handle error, so we needn't return it */
6036 static void btrfs_submit_direct(int rw
, struct bio
*bio
, struct inode
*inode
,
6039 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6040 struct btrfs_dio_private
*dip
;
6041 struct bio_vec
*bvec
= bio
->bi_io_vec
;
6043 int write
= rw
& REQ_WRITE
;
6046 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
6048 dip
= kmalloc(sizeof(*dip
), GFP_NOFS
);
6056 dip
->csums
= kmalloc(sizeof(u32
) * bio
->bi_vcnt
, GFP_NOFS
);
6064 dip
->private = bio
->bi_private
;
6066 dip
->logical_offset
= file_offset
;
6070 dip
->bytes
+= bvec
->bv_len
;
6072 } while (bvec
<= (bio
->bi_io_vec
+ bio
->bi_vcnt
- 1));
6074 dip
->disk_bytenr
= (u64
)bio
->bi_sector
<< 9;
6075 bio
->bi_private
= dip
;
6077 dip
->orig_bio
= bio
;
6078 atomic_set(&dip
->pending_bios
, 0);
6081 bio
->bi_end_io
= btrfs_endio_direct_write
;
6083 bio
->bi_end_io
= btrfs_endio_direct_read
;
6085 ret
= btrfs_submit_direct_hook(rw
, dip
, skip_sum
);
6090 * If this is a write, we need to clean up the reserved space and kill
6091 * the ordered extent.
6094 struct btrfs_ordered_extent
*ordered
;
6095 ordered
= btrfs_lookup_ordered_extent(inode
, file_offset
);
6096 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
6097 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
6098 btrfs_free_reserved_extent(root
, ordered
->start
,
6100 btrfs_put_ordered_extent(ordered
);
6101 btrfs_put_ordered_extent(ordered
);
6103 bio_endio(bio
, ret
);
6106 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
6107 const struct iovec
*iov
, loff_t offset
,
6108 unsigned long nr_segs
)
6113 unsigned blocksize_mask
= root
->sectorsize
- 1;
6114 ssize_t retval
= -EINVAL
;
6115 loff_t end
= offset
;
6117 if (offset
& blocksize_mask
)
6120 /* Check the memory alignment. Blocks cannot straddle pages */
6121 for (seg
= 0; seg
< nr_segs
; seg
++) {
6122 addr
= (unsigned long)iov
[seg
].iov_base
;
6123 size
= iov
[seg
].iov_len
;
6125 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
6132 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
6133 const struct iovec
*iov
, loff_t offset
,
6134 unsigned long nr_segs
)
6136 struct file
*file
= iocb
->ki_filp
;
6137 struct inode
*inode
= file
->f_mapping
->host
;
6138 struct btrfs_ordered_extent
*ordered
;
6139 struct extent_state
*cached_state
= NULL
;
6140 u64 lockstart
, lockend
;
6142 int writing
= rw
& WRITE
;
6144 size_t count
= iov_length(iov
, nr_segs
);
6146 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iov
,
6152 lockend
= offset
+ count
- 1;
6155 ret
= btrfs_delalloc_reserve_space(inode
, count
);
6161 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6162 0, &cached_state
, GFP_NOFS
);
6164 * We're concerned with the entire range that we're going to be
6165 * doing DIO to, so we need to make sure theres no ordered
6166 * extents in this range.
6168 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
6169 lockend
- lockstart
+ 1);
6172 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6173 &cached_state
, GFP_NOFS
);
6174 btrfs_start_ordered_extent(inode
, ordered
, 1);
6175 btrfs_put_ordered_extent(ordered
);
6180 * we don't use btrfs_set_extent_delalloc because we don't want
6181 * the dirty or uptodate bits
6184 write_bits
= EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
;
6185 ret
= set_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6186 EXTENT_DELALLOC
, 0, NULL
, &cached_state
,
6189 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6190 lockend
, EXTENT_LOCKED
| write_bits
,
6191 1, 0, &cached_state
, GFP_NOFS
);
6196 free_extent_state(cached_state
);
6197 cached_state
= NULL
;
6199 ret
= __blockdev_direct_IO(rw
, iocb
, inode
,
6200 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
6201 iov
, offset
, nr_segs
, btrfs_get_blocks_direct
, NULL
,
6202 btrfs_submit_direct
, 0);
6204 if (ret
< 0 && ret
!= -EIOCBQUEUED
) {
6205 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, offset
,
6206 offset
+ iov_length(iov
, nr_segs
) - 1,
6207 EXTENT_LOCKED
| write_bits
, 1, 0,
6208 &cached_state
, GFP_NOFS
);
6209 } else if (ret
>= 0 && ret
< iov_length(iov
, nr_segs
)) {
6211 * We're falling back to buffered, unlock the section we didn't
6214 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, offset
+ ret
,
6215 offset
+ iov_length(iov
, nr_segs
) - 1,
6216 EXTENT_LOCKED
| write_bits
, 1, 0,
6217 &cached_state
, GFP_NOFS
);
6220 free_extent_state(cached_state
);
6224 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
6225 __u64 start
, __u64 len
)
6227 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
6230 int btrfs_readpage(struct file
*file
, struct page
*page
)
6232 struct extent_io_tree
*tree
;
6233 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6234 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
6237 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
6239 struct extent_io_tree
*tree
;
6242 if (current
->flags
& PF_MEMALLOC
) {
6243 redirty_page_for_writepage(wbc
, page
);
6247 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6248 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
6251 int btrfs_writepages(struct address_space
*mapping
,
6252 struct writeback_control
*wbc
)
6254 struct extent_io_tree
*tree
;
6256 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
6257 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
6261 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
6262 struct list_head
*pages
, unsigned nr_pages
)
6264 struct extent_io_tree
*tree
;
6265 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
6266 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
6269 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
6271 struct extent_io_tree
*tree
;
6272 struct extent_map_tree
*map
;
6275 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6276 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
6277 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
6279 ClearPagePrivate(page
);
6280 set_page_private(page
, 0);
6281 page_cache_release(page
);
6286 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
6288 if (PageWriteback(page
) || PageDirty(page
))
6290 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
6293 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
6295 struct extent_io_tree
*tree
;
6296 struct btrfs_ordered_extent
*ordered
;
6297 struct extent_state
*cached_state
= NULL
;
6298 u64 page_start
= page_offset(page
);
6299 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6303 * we have the page locked, so new writeback can't start,
6304 * and the dirty bit won't be cleared while we are here.
6306 * Wait for IO on this page so that we can safely clear
6307 * the PagePrivate2 bit and do ordered accounting
6309 wait_on_page_writeback(page
);
6311 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6313 btrfs_releasepage(page
, GFP_NOFS
);
6316 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
,
6318 ordered
= btrfs_lookup_ordered_extent(page
->mapping
->host
,
6322 * IO on this page will never be started, so we need
6323 * to account for any ordered extents now
6325 clear_extent_bit(tree
, page_start
, page_end
,
6326 EXTENT_DIRTY
| EXTENT_DELALLOC
|
6327 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
, 1, 0,
6328 &cached_state
, GFP_NOFS
);
6330 * whoever cleared the private bit is responsible
6331 * for the finish_ordered_io
6333 if (TestClearPagePrivate2(page
)) {
6334 btrfs_finish_ordered_io(page
->mapping
->host
,
6335 page_start
, page_end
);
6337 btrfs_put_ordered_extent(ordered
);
6338 cached_state
= NULL
;
6339 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
,
6342 clear_extent_bit(tree
, page_start
, page_end
,
6343 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
6344 EXTENT_DO_ACCOUNTING
, 1, 1, &cached_state
, GFP_NOFS
);
6345 __btrfs_releasepage(page
, GFP_NOFS
);
6347 ClearPageChecked(page
);
6348 if (PagePrivate(page
)) {
6349 ClearPagePrivate(page
);
6350 set_page_private(page
, 0);
6351 page_cache_release(page
);
6356 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6357 * called from a page fault handler when a page is first dirtied. Hence we must
6358 * be careful to check for EOF conditions here. We set the page up correctly
6359 * for a written page which means we get ENOSPC checking when writing into
6360 * holes and correct delalloc and unwritten extent mapping on filesystems that
6361 * support these features.
6363 * We are not allowed to take the i_mutex here so we have to play games to
6364 * protect against truncate races as the page could now be beyond EOF. Because
6365 * vmtruncate() writes the inode size before removing pages, once we have the
6366 * page lock we can determine safely if the page is beyond EOF. If it is not
6367 * beyond EOF, then the page is guaranteed safe against truncation until we
6370 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
6372 struct page
*page
= vmf
->page
;
6373 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
6374 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6375 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6376 struct btrfs_ordered_extent
*ordered
;
6377 struct extent_state
*cached_state
= NULL
;
6379 unsigned long zero_start
;
6385 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
6389 else /* -ENOSPC, -EIO, etc */
6390 ret
= VM_FAULT_SIGBUS
;
6394 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
6397 size
= i_size_read(inode
);
6398 page_start
= page_offset(page
);
6399 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6401 if ((page
->mapping
!= inode
->i_mapping
) ||
6402 (page_start
>= size
)) {
6403 /* page got truncated out from underneath us */
6406 wait_on_page_writeback(page
);
6408 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
,
6410 set_page_extent_mapped(page
);
6413 * we can't set the delalloc bits if there are pending ordered
6414 * extents. Drop our locks and wait for them to finish
6416 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
6418 unlock_extent_cached(io_tree
, page_start
, page_end
,
6419 &cached_state
, GFP_NOFS
);
6421 btrfs_start_ordered_extent(inode
, ordered
, 1);
6422 btrfs_put_ordered_extent(ordered
);
6427 * XXX - page_mkwrite gets called every time the page is dirtied, even
6428 * if it was already dirty, so for space accounting reasons we need to
6429 * clear any delalloc bits for the range we are fixing to save. There
6430 * is probably a better way to do this, but for now keep consistent with
6431 * prepare_pages in the normal write path.
6433 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
6434 EXTENT_DIRTY
| EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
,
6435 0, 0, &cached_state
, GFP_NOFS
);
6437 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
6440 unlock_extent_cached(io_tree
, page_start
, page_end
,
6441 &cached_state
, GFP_NOFS
);
6442 ret
= VM_FAULT_SIGBUS
;
6447 /* page is wholly or partially inside EOF */
6448 if (page_start
+ PAGE_CACHE_SIZE
> size
)
6449 zero_start
= size
& ~PAGE_CACHE_MASK
;
6451 zero_start
= PAGE_CACHE_SIZE
;
6453 if (zero_start
!= PAGE_CACHE_SIZE
) {
6455 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
6456 flush_dcache_page(page
);
6459 ClearPageChecked(page
);
6460 set_page_dirty(page
);
6461 SetPageUptodate(page
);
6463 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
6464 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
6466 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
6470 return VM_FAULT_LOCKED
;
6472 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
6477 static void btrfs_truncate(struct inode
*inode
)
6479 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6481 struct btrfs_trans_handle
*trans
;
6483 u64 mask
= root
->sectorsize
- 1;
6485 if (!S_ISREG(inode
->i_mode
)) {
6490 ret
= btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
6494 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
6495 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
6497 trans
= btrfs_start_transaction(root
, 0);
6498 BUG_ON(IS_ERR(trans
));
6499 btrfs_set_trans_block_group(trans
, inode
);
6500 trans
->block_rsv
= root
->orphan_block_rsv
;
6503 * setattr is responsible for setting the ordered_data_close flag,
6504 * but that is only tested during the last file release. That
6505 * could happen well after the next commit, leaving a great big
6506 * window where new writes may get lost if someone chooses to write
6507 * to this file after truncating to zero
6509 * The inode doesn't have any dirty data here, and so if we commit
6510 * this is a noop. If someone immediately starts writing to the inode
6511 * it is very likely we'll catch some of their writes in this
6512 * transaction, and the commit will find this file on the ordered
6513 * data list with good things to send down.
6515 * This is a best effort solution, there is still a window where
6516 * using truncate to replace the contents of the file will
6517 * end up with a zero length file after a crash.
6519 if (inode
->i_size
== 0 && BTRFS_I(inode
)->ordered_data_close
)
6520 btrfs_add_ordered_operation(trans
, root
, inode
);
6524 trans
= btrfs_start_transaction(root
, 0);
6525 BUG_ON(IS_ERR(trans
));
6526 btrfs_set_trans_block_group(trans
, inode
);
6527 trans
->block_rsv
= root
->orphan_block_rsv
;
6530 ret
= btrfs_block_rsv_check(trans
, root
,
6531 root
->orphan_block_rsv
, 0, 5);
6533 BUG_ON(ret
!= -EAGAIN
);
6534 ret
= btrfs_commit_transaction(trans
, root
);
6540 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
6542 BTRFS_EXTENT_DATA_KEY
);
6546 ret
= btrfs_update_inode(trans
, root
, inode
);
6549 nr
= trans
->blocks_used
;
6550 btrfs_end_transaction(trans
, root
);
6552 btrfs_btree_balance_dirty(root
, nr
);
6555 if (ret
== 0 && inode
->i_nlink
> 0) {
6556 ret
= btrfs_orphan_del(trans
, inode
);
6560 ret
= btrfs_update_inode(trans
, root
, inode
);
6563 nr
= trans
->blocks_used
;
6564 ret
= btrfs_end_transaction_throttle(trans
, root
);
6566 btrfs_btree_balance_dirty(root
, nr
);
6570 * create a new subvolume directory/inode (helper for the ioctl).
6572 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
6573 struct btrfs_root
*new_root
,
6574 u64 new_dirid
, u64 alloc_hint
)
6576 struct inode
*inode
;
6580 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2, new_dirid
,
6581 new_dirid
, alloc_hint
, S_IFDIR
| 0700, &index
);
6583 return PTR_ERR(inode
);
6584 inode
->i_op
= &btrfs_dir_inode_operations
;
6585 inode
->i_fop
= &btrfs_dir_file_operations
;
6588 btrfs_i_size_write(inode
, 0);
6590 err
= btrfs_update_inode(trans
, new_root
, inode
);
6597 /* helper function for file defrag and space balancing. This
6598 * forces readahead on a given range of bytes in an inode
6600 unsigned long btrfs_force_ra(struct address_space
*mapping
,
6601 struct file_ra_state
*ra
, struct file
*file
,
6602 pgoff_t offset
, pgoff_t last_index
)
6604 pgoff_t req_size
= last_index
- offset
+ 1;
6606 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
6607 return offset
+ req_size
;
6610 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
6612 struct btrfs_inode
*ei
;
6613 struct inode
*inode
;
6615 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
6620 ei
->space_info
= NULL
;
6624 ei
->last_sub_trans
= 0;
6625 ei
->logged_trans
= 0;
6626 ei
->delalloc_bytes
= 0;
6627 ei
->reserved_bytes
= 0;
6628 ei
->disk_i_size
= 0;
6630 ei
->index_cnt
= (u64
)-1;
6631 ei
->last_unlink_trans
= 0;
6633 spin_lock_init(&ei
->accounting_lock
);
6634 atomic_set(&ei
->outstanding_extents
, 0);
6635 ei
->reserved_extents
= 0;
6637 ei
->ordered_data_close
= 0;
6638 ei
->orphan_meta_reserved
= 0;
6639 ei
->dummy_inode
= 0;
6640 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
6642 inode
= &ei
->vfs_inode
;
6643 extent_map_tree_init(&ei
->extent_tree
, GFP_NOFS
);
6644 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
, GFP_NOFS
);
6645 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
, GFP_NOFS
);
6646 mutex_init(&ei
->log_mutex
);
6647 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
6648 INIT_LIST_HEAD(&ei
->i_orphan
);
6649 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
6650 INIT_LIST_HEAD(&ei
->ordered_operations
);
6651 RB_CLEAR_NODE(&ei
->rb_node
);
6656 static void btrfs_i_callback(struct rcu_head
*head
)
6658 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
6659 INIT_LIST_HEAD(&inode
->i_dentry
);
6660 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
6663 void btrfs_destroy_inode(struct inode
*inode
)
6665 struct btrfs_ordered_extent
*ordered
;
6666 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6668 WARN_ON(!list_empty(&inode
->i_dentry
));
6669 WARN_ON(inode
->i_data
.nrpages
);
6670 WARN_ON(atomic_read(&BTRFS_I(inode
)->outstanding_extents
));
6671 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
6674 * This can happen where we create an inode, but somebody else also
6675 * created the same inode and we need to destroy the one we already
6682 * Make sure we're properly removed from the ordered operation
6686 if (!list_empty(&BTRFS_I(inode
)->ordered_operations
)) {
6687 spin_lock(&root
->fs_info
->ordered_extent_lock
);
6688 list_del_init(&BTRFS_I(inode
)->ordered_operations
);
6689 spin_unlock(&root
->fs_info
->ordered_extent_lock
);
6692 if (root
== root
->fs_info
->tree_root
) {
6693 struct btrfs_block_group_cache
*block_group
;
6695 block_group
= btrfs_lookup_block_group(root
->fs_info
,
6696 BTRFS_I(inode
)->block_group
);
6697 if (block_group
&& block_group
->inode
== inode
) {
6698 spin_lock(&block_group
->lock
);
6699 block_group
->inode
= NULL
;
6700 spin_unlock(&block_group
->lock
);
6701 btrfs_put_block_group(block_group
);
6702 } else if (block_group
) {
6703 btrfs_put_block_group(block_group
);
6707 spin_lock(&root
->orphan_lock
);
6708 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
6709 printk(KERN_INFO
"BTRFS: inode %lu still on the orphan list\n",
6711 list_del_init(&BTRFS_I(inode
)->i_orphan
);
6713 spin_unlock(&root
->orphan_lock
);
6716 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
6720 printk(KERN_ERR
"btrfs found ordered "
6721 "extent %llu %llu on inode cleanup\n",
6722 (unsigned long long)ordered
->file_offset
,
6723 (unsigned long long)ordered
->len
);
6724 btrfs_remove_ordered_extent(inode
, ordered
);
6725 btrfs_put_ordered_extent(ordered
);
6726 btrfs_put_ordered_extent(ordered
);
6729 inode_tree_del(inode
);
6730 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
6732 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
6735 int btrfs_drop_inode(struct inode
*inode
)
6737 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6739 if (btrfs_root_refs(&root
->root_item
) == 0 &&
6740 root
!= root
->fs_info
->tree_root
)
6743 return generic_drop_inode(inode
);
6746 static void init_once(void *foo
)
6748 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
6750 inode_init_once(&ei
->vfs_inode
);
6753 void btrfs_destroy_cachep(void)
6755 if (btrfs_inode_cachep
)
6756 kmem_cache_destroy(btrfs_inode_cachep
);
6757 if (btrfs_trans_handle_cachep
)
6758 kmem_cache_destroy(btrfs_trans_handle_cachep
);
6759 if (btrfs_transaction_cachep
)
6760 kmem_cache_destroy(btrfs_transaction_cachep
);
6761 if (btrfs_path_cachep
)
6762 kmem_cache_destroy(btrfs_path_cachep
);
6765 int btrfs_init_cachep(void)
6767 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode_cache",
6768 sizeof(struct btrfs_inode
), 0,
6769 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
6770 if (!btrfs_inode_cachep
)
6773 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle_cache",
6774 sizeof(struct btrfs_trans_handle
), 0,
6775 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6776 if (!btrfs_trans_handle_cachep
)
6779 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction_cache",
6780 sizeof(struct btrfs_transaction
), 0,
6781 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6782 if (!btrfs_transaction_cachep
)
6785 btrfs_path_cachep
= kmem_cache_create("btrfs_path_cache",
6786 sizeof(struct btrfs_path
), 0,
6787 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6788 if (!btrfs_path_cachep
)
6793 btrfs_destroy_cachep();
6797 static int btrfs_getattr(struct vfsmount
*mnt
,
6798 struct dentry
*dentry
, struct kstat
*stat
)
6800 struct inode
*inode
= dentry
->d_inode
;
6801 generic_fillattr(inode
, stat
);
6802 stat
->dev
= BTRFS_I(inode
)->root
->anon_super
.s_dev
;
6803 stat
->blksize
= PAGE_CACHE_SIZE
;
6804 stat
->blocks
= (inode_get_bytes(inode
) +
6805 BTRFS_I(inode
)->delalloc_bytes
) >> 9;
6809 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
6810 struct inode
*new_dir
, struct dentry
*new_dentry
)
6812 struct btrfs_trans_handle
*trans
;
6813 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
6814 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
6815 struct inode
*new_inode
= new_dentry
->d_inode
;
6816 struct inode
*old_inode
= old_dentry
->d_inode
;
6817 struct timespec ctime
= CURRENT_TIME
;
6822 if (new_dir
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
6825 /* we only allow rename subvolume link between subvolumes */
6826 if (old_inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
6829 if (old_inode
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
6830 (new_inode
&& new_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
))
6833 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
6834 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
6837 * we're using rename to replace one file with another.
6838 * and the replacement file is large. Start IO on it now so
6839 * we don't add too much work to the end of the transaction
6841 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
&&
6842 old_inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
6843 filemap_flush(old_inode
->i_mapping
);
6845 /* close the racy window with snapshot create/destroy ioctl */
6846 if (old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)
6847 down_read(&root
->fs_info
->subvol_sem
);
6849 * We want to reserve the absolute worst case amount of items. So if
6850 * both inodes are subvols and we need to unlink them then that would
6851 * require 4 item modifications, but if they are both normal inodes it
6852 * would require 5 item modifications, so we'll assume their normal
6853 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
6854 * should cover the worst case number of items we'll modify.
6856 trans
= btrfs_start_transaction(root
, 20);
6858 return PTR_ERR(trans
);
6860 btrfs_set_trans_block_group(trans
, new_dir
);
6863 btrfs_record_root_in_trans(trans
, dest
);
6865 ret
= btrfs_set_inode_index(new_dir
, &index
);
6869 if (unlikely(old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6870 /* force full log commit if subvolume involved. */
6871 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
6873 ret
= btrfs_insert_inode_ref(trans
, dest
,
6874 new_dentry
->d_name
.name
,
6875 new_dentry
->d_name
.len
,
6877 new_dir
->i_ino
, index
);
6881 * this is an ugly little race, but the rename is required
6882 * to make sure that if we crash, the inode is either at the
6883 * old name or the new one. pinning the log transaction lets
6884 * us make sure we don't allow a log commit to come in after
6885 * we unlink the name but before we add the new name back in.
6887 btrfs_pin_log_trans(root
);
6890 * make sure the inode gets flushed if it is replacing
6893 if (new_inode
&& new_inode
->i_size
&&
6894 old_inode
&& S_ISREG(old_inode
->i_mode
)) {
6895 btrfs_add_ordered_operation(trans
, root
, old_inode
);
6898 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
6899 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
6900 old_inode
->i_ctime
= ctime
;
6902 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
6903 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
6905 if (unlikely(old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6906 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
6907 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
6908 old_dentry
->d_name
.name
,
6909 old_dentry
->d_name
.len
);
6911 btrfs_inc_nlink(old_dentry
->d_inode
);
6912 ret
= btrfs_unlink_inode(trans
, root
, old_dir
,
6913 old_dentry
->d_inode
,
6914 old_dentry
->d_name
.name
,
6915 old_dentry
->d_name
.len
);
6920 new_inode
->i_ctime
= CURRENT_TIME
;
6921 if (unlikely(new_inode
->i_ino
==
6922 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
6923 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
6924 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
6926 new_dentry
->d_name
.name
,
6927 new_dentry
->d_name
.len
);
6928 BUG_ON(new_inode
->i_nlink
== 0);
6930 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
6931 new_dentry
->d_inode
,
6932 new_dentry
->d_name
.name
,
6933 new_dentry
->d_name
.len
);
6936 if (new_inode
->i_nlink
== 0) {
6937 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
6942 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
6943 new_dentry
->d_name
.name
,
6944 new_dentry
->d_name
.len
, 0, index
);
6947 if (old_inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
6948 struct dentry
*parent
= dget_parent(new_dentry
);
6949 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
6951 btrfs_end_log_trans(root
);
6954 btrfs_end_transaction_throttle(trans
, root
);
6956 if (old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)
6957 up_read(&root
->fs_info
->subvol_sem
);
6963 * some fairly slow code that needs optimization. This walks the list
6964 * of all the inodes with pending delalloc and forces them to disk.
6966 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
6968 struct list_head
*head
= &root
->fs_info
->delalloc_inodes
;
6969 struct btrfs_inode
*binode
;
6970 struct inode
*inode
;
6972 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
6975 spin_lock(&root
->fs_info
->delalloc_lock
);
6976 while (!list_empty(head
)) {
6977 binode
= list_entry(head
->next
, struct btrfs_inode
,
6979 inode
= igrab(&binode
->vfs_inode
);
6981 list_del_init(&binode
->delalloc_inodes
);
6982 spin_unlock(&root
->fs_info
->delalloc_lock
);
6984 filemap_flush(inode
->i_mapping
);
6986 btrfs_add_delayed_iput(inode
);
6991 spin_lock(&root
->fs_info
->delalloc_lock
);
6993 spin_unlock(&root
->fs_info
->delalloc_lock
);
6995 /* the filemap_flush will queue IO into the worker threads, but
6996 * we have to make sure the IO is actually started and that
6997 * ordered extents get created before we return
6999 atomic_inc(&root
->fs_info
->async_submit_draining
);
7000 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
7001 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
7002 wait_event(root
->fs_info
->async_submit_wait
,
7003 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
7004 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
7006 atomic_dec(&root
->fs_info
->async_submit_draining
);
7010 int btrfs_start_one_delalloc_inode(struct btrfs_root
*root
, int delay_iput
,
7013 struct btrfs_inode
*binode
;
7014 struct inode
*inode
= NULL
;
7016 spin_lock(&root
->fs_info
->delalloc_lock
);
7017 while (!list_empty(&root
->fs_info
->delalloc_inodes
)) {
7018 binode
= list_entry(root
->fs_info
->delalloc_inodes
.next
,
7019 struct btrfs_inode
, delalloc_inodes
);
7020 inode
= igrab(&binode
->vfs_inode
);
7022 list_move_tail(&binode
->delalloc_inodes
,
7023 &root
->fs_info
->delalloc_inodes
);
7027 list_del_init(&binode
->delalloc_inodes
);
7028 cond_resched_lock(&root
->fs_info
->delalloc_lock
);
7030 spin_unlock(&root
->fs_info
->delalloc_lock
);
7034 filemap_write_and_wait(inode
->i_mapping
);
7036 * We have to do this because compression doesn't
7037 * actually set PG_writeback until it submits the pages
7038 * for IO, which happens in an async thread, so we could
7039 * race and not actually wait for any writeback pages
7040 * because they've not been submitted yet. Technically
7041 * this could still be the case for the ordered stuff
7042 * since the async thread may not have started to do its
7043 * work yet. If this becomes the case then we need to
7044 * figure out a way to make sure that in writepage we
7045 * wait for any async pages to be submitted before
7046 * returning so that fdatawait does what its supposed to
7049 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
7051 filemap_flush(inode
->i_mapping
);
7054 btrfs_add_delayed_iput(inode
);
7062 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
7063 const char *symname
)
7065 struct btrfs_trans_handle
*trans
;
7066 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
7067 struct btrfs_path
*path
;
7068 struct btrfs_key key
;
7069 struct inode
*inode
= NULL
;
7077 struct btrfs_file_extent_item
*ei
;
7078 struct extent_buffer
*leaf
;
7079 unsigned long nr
= 0;
7081 name_len
= strlen(symname
) + 1;
7082 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
7083 return -ENAMETOOLONG
;
7085 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
7089 * 2 items for inode item and ref
7090 * 2 items for dir items
7091 * 1 item for xattr if selinux is on
7093 trans
= btrfs_start_transaction(root
, 5);
7095 return PTR_ERR(trans
);
7097 btrfs_set_trans_block_group(trans
, dir
);
7099 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
7100 dentry
->d_name
.len
, dir
->i_ino
, objectid
,
7101 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
,
7103 err
= PTR_ERR(inode
);
7107 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
7113 btrfs_set_trans_block_group(trans
, inode
);
7114 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
7118 inode
->i_mapping
->a_ops
= &btrfs_aops
;
7119 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
7120 inode
->i_fop
= &btrfs_file_operations
;
7121 inode
->i_op
= &btrfs_file_inode_operations
;
7122 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
7124 btrfs_update_inode_block_group(trans
, inode
);
7125 btrfs_update_inode_block_group(trans
, dir
);
7129 path
= btrfs_alloc_path();
7131 key
.objectid
= inode
->i_ino
;
7133 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
7134 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
7135 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
7141 leaf
= path
->nodes
[0];
7142 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
7143 struct btrfs_file_extent_item
);
7144 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
7145 btrfs_set_file_extent_type(leaf
, ei
,
7146 BTRFS_FILE_EXTENT_INLINE
);
7147 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
7148 btrfs_set_file_extent_compression(leaf
, ei
, 0);
7149 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
7150 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
7152 ptr
= btrfs_file_extent_inline_start(ei
);
7153 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
7154 btrfs_mark_buffer_dirty(leaf
);
7155 btrfs_free_path(path
);
7157 inode
->i_op
= &btrfs_symlink_inode_operations
;
7158 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
7159 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
7160 inode_set_bytes(inode
, name_len
);
7161 btrfs_i_size_write(inode
, name_len
- 1);
7162 err
= btrfs_update_inode(trans
, root
, inode
);
7167 nr
= trans
->blocks_used
;
7168 btrfs_end_transaction_throttle(trans
, root
);
7170 inode_dec_link_count(inode
);
7173 btrfs_btree_balance_dirty(root
, nr
);
7177 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
7178 u64 start
, u64 num_bytes
, u64 min_size
,
7179 loff_t actual_len
, u64
*alloc_hint
,
7180 struct btrfs_trans_handle
*trans
)
7182 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7183 struct btrfs_key ins
;
7184 u64 cur_offset
= start
;
7187 bool own_trans
= true;
7191 while (num_bytes
> 0) {
7193 trans
= btrfs_start_transaction(root
, 3);
7194 if (IS_ERR(trans
)) {
7195 ret
= PTR_ERR(trans
);
7200 ret
= btrfs_reserve_extent(trans
, root
, num_bytes
, min_size
,
7201 0, *alloc_hint
, (u64
)-1, &ins
, 1);
7204 btrfs_end_transaction(trans
, root
);
7208 ret
= insert_reserved_file_extent(trans
, inode
,
7209 cur_offset
, ins
.objectid
,
7210 ins
.offset
, ins
.offset
,
7211 ins
.offset
, 0, 0, 0,
7212 BTRFS_FILE_EXTENT_PREALLOC
);
7214 btrfs_drop_extent_cache(inode
, cur_offset
,
7215 cur_offset
+ ins
.offset
-1, 0);
7217 num_bytes
-= ins
.offset
;
7218 cur_offset
+= ins
.offset
;
7219 *alloc_hint
= ins
.objectid
+ ins
.offset
;
7221 inode
->i_ctime
= CURRENT_TIME
;
7222 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
7223 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
7224 (actual_len
> inode
->i_size
) &&
7225 (cur_offset
> inode
->i_size
)) {
7226 if (cur_offset
> actual_len
)
7227 i_size
= actual_len
;
7229 i_size
= cur_offset
;
7230 i_size_write(inode
, i_size
);
7231 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
7234 ret
= btrfs_update_inode(trans
, root
, inode
);
7238 btrfs_end_transaction(trans
, root
);
7243 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
7244 u64 start
, u64 num_bytes
, u64 min_size
,
7245 loff_t actual_len
, u64
*alloc_hint
)
7247 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
7248 min_size
, actual_len
, alloc_hint
,
7252 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
7253 struct btrfs_trans_handle
*trans
, int mode
,
7254 u64 start
, u64 num_bytes
, u64 min_size
,
7255 loff_t actual_len
, u64
*alloc_hint
)
7257 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
7258 min_size
, actual_len
, alloc_hint
, trans
);
7261 static int btrfs_set_page_dirty(struct page
*page
)
7263 return __set_page_dirty_nobuffers(page
);
7266 static int btrfs_permission(struct inode
*inode
, int mask
, unsigned int flags
)
7268 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7270 if (btrfs_root_readonly(root
) && (mask
& MAY_WRITE
))
7272 if ((BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
) && (mask
& MAY_WRITE
))
7274 return generic_permission(inode
, mask
, flags
, btrfs_check_acl
);
7277 static const struct inode_operations btrfs_dir_inode_operations
= {
7278 .getattr
= btrfs_getattr
,
7279 .lookup
= btrfs_lookup
,
7280 .create
= btrfs_create
,
7281 .unlink
= btrfs_unlink
,
7283 .mkdir
= btrfs_mkdir
,
7284 .rmdir
= btrfs_rmdir
,
7285 .rename
= btrfs_rename
,
7286 .symlink
= btrfs_symlink
,
7287 .setattr
= btrfs_setattr
,
7288 .mknod
= btrfs_mknod
,
7289 .setxattr
= btrfs_setxattr
,
7290 .getxattr
= btrfs_getxattr
,
7291 .listxattr
= btrfs_listxattr
,
7292 .removexattr
= btrfs_removexattr
,
7293 .permission
= btrfs_permission
,
7295 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
7296 .lookup
= btrfs_lookup
,
7297 .permission
= btrfs_permission
,
7300 static const struct file_operations btrfs_dir_file_operations
= {
7301 .llseek
= generic_file_llseek
,
7302 .read
= generic_read_dir
,
7303 .readdir
= btrfs_real_readdir
,
7304 .unlocked_ioctl
= btrfs_ioctl
,
7305 #ifdef CONFIG_COMPAT
7306 .compat_ioctl
= btrfs_ioctl
,
7308 .release
= btrfs_release_file
,
7309 .fsync
= btrfs_sync_file
,
7312 static struct extent_io_ops btrfs_extent_io_ops
= {
7313 .fill_delalloc
= run_delalloc_range
,
7314 .submit_bio_hook
= btrfs_submit_bio_hook
,
7315 .merge_bio_hook
= btrfs_merge_bio_hook
,
7316 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
7317 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
7318 .writepage_start_hook
= btrfs_writepage_start_hook
,
7319 .readpage_io_failed_hook
= btrfs_io_failed_hook
,
7320 .set_bit_hook
= btrfs_set_bit_hook
,
7321 .clear_bit_hook
= btrfs_clear_bit_hook
,
7322 .merge_extent_hook
= btrfs_merge_extent_hook
,
7323 .split_extent_hook
= btrfs_split_extent_hook
,
7327 * btrfs doesn't support the bmap operation because swapfiles
7328 * use bmap to make a mapping of extents in the file. They assume
7329 * these extents won't change over the life of the file and they
7330 * use the bmap result to do IO directly to the drive.
7332 * the btrfs bmap call would return logical addresses that aren't
7333 * suitable for IO and they also will change frequently as COW
7334 * operations happen. So, swapfile + btrfs == corruption.
7336 * For now we're avoiding this by dropping bmap.
7338 static const struct address_space_operations btrfs_aops
= {
7339 .readpage
= btrfs_readpage
,
7340 .writepage
= btrfs_writepage
,
7341 .writepages
= btrfs_writepages
,
7342 .readpages
= btrfs_readpages
,
7343 .direct_IO
= btrfs_direct_IO
,
7344 .invalidatepage
= btrfs_invalidatepage
,
7345 .releasepage
= btrfs_releasepage
,
7346 .set_page_dirty
= btrfs_set_page_dirty
,
7347 .error_remove_page
= generic_error_remove_page
,
7350 static const struct address_space_operations btrfs_symlink_aops
= {
7351 .readpage
= btrfs_readpage
,
7352 .writepage
= btrfs_writepage
,
7353 .invalidatepage
= btrfs_invalidatepage
,
7354 .releasepage
= btrfs_releasepage
,
7357 static const struct inode_operations btrfs_file_inode_operations
= {
7358 .truncate
= btrfs_truncate
,
7359 .getattr
= btrfs_getattr
,
7360 .setattr
= btrfs_setattr
,
7361 .setxattr
= btrfs_setxattr
,
7362 .getxattr
= btrfs_getxattr
,
7363 .listxattr
= btrfs_listxattr
,
7364 .removexattr
= btrfs_removexattr
,
7365 .permission
= btrfs_permission
,
7366 .fiemap
= btrfs_fiemap
,
7368 static const struct inode_operations btrfs_special_inode_operations
= {
7369 .getattr
= btrfs_getattr
,
7370 .setattr
= btrfs_setattr
,
7371 .permission
= btrfs_permission
,
7372 .setxattr
= btrfs_setxattr
,
7373 .getxattr
= btrfs_getxattr
,
7374 .listxattr
= btrfs_listxattr
,
7375 .removexattr
= btrfs_removexattr
,
7377 static const struct inode_operations btrfs_symlink_inode_operations
= {
7378 .readlink
= generic_readlink
,
7379 .follow_link
= page_follow_link_light
,
7380 .put_link
= page_put_link
,
7381 .getattr
= btrfs_getattr
,
7382 .permission
= btrfs_permission
,
7383 .setxattr
= btrfs_setxattr
,
7384 .getxattr
= btrfs_getxattr
,
7385 .listxattr
= btrfs_listxattr
,
7386 .removexattr
= btrfs_removexattr
,
7389 const struct dentry_operations btrfs_dentry_operations
= {
7390 .d_delete
= btrfs_dentry_delete
,