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>
40 #include <linux/ratelimit.h>
41 #include <linux/mount.h>
42 #include <linux/btrfs.h>
43 #include <linux/blkdev.h>
47 #include "transaction.h"
48 #include "btrfs_inode.h"
49 #include "print-tree.h"
50 #include "ordered-data.h"
54 #include "compression.h"
56 #include "free-space-cache.h"
57 #include "inode-map.h"
59 struct btrfs_iget_args
{
61 struct btrfs_root
*root
;
64 static const struct inode_operations btrfs_dir_inode_operations
;
65 static const struct inode_operations btrfs_symlink_inode_operations
;
66 static const struct inode_operations btrfs_dir_ro_inode_operations
;
67 static const struct inode_operations btrfs_special_inode_operations
;
68 static const struct inode_operations btrfs_file_inode_operations
;
69 static const struct address_space_operations btrfs_aops
;
70 static const struct address_space_operations btrfs_symlink_aops
;
71 static const struct file_operations btrfs_dir_file_operations
;
72 static struct extent_io_ops btrfs_extent_io_ops
;
74 static struct kmem_cache
*btrfs_inode_cachep
;
75 static struct kmem_cache
*btrfs_delalloc_work_cachep
;
76 struct kmem_cache
*btrfs_trans_handle_cachep
;
77 struct kmem_cache
*btrfs_transaction_cachep
;
78 struct kmem_cache
*btrfs_path_cachep
;
79 struct kmem_cache
*btrfs_free_space_cachep
;
82 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
83 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
84 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
85 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
86 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
87 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
88 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
89 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
92 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
);
93 static int btrfs_truncate(struct inode
*inode
);
94 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
);
95 static noinline
int cow_file_range(struct inode
*inode
,
96 struct page
*locked_page
,
97 u64 start
, u64 end
, int *page_started
,
98 unsigned long *nr_written
, int unlock
);
99 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
100 u64 len
, u64 orig_start
,
101 u64 block_start
, u64 block_len
,
102 u64 orig_block_len
, int type
);
104 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
105 struct inode
*inode
, struct inode
*dir
,
106 const struct qstr
*qstr
)
110 err
= btrfs_init_acl(trans
, inode
, dir
);
112 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
117 * this does all the hard work for inserting an inline extent into
118 * the btree. The caller should have done a btrfs_drop_extents so that
119 * no overlapping inline items exist in the btree
121 static noinline
int insert_inline_extent(struct btrfs_trans_handle
*trans
,
122 struct btrfs_root
*root
, struct inode
*inode
,
123 u64 start
, size_t size
, size_t compressed_size
,
125 struct page
**compressed_pages
)
127 struct btrfs_key key
;
128 struct btrfs_path
*path
;
129 struct extent_buffer
*leaf
;
130 struct page
*page
= NULL
;
133 struct btrfs_file_extent_item
*ei
;
136 size_t cur_size
= size
;
138 unsigned long offset
;
140 if (compressed_size
&& compressed_pages
)
141 cur_size
= compressed_size
;
143 path
= btrfs_alloc_path();
147 path
->leave_spinning
= 1;
149 key
.objectid
= btrfs_ino(inode
);
151 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
152 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
154 inode_add_bytes(inode
, size
);
155 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
161 leaf
= path
->nodes
[0];
162 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
163 struct btrfs_file_extent_item
);
164 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
165 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
166 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
167 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
168 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
169 ptr
= btrfs_file_extent_inline_start(ei
);
171 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
174 while (compressed_size
> 0) {
175 cpage
= compressed_pages
[i
];
176 cur_size
= min_t(unsigned long, compressed_size
,
179 kaddr
= kmap_atomic(cpage
);
180 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
181 kunmap_atomic(kaddr
);
185 compressed_size
-= cur_size
;
187 btrfs_set_file_extent_compression(leaf
, ei
,
190 page
= find_get_page(inode
->i_mapping
,
191 start
>> PAGE_CACHE_SHIFT
);
192 btrfs_set_file_extent_compression(leaf
, ei
, 0);
193 kaddr
= kmap_atomic(page
);
194 offset
= start
& (PAGE_CACHE_SIZE
- 1);
195 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
196 kunmap_atomic(kaddr
);
197 page_cache_release(page
);
199 btrfs_mark_buffer_dirty(leaf
);
200 btrfs_free_path(path
);
203 * we're an inline extent, so nobody can
204 * extend the file past i_size without locking
205 * a page we already have locked.
207 * We must do any isize and inode updates
208 * before we unlock the pages. Otherwise we
209 * could end up racing with unlink.
211 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
212 ret
= btrfs_update_inode(trans
, root
, inode
);
216 btrfs_free_path(path
);
222 * conditionally insert an inline extent into the file. This
223 * does the checks required to make sure the data is small enough
224 * to fit as an inline extent.
226 static noinline
int cow_file_range_inline(struct btrfs_trans_handle
*trans
,
227 struct btrfs_root
*root
,
228 struct inode
*inode
, u64 start
, u64 end
,
229 size_t compressed_size
, int compress_type
,
230 struct page
**compressed_pages
)
232 u64 isize
= i_size_read(inode
);
233 u64 actual_end
= min(end
+ 1, isize
);
234 u64 inline_len
= actual_end
- start
;
235 u64 aligned_end
= (end
+ root
->sectorsize
- 1) &
236 ~((u64
)root
->sectorsize
- 1);
237 u64 data_len
= inline_len
;
241 data_len
= compressed_size
;
244 actual_end
>= PAGE_CACHE_SIZE
||
245 data_len
>= BTRFS_MAX_INLINE_DATA_SIZE(root
) ||
247 (actual_end
& (root
->sectorsize
- 1)) == 0) ||
249 data_len
> root
->fs_info
->max_inline
) {
253 ret
= btrfs_drop_extents(trans
, root
, inode
, start
, aligned_end
, 1);
257 if (isize
> actual_end
)
258 inline_len
= min_t(u64
, isize
, actual_end
);
259 ret
= insert_inline_extent(trans
, root
, inode
, start
,
260 inline_len
, compressed_size
,
261 compress_type
, compressed_pages
);
262 if (ret
&& ret
!= -ENOSPC
) {
263 btrfs_abort_transaction(trans
, root
, ret
);
265 } else if (ret
== -ENOSPC
) {
269 btrfs_delalloc_release_metadata(inode
, end
+ 1 - start
);
270 btrfs_drop_extent_cache(inode
, start
, aligned_end
- 1, 0);
274 struct async_extent
{
279 unsigned long nr_pages
;
281 struct list_head list
;
286 struct btrfs_root
*root
;
287 struct page
*locked_page
;
290 struct list_head extents
;
291 struct btrfs_work work
;
294 static noinline
int add_async_extent(struct async_cow
*cow
,
295 u64 start
, u64 ram_size
,
298 unsigned long nr_pages
,
301 struct async_extent
*async_extent
;
303 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
304 BUG_ON(!async_extent
); /* -ENOMEM */
305 async_extent
->start
= start
;
306 async_extent
->ram_size
= ram_size
;
307 async_extent
->compressed_size
= compressed_size
;
308 async_extent
->pages
= pages
;
309 async_extent
->nr_pages
= nr_pages
;
310 async_extent
->compress_type
= compress_type
;
311 list_add_tail(&async_extent
->list
, &cow
->extents
);
316 * we create compressed extents in two phases. The first
317 * phase compresses a range of pages that have already been
318 * locked (both pages and state bits are locked).
320 * This is done inside an ordered work queue, and the compression
321 * is spread across many cpus. The actual IO submission is step
322 * two, and the ordered work queue takes care of making sure that
323 * happens in the same order things were put onto the queue by
324 * writepages and friends.
326 * If this code finds it can't get good compression, it puts an
327 * entry onto the work queue to write the uncompressed bytes. This
328 * makes sure that both compressed inodes and uncompressed inodes
329 * are written in the same order that the flusher thread sent them
332 static noinline
int compress_file_range(struct inode
*inode
,
333 struct page
*locked_page
,
335 struct async_cow
*async_cow
,
338 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
339 struct btrfs_trans_handle
*trans
;
341 u64 blocksize
= root
->sectorsize
;
343 u64 isize
= i_size_read(inode
);
345 struct page
**pages
= NULL
;
346 unsigned long nr_pages
;
347 unsigned long nr_pages_ret
= 0;
348 unsigned long total_compressed
= 0;
349 unsigned long total_in
= 0;
350 unsigned long max_compressed
= 128 * 1024;
351 unsigned long max_uncompressed
= 128 * 1024;
354 int compress_type
= root
->fs_info
->compress_type
;
356 /* if this is a small write inside eof, kick off a defrag */
357 if ((end
- start
+ 1) < 16 * 1024 &&
358 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
359 btrfs_add_inode_defrag(NULL
, inode
);
361 actual_end
= min_t(u64
, isize
, end
+ 1);
364 nr_pages
= (end
>> PAGE_CACHE_SHIFT
) - (start
>> PAGE_CACHE_SHIFT
) + 1;
365 nr_pages
= min(nr_pages
, (128 * 1024UL) / PAGE_CACHE_SIZE
);
368 * we don't want to send crud past the end of i_size through
369 * compression, that's just a waste of CPU time. So, if the
370 * end of the file is before the start of our current
371 * requested range of bytes, we bail out to the uncompressed
372 * cleanup code that can deal with all of this.
374 * It isn't really the fastest way to fix things, but this is a
375 * very uncommon corner.
377 if (actual_end
<= start
)
378 goto cleanup_and_bail_uncompressed
;
380 total_compressed
= actual_end
- start
;
382 /* we want to make sure that amount of ram required to uncompress
383 * an extent is reasonable, so we limit the total size in ram
384 * of a compressed extent to 128k. This is a crucial number
385 * because it also controls how easily we can spread reads across
386 * cpus for decompression.
388 * We also want to make sure the amount of IO required to do
389 * a random read is reasonably small, so we limit the size of
390 * a compressed extent to 128k.
392 total_compressed
= min(total_compressed
, max_uncompressed
);
393 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
394 num_bytes
= max(blocksize
, num_bytes
);
399 * we do compression for mount -o compress and when the
400 * inode has not been flagged as nocompress. This flag can
401 * change at any time if we discover bad compression ratios.
403 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
) &&
404 (btrfs_test_opt(root
, COMPRESS
) ||
405 (BTRFS_I(inode
)->force_compress
) ||
406 (BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
))) {
408 pages
= kzalloc(sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
410 /* just bail out to the uncompressed code */
414 if (BTRFS_I(inode
)->force_compress
)
415 compress_type
= BTRFS_I(inode
)->force_compress
;
417 ret
= btrfs_compress_pages(compress_type
,
418 inode
->i_mapping
, start
,
419 total_compressed
, pages
,
420 nr_pages
, &nr_pages_ret
,
426 unsigned long offset
= total_compressed
&
427 (PAGE_CACHE_SIZE
- 1);
428 struct page
*page
= pages
[nr_pages_ret
- 1];
431 /* zero the tail end of the last page, we might be
432 * sending it down to disk
435 kaddr
= kmap_atomic(page
);
436 memset(kaddr
+ offset
, 0,
437 PAGE_CACHE_SIZE
- offset
);
438 kunmap_atomic(kaddr
);
445 trans
= btrfs_join_transaction(root
);
447 ret
= PTR_ERR(trans
);
449 goto cleanup_and_out
;
451 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
453 /* lets try to make an inline extent */
454 if (ret
|| total_in
< (actual_end
- start
)) {
455 /* we didn't compress the entire range, try
456 * to make an uncompressed inline extent.
458 ret
= cow_file_range_inline(trans
, root
, inode
,
459 start
, end
, 0, 0, NULL
);
461 /* try making a compressed inline extent */
462 ret
= cow_file_range_inline(trans
, root
, inode
,
465 compress_type
, pages
);
469 * inline extent creation worked or returned error,
470 * we don't need to create any more async work items.
471 * Unlock and free up our temp pages.
473 extent_clear_unlock_delalloc(inode
,
474 &BTRFS_I(inode
)->io_tree
,
476 EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
477 EXTENT_CLEAR_DELALLOC
|
478 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
);
480 btrfs_end_transaction(trans
, root
);
483 btrfs_end_transaction(trans
, root
);
488 * we aren't doing an inline extent round the compressed size
489 * up to a block size boundary so the allocator does sane
492 total_compressed
= (total_compressed
+ blocksize
- 1) &
496 * one last check to make sure the compression is really a
497 * win, compare the page count read with the blocks on disk
499 total_in
= (total_in
+ PAGE_CACHE_SIZE
- 1) &
500 ~(PAGE_CACHE_SIZE
- 1);
501 if (total_compressed
>= total_in
) {
504 num_bytes
= total_in
;
507 if (!will_compress
&& pages
) {
509 * the compression code ran but failed to make things smaller,
510 * free any pages it allocated and our page pointer array
512 for (i
= 0; i
< nr_pages_ret
; i
++) {
513 WARN_ON(pages
[i
]->mapping
);
514 page_cache_release(pages
[i
]);
518 total_compressed
= 0;
521 /* flag the file so we don't compress in the future */
522 if (!btrfs_test_opt(root
, FORCE_COMPRESS
) &&
523 !(BTRFS_I(inode
)->force_compress
)) {
524 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
530 /* the async work queues will take care of doing actual
531 * allocation on disk for these compressed pages,
532 * and will submit them to the elevator.
534 add_async_extent(async_cow
, start
, num_bytes
,
535 total_compressed
, pages
, nr_pages_ret
,
538 if (start
+ num_bytes
< end
) {
545 cleanup_and_bail_uncompressed
:
547 * No compression, but we still need to write the pages in
548 * the file we've been given so far. redirty the locked
549 * page if it corresponds to our extent and set things up
550 * for the async work queue to run cow_file_range to do
551 * the normal delalloc dance
553 if (page_offset(locked_page
) >= start
&&
554 page_offset(locked_page
) <= end
) {
555 __set_page_dirty_nobuffers(locked_page
);
556 /* unlocked later on in the async handlers */
558 add_async_extent(async_cow
, start
, end
- start
+ 1,
559 0, NULL
, 0, BTRFS_COMPRESS_NONE
);
567 for (i
= 0; i
< nr_pages_ret
; i
++) {
568 WARN_ON(pages
[i
]->mapping
);
569 page_cache_release(pages
[i
]);
576 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
578 EXTENT_CLEAR_UNLOCK_PAGE
|
580 EXTENT_CLEAR_DELALLOC
|
581 EXTENT_SET_WRITEBACK
|
582 EXTENT_END_WRITEBACK
);
583 if (!trans
|| IS_ERR(trans
))
584 btrfs_error(root
->fs_info
, ret
, "Failed to join transaction");
586 btrfs_abort_transaction(trans
, root
, ret
);
591 * phase two of compressed writeback. This is the ordered portion
592 * of the code, which only gets called in the order the work was
593 * queued. We walk all the async extents created by compress_file_range
594 * and send them down to the disk.
596 static noinline
int submit_compressed_extents(struct inode
*inode
,
597 struct async_cow
*async_cow
)
599 struct async_extent
*async_extent
;
601 struct btrfs_trans_handle
*trans
;
602 struct btrfs_key ins
;
603 struct extent_map
*em
;
604 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
605 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
606 struct extent_io_tree
*io_tree
;
609 if (list_empty(&async_cow
->extents
))
613 while (!list_empty(&async_cow
->extents
)) {
614 async_extent
= list_entry(async_cow
->extents
.next
,
615 struct async_extent
, list
);
616 list_del(&async_extent
->list
);
618 io_tree
= &BTRFS_I(inode
)->io_tree
;
621 /* did the compression code fall back to uncompressed IO? */
622 if (!async_extent
->pages
) {
623 int page_started
= 0;
624 unsigned long nr_written
= 0;
626 lock_extent(io_tree
, async_extent
->start
,
627 async_extent
->start
+
628 async_extent
->ram_size
- 1);
630 /* allocate blocks */
631 ret
= cow_file_range(inode
, async_cow
->locked_page
,
633 async_extent
->start
+
634 async_extent
->ram_size
- 1,
635 &page_started
, &nr_written
, 0);
640 * if page_started, cow_file_range inserted an
641 * inline extent and took care of all the unlocking
642 * and IO for us. Otherwise, we need to submit
643 * all those pages down to the drive.
645 if (!page_started
&& !ret
)
646 extent_write_locked_range(io_tree
,
647 inode
, async_extent
->start
,
648 async_extent
->start
+
649 async_extent
->ram_size
- 1,
653 unlock_page(async_cow
->locked_page
);
659 lock_extent(io_tree
, async_extent
->start
,
660 async_extent
->start
+ async_extent
->ram_size
- 1);
662 trans
= btrfs_join_transaction(root
);
664 ret
= PTR_ERR(trans
);
666 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
667 ret
= btrfs_reserve_extent(trans
, root
,
668 async_extent
->compressed_size
,
669 async_extent
->compressed_size
,
670 0, alloc_hint
, &ins
, 1);
671 if (ret
&& ret
!= -ENOSPC
)
672 btrfs_abort_transaction(trans
, root
, ret
);
673 btrfs_end_transaction(trans
, root
);
679 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
680 WARN_ON(async_extent
->pages
[i
]->mapping
);
681 page_cache_release(async_extent
->pages
[i
]);
683 kfree(async_extent
->pages
);
684 async_extent
->nr_pages
= 0;
685 async_extent
->pages
= NULL
;
693 * here we're doing allocation and writeback of the
696 btrfs_drop_extent_cache(inode
, async_extent
->start
,
697 async_extent
->start
+
698 async_extent
->ram_size
- 1, 0);
700 em
= alloc_extent_map();
702 goto out_free_reserve
;
703 em
->start
= async_extent
->start
;
704 em
->len
= async_extent
->ram_size
;
705 em
->orig_start
= em
->start
;
706 em
->mod_start
= em
->start
;
707 em
->mod_len
= em
->len
;
709 em
->block_start
= ins
.objectid
;
710 em
->block_len
= ins
.offset
;
711 em
->orig_block_len
= ins
.offset
;
712 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
713 em
->compress_type
= async_extent
->compress_type
;
714 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
715 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
719 write_lock(&em_tree
->lock
);
720 ret
= add_extent_mapping(em_tree
, em
);
723 &em_tree
->modified_extents
);
724 write_unlock(&em_tree
->lock
);
725 if (ret
!= -EEXIST
) {
729 btrfs_drop_extent_cache(inode
, async_extent
->start
,
730 async_extent
->start
+
731 async_extent
->ram_size
- 1, 0);
735 goto out_free_reserve
;
737 ret
= btrfs_add_ordered_extent_compress(inode
,
740 async_extent
->ram_size
,
742 BTRFS_ORDERED_COMPRESSED
,
743 async_extent
->compress_type
);
745 goto out_free_reserve
;
748 * clear dirty, set writeback and unlock the pages.
750 extent_clear_unlock_delalloc(inode
,
751 &BTRFS_I(inode
)->io_tree
,
753 async_extent
->start
+
754 async_extent
->ram_size
- 1,
755 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
756 EXTENT_CLEAR_UNLOCK
|
757 EXTENT_CLEAR_DELALLOC
|
758 EXTENT_CLEAR_DIRTY
| EXTENT_SET_WRITEBACK
);
760 ret
= btrfs_submit_compressed_write(inode
,
762 async_extent
->ram_size
,
764 ins
.offset
, async_extent
->pages
,
765 async_extent
->nr_pages
);
766 alloc_hint
= ins
.objectid
+ ins
.offset
;
776 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
778 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
780 async_extent
->start
+
781 async_extent
->ram_size
- 1,
782 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
783 EXTENT_CLEAR_UNLOCK
|
784 EXTENT_CLEAR_DELALLOC
|
786 EXTENT_SET_WRITEBACK
|
787 EXTENT_END_WRITEBACK
);
792 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
795 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
796 struct extent_map
*em
;
799 read_lock(&em_tree
->lock
);
800 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
803 * if block start isn't an actual block number then find the
804 * first block in this inode and use that as a hint. If that
805 * block is also bogus then just don't worry about it.
807 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
809 em
= search_extent_mapping(em_tree
, 0, 0);
810 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
811 alloc_hint
= em
->block_start
;
815 alloc_hint
= em
->block_start
;
819 read_unlock(&em_tree
->lock
);
825 * when extent_io.c finds a delayed allocation range in the file,
826 * the call backs end up in this code. The basic idea is to
827 * allocate extents on disk for the range, and create ordered data structs
828 * in ram to track those extents.
830 * locked_page is the page that writepage had locked already. We use
831 * it to make sure we don't do extra locks or unlocks.
833 * *page_started is set to one if we unlock locked_page and do everything
834 * required to start IO on it. It may be clean and already done with
837 static noinline
int __cow_file_range(struct btrfs_trans_handle
*trans
,
839 struct btrfs_root
*root
,
840 struct page
*locked_page
,
841 u64 start
, u64 end
, int *page_started
,
842 unsigned long *nr_written
,
847 unsigned long ram_size
;
850 u64 blocksize
= root
->sectorsize
;
851 struct btrfs_key ins
;
852 struct extent_map
*em
;
853 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
856 BUG_ON(btrfs_is_free_space_inode(inode
));
858 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
859 num_bytes
= max(blocksize
, num_bytes
);
860 disk_num_bytes
= num_bytes
;
862 /* if this is a small write inside eof, kick off defrag */
863 if (num_bytes
< 64 * 1024 &&
864 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
865 btrfs_add_inode_defrag(trans
, inode
);
868 /* lets try to make an inline extent */
869 ret
= cow_file_range_inline(trans
, root
, inode
,
870 start
, end
, 0, 0, NULL
);
872 extent_clear_unlock_delalloc(inode
,
873 &BTRFS_I(inode
)->io_tree
,
875 EXTENT_CLEAR_UNLOCK_PAGE
|
876 EXTENT_CLEAR_UNLOCK
|
877 EXTENT_CLEAR_DELALLOC
|
879 EXTENT_SET_WRITEBACK
|
880 EXTENT_END_WRITEBACK
);
882 *nr_written
= *nr_written
+
883 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
886 } else if (ret
< 0) {
887 btrfs_abort_transaction(trans
, root
, ret
);
892 BUG_ON(disk_num_bytes
>
893 btrfs_super_total_bytes(root
->fs_info
->super_copy
));
895 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
896 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
898 while (disk_num_bytes
> 0) {
901 cur_alloc_size
= disk_num_bytes
;
902 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
903 root
->sectorsize
, 0, alloc_hint
,
906 btrfs_abort_transaction(trans
, root
, ret
);
910 em
= alloc_extent_map();
911 BUG_ON(!em
); /* -ENOMEM */
913 em
->orig_start
= em
->start
;
914 ram_size
= ins
.offset
;
915 em
->len
= ins
.offset
;
916 em
->mod_start
= em
->start
;
917 em
->mod_len
= em
->len
;
919 em
->block_start
= ins
.objectid
;
920 em
->block_len
= ins
.offset
;
921 em
->orig_block_len
= ins
.offset
;
922 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
923 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
927 write_lock(&em_tree
->lock
);
928 ret
= add_extent_mapping(em_tree
, em
);
931 &em_tree
->modified_extents
);
932 write_unlock(&em_tree
->lock
);
933 if (ret
!= -EEXIST
) {
937 btrfs_drop_extent_cache(inode
, start
,
938 start
+ ram_size
- 1, 0);
941 cur_alloc_size
= ins
.offset
;
942 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
943 ram_size
, cur_alloc_size
, 0);
944 BUG_ON(ret
); /* -ENOMEM */
946 if (root
->root_key
.objectid
==
947 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
948 ret
= btrfs_reloc_clone_csums(inode
, start
,
951 btrfs_abort_transaction(trans
, root
, ret
);
956 if (disk_num_bytes
< cur_alloc_size
)
959 /* we're not doing compressed IO, don't unlock the first
960 * page (which the caller expects to stay locked), don't
961 * clear any dirty bits and don't set any writeback bits
963 * Do set the Private2 bit so we know this page was properly
964 * setup for writepage
966 op
= unlock
? EXTENT_CLEAR_UNLOCK_PAGE
: 0;
967 op
|= EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
970 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
971 start
, start
+ ram_size
- 1,
973 disk_num_bytes
-= cur_alloc_size
;
974 num_bytes
-= cur_alloc_size
;
975 alloc_hint
= ins
.objectid
+ ins
.offset
;
976 start
+= cur_alloc_size
;
982 extent_clear_unlock_delalloc(inode
,
983 &BTRFS_I(inode
)->io_tree
,
984 start
, end
, locked_page
,
985 EXTENT_CLEAR_UNLOCK_PAGE
|
986 EXTENT_CLEAR_UNLOCK
|
987 EXTENT_CLEAR_DELALLOC
|
989 EXTENT_SET_WRITEBACK
|
990 EXTENT_END_WRITEBACK
);
995 static noinline
int cow_file_range(struct inode
*inode
,
996 struct page
*locked_page
,
997 u64 start
, u64 end
, int *page_started
,
998 unsigned long *nr_written
,
1001 struct btrfs_trans_handle
*trans
;
1002 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1005 trans
= btrfs_join_transaction(root
);
1006 if (IS_ERR(trans
)) {
1007 extent_clear_unlock_delalloc(inode
,
1008 &BTRFS_I(inode
)->io_tree
,
1009 start
, end
, locked_page
,
1010 EXTENT_CLEAR_UNLOCK_PAGE
|
1011 EXTENT_CLEAR_UNLOCK
|
1012 EXTENT_CLEAR_DELALLOC
|
1013 EXTENT_CLEAR_DIRTY
|
1014 EXTENT_SET_WRITEBACK
|
1015 EXTENT_END_WRITEBACK
);
1016 return PTR_ERR(trans
);
1018 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1020 ret
= __cow_file_range(trans
, inode
, root
, locked_page
, start
, end
,
1021 page_started
, nr_written
, unlock
);
1023 btrfs_end_transaction(trans
, root
);
1029 * work queue call back to started compression on a file and pages
1031 static noinline
void async_cow_start(struct btrfs_work
*work
)
1033 struct async_cow
*async_cow
;
1035 async_cow
= container_of(work
, struct async_cow
, work
);
1037 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
1038 async_cow
->start
, async_cow
->end
, async_cow
,
1040 if (num_added
== 0) {
1041 btrfs_add_delayed_iput(async_cow
->inode
);
1042 async_cow
->inode
= NULL
;
1047 * work queue call back to submit previously compressed pages
1049 static noinline
void async_cow_submit(struct btrfs_work
*work
)
1051 struct async_cow
*async_cow
;
1052 struct btrfs_root
*root
;
1053 unsigned long nr_pages
;
1055 async_cow
= container_of(work
, struct async_cow
, work
);
1057 root
= async_cow
->root
;
1058 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
1061 if (atomic_sub_return(nr_pages
, &root
->fs_info
->async_delalloc_pages
) <
1063 waitqueue_active(&root
->fs_info
->async_submit_wait
))
1064 wake_up(&root
->fs_info
->async_submit_wait
);
1066 if (async_cow
->inode
)
1067 submit_compressed_extents(async_cow
->inode
, async_cow
);
1070 static noinline
void async_cow_free(struct btrfs_work
*work
)
1072 struct async_cow
*async_cow
;
1073 async_cow
= container_of(work
, struct async_cow
, work
);
1074 if (async_cow
->inode
)
1075 btrfs_add_delayed_iput(async_cow
->inode
);
1079 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
1080 u64 start
, u64 end
, int *page_started
,
1081 unsigned long *nr_written
)
1083 struct async_cow
*async_cow
;
1084 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1085 unsigned long nr_pages
;
1087 int limit
= 10 * 1024 * 1024;
1089 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
1090 1, 0, NULL
, GFP_NOFS
);
1091 while (start
< end
) {
1092 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
1093 BUG_ON(!async_cow
); /* -ENOMEM */
1094 async_cow
->inode
= igrab(inode
);
1095 async_cow
->root
= root
;
1096 async_cow
->locked_page
= locked_page
;
1097 async_cow
->start
= start
;
1099 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
1102 cur_end
= min(end
, start
+ 512 * 1024 - 1);
1104 async_cow
->end
= cur_end
;
1105 INIT_LIST_HEAD(&async_cow
->extents
);
1107 async_cow
->work
.func
= async_cow_start
;
1108 async_cow
->work
.ordered_func
= async_cow_submit
;
1109 async_cow
->work
.ordered_free
= async_cow_free
;
1110 async_cow
->work
.flags
= 0;
1112 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
1114 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
1116 btrfs_queue_worker(&root
->fs_info
->delalloc_workers
,
1119 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
1120 wait_event(root
->fs_info
->async_submit_wait
,
1121 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
1125 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
1126 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1127 wait_event(root
->fs_info
->async_submit_wait
,
1128 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
1132 *nr_written
+= nr_pages
;
1133 start
= cur_end
+ 1;
1139 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
1140 u64 bytenr
, u64 num_bytes
)
1143 struct btrfs_ordered_sum
*sums
;
1146 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
1147 bytenr
+ num_bytes
- 1, &list
, 0);
1148 if (ret
== 0 && list_empty(&list
))
1151 while (!list_empty(&list
)) {
1152 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1153 list_del(&sums
->list
);
1160 * when nowcow writeback call back. This checks for snapshots or COW copies
1161 * of the extents that exist in the file, and COWs the file as required.
1163 * If no cow copies or snapshots exist, we write directly to the existing
1166 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1167 struct page
*locked_page
,
1168 u64 start
, u64 end
, int *page_started
, int force
,
1169 unsigned long *nr_written
)
1171 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1172 struct btrfs_trans_handle
*trans
;
1173 struct extent_buffer
*leaf
;
1174 struct btrfs_path
*path
;
1175 struct btrfs_file_extent_item
*fi
;
1176 struct btrfs_key found_key
;
1190 u64 ino
= btrfs_ino(inode
);
1192 path
= btrfs_alloc_path();
1194 extent_clear_unlock_delalloc(inode
,
1195 &BTRFS_I(inode
)->io_tree
,
1196 start
, end
, locked_page
,
1197 EXTENT_CLEAR_UNLOCK_PAGE
|
1198 EXTENT_CLEAR_UNLOCK
|
1199 EXTENT_CLEAR_DELALLOC
|
1200 EXTENT_CLEAR_DIRTY
|
1201 EXTENT_SET_WRITEBACK
|
1202 EXTENT_END_WRITEBACK
);
1206 nolock
= btrfs_is_free_space_inode(inode
);
1209 trans
= btrfs_join_transaction_nolock(root
);
1211 trans
= btrfs_join_transaction(root
);
1213 if (IS_ERR(trans
)) {
1214 extent_clear_unlock_delalloc(inode
,
1215 &BTRFS_I(inode
)->io_tree
,
1216 start
, end
, locked_page
,
1217 EXTENT_CLEAR_UNLOCK_PAGE
|
1218 EXTENT_CLEAR_UNLOCK
|
1219 EXTENT_CLEAR_DELALLOC
|
1220 EXTENT_CLEAR_DIRTY
|
1221 EXTENT_SET_WRITEBACK
|
1222 EXTENT_END_WRITEBACK
);
1223 btrfs_free_path(path
);
1224 return PTR_ERR(trans
);
1227 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1229 cow_start
= (u64
)-1;
1232 ret
= btrfs_lookup_file_extent(trans
, root
, path
, ino
,
1235 btrfs_abort_transaction(trans
, root
, ret
);
1238 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1239 leaf
= path
->nodes
[0];
1240 btrfs_item_key_to_cpu(leaf
, &found_key
,
1241 path
->slots
[0] - 1);
1242 if (found_key
.objectid
== ino
&&
1243 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1248 leaf
= path
->nodes
[0];
1249 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1250 ret
= btrfs_next_leaf(root
, path
);
1252 btrfs_abort_transaction(trans
, root
, ret
);
1257 leaf
= path
->nodes
[0];
1263 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1265 if (found_key
.objectid
> ino
||
1266 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1267 found_key
.offset
> end
)
1270 if (found_key
.offset
> cur_offset
) {
1271 extent_end
= found_key
.offset
;
1276 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1277 struct btrfs_file_extent_item
);
1278 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1280 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1281 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1282 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1283 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1284 extent_end
= found_key
.offset
+
1285 btrfs_file_extent_num_bytes(leaf
, fi
);
1287 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1288 if (extent_end
<= start
) {
1292 if (disk_bytenr
== 0)
1294 if (btrfs_file_extent_compression(leaf
, fi
) ||
1295 btrfs_file_extent_encryption(leaf
, fi
) ||
1296 btrfs_file_extent_other_encoding(leaf
, fi
))
1298 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1300 if (btrfs_extent_readonly(root
, disk_bytenr
))
1302 if (btrfs_cross_ref_exist(trans
, root
, ino
,
1304 extent_offset
, disk_bytenr
))
1306 disk_bytenr
+= extent_offset
;
1307 disk_bytenr
+= cur_offset
- found_key
.offset
;
1308 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1310 * force cow if csum exists in the range.
1311 * this ensure that csum for a given extent are
1312 * either valid or do not exist.
1314 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1317 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1318 extent_end
= found_key
.offset
+
1319 btrfs_file_extent_inline_len(leaf
, fi
);
1320 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1325 if (extent_end
<= start
) {
1330 if (cow_start
== (u64
)-1)
1331 cow_start
= cur_offset
;
1332 cur_offset
= extent_end
;
1333 if (cur_offset
> end
)
1339 btrfs_release_path(path
);
1340 if (cow_start
!= (u64
)-1) {
1341 ret
= __cow_file_range(trans
, inode
, root
, locked_page
,
1342 cow_start
, found_key
.offset
- 1,
1343 page_started
, nr_written
, 1);
1345 btrfs_abort_transaction(trans
, root
, ret
);
1348 cow_start
= (u64
)-1;
1351 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1352 struct extent_map
*em
;
1353 struct extent_map_tree
*em_tree
;
1354 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1355 em
= alloc_extent_map();
1356 BUG_ON(!em
); /* -ENOMEM */
1357 em
->start
= cur_offset
;
1358 em
->orig_start
= found_key
.offset
- extent_offset
;
1359 em
->len
= num_bytes
;
1360 em
->block_len
= num_bytes
;
1361 em
->block_start
= disk_bytenr
;
1362 em
->orig_block_len
= disk_num_bytes
;
1363 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1364 em
->mod_start
= em
->start
;
1365 em
->mod_len
= em
->len
;
1366 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1367 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
1368 em
->generation
= -1;
1370 write_lock(&em_tree
->lock
);
1371 ret
= add_extent_mapping(em_tree
, em
);
1373 list_move(&em
->list
,
1374 &em_tree
->modified_extents
);
1375 write_unlock(&em_tree
->lock
);
1376 if (ret
!= -EEXIST
) {
1377 free_extent_map(em
);
1380 btrfs_drop_extent_cache(inode
, em
->start
,
1381 em
->start
+ em
->len
- 1, 0);
1383 type
= BTRFS_ORDERED_PREALLOC
;
1385 type
= BTRFS_ORDERED_NOCOW
;
1388 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1389 num_bytes
, num_bytes
, type
);
1390 BUG_ON(ret
); /* -ENOMEM */
1392 if (root
->root_key
.objectid
==
1393 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1394 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1397 btrfs_abort_transaction(trans
, root
, ret
);
1402 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
1403 cur_offset
, cur_offset
+ num_bytes
- 1,
1404 locked_page
, EXTENT_CLEAR_UNLOCK_PAGE
|
1405 EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
1406 EXTENT_SET_PRIVATE2
);
1407 cur_offset
= extent_end
;
1408 if (cur_offset
> end
)
1411 btrfs_release_path(path
);
1413 if (cur_offset
<= end
&& cow_start
== (u64
)-1) {
1414 cow_start
= cur_offset
;
1418 if (cow_start
!= (u64
)-1) {
1419 ret
= __cow_file_range(trans
, inode
, root
, locked_page
,
1421 page_started
, nr_written
, 1);
1423 btrfs_abort_transaction(trans
, root
, ret
);
1429 err
= btrfs_end_transaction(trans
, root
);
1433 if (ret
&& cur_offset
< end
)
1434 extent_clear_unlock_delalloc(inode
,
1435 &BTRFS_I(inode
)->io_tree
,
1436 cur_offset
, end
, locked_page
,
1437 EXTENT_CLEAR_UNLOCK_PAGE
|
1438 EXTENT_CLEAR_UNLOCK
|
1439 EXTENT_CLEAR_DELALLOC
|
1440 EXTENT_CLEAR_DIRTY
|
1441 EXTENT_SET_WRITEBACK
|
1442 EXTENT_END_WRITEBACK
);
1444 btrfs_free_path(path
);
1449 * extent_io.c call back to do delayed allocation processing
1451 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1452 u64 start
, u64 end
, int *page_started
,
1453 unsigned long *nr_written
)
1456 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1458 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) {
1459 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1460 page_started
, 1, nr_written
);
1461 } else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
) {
1462 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1463 page_started
, 0, nr_written
);
1464 } else if (!btrfs_test_opt(root
, COMPRESS
) &&
1465 !(BTRFS_I(inode
)->force_compress
) &&
1466 !(BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
)) {
1467 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1468 page_started
, nr_written
, 1);
1470 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1471 &BTRFS_I(inode
)->runtime_flags
);
1472 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1473 page_started
, nr_written
);
1478 static void btrfs_split_extent_hook(struct inode
*inode
,
1479 struct extent_state
*orig
, u64 split
)
1481 /* not delalloc, ignore it */
1482 if (!(orig
->state
& EXTENT_DELALLOC
))
1485 spin_lock(&BTRFS_I(inode
)->lock
);
1486 BTRFS_I(inode
)->outstanding_extents
++;
1487 spin_unlock(&BTRFS_I(inode
)->lock
);
1491 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1492 * extents so we can keep track of new extents that are just merged onto old
1493 * extents, such as when we are doing sequential writes, so we can properly
1494 * account for the metadata space we'll need.
1496 static void btrfs_merge_extent_hook(struct inode
*inode
,
1497 struct extent_state
*new,
1498 struct extent_state
*other
)
1500 /* not delalloc, ignore it */
1501 if (!(other
->state
& EXTENT_DELALLOC
))
1504 spin_lock(&BTRFS_I(inode
)->lock
);
1505 BTRFS_I(inode
)->outstanding_extents
--;
1506 spin_unlock(&BTRFS_I(inode
)->lock
);
1510 * extent_io.c set_bit_hook, used to track delayed allocation
1511 * bytes in this file, and to maintain the list of inodes that
1512 * have pending delalloc work to be done.
1514 static void btrfs_set_bit_hook(struct inode
*inode
,
1515 struct extent_state
*state
, int *bits
)
1519 * set_bit and clear bit hooks normally require _irqsave/restore
1520 * but in this case, we are only testing for the DELALLOC
1521 * bit, which is only set or cleared with irqs on
1523 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1524 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1525 u64 len
= state
->end
+ 1 - state
->start
;
1526 bool do_list
= !btrfs_is_free_space_inode(inode
);
1528 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1529 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1531 spin_lock(&BTRFS_I(inode
)->lock
);
1532 BTRFS_I(inode
)->outstanding_extents
++;
1533 spin_unlock(&BTRFS_I(inode
)->lock
);
1536 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, len
,
1537 root
->fs_info
->delalloc_batch
);
1538 spin_lock(&BTRFS_I(inode
)->lock
);
1539 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1540 if (do_list
&& !test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1541 &BTRFS_I(inode
)->runtime_flags
)) {
1542 spin_lock(&root
->fs_info
->delalloc_lock
);
1543 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1544 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1545 &root
->fs_info
->delalloc_inodes
);
1546 set_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1547 &BTRFS_I(inode
)->runtime_flags
);
1549 spin_unlock(&root
->fs_info
->delalloc_lock
);
1551 spin_unlock(&BTRFS_I(inode
)->lock
);
1556 * extent_io.c clear_bit_hook, see set_bit_hook for why
1558 static void btrfs_clear_bit_hook(struct inode
*inode
,
1559 struct extent_state
*state
, int *bits
)
1562 * set_bit and clear bit hooks normally require _irqsave/restore
1563 * but in this case, we are only testing for the DELALLOC
1564 * bit, which is only set or cleared with irqs on
1566 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1567 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1568 u64 len
= state
->end
+ 1 - state
->start
;
1569 bool do_list
= !btrfs_is_free_space_inode(inode
);
1571 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1572 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1573 } else if (!(*bits
& EXTENT_DO_ACCOUNTING
)) {
1574 spin_lock(&BTRFS_I(inode
)->lock
);
1575 BTRFS_I(inode
)->outstanding_extents
--;
1576 spin_unlock(&BTRFS_I(inode
)->lock
);
1579 if (*bits
& EXTENT_DO_ACCOUNTING
)
1580 btrfs_delalloc_release_metadata(inode
, len
);
1582 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1584 btrfs_free_reserved_data_space(inode
, len
);
1586 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, -len
,
1587 root
->fs_info
->delalloc_batch
);
1588 spin_lock(&BTRFS_I(inode
)->lock
);
1589 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1590 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1591 test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1592 &BTRFS_I(inode
)->runtime_flags
)) {
1593 spin_lock(&root
->fs_info
->delalloc_lock
);
1594 if (!list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1595 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1596 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1597 &BTRFS_I(inode
)->runtime_flags
);
1599 spin_unlock(&root
->fs_info
->delalloc_lock
);
1601 spin_unlock(&BTRFS_I(inode
)->lock
);
1606 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1607 * we don't create bios that span stripes or chunks
1609 int btrfs_merge_bio_hook(int rw
, struct page
*page
, unsigned long offset
,
1610 size_t size
, struct bio
*bio
,
1611 unsigned long bio_flags
)
1613 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1614 u64 logical
= (u64
)bio
->bi_sector
<< 9;
1619 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1622 length
= bio
->bi_size
;
1623 map_length
= length
;
1624 ret
= btrfs_map_block(root
->fs_info
, rw
, logical
,
1625 &map_length
, NULL
, 0);
1626 /* Will always return 0 with map_multi == NULL */
1628 if (map_length
< length
+ size
)
1634 * in order to insert checksums into the metadata in large chunks,
1635 * we wait until bio submission time. All the pages in the bio are
1636 * checksummed and sums are attached onto the ordered extent record.
1638 * At IO completion time the cums attached on the ordered extent record
1639 * are inserted into the btree
1641 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1642 struct bio
*bio
, int mirror_num
,
1643 unsigned long bio_flags
,
1646 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1649 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1650 BUG_ON(ret
); /* -ENOMEM */
1655 * in order to insert checksums into the metadata in large chunks,
1656 * we wait until bio submission time. All the pages in the bio are
1657 * checksummed and sums are attached onto the ordered extent record.
1659 * At IO completion time the cums attached on the ordered extent record
1660 * are inserted into the btree
1662 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1663 int mirror_num
, unsigned long bio_flags
,
1666 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1669 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1671 bio_endio(bio
, ret
);
1676 * extent_io.c submission hook. This does the right thing for csum calculation
1677 * on write, or reading the csums from the tree before a read
1679 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1680 int mirror_num
, unsigned long bio_flags
,
1683 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1687 int async
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
1689 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1691 if (btrfs_is_free_space_inode(inode
))
1694 if (!(rw
& REQ_WRITE
)) {
1695 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, metadata
);
1699 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1700 ret
= btrfs_submit_compressed_read(inode
, bio
,
1704 } else if (!skip_sum
) {
1705 ret
= btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1710 } else if (async
&& !skip_sum
) {
1711 /* csum items have already been cloned */
1712 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1714 /* we're doing a write, do the async checksumming */
1715 ret
= btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1716 inode
, rw
, bio
, mirror_num
,
1717 bio_flags
, bio_offset
,
1718 __btrfs_submit_bio_start
,
1719 __btrfs_submit_bio_done
);
1721 } else if (!skip_sum
) {
1722 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1728 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1732 bio_endio(bio
, ret
);
1737 * given a list of ordered sums record them in the inode. This happens
1738 * at IO completion time based on sums calculated at bio submission time.
1740 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1741 struct inode
*inode
, u64 file_offset
,
1742 struct list_head
*list
)
1744 struct btrfs_ordered_sum
*sum
;
1746 list_for_each_entry(sum
, list
, list
) {
1747 btrfs_csum_file_blocks(trans
,
1748 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1753 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1754 struct extent_state
**cached_state
)
1756 WARN_ON((end
& (PAGE_CACHE_SIZE
- 1)) == 0);
1757 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1758 cached_state
, GFP_NOFS
);
1761 /* see btrfs_writepage_start_hook for details on why this is required */
1762 struct btrfs_writepage_fixup
{
1764 struct btrfs_work work
;
1767 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1769 struct btrfs_writepage_fixup
*fixup
;
1770 struct btrfs_ordered_extent
*ordered
;
1771 struct extent_state
*cached_state
= NULL
;
1773 struct inode
*inode
;
1778 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1782 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1783 ClearPageChecked(page
);
1787 inode
= page
->mapping
->host
;
1788 page_start
= page_offset(page
);
1789 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1791 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1794 /* already ordered? We're done */
1795 if (PagePrivate2(page
))
1798 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1800 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1801 page_end
, &cached_state
, GFP_NOFS
);
1803 btrfs_start_ordered_extent(inode
, ordered
, 1);
1804 btrfs_put_ordered_extent(ordered
);
1808 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
1810 mapping_set_error(page
->mapping
, ret
);
1811 end_extent_writepage(page
, ret
, page_start
, page_end
);
1812 ClearPageChecked(page
);
1816 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1817 ClearPageChecked(page
);
1818 set_page_dirty(page
);
1820 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1821 &cached_state
, GFP_NOFS
);
1824 page_cache_release(page
);
1829 * There are a few paths in the higher layers of the kernel that directly
1830 * set the page dirty bit without asking the filesystem if it is a
1831 * good idea. This causes problems because we want to make sure COW
1832 * properly happens and the data=ordered rules are followed.
1834 * In our case any range that doesn't have the ORDERED bit set
1835 * hasn't been properly setup for IO. We kick off an async process
1836 * to fix it up. The async helper will wait for ordered extents, set
1837 * the delalloc bit and make it safe to write the page.
1839 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1841 struct inode
*inode
= page
->mapping
->host
;
1842 struct btrfs_writepage_fixup
*fixup
;
1843 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1845 /* this page is properly in the ordered list */
1846 if (TestClearPagePrivate2(page
))
1849 if (PageChecked(page
))
1852 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1856 SetPageChecked(page
);
1857 page_cache_get(page
);
1858 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
1860 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
1864 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1865 struct inode
*inode
, u64 file_pos
,
1866 u64 disk_bytenr
, u64 disk_num_bytes
,
1867 u64 num_bytes
, u64 ram_bytes
,
1868 u8 compression
, u8 encryption
,
1869 u16 other_encoding
, int extent_type
)
1871 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1872 struct btrfs_file_extent_item
*fi
;
1873 struct btrfs_path
*path
;
1874 struct extent_buffer
*leaf
;
1875 struct btrfs_key ins
;
1878 path
= btrfs_alloc_path();
1882 path
->leave_spinning
= 1;
1885 * we may be replacing one extent in the tree with another.
1886 * The new extent is pinned in the extent map, and we don't want
1887 * to drop it from the cache until it is completely in the btree.
1889 * So, tell btrfs_drop_extents to leave this extent in the cache.
1890 * the caller is expected to unpin it and allow it to be merged
1893 ret
= btrfs_drop_extents(trans
, root
, inode
, file_pos
,
1894 file_pos
+ num_bytes
, 0);
1898 ins
.objectid
= btrfs_ino(inode
);
1899 ins
.offset
= file_pos
;
1900 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1901 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
, sizeof(*fi
));
1904 leaf
= path
->nodes
[0];
1905 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1906 struct btrfs_file_extent_item
);
1907 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1908 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1909 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1910 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1911 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1912 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1913 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1914 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1915 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1916 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1918 btrfs_mark_buffer_dirty(leaf
);
1919 btrfs_release_path(path
);
1921 inode_add_bytes(inode
, num_bytes
);
1923 ins
.objectid
= disk_bytenr
;
1924 ins
.offset
= disk_num_bytes
;
1925 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1926 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
1927 root
->root_key
.objectid
,
1928 btrfs_ino(inode
), file_pos
, &ins
);
1930 btrfs_free_path(path
);
1936 * helper function for btrfs_finish_ordered_io, this
1937 * just reads in some of the csum leaves to prime them into ram
1938 * before we start the transaction. It limits the amount of btree
1939 * reads required while inside the transaction.
1941 /* as ordered data IO finishes, this gets called so we can finish
1942 * an ordered extent if the range of bytes in the file it covers are
1945 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
)
1947 struct inode
*inode
= ordered_extent
->inode
;
1948 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1949 struct btrfs_trans_handle
*trans
= NULL
;
1950 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1951 struct extent_state
*cached_state
= NULL
;
1952 int compress_type
= 0;
1956 nolock
= btrfs_is_free_space_inode(inode
);
1958 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered_extent
->flags
)) {
1963 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
1964 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
1965 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
1967 trans
= btrfs_join_transaction_nolock(root
);
1969 trans
= btrfs_join_transaction(root
);
1970 if (IS_ERR(trans
)) {
1971 ret
= PTR_ERR(trans
);
1975 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1976 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
1977 if (ret
) /* -ENOMEM or corruption */
1978 btrfs_abort_transaction(trans
, root
, ret
);
1982 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
1983 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
1987 trans
= btrfs_join_transaction_nolock(root
);
1989 trans
= btrfs_join_transaction(root
);
1990 if (IS_ERR(trans
)) {
1991 ret
= PTR_ERR(trans
);
1995 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1997 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
1998 compress_type
= ordered_extent
->compress_type
;
1999 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
2000 BUG_ON(compress_type
);
2001 ret
= btrfs_mark_extent_written(trans
, inode
,
2002 ordered_extent
->file_offset
,
2003 ordered_extent
->file_offset
+
2004 ordered_extent
->len
);
2006 BUG_ON(root
== root
->fs_info
->tree_root
);
2007 ret
= insert_reserved_file_extent(trans
, inode
,
2008 ordered_extent
->file_offset
,
2009 ordered_extent
->start
,
2010 ordered_extent
->disk_len
,
2011 ordered_extent
->len
,
2012 ordered_extent
->len
,
2013 compress_type
, 0, 0,
2014 BTRFS_FILE_EXTENT_REG
);
2016 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
2017 ordered_extent
->file_offset
, ordered_extent
->len
,
2020 btrfs_abort_transaction(trans
, root
, ret
);
2024 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
2025 &ordered_extent
->list
);
2027 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2028 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2029 if (ret
) { /* -ENOMEM or corruption */
2030 btrfs_abort_transaction(trans
, root
, ret
);
2035 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
2036 ordered_extent
->file_offset
+
2037 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
2039 if (root
!= root
->fs_info
->tree_root
)
2040 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
2042 btrfs_end_transaction(trans
, root
);
2045 clear_extent_uptodate(io_tree
, ordered_extent
->file_offset
,
2046 ordered_extent
->file_offset
+
2047 ordered_extent
->len
- 1, NULL
, GFP_NOFS
);
2050 * If the ordered extent had an IOERR or something else went
2051 * wrong we need to return the space for this ordered extent
2052 * back to the allocator.
2054 if (!test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
2055 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
))
2056 btrfs_free_reserved_extent(root
, ordered_extent
->start
,
2057 ordered_extent
->disk_len
);
2062 * This needs to be done to make sure anybody waiting knows we are done
2063 * updating everything for this ordered extent.
2065 btrfs_remove_ordered_extent(inode
, ordered_extent
);
2068 btrfs_put_ordered_extent(ordered_extent
);
2069 /* once for the tree */
2070 btrfs_put_ordered_extent(ordered_extent
);
2075 static void finish_ordered_fn(struct btrfs_work
*work
)
2077 struct btrfs_ordered_extent
*ordered_extent
;
2078 ordered_extent
= container_of(work
, struct btrfs_ordered_extent
, work
);
2079 btrfs_finish_ordered_io(ordered_extent
);
2082 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2083 struct extent_state
*state
, int uptodate
)
2085 struct inode
*inode
= page
->mapping
->host
;
2086 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2087 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
2088 struct btrfs_workers
*workers
;
2090 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
2092 ClearPagePrivate2(page
);
2093 if (!btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
2094 end
- start
+ 1, uptodate
))
2097 ordered_extent
->work
.func
= finish_ordered_fn
;
2098 ordered_extent
->work
.flags
= 0;
2100 if (btrfs_is_free_space_inode(inode
))
2101 workers
= &root
->fs_info
->endio_freespace_worker
;
2103 workers
= &root
->fs_info
->endio_write_workers
;
2104 btrfs_queue_worker(workers
, &ordered_extent
->work
);
2110 * when reads are done, we need to check csums to verify the data is correct
2111 * if there's a match, we allow the bio to finish. If not, the code in
2112 * extent_io.c will try to find good copies for us.
2114 static int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2115 struct extent_state
*state
, int mirror
)
2117 size_t offset
= start
- page_offset(page
);
2118 struct inode
*inode
= page
->mapping
->host
;
2119 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2121 u64
private = ~(u32
)0;
2123 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2126 if (PageChecked(page
)) {
2127 ClearPageChecked(page
);
2131 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
2134 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
2135 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
2136 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
2141 if (state
&& state
->start
== start
) {
2142 private = state
->private;
2145 ret
= get_state_private(io_tree
, start
, &private);
2147 kaddr
= kmap_atomic(page
);
2151 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
2152 btrfs_csum_final(csum
, (char *)&csum
);
2153 if (csum
!= private)
2156 kunmap_atomic(kaddr
);
2161 printk_ratelimited(KERN_INFO
"btrfs csum failed ino %llu off %llu csum %u "
2163 (unsigned long long)btrfs_ino(page
->mapping
->host
),
2164 (unsigned long long)start
, csum
,
2165 (unsigned long long)private);
2166 memset(kaddr
+ offset
, 1, end
- start
+ 1);
2167 flush_dcache_page(page
);
2168 kunmap_atomic(kaddr
);
2174 struct delayed_iput
{
2175 struct list_head list
;
2176 struct inode
*inode
;
2179 /* JDM: If this is fs-wide, why can't we add a pointer to
2180 * btrfs_inode instead and avoid the allocation? */
2181 void btrfs_add_delayed_iput(struct inode
*inode
)
2183 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2184 struct delayed_iput
*delayed
;
2186 if (atomic_add_unless(&inode
->i_count
, -1, 1))
2189 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
2190 delayed
->inode
= inode
;
2192 spin_lock(&fs_info
->delayed_iput_lock
);
2193 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
2194 spin_unlock(&fs_info
->delayed_iput_lock
);
2197 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
2200 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2201 struct delayed_iput
*delayed
;
2204 spin_lock(&fs_info
->delayed_iput_lock
);
2205 empty
= list_empty(&fs_info
->delayed_iputs
);
2206 spin_unlock(&fs_info
->delayed_iput_lock
);
2210 spin_lock(&fs_info
->delayed_iput_lock
);
2211 list_splice_init(&fs_info
->delayed_iputs
, &list
);
2212 spin_unlock(&fs_info
->delayed_iput_lock
);
2214 while (!list_empty(&list
)) {
2215 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
2216 list_del(&delayed
->list
);
2217 iput(delayed
->inode
);
2223 * This is called in transaction commit time. If there are no orphan
2224 * files in the subvolume, it removes orphan item and frees block_rsv
2227 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
2228 struct btrfs_root
*root
)
2230 struct btrfs_block_rsv
*block_rsv
;
2233 if (atomic_read(&root
->orphan_inodes
) ||
2234 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
2237 spin_lock(&root
->orphan_lock
);
2238 if (atomic_read(&root
->orphan_inodes
)) {
2239 spin_unlock(&root
->orphan_lock
);
2243 if (root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
) {
2244 spin_unlock(&root
->orphan_lock
);
2248 block_rsv
= root
->orphan_block_rsv
;
2249 root
->orphan_block_rsv
= NULL
;
2250 spin_unlock(&root
->orphan_lock
);
2252 if (root
->orphan_item_inserted
&&
2253 btrfs_root_refs(&root
->root_item
) > 0) {
2254 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
2255 root
->root_key
.objectid
);
2257 root
->orphan_item_inserted
= 0;
2261 WARN_ON(block_rsv
->size
> 0);
2262 btrfs_free_block_rsv(root
, block_rsv
);
2267 * This creates an orphan entry for the given inode in case something goes
2268 * wrong in the middle of an unlink/truncate.
2270 * NOTE: caller of this function should reserve 5 units of metadata for
2273 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2275 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2276 struct btrfs_block_rsv
*block_rsv
= NULL
;
2281 if (!root
->orphan_block_rsv
) {
2282 block_rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
2287 spin_lock(&root
->orphan_lock
);
2288 if (!root
->orphan_block_rsv
) {
2289 root
->orphan_block_rsv
= block_rsv
;
2290 } else if (block_rsv
) {
2291 btrfs_free_block_rsv(root
, block_rsv
);
2295 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2296 &BTRFS_I(inode
)->runtime_flags
)) {
2299 * For proper ENOSPC handling, we should do orphan
2300 * cleanup when mounting. But this introduces backward
2301 * compatibility issue.
2303 if (!xchg(&root
->orphan_item_inserted
, 1))
2309 atomic_inc(&root
->orphan_inodes
);
2312 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
2313 &BTRFS_I(inode
)->runtime_flags
))
2315 spin_unlock(&root
->orphan_lock
);
2317 /* grab metadata reservation from transaction handle */
2319 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
2320 BUG_ON(ret
); /* -ENOSPC in reservation; Logic error? JDM */
2323 /* insert an orphan item to track this unlinked/truncated file */
2325 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
2326 if (ret
&& ret
!= -EEXIST
) {
2327 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2328 &BTRFS_I(inode
)->runtime_flags
);
2329 btrfs_abort_transaction(trans
, root
, ret
);
2335 /* insert an orphan item to track subvolume contains orphan files */
2337 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
2338 root
->root_key
.objectid
);
2339 if (ret
&& ret
!= -EEXIST
) {
2340 btrfs_abort_transaction(trans
, root
, ret
);
2348 * We have done the truncate/delete so we can go ahead and remove the orphan
2349 * item for this particular inode.
2351 int btrfs_orphan_del(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2353 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2354 int delete_item
= 0;
2355 int release_rsv
= 0;
2358 spin_lock(&root
->orphan_lock
);
2359 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2360 &BTRFS_I(inode
)->runtime_flags
))
2363 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
2364 &BTRFS_I(inode
)->runtime_flags
))
2366 spin_unlock(&root
->orphan_lock
);
2368 if (trans
&& delete_item
) {
2369 ret
= btrfs_del_orphan_item(trans
, root
, btrfs_ino(inode
));
2370 BUG_ON(ret
); /* -ENOMEM or corruption (JDM: Recheck) */
2374 btrfs_orphan_release_metadata(inode
);
2375 atomic_dec(&root
->orphan_inodes
);
2382 * this cleans up any orphans that may be left on the list from the last use
2385 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
2387 struct btrfs_path
*path
;
2388 struct extent_buffer
*leaf
;
2389 struct btrfs_key key
, found_key
;
2390 struct btrfs_trans_handle
*trans
;
2391 struct inode
*inode
;
2392 u64 last_objectid
= 0;
2393 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
2395 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
2398 path
= btrfs_alloc_path();
2405 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
2406 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
2407 key
.offset
= (u64
)-1;
2410 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2415 * if ret == 0 means we found what we were searching for, which
2416 * is weird, but possible, so only screw with path if we didn't
2417 * find the key and see if we have stuff that matches
2421 if (path
->slots
[0] == 0)
2426 /* pull out the item */
2427 leaf
= path
->nodes
[0];
2428 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2430 /* make sure the item matches what we want */
2431 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
2433 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
2436 /* release the path since we're done with it */
2437 btrfs_release_path(path
);
2440 * this is where we are basically btrfs_lookup, without the
2441 * crossing root thing. we store the inode number in the
2442 * offset of the orphan item.
2445 if (found_key
.offset
== last_objectid
) {
2446 printk(KERN_ERR
"btrfs: Error removing orphan entry, "
2447 "stopping orphan cleanup\n");
2452 last_objectid
= found_key
.offset
;
2454 found_key
.objectid
= found_key
.offset
;
2455 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
2456 found_key
.offset
= 0;
2457 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
2458 ret
= PTR_RET(inode
);
2459 if (ret
&& ret
!= -ESTALE
)
2462 if (ret
== -ESTALE
&& root
== root
->fs_info
->tree_root
) {
2463 struct btrfs_root
*dead_root
;
2464 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2465 int is_dead_root
= 0;
2468 * this is an orphan in the tree root. Currently these
2469 * could come from 2 sources:
2470 * a) a snapshot deletion in progress
2471 * b) a free space cache inode
2472 * We need to distinguish those two, as the snapshot
2473 * orphan must not get deleted.
2474 * find_dead_roots already ran before us, so if this
2475 * is a snapshot deletion, we should find the root
2476 * in the dead_roots list
2478 spin_lock(&fs_info
->trans_lock
);
2479 list_for_each_entry(dead_root
, &fs_info
->dead_roots
,
2481 if (dead_root
->root_key
.objectid
==
2482 found_key
.objectid
) {
2487 spin_unlock(&fs_info
->trans_lock
);
2489 /* prevent this orphan from being found again */
2490 key
.offset
= found_key
.objectid
- 1;
2495 * Inode is already gone but the orphan item is still there,
2496 * kill the orphan item.
2498 if (ret
== -ESTALE
) {
2499 trans
= btrfs_start_transaction(root
, 1);
2500 if (IS_ERR(trans
)) {
2501 ret
= PTR_ERR(trans
);
2504 printk(KERN_ERR
"auto deleting %Lu\n",
2505 found_key
.objectid
);
2506 ret
= btrfs_del_orphan_item(trans
, root
,
2507 found_key
.objectid
);
2508 BUG_ON(ret
); /* -ENOMEM or corruption (JDM: Recheck) */
2509 btrfs_end_transaction(trans
, root
);
2514 * add this inode to the orphan list so btrfs_orphan_del does
2515 * the proper thing when we hit it
2517 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2518 &BTRFS_I(inode
)->runtime_flags
);
2519 atomic_inc(&root
->orphan_inodes
);
2521 /* if we have links, this was a truncate, lets do that */
2522 if (inode
->i_nlink
) {
2523 if (!S_ISREG(inode
->i_mode
)) {
2530 /* 1 for the orphan item deletion. */
2531 trans
= btrfs_start_transaction(root
, 1);
2532 if (IS_ERR(trans
)) {
2533 ret
= PTR_ERR(trans
);
2536 ret
= btrfs_orphan_add(trans
, inode
);
2537 btrfs_end_transaction(trans
, root
);
2541 ret
= btrfs_truncate(inode
);
2543 btrfs_orphan_del(NULL
, inode
);
2548 /* this will do delete_inode and everything for us */
2553 /* release the path since we're done with it */
2554 btrfs_release_path(path
);
2556 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
2558 if (root
->orphan_block_rsv
)
2559 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
2562 if (root
->orphan_block_rsv
|| root
->orphan_item_inserted
) {
2563 trans
= btrfs_join_transaction(root
);
2565 btrfs_end_transaction(trans
, root
);
2569 printk(KERN_INFO
"btrfs: unlinked %d orphans\n", nr_unlink
);
2571 printk(KERN_INFO
"btrfs: truncated %d orphans\n", nr_truncate
);
2575 printk(KERN_CRIT
"btrfs: could not do orphan cleanup %d\n", ret
);
2576 btrfs_free_path(path
);
2581 * very simple check to peek ahead in the leaf looking for xattrs. If we
2582 * don't find any xattrs, we know there can't be any acls.
2584 * slot is the slot the inode is in, objectid is the objectid of the inode
2586 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
2587 int slot
, u64 objectid
)
2589 u32 nritems
= btrfs_header_nritems(leaf
);
2590 struct btrfs_key found_key
;
2594 while (slot
< nritems
) {
2595 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2597 /* we found a different objectid, there must not be acls */
2598 if (found_key
.objectid
!= objectid
)
2601 /* we found an xattr, assume we've got an acl */
2602 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
)
2606 * we found a key greater than an xattr key, there can't
2607 * be any acls later on
2609 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
2616 * it goes inode, inode backrefs, xattrs, extents,
2617 * so if there are a ton of hard links to an inode there can
2618 * be a lot of backrefs. Don't waste time searching too hard,
2619 * this is just an optimization
2624 /* we hit the end of the leaf before we found an xattr or
2625 * something larger than an xattr. We have to assume the inode
2632 * read an inode from the btree into the in-memory inode
2634 static void btrfs_read_locked_inode(struct inode
*inode
)
2636 struct btrfs_path
*path
;
2637 struct extent_buffer
*leaf
;
2638 struct btrfs_inode_item
*inode_item
;
2639 struct btrfs_timespec
*tspec
;
2640 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2641 struct btrfs_key location
;
2645 bool filled
= false;
2647 ret
= btrfs_fill_inode(inode
, &rdev
);
2651 path
= btrfs_alloc_path();
2655 path
->leave_spinning
= 1;
2656 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
2658 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
2662 leaf
= path
->nodes
[0];
2667 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2668 struct btrfs_inode_item
);
2669 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
2670 set_nlink(inode
, btrfs_inode_nlink(leaf
, inode_item
));
2671 i_uid_write(inode
, btrfs_inode_uid(leaf
, inode_item
));
2672 i_gid_write(inode
, btrfs_inode_gid(leaf
, inode_item
));
2673 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
2675 tspec
= btrfs_inode_atime(inode_item
);
2676 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2677 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2679 tspec
= btrfs_inode_mtime(inode_item
);
2680 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2681 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2683 tspec
= btrfs_inode_ctime(inode_item
);
2684 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2685 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2687 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
2688 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
2689 BTRFS_I(inode
)->last_trans
= btrfs_inode_transid(leaf
, inode_item
);
2692 * If we were modified in the current generation and evicted from memory
2693 * and then re-read we need to do a full sync since we don't have any
2694 * idea about which extents were modified before we were evicted from
2697 if (BTRFS_I(inode
)->last_trans
== root
->fs_info
->generation
)
2698 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
2699 &BTRFS_I(inode
)->runtime_flags
);
2701 inode
->i_version
= btrfs_inode_sequence(leaf
, inode_item
);
2702 inode
->i_generation
= BTRFS_I(inode
)->generation
;
2704 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
2706 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
2707 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
2710 * try to precache a NULL acl entry for files that don't have
2711 * any xattrs or acls
2713 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
2716 cache_no_acl(inode
);
2718 btrfs_free_path(path
);
2720 switch (inode
->i_mode
& S_IFMT
) {
2722 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2723 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2724 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2725 inode
->i_fop
= &btrfs_file_operations
;
2726 inode
->i_op
= &btrfs_file_inode_operations
;
2729 inode
->i_fop
= &btrfs_dir_file_operations
;
2730 if (root
== root
->fs_info
->tree_root
)
2731 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
2733 inode
->i_op
= &btrfs_dir_inode_operations
;
2736 inode
->i_op
= &btrfs_symlink_inode_operations
;
2737 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2738 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2741 inode
->i_op
= &btrfs_special_inode_operations
;
2742 init_special_inode(inode
, inode
->i_mode
, rdev
);
2746 btrfs_update_iflags(inode
);
2750 btrfs_free_path(path
);
2751 make_bad_inode(inode
);
2755 * given a leaf and an inode, copy the inode fields into the leaf
2757 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
2758 struct extent_buffer
*leaf
,
2759 struct btrfs_inode_item
*item
,
2760 struct inode
*inode
)
2762 struct btrfs_map_token token
;
2764 btrfs_init_map_token(&token
);
2766 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
2767 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
2768 btrfs_set_token_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
,
2770 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
2771 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
2773 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_atime(item
),
2774 inode
->i_atime
.tv_sec
, &token
);
2775 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_atime(item
),
2776 inode
->i_atime
.tv_nsec
, &token
);
2778 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_mtime(item
),
2779 inode
->i_mtime
.tv_sec
, &token
);
2780 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
2781 inode
->i_mtime
.tv_nsec
, &token
);
2783 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_ctime(item
),
2784 inode
->i_ctime
.tv_sec
, &token
);
2785 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
2786 inode
->i_ctime
.tv_nsec
, &token
);
2788 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
2790 btrfs_set_token_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
,
2792 btrfs_set_token_inode_sequence(leaf
, item
, inode
->i_version
, &token
);
2793 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
2794 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
2795 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
2796 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
2800 * copy everything in the in-memory inode into the btree.
2802 static noinline
int btrfs_update_inode_item(struct btrfs_trans_handle
*trans
,
2803 struct btrfs_root
*root
, struct inode
*inode
)
2805 struct btrfs_inode_item
*inode_item
;
2806 struct btrfs_path
*path
;
2807 struct extent_buffer
*leaf
;
2810 path
= btrfs_alloc_path();
2814 path
->leave_spinning
= 1;
2815 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
2823 btrfs_unlock_up_safe(path
, 1);
2824 leaf
= path
->nodes
[0];
2825 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2826 struct btrfs_inode_item
);
2828 fill_inode_item(trans
, leaf
, inode_item
, inode
);
2829 btrfs_mark_buffer_dirty(leaf
);
2830 btrfs_set_inode_last_trans(trans
, inode
);
2833 btrfs_free_path(path
);
2838 * copy everything in the in-memory inode into the btree.
2840 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
2841 struct btrfs_root
*root
, struct inode
*inode
)
2846 * If the inode is a free space inode, we can deadlock during commit
2847 * if we put it into the delayed code.
2849 * The data relocation inode should also be directly updated
2852 if (!btrfs_is_free_space_inode(inode
)
2853 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
) {
2854 btrfs_update_root_times(trans
, root
);
2856 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
2858 btrfs_set_inode_last_trans(trans
, inode
);
2862 return btrfs_update_inode_item(trans
, root
, inode
);
2865 noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
2866 struct btrfs_root
*root
,
2867 struct inode
*inode
)
2871 ret
= btrfs_update_inode(trans
, root
, inode
);
2873 return btrfs_update_inode_item(trans
, root
, inode
);
2878 * unlink helper that gets used here in inode.c and in the tree logging
2879 * recovery code. It remove a link in a directory with a given name, and
2880 * also drops the back refs in the inode to the directory
2882 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
2883 struct btrfs_root
*root
,
2884 struct inode
*dir
, struct inode
*inode
,
2885 const char *name
, int name_len
)
2887 struct btrfs_path
*path
;
2889 struct extent_buffer
*leaf
;
2890 struct btrfs_dir_item
*di
;
2891 struct btrfs_key key
;
2893 u64 ino
= btrfs_ino(inode
);
2894 u64 dir_ino
= btrfs_ino(dir
);
2896 path
= btrfs_alloc_path();
2902 path
->leave_spinning
= 1;
2903 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
2904 name
, name_len
, -1);
2913 leaf
= path
->nodes
[0];
2914 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2915 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2918 btrfs_release_path(path
);
2920 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
2923 printk(KERN_INFO
"btrfs failed to delete reference to %.*s, "
2924 "inode %llu parent %llu\n", name_len
, name
,
2925 (unsigned long long)ino
, (unsigned long long)dir_ino
);
2926 btrfs_abort_transaction(trans
, root
, ret
);
2930 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
2932 btrfs_abort_transaction(trans
, root
, ret
);
2936 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
2938 if (ret
!= 0 && ret
!= -ENOENT
) {
2939 btrfs_abort_transaction(trans
, root
, ret
);
2943 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
2948 btrfs_free_path(path
);
2952 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
2953 inode_inc_iversion(inode
);
2954 inode_inc_iversion(dir
);
2955 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
2956 ret
= btrfs_update_inode(trans
, root
, dir
);
2961 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
2962 struct btrfs_root
*root
,
2963 struct inode
*dir
, struct inode
*inode
,
2964 const char *name
, int name_len
)
2967 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
2969 btrfs_drop_nlink(inode
);
2970 ret
= btrfs_update_inode(trans
, root
, inode
);
2976 /* helper to check if there is any shared block in the path */
2977 static int check_path_shared(struct btrfs_root
*root
,
2978 struct btrfs_path
*path
)
2980 struct extent_buffer
*eb
;
2984 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
2987 if (!path
->nodes
[level
])
2989 eb
= path
->nodes
[level
];
2990 if (!btrfs_block_can_be_shared(root
, eb
))
2992 ret
= btrfs_lookup_extent_info(NULL
, root
, eb
->start
, eb
->len
,
3001 * helper to start transaction for unlink and rmdir.
3003 * unlink and rmdir are special in btrfs, they do not always free space.
3004 * so in enospc case, we should make sure they will free space before
3005 * allowing them to use the global metadata reservation.
3007 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
,
3008 struct dentry
*dentry
)
3010 struct btrfs_trans_handle
*trans
;
3011 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3012 struct btrfs_path
*path
;
3013 struct btrfs_dir_item
*di
;
3014 struct inode
*inode
= dentry
->d_inode
;
3019 u64 ino
= btrfs_ino(inode
);
3020 u64 dir_ino
= btrfs_ino(dir
);
3023 * 1 for the possible orphan item
3024 * 1 for the dir item
3025 * 1 for the dir index
3026 * 1 for the inode ref
3027 * 1 for the inode ref in the tree log
3028 * 2 for the dir entries in the log
3031 trans
= btrfs_start_transaction(root
, 8);
3032 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
3035 if (ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
3036 return ERR_PTR(-ENOSPC
);
3038 /* check if there is someone else holds reference */
3039 if (S_ISDIR(inode
->i_mode
) && atomic_read(&inode
->i_count
) > 1)
3040 return ERR_PTR(-ENOSPC
);
3042 if (atomic_read(&inode
->i_count
) > 2)
3043 return ERR_PTR(-ENOSPC
);
3045 if (xchg(&root
->fs_info
->enospc_unlink
, 1))
3046 return ERR_PTR(-ENOSPC
);
3048 path
= btrfs_alloc_path();
3050 root
->fs_info
->enospc_unlink
= 0;
3051 return ERR_PTR(-ENOMEM
);
3054 /* 1 for the orphan item */
3055 trans
= btrfs_start_transaction(root
, 1);
3056 if (IS_ERR(trans
)) {
3057 btrfs_free_path(path
);
3058 root
->fs_info
->enospc_unlink
= 0;
3062 path
->skip_locking
= 1;
3063 path
->search_commit_root
= 1;
3065 ret
= btrfs_lookup_inode(trans
, root
, path
,
3066 &BTRFS_I(dir
)->location
, 0);
3072 if (check_path_shared(root
, path
))
3077 btrfs_release_path(path
);
3079 ret
= btrfs_lookup_inode(trans
, root
, path
,
3080 &BTRFS_I(inode
)->location
, 0);
3086 if (check_path_shared(root
, path
))
3091 btrfs_release_path(path
);
3093 if (ret
== 0 && S_ISREG(inode
->i_mode
)) {
3094 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
3100 BUG_ON(ret
== 0); /* Corruption */
3101 if (check_path_shared(root
, path
))
3103 btrfs_release_path(path
);
3111 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3112 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
3118 if (check_path_shared(root
, path
))
3124 btrfs_release_path(path
);
3126 ret
= btrfs_get_inode_ref_index(trans
, root
, path
, dentry
->d_name
.name
,
3127 dentry
->d_name
.len
, ino
, dir_ino
, 0,
3134 if (check_path_shared(root
, path
))
3137 btrfs_release_path(path
);
3140 * This is a commit root search, if we can lookup inode item and other
3141 * relative items in the commit root, it means the transaction of
3142 * dir/file creation has been committed, and the dir index item that we
3143 * delay to insert has also been inserted into the commit root. So
3144 * we needn't worry about the delayed insertion of the dir index item
3147 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir_ino
, index
,
3148 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
3153 BUG_ON(ret
== -ENOENT
);
3154 if (check_path_shared(root
, path
))
3159 btrfs_free_path(path
);
3160 /* Migrate the orphan reservation over */
3162 err
= btrfs_block_rsv_migrate(trans
->block_rsv
,
3163 &root
->fs_info
->global_block_rsv
,
3164 trans
->bytes_reserved
);
3167 btrfs_end_transaction(trans
, root
);
3168 root
->fs_info
->enospc_unlink
= 0;
3169 return ERR_PTR(err
);
3172 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
3176 static void __unlink_end_trans(struct btrfs_trans_handle
*trans
,
3177 struct btrfs_root
*root
)
3179 if (trans
->block_rsv
->type
== BTRFS_BLOCK_RSV_GLOBAL
) {
3180 btrfs_block_rsv_release(root
, trans
->block_rsv
,
3181 trans
->bytes_reserved
);
3182 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
3183 BUG_ON(!root
->fs_info
->enospc_unlink
);
3184 root
->fs_info
->enospc_unlink
= 0;
3186 btrfs_end_transaction(trans
, root
);
3189 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
3191 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3192 struct btrfs_trans_handle
*trans
;
3193 struct inode
*inode
= dentry
->d_inode
;
3196 trans
= __unlink_start_trans(dir
, dentry
);
3198 return PTR_ERR(trans
);
3200 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
3202 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3203 dentry
->d_name
.name
, dentry
->d_name
.len
);
3207 if (inode
->i_nlink
== 0) {
3208 ret
= btrfs_orphan_add(trans
, inode
);
3214 __unlink_end_trans(trans
, root
);
3215 btrfs_btree_balance_dirty(root
);
3219 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
3220 struct btrfs_root
*root
,
3221 struct inode
*dir
, u64 objectid
,
3222 const char *name
, int name_len
)
3224 struct btrfs_path
*path
;
3225 struct extent_buffer
*leaf
;
3226 struct btrfs_dir_item
*di
;
3227 struct btrfs_key key
;
3230 u64 dir_ino
= btrfs_ino(dir
);
3232 path
= btrfs_alloc_path();
3236 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3237 name
, name_len
, -1);
3238 if (IS_ERR_OR_NULL(di
)) {
3246 leaf
= path
->nodes
[0];
3247 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3248 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
3249 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3251 btrfs_abort_transaction(trans
, root
, ret
);
3254 btrfs_release_path(path
);
3256 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
3257 objectid
, root
->root_key
.objectid
,
3258 dir_ino
, &index
, name
, name_len
);
3260 if (ret
!= -ENOENT
) {
3261 btrfs_abort_transaction(trans
, root
, ret
);
3264 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
3266 if (IS_ERR_OR_NULL(di
)) {
3271 btrfs_abort_transaction(trans
, root
, ret
);
3275 leaf
= path
->nodes
[0];
3276 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3277 btrfs_release_path(path
);
3280 btrfs_release_path(path
);
3282 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3284 btrfs_abort_transaction(trans
, root
, ret
);
3288 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3289 inode_inc_iversion(dir
);
3290 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3291 ret
= btrfs_update_inode_fallback(trans
, root
, dir
);
3293 btrfs_abort_transaction(trans
, root
, ret
);
3295 btrfs_free_path(path
);
3299 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3301 struct inode
*inode
= dentry
->d_inode
;
3303 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3304 struct btrfs_trans_handle
*trans
;
3306 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
3308 if (btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
)
3311 trans
= __unlink_start_trans(dir
, dentry
);
3313 return PTR_ERR(trans
);
3315 if (unlikely(btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
3316 err
= btrfs_unlink_subvol(trans
, root
, dir
,
3317 BTRFS_I(inode
)->location
.objectid
,
3318 dentry
->d_name
.name
,
3319 dentry
->d_name
.len
);
3323 err
= btrfs_orphan_add(trans
, inode
);
3327 /* now the directory is empty */
3328 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3329 dentry
->d_name
.name
, dentry
->d_name
.len
);
3331 btrfs_i_size_write(inode
, 0);
3333 __unlink_end_trans(trans
, root
);
3334 btrfs_btree_balance_dirty(root
);
3340 * this can truncate away extent items, csum items and directory items.
3341 * It starts at a high offset and removes keys until it can't find
3342 * any higher than new_size
3344 * csum items that cross the new i_size are truncated to the new size
3347 * min_type is the minimum key type to truncate down to. If set to 0, this
3348 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3350 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
3351 struct btrfs_root
*root
,
3352 struct inode
*inode
,
3353 u64 new_size
, u32 min_type
)
3355 struct btrfs_path
*path
;
3356 struct extent_buffer
*leaf
;
3357 struct btrfs_file_extent_item
*fi
;
3358 struct btrfs_key key
;
3359 struct btrfs_key found_key
;
3360 u64 extent_start
= 0;
3361 u64 extent_num_bytes
= 0;
3362 u64 extent_offset
= 0;
3364 u64 mask
= root
->sectorsize
- 1;
3365 u32 found_type
= (u8
)-1;
3368 int pending_del_nr
= 0;
3369 int pending_del_slot
= 0;
3370 int extent_type
= -1;
3373 u64 ino
= btrfs_ino(inode
);
3375 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
3377 path
= btrfs_alloc_path();
3383 * We want to drop from the next block forward in case this new size is
3384 * not block aligned since we will be keeping the last block of the
3385 * extent just the way it is.
3387 if (root
->ref_cows
|| root
== root
->fs_info
->tree_root
)
3388 btrfs_drop_extent_cache(inode
, (new_size
+ mask
) & (~mask
), (u64
)-1, 0);
3391 * This function is also used to drop the items in the log tree before
3392 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3393 * it is used to drop the loged items. So we shouldn't kill the delayed
3396 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
3397 btrfs_kill_delayed_inode_items(inode
);
3400 key
.offset
= (u64
)-1;
3404 path
->leave_spinning
= 1;
3405 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3412 /* there are no items in the tree for us to truncate, we're
3415 if (path
->slots
[0] == 0)
3422 leaf
= path
->nodes
[0];
3423 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3424 found_type
= btrfs_key_type(&found_key
);
3426 if (found_key
.objectid
!= ino
)
3429 if (found_type
< min_type
)
3432 item_end
= found_key
.offset
;
3433 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
3434 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3435 struct btrfs_file_extent_item
);
3436 extent_type
= btrfs_file_extent_type(leaf
, fi
);
3437 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3439 btrfs_file_extent_num_bytes(leaf
, fi
);
3440 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3441 item_end
+= btrfs_file_extent_inline_len(leaf
,
3446 if (found_type
> min_type
) {
3449 if (item_end
< new_size
)
3451 if (found_key
.offset
>= new_size
)
3457 /* FIXME, shrink the extent if the ref count is only 1 */
3458 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
3461 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3463 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
3465 u64 orig_num_bytes
=
3466 btrfs_file_extent_num_bytes(leaf
, fi
);
3467 extent_num_bytes
= new_size
-
3468 found_key
.offset
+ root
->sectorsize
- 1;
3469 extent_num_bytes
= extent_num_bytes
&
3470 ~((u64
)root
->sectorsize
- 1);
3471 btrfs_set_file_extent_num_bytes(leaf
, fi
,
3473 num_dec
= (orig_num_bytes
-
3475 if (root
->ref_cows
&& extent_start
!= 0)
3476 inode_sub_bytes(inode
, num_dec
);
3477 btrfs_mark_buffer_dirty(leaf
);
3480 btrfs_file_extent_disk_num_bytes(leaf
,
3482 extent_offset
= found_key
.offset
-
3483 btrfs_file_extent_offset(leaf
, fi
);
3485 /* FIXME blocksize != 4096 */
3486 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
3487 if (extent_start
!= 0) {
3490 inode_sub_bytes(inode
, num_dec
);
3493 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3495 * we can't truncate inline items that have had
3499 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
3500 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
3501 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
3502 u32 size
= new_size
- found_key
.offset
;
3504 if (root
->ref_cows
) {
3505 inode_sub_bytes(inode
, item_end
+ 1 -
3509 btrfs_file_extent_calc_inline_size(size
);
3510 btrfs_truncate_item(trans
, root
, path
,
3512 } else if (root
->ref_cows
) {
3513 inode_sub_bytes(inode
, item_end
+ 1 -
3519 if (!pending_del_nr
) {
3520 /* no pending yet, add ourselves */
3521 pending_del_slot
= path
->slots
[0];
3523 } else if (pending_del_nr
&&
3524 path
->slots
[0] + 1 == pending_del_slot
) {
3525 /* hop on the pending chunk */
3527 pending_del_slot
= path
->slots
[0];
3534 if (found_extent
&& (root
->ref_cows
||
3535 root
== root
->fs_info
->tree_root
)) {
3536 btrfs_set_path_blocking(path
);
3537 ret
= btrfs_free_extent(trans
, root
, extent_start
,
3538 extent_num_bytes
, 0,
3539 btrfs_header_owner(leaf
),
3540 ino
, extent_offset
, 0);
3544 if (found_type
== BTRFS_INODE_ITEM_KEY
)
3547 if (path
->slots
[0] == 0 ||
3548 path
->slots
[0] != pending_del_slot
) {
3549 if (pending_del_nr
) {
3550 ret
= btrfs_del_items(trans
, root
, path
,
3554 btrfs_abort_transaction(trans
,
3560 btrfs_release_path(path
);
3567 if (pending_del_nr
) {
3568 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
3571 btrfs_abort_transaction(trans
, root
, ret
);
3574 btrfs_free_path(path
);
3579 * btrfs_truncate_page - read, zero a chunk and write a page
3580 * @inode - inode that we're zeroing
3581 * @from - the offset to start zeroing
3582 * @len - the length to zero, 0 to zero the entire range respective to the
3584 * @front - zero up to the offset instead of from the offset on
3586 * This will find the page for the "from" offset and cow the page and zero the
3587 * part we want to zero. This is used with truncate and hole punching.
3589 int btrfs_truncate_page(struct inode
*inode
, loff_t from
, loff_t len
,
3592 struct address_space
*mapping
= inode
->i_mapping
;
3593 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3594 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3595 struct btrfs_ordered_extent
*ordered
;
3596 struct extent_state
*cached_state
= NULL
;
3598 u32 blocksize
= root
->sectorsize
;
3599 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
3600 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
3602 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
3607 if ((offset
& (blocksize
- 1)) == 0 &&
3608 (!len
|| ((len
& (blocksize
- 1)) == 0)))
3610 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
3615 page
= find_or_create_page(mapping
, index
, mask
);
3617 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
3622 page_start
= page_offset(page
);
3623 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3625 if (!PageUptodate(page
)) {
3626 ret
= btrfs_readpage(NULL
, page
);
3628 if (page
->mapping
!= mapping
) {
3630 page_cache_release(page
);
3633 if (!PageUptodate(page
)) {
3638 wait_on_page_writeback(page
);
3640 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
3641 set_page_extent_mapped(page
);
3643 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
3645 unlock_extent_cached(io_tree
, page_start
, page_end
,
3646 &cached_state
, GFP_NOFS
);
3648 page_cache_release(page
);
3649 btrfs_start_ordered_extent(inode
, ordered
, 1);
3650 btrfs_put_ordered_extent(ordered
);
3654 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
3655 EXTENT_DIRTY
| EXTENT_DELALLOC
|
3656 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
3657 0, 0, &cached_state
, GFP_NOFS
);
3659 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
3662 unlock_extent_cached(io_tree
, page_start
, page_end
,
3663 &cached_state
, GFP_NOFS
);
3667 if (offset
!= PAGE_CACHE_SIZE
) {
3669 len
= PAGE_CACHE_SIZE
- offset
;
3672 memset(kaddr
, 0, offset
);
3674 memset(kaddr
+ offset
, 0, len
);
3675 flush_dcache_page(page
);
3678 ClearPageChecked(page
);
3679 set_page_dirty(page
);
3680 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
3685 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
3687 page_cache_release(page
);
3693 * This function puts in dummy file extents for the area we're creating a hole
3694 * for. So if we are truncating this file to a larger size we need to insert
3695 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3696 * the range between oldsize and size
3698 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
3700 struct btrfs_trans_handle
*trans
;
3701 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3702 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3703 struct extent_map
*em
= NULL
;
3704 struct extent_state
*cached_state
= NULL
;
3705 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
3706 u64 mask
= root
->sectorsize
- 1;
3707 u64 hole_start
= (oldsize
+ mask
) & ~mask
;
3708 u64 block_end
= (size
+ mask
) & ~mask
;
3714 if (size
<= hole_start
)
3718 struct btrfs_ordered_extent
*ordered
;
3719 btrfs_wait_ordered_range(inode
, hole_start
,
3720 block_end
- hole_start
);
3721 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
3723 ordered
= btrfs_lookup_ordered_extent(inode
, hole_start
);
3726 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
3727 &cached_state
, GFP_NOFS
);
3728 btrfs_put_ordered_extent(ordered
);
3731 cur_offset
= hole_start
;
3733 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
3734 block_end
- cur_offset
, 0);
3740 last_byte
= min(extent_map_end(em
), block_end
);
3741 last_byte
= (last_byte
+ mask
) & ~mask
;
3742 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
3743 struct extent_map
*hole_em
;
3744 hole_size
= last_byte
- cur_offset
;
3746 trans
= btrfs_start_transaction(root
, 3);
3747 if (IS_ERR(trans
)) {
3748 err
= PTR_ERR(trans
);
3752 err
= btrfs_drop_extents(trans
, root
, inode
,
3754 cur_offset
+ hole_size
, 1);
3756 btrfs_abort_transaction(trans
, root
, err
);
3757 btrfs_end_transaction(trans
, root
);
3761 err
= btrfs_insert_file_extent(trans
, root
,
3762 btrfs_ino(inode
), cur_offset
, 0,
3763 0, hole_size
, 0, hole_size
,
3766 btrfs_abort_transaction(trans
, root
, err
);
3767 btrfs_end_transaction(trans
, root
);
3771 btrfs_drop_extent_cache(inode
, cur_offset
,
3772 cur_offset
+ hole_size
- 1, 0);
3773 hole_em
= alloc_extent_map();
3775 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3776 &BTRFS_I(inode
)->runtime_flags
);
3779 hole_em
->start
= cur_offset
;
3780 hole_em
->len
= hole_size
;
3781 hole_em
->orig_start
= cur_offset
;
3783 hole_em
->block_start
= EXTENT_MAP_HOLE
;
3784 hole_em
->block_len
= 0;
3785 hole_em
->orig_block_len
= 0;
3786 hole_em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
3787 hole_em
->compress_type
= BTRFS_COMPRESS_NONE
;
3788 hole_em
->generation
= trans
->transid
;
3791 write_lock(&em_tree
->lock
);
3792 err
= add_extent_mapping(em_tree
, hole_em
);
3794 list_move(&hole_em
->list
,
3795 &em_tree
->modified_extents
);
3796 write_unlock(&em_tree
->lock
);
3799 btrfs_drop_extent_cache(inode
, cur_offset
,
3803 free_extent_map(hole_em
);
3805 btrfs_update_inode(trans
, root
, inode
);
3806 btrfs_end_transaction(trans
, root
);
3808 free_extent_map(em
);
3810 cur_offset
= last_byte
;
3811 if (cur_offset
>= block_end
)
3815 free_extent_map(em
);
3816 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
3821 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
)
3823 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3824 struct btrfs_trans_handle
*trans
;
3825 loff_t oldsize
= i_size_read(inode
);
3826 loff_t newsize
= attr
->ia_size
;
3827 int mask
= attr
->ia_valid
;
3830 if (newsize
== oldsize
)
3834 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
3835 * special case where we need to update the times despite not having
3836 * these flags set. For all other operations the VFS set these flags
3837 * explicitly if it wants a timestamp update.
3839 if (newsize
!= oldsize
&& (!(mask
& (ATTR_CTIME
| ATTR_MTIME
))))
3840 inode
->i_ctime
= inode
->i_mtime
= current_fs_time(inode
->i_sb
);
3842 if (newsize
> oldsize
) {
3843 truncate_pagecache(inode
, oldsize
, newsize
);
3844 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
3848 trans
= btrfs_start_transaction(root
, 1);
3850 return PTR_ERR(trans
);
3852 i_size_write(inode
, newsize
);
3853 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
3854 ret
= btrfs_update_inode(trans
, root
, inode
);
3855 btrfs_end_transaction(trans
, root
);
3859 * We're truncating a file that used to have good data down to
3860 * zero. Make sure it gets into the ordered flush list so that
3861 * any new writes get down to disk quickly.
3864 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
3865 &BTRFS_I(inode
)->runtime_flags
);
3868 * 1 for the orphan item we're going to add
3869 * 1 for the orphan item deletion.
3871 trans
= btrfs_start_transaction(root
, 2);
3873 return PTR_ERR(trans
);
3876 * We need to do this in case we fail at _any_ point during the
3877 * actual truncate. Once we do the truncate_setsize we could
3878 * invalidate pages which forces any outstanding ordered io to
3879 * be instantly completed which will give us extents that need
3880 * to be truncated. If we fail to get an orphan inode down we
3881 * could have left over extents that were never meant to live,
3882 * so we need to garuntee from this point on that everything
3883 * will be consistent.
3885 ret
= btrfs_orphan_add(trans
, inode
);
3886 btrfs_end_transaction(trans
, root
);
3890 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3891 truncate_setsize(inode
, newsize
);
3893 /* Disable nonlocked read DIO to avoid the end less truncate */
3894 btrfs_inode_block_unlocked_dio(inode
);
3895 inode_dio_wait(inode
);
3896 btrfs_inode_resume_unlocked_dio(inode
);
3898 ret
= btrfs_truncate(inode
);
3899 if (ret
&& inode
->i_nlink
)
3900 btrfs_orphan_del(NULL
, inode
);
3906 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
3908 struct inode
*inode
= dentry
->d_inode
;
3909 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3912 if (btrfs_root_readonly(root
))
3915 err
= inode_change_ok(inode
, attr
);
3919 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
3920 err
= btrfs_setsize(inode
, attr
);
3925 if (attr
->ia_valid
) {
3926 setattr_copy(inode
, attr
);
3927 inode_inc_iversion(inode
);
3928 err
= btrfs_dirty_inode(inode
);
3930 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
3931 err
= btrfs_acl_chmod(inode
);
3937 void btrfs_evict_inode(struct inode
*inode
)
3939 struct btrfs_trans_handle
*trans
;
3940 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3941 struct btrfs_block_rsv
*rsv
, *global_rsv
;
3942 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
3945 trace_btrfs_inode_evict(inode
);
3947 truncate_inode_pages(&inode
->i_data
, 0);
3948 if (inode
->i_nlink
&& (btrfs_root_refs(&root
->root_item
) != 0 ||
3949 btrfs_is_free_space_inode(inode
)))
3952 if (is_bad_inode(inode
)) {
3953 btrfs_orphan_del(NULL
, inode
);
3956 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
3957 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
3959 if (root
->fs_info
->log_root_recovering
) {
3960 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3961 &BTRFS_I(inode
)->runtime_flags
));
3965 if (inode
->i_nlink
> 0) {
3966 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0);
3970 ret
= btrfs_commit_inode_delayed_inode(inode
);
3972 btrfs_orphan_del(NULL
, inode
);
3976 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
3978 btrfs_orphan_del(NULL
, inode
);
3981 rsv
->size
= min_size
;
3983 global_rsv
= &root
->fs_info
->global_block_rsv
;
3985 btrfs_i_size_write(inode
, 0);
3988 * This is a bit simpler than btrfs_truncate since we've already
3989 * reserved our space for our orphan item in the unlink, so we just
3990 * need to reserve some slack space in case we add bytes and update
3991 * inode item when doing the truncate.
3994 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
,
3995 BTRFS_RESERVE_FLUSH_LIMIT
);
3998 * Try and steal from the global reserve since we will
3999 * likely not use this space anyway, we want to try as
4000 * hard as possible to get this to work.
4003 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
, min_size
);
4006 printk(KERN_WARNING
"Could not get space for a "
4007 "delete, will truncate on mount %d\n", ret
);
4008 btrfs_orphan_del(NULL
, inode
);
4009 btrfs_free_block_rsv(root
, rsv
);
4013 trans
= btrfs_join_transaction(root
);
4014 if (IS_ERR(trans
)) {
4015 btrfs_orphan_del(NULL
, inode
);
4016 btrfs_free_block_rsv(root
, rsv
);
4020 trans
->block_rsv
= rsv
;
4022 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
4026 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4027 btrfs_end_transaction(trans
, root
);
4029 btrfs_btree_balance_dirty(root
);
4032 btrfs_free_block_rsv(root
, rsv
);
4035 trans
->block_rsv
= root
->orphan_block_rsv
;
4036 ret
= btrfs_orphan_del(trans
, inode
);
4040 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4041 if (!(root
== root
->fs_info
->tree_root
||
4042 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
4043 btrfs_return_ino(root
, btrfs_ino(inode
));
4045 btrfs_end_transaction(trans
, root
);
4046 btrfs_btree_balance_dirty(root
);
4053 * this returns the key found in the dir entry in the location pointer.
4054 * If no dir entries were found, location->objectid is 0.
4056 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
4057 struct btrfs_key
*location
)
4059 const char *name
= dentry
->d_name
.name
;
4060 int namelen
= dentry
->d_name
.len
;
4061 struct btrfs_dir_item
*di
;
4062 struct btrfs_path
*path
;
4063 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4066 path
= btrfs_alloc_path();
4070 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(dir
), name
,
4075 if (IS_ERR_OR_NULL(di
))
4078 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
4080 btrfs_free_path(path
);
4083 location
->objectid
= 0;
4088 * when we hit a tree root in a directory, the btrfs part of the inode
4089 * needs to be changed to reflect the root directory of the tree root. This
4090 * is kind of like crossing a mount point.
4092 static int fixup_tree_root_location(struct btrfs_root
*root
,
4094 struct dentry
*dentry
,
4095 struct btrfs_key
*location
,
4096 struct btrfs_root
**sub_root
)
4098 struct btrfs_path
*path
;
4099 struct btrfs_root
*new_root
;
4100 struct btrfs_root_ref
*ref
;
4101 struct extent_buffer
*leaf
;
4105 path
= btrfs_alloc_path();
4112 ret
= btrfs_find_root_ref(root
->fs_info
->tree_root
, path
,
4113 BTRFS_I(dir
)->root
->root_key
.objectid
,
4114 location
->objectid
);
4121 leaf
= path
->nodes
[0];
4122 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
4123 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(dir
) ||
4124 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
4127 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
4128 (unsigned long)(ref
+ 1),
4129 dentry
->d_name
.len
);
4133 btrfs_release_path(path
);
4135 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
4136 if (IS_ERR(new_root
)) {
4137 err
= PTR_ERR(new_root
);
4141 if (btrfs_root_refs(&new_root
->root_item
) == 0) {
4146 *sub_root
= new_root
;
4147 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
4148 location
->type
= BTRFS_INODE_ITEM_KEY
;
4149 location
->offset
= 0;
4152 btrfs_free_path(path
);
4156 static void inode_tree_add(struct inode
*inode
)
4158 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4159 struct btrfs_inode
*entry
;
4161 struct rb_node
*parent
;
4162 u64 ino
= btrfs_ino(inode
);
4164 p
= &root
->inode_tree
.rb_node
;
4167 if (inode_unhashed(inode
))
4170 spin_lock(&root
->inode_lock
);
4173 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
4175 if (ino
< btrfs_ino(&entry
->vfs_inode
))
4176 p
= &parent
->rb_left
;
4177 else if (ino
> btrfs_ino(&entry
->vfs_inode
))
4178 p
= &parent
->rb_right
;
4180 WARN_ON(!(entry
->vfs_inode
.i_state
&
4181 (I_WILL_FREE
| I_FREEING
)));
4182 rb_erase(parent
, &root
->inode_tree
);
4183 RB_CLEAR_NODE(parent
);
4184 spin_unlock(&root
->inode_lock
);
4188 rb_link_node(&BTRFS_I(inode
)->rb_node
, parent
, p
);
4189 rb_insert_color(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4190 spin_unlock(&root
->inode_lock
);
4193 static void inode_tree_del(struct inode
*inode
)
4195 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4198 spin_lock(&root
->inode_lock
);
4199 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
4200 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4201 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
4202 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4204 spin_unlock(&root
->inode_lock
);
4207 * Free space cache has inodes in the tree root, but the tree root has a
4208 * root_refs of 0, so this could end up dropping the tree root as a
4209 * snapshot, so we need the extra !root->fs_info->tree_root check to
4210 * make sure we don't drop it.
4212 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0 &&
4213 root
!= root
->fs_info
->tree_root
) {
4214 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
4215 spin_lock(&root
->inode_lock
);
4216 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4217 spin_unlock(&root
->inode_lock
);
4219 btrfs_add_dead_root(root
);
4223 void btrfs_invalidate_inodes(struct btrfs_root
*root
)
4225 struct rb_node
*node
;
4226 struct rb_node
*prev
;
4227 struct btrfs_inode
*entry
;
4228 struct inode
*inode
;
4231 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
4233 spin_lock(&root
->inode_lock
);
4235 node
= root
->inode_tree
.rb_node
;
4239 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4241 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
4242 node
= node
->rb_left
;
4243 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
4244 node
= node
->rb_right
;
4250 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
4251 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
4255 prev
= rb_next(prev
);
4259 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4260 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
4261 inode
= igrab(&entry
->vfs_inode
);
4263 spin_unlock(&root
->inode_lock
);
4264 if (atomic_read(&inode
->i_count
) > 1)
4265 d_prune_aliases(inode
);
4267 * btrfs_drop_inode will have it removed from
4268 * the inode cache when its usage count
4273 spin_lock(&root
->inode_lock
);
4277 if (cond_resched_lock(&root
->inode_lock
))
4280 node
= rb_next(node
);
4282 spin_unlock(&root
->inode_lock
);
4285 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
4287 struct btrfs_iget_args
*args
= p
;
4288 inode
->i_ino
= args
->ino
;
4289 BTRFS_I(inode
)->root
= args
->root
;
4293 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
4295 struct btrfs_iget_args
*args
= opaque
;
4296 return args
->ino
== btrfs_ino(inode
) &&
4297 args
->root
== BTRFS_I(inode
)->root
;
4300 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
4302 struct btrfs_root
*root
)
4304 struct inode
*inode
;
4305 struct btrfs_iget_args args
;
4306 args
.ino
= objectid
;
4309 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
4310 btrfs_init_locked_inode
,
4315 /* Get an inode object given its location and corresponding root.
4316 * Returns in *is_new if the inode was read from disk
4318 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
4319 struct btrfs_root
*root
, int *new)
4321 struct inode
*inode
;
4323 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
4325 return ERR_PTR(-ENOMEM
);
4327 if (inode
->i_state
& I_NEW
) {
4328 BTRFS_I(inode
)->root
= root
;
4329 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
4330 btrfs_read_locked_inode(inode
);
4331 if (!is_bad_inode(inode
)) {
4332 inode_tree_add(inode
);
4333 unlock_new_inode(inode
);
4337 unlock_new_inode(inode
);
4339 inode
= ERR_PTR(-ESTALE
);
4346 static struct inode
*new_simple_dir(struct super_block
*s
,
4347 struct btrfs_key
*key
,
4348 struct btrfs_root
*root
)
4350 struct inode
*inode
= new_inode(s
);
4353 return ERR_PTR(-ENOMEM
);
4355 BTRFS_I(inode
)->root
= root
;
4356 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
4357 set_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
);
4359 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
4360 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
4361 inode
->i_fop
= &simple_dir_operations
;
4362 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
4363 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4368 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
4370 struct inode
*inode
;
4371 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4372 struct btrfs_root
*sub_root
= root
;
4373 struct btrfs_key location
;
4377 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
4378 return ERR_PTR(-ENAMETOOLONG
);
4380 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
4382 return ERR_PTR(ret
);
4384 if (location
.objectid
== 0)
4387 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
4388 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
4392 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
4394 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
4395 ret
= fixup_tree_root_location(root
, dir
, dentry
,
4396 &location
, &sub_root
);
4399 inode
= ERR_PTR(ret
);
4401 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
4403 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
4405 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
4407 if (!IS_ERR(inode
) && root
!= sub_root
) {
4408 down_read(&root
->fs_info
->cleanup_work_sem
);
4409 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
4410 ret
= btrfs_orphan_cleanup(sub_root
);
4411 up_read(&root
->fs_info
->cleanup_work_sem
);
4413 inode
= ERR_PTR(ret
);
4419 static int btrfs_dentry_delete(const struct dentry
*dentry
)
4421 struct btrfs_root
*root
;
4422 struct inode
*inode
= dentry
->d_inode
;
4424 if (!inode
&& !IS_ROOT(dentry
))
4425 inode
= dentry
->d_parent
->d_inode
;
4428 root
= BTRFS_I(inode
)->root
;
4429 if (btrfs_root_refs(&root
->root_item
) == 0)
4432 if (btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
4438 static void btrfs_dentry_release(struct dentry
*dentry
)
4440 if (dentry
->d_fsdata
)
4441 kfree(dentry
->d_fsdata
);
4444 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
4449 ret
= d_splice_alias(btrfs_lookup_dentry(dir
, dentry
), dentry
);
4453 unsigned char btrfs_filetype_table
[] = {
4454 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
4457 static int btrfs_real_readdir(struct file
*filp
, void *dirent
,
4460 struct inode
*inode
= filp
->f_dentry
->d_inode
;
4461 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4462 struct btrfs_item
*item
;
4463 struct btrfs_dir_item
*di
;
4464 struct btrfs_key key
;
4465 struct btrfs_key found_key
;
4466 struct btrfs_path
*path
;
4467 struct list_head ins_list
;
4468 struct list_head del_list
;
4470 struct extent_buffer
*leaf
;
4472 unsigned char d_type
;
4477 int key_type
= BTRFS_DIR_INDEX_KEY
;
4481 int is_curr
= 0; /* filp->f_pos points to the current index? */
4483 /* FIXME, use a real flag for deciding about the key type */
4484 if (root
->fs_info
->tree_root
== root
)
4485 key_type
= BTRFS_DIR_ITEM_KEY
;
4487 /* special case for "." */
4488 if (filp
->f_pos
== 0) {
4489 over
= filldir(dirent
, ".", 1,
4490 filp
->f_pos
, btrfs_ino(inode
), DT_DIR
);
4495 /* special case for .., just use the back ref */
4496 if (filp
->f_pos
== 1) {
4497 u64 pino
= parent_ino(filp
->f_path
.dentry
);
4498 over
= filldir(dirent
, "..", 2,
4499 filp
->f_pos
, pino
, DT_DIR
);
4504 path
= btrfs_alloc_path();
4510 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
4511 INIT_LIST_HEAD(&ins_list
);
4512 INIT_LIST_HEAD(&del_list
);
4513 btrfs_get_delayed_items(inode
, &ins_list
, &del_list
);
4516 btrfs_set_key_type(&key
, key_type
);
4517 key
.offset
= filp
->f_pos
;
4518 key
.objectid
= btrfs_ino(inode
);
4520 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4525 leaf
= path
->nodes
[0];
4526 slot
= path
->slots
[0];
4527 if (slot
>= btrfs_header_nritems(leaf
)) {
4528 ret
= btrfs_next_leaf(root
, path
);
4536 item
= btrfs_item_nr(leaf
, slot
);
4537 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
4539 if (found_key
.objectid
!= key
.objectid
)
4541 if (btrfs_key_type(&found_key
) != key_type
)
4543 if (found_key
.offset
< filp
->f_pos
)
4545 if (key_type
== BTRFS_DIR_INDEX_KEY
&&
4546 btrfs_should_delete_dir_index(&del_list
,
4550 filp
->f_pos
= found_key
.offset
;
4553 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
4555 di_total
= btrfs_item_size(leaf
, item
);
4557 while (di_cur
< di_total
) {
4558 struct btrfs_key location
;
4560 if (verify_dir_item(root
, leaf
, di
))
4563 name_len
= btrfs_dir_name_len(leaf
, di
);
4564 if (name_len
<= sizeof(tmp_name
)) {
4565 name_ptr
= tmp_name
;
4567 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
4573 read_extent_buffer(leaf
, name_ptr
,
4574 (unsigned long)(di
+ 1), name_len
);
4576 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
4577 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
4580 /* is this a reference to our own snapshot? If so
4583 * In contrast to old kernels, we insert the snapshot's
4584 * dir item and dir index after it has been created, so
4585 * we won't find a reference to our own snapshot. We
4586 * still keep the following code for backward
4589 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
4590 location
.objectid
== root
->root_key
.objectid
) {
4594 over
= filldir(dirent
, name_ptr
, name_len
,
4595 found_key
.offset
, location
.objectid
,
4599 if (name_ptr
!= tmp_name
)
4604 di_len
= btrfs_dir_name_len(leaf
, di
) +
4605 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
4607 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
4613 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
4616 ret
= btrfs_readdir_delayed_dir_index(filp
, dirent
, filldir
,
4622 /* Reached end of directory/root. Bump pos past the last item. */
4623 if (key_type
== BTRFS_DIR_INDEX_KEY
)
4625 * 32-bit glibc will use getdents64, but then strtol -
4626 * so the last number we can serve is this.
4628 filp
->f_pos
= 0x7fffffff;
4634 if (key_type
== BTRFS_DIR_INDEX_KEY
)
4635 btrfs_put_delayed_items(&ins_list
, &del_list
);
4636 btrfs_free_path(path
);
4640 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
4642 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4643 struct btrfs_trans_handle
*trans
;
4645 bool nolock
= false;
4647 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
4650 if (btrfs_fs_closing(root
->fs_info
) && btrfs_is_free_space_inode(inode
))
4653 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
4655 trans
= btrfs_join_transaction_nolock(root
);
4657 trans
= btrfs_join_transaction(root
);
4659 return PTR_ERR(trans
);
4660 ret
= btrfs_commit_transaction(trans
, root
);
4666 * This is somewhat expensive, updating the tree every time the
4667 * inode changes. But, it is most likely to find the inode in cache.
4668 * FIXME, needs more benchmarking...there are no reasons other than performance
4669 * to keep or drop this code.
4671 int btrfs_dirty_inode(struct inode
*inode
)
4673 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4674 struct btrfs_trans_handle
*trans
;
4677 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
4680 trans
= btrfs_join_transaction(root
);
4682 return PTR_ERR(trans
);
4684 ret
= btrfs_update_inode(trans
, root
, inode
);
4685 if (ret
&& ret
== -ENOSPC
) {
4686 /* whoops, lets try again with the full transaction */
4687 btrfs_end_transaction(trans
, root
);
4688 trans
= btrfs_start_transaction(root
, 1);
4690 return PTR_ERR(trans
);
4692 ret
= btrfs_update_inode(trans
, root
, inode
);
4694 btrfs_end_transaction(trans
, root
);
4695 if (BTRFS_I(inode
)->delayed_node
)
4696 btrfs_balance_delayed_items(root
);
4702 * This is a copy of file_update_time. We need this so we can return error on
4703 * ENOSPC for updating the inode in the case of file write and mmap writes.
4705 static int btrfs_update_time(struct inode
*inode
, struct timespec
*now
,
4708 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4710 if (btrfs_root_readonly(root
))
4713 if (flags
& S_VERSION
)
4714 inode_inc_iversion(inode
);
4715 if (flags
& S_CTIME
)
4716 inode
->i_ctime
= *now
;
4717 if (flags
& S_MTIME
)
4718 inode
->i_mtime
= *now
;
4719 if (flags
& S_ATIME
)
4720 inode
->i_atime
= *now
;
4721 return btrfs_dirty_inode(inode
);
4725 * find the highest existing sequence number in a directory
4726 * and then set the in-memory index_cnt variable to reflect
4727 * free sequence numbers
4729 static int btrfs_set_inode_index_count(struct inode
*inode
)
4731 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4732 struct btrfs_key key
, found_key
;
4733 struct btrfs_path
*path
;
4734 struct extent_buffer
*leaf
;
4737 key
.objectid
= btrfs_ino(inode
);
4738 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
4739 key
.offset
= (u64
)-1;
4741 path
= btrfs_alloc_path();
4745 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4748 /* FIXME: we should be able to handle this */
4754 * MAGIC NUMBER EXPLANATION:
4755 * since we search a directory based on f_pos we have to start at 2
4756 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4757 * else has to start at 2
4759 if (path
->slots
[0] == 0) {
4760 BTRFS_I(inode
)->index_cnt
= 2;
4766 leaf
= path
->nodes
[0];
4767 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4769 if (found_key
.objectid
!= btrfs_ino(inode
) ||
4770 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
4771 BTRFS_I(inode
)->index_cnt
= 2;
4775 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
4777 btrfs_free_path(path
);
4782 * helper to find a free sequence number in a given directory. This current
4783 * code is very simple, later versions will do smarter things in the btree
4785 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
4789 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
4790 ret
= btrfs_inode_delayed_dir_index_count(dir
);
4792 ret
= btrfs_set_inode_index_count(dir
);
4798 *index
= BTRFS_I(dir
)->index_cnt
;
4799 BTRFS_I(dir
)->index_cnt
++;
4804 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
4805 struct btrfs_root
*root
,
4807 const char *name
, int name_len
,
4808 u64 ref_objectid
, u64 objectid
,
4809 umode_t mode
, u64
*index
)
4811 struct inode
*inode
;
4812 struct btrfs_inode_item
*inode_item
;
4813 struct btrfs_key
*location
;
4814 struct btrfs_path
*path
;
4815 struct btrfs_inode_ref
*ref
;
4816 struct btrfs_key key
[2];
4822 path
= btrfs_alloc_path();
4824 return ERR_PTR(-ENOMEM
);
4826 inode
= new_inode(root
->fs_info
->sb
);
4828 btrfs_free_path(path
);
4829 return ERR_PTR(-ENOMEM
);
4833 * we have to initialize this early, so we can reclaim the inode
4834 * number if we fail afterwards in this function.
4836 inode
->i_ino
= objectid
;
4839 trace_btrfs_inode_request(dir
);
4841 ret
= btrfs_set_inode_index(dir
, index
);
4843 btrfs_free_path(path
);
4845 return ERR_PTR(ret
);
4849 * index_cnt is ignored for everything but a dir,
4850 * btrfs_get_inode_index_count has an explanation for the magic
4853 BTRFS_I(inode
)->index_cnt
= 2;
4854 BTRFS_I(inode
)->root
= root
;
4855 BTRFS_I(inode
)->generation
= trans
->transid
;
4856 inode
->i_generation
= BTRFS_I(inode
)->generation
;
4859 * We could have gotten an inode number from somebody who was fsynced
4860 * and then removed in this same transaction, so let's just set full
4861 * sync since it will be a full sync anyway and this will blow away the
4862 * old info in the log.
4864 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
4871 key
[0].objectid
= objectid
;
4872 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
4876 * Start new inodes with an inode_ref. This is slightly more
4877 * efficient for small numbers of hard links since they will
4878 * be packed into one item. Extended refs will kick in if we
4879 * add more hard links than can fit in the ref item.
4881 key
[1].objectid
= objectid
;
4882 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
4883 key
[1].offset
= ref_objectid
;
4885 sizes
[0] = sizeof(struct btrfs_inode_item
);
4886 sizes
[1] = name_len
+ sizeof(*ref
);
4888 path
->leave_spinning
= 1;
4889 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
4893 inode_init_owner(inode
, dir
, mode
);
4894 inode_set_bytes(inode
, 0);
4895 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4896 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4897 struct btrfs_inode_item
);
4898 memset_extent_buffer(path
->nodes
[0], 0, (unsigned long)inode_item
,
4899 sizeof(*inode_item
));
4900 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
4902 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
4903 struct btrfs_inode_ref
);
4904 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
4905 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
4906 ptr
= (unsigned long)(ref
+ 1);
4907 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
4909 btrfs_mark_buffer_dirty(path
->nodes
[0]);
4910 btrfs_free_path(path
);
4912 location
= &BTRFS_I(inode
)->location
;
4913 location
->objectid
= objectid
;
4914 location
->offset
= 0;
4915 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
4917 btrfs_inherit_iflags(inode
, dir
);
4919 if (S_ISREG(mode
)) {
4920 if (btrfs_test_opt(root
, NODATASUM
))
4921 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
4922 if (btrfs_test_opt(root
, NODATACOW
))
4923 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
;
4926 insert_inode_hash(inode
);
4927 inode_tree_add(inode
);
4929 trace_btrfs_inode_new(inode
);
4930 btrfs_set_inode_last_trans(trans
, inode
);
4932 btrfs_update_root_times(trans
, root
);
4937 BTRFS_I(dir
)->index_cnt
--;
4938 btrfs_free_path(path
);
4940 return ERR_PTR(ret
);
4943 static inline u8
btrfs_inode_type(struct inode
*inode
)
4945 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
4949 * utility function to add 'inode' into 'parent_inode' with
4950 * a give name and a given sequence number.
4951 * if 'add_backref' is true, also insert a backref from the
4952 * inode to the parent directory.
4954 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
4955 struct inode
*parent_inode
, struct inode
*inode
,
4956 const char *name
, int name_len
, int add_backref
, u64 index
)
4959 struct btrfs_key key
;
4960 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
4961 u64 ino
= btrfs_ino(inode
);
4962 u64 parent_ino
= btrfs_ino(parent_inode
);
4964 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4965 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
4968 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
4972 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4973 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
4974 key
.objectid
, root
->root_key
.objectid
,
4975 parent_ino
, index
, name
, name_len
);
4976 } else if (add_backref
) {
4977 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
4981 /* Nothing to clean up yet */
4985 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
4987 btrfs_inode_type(inode
), index
);
4988 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
)
4991 btrfs_abort_transaction(trans
, root
, ret
);
4995 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
4997 inode_inc_iversion(parent_inode
);
4998 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
4999 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
5001 btrfs_abort_transaction(trans
, root
, ret
);
5005 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5008 err
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
5009 key
.objectid
, root
->root_key
.objectid
,
5010 parent_ino
, &local_index
, name
, name_len
);
5012 } else if (add_backref
) {
5016 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
5017 ino
, parent_ino
, &local_index
);
5022 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
5023 struct inode
*dir
, struct dentry
*dentry
,
5024 struct inode
*inode
, int backref
, u64 index
)
5026 int err
= btrfs_add_link(trans
, dir
, inode
,
5027 dentry
->d_name
.name
, dentry
->d_name
.len
,
5034 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
5035 umode_t mode
, dev_t rdev
)
5037 struct btrfs_trans_handle
*trans
;
5038 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5039 struct inode
*inode
= NULL
;
5045 if (!new_valid_dev(rdev
))
5049 * 2 for inode item and ref
5051 * 1 for xattr if selinux is on
5053 trans
= btrfs_start_transaction(root
, 5);
5055 return PTR_ERR(trans
);
5057 err
= btrfs_find_free_ino(root
, &objectid
);
5061 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5062 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5064 if (IS_ERR(inode
)) {
5065 err
= PTR_ERR(inode
);
5069 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5076 * If the active LSM wants to access the inode during
5077 * d_instantiate it needs these. Smack checks to see
5078 * if the filesystem supports xattrs by looking at the
5082 inode
->i_op
= &btrfs_special_inode_operations
;
5083 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5087 init_special_inode(inode
, inode
->i_mode
, rdev
);
5088 btrfs_update_inode(trans
, root
, inode
);
5089 d_instantiate(dentry
, inode
);
5092 btrfs_end_transaction(trans
, root
);
5093 btrfs_btree_balance_dirty(root
);
5095 inode_dec_link_count(inode
);
5101 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
5102 umode_t mode
, bool excl
)
5104 struct btrfs_trans_handle
*trans
;
5105 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5106 struct inode
*inode
= NULL
;
5107 int drop_inode_on_err
= 0;
5113 * 2 for inode item and ref
5115 * 1 for xattr if selinux is on
5117 trans
= btrfs_start_transaction(root
, 5);
5119 return PTR_ERR(trans
);
5121 err
= btrfs_find_free_ino(root
, &objectid
);
5125 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5126 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5128 if (IS_ERR(inode
)) {
5129 err
= PTR_ERR(inode
);
5132 drop_inode_on_err
= 1;
5134 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5138 err
= btrfs_update_inode(trans
, root
, inode
);
5143 * If the active LSM wants to access the inode during
5144 * d_instantiate it needs these. Smack checks to see
5145 * if the filesystem supports xattrs by looking at the
5148 inode
->i_fop
= &btrfs_file_operations
;
5149 inode
->i_op
= &btrfs_file_inode_operations
;
5151 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5155 inode
->i_mapping
->a_ops
= &btrfs_aops
;
5156 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
5157 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
5158 d_instantiate(dentry
, inode
);
5161 btrfs_end_transaction(trans
, root
);
5162 if (err
&& drop_inode_on_err
) {
5163 inode_dec_link_count(inode
);
5166 btrfs_btree_balance_dirty(root
);
5170 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
5171 struct dentry
*dentry
)
5173 struct btrfs_trans_handle
*trans
;
5174 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5175 struct inode
*inode
= old_dentry
->d_inode
;
5180 /* do not allow sys_link's with other subvols of the same device */
5181 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
5184 if (inode
->i_nlink
>= BTRFS_LINK_MAX
)
5187 err
= btrfs_set_inode_index(dir
, &index
);
5192 * 2 items for inode and inode ref
5193 * 2 items for dir items
5194 * 1 item for parent inode
5196 trans
= btrfs_start_transaction(root
, 5);
5197 if (IS_ERR(trans
)) {
5198 err
= PTR_ERR(trans
);
5202 btrfs_inc_nlink(inode
);
5203 inode_inc_iversion(inode
);
5204 inode
->i_ctime
= CURRENT_TIME
;
5206 set_bit(BTRFS_INODE_COPY_EVERYTHING
, &BTRFS_I(inode
)->runtime_flags
);
5208 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
5213 struct dentry
*parent
= dentry
->d_parent
;
5214 err
= btrfs_update_inode(trans
, root
, inode
);
5217 d_instantiate(dentry
, inode
);
5218 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
5221 btrfs_end_transaction(trans
, root
);
5224 inode_dec_link_count(inode
);
5227 btrfs_btree_balance_dirty(root
);
5231 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
5233 struct inode
*inode
= NULL
;
5234 struct btrfs_trans_handle
*trans
;
5235 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5237 int drop_on_err
= 0;
5242 * 2 items for inode and ref
5243 * 2 items for dir items
5244 * 1 for xattr if selinux is on
5246 trans
= btrfs_start_transaction(root
, 5);
5248 return PTR_ERR(trans
);
5250 err
= btrfs_find_free_ino(root
, &objectid
);
5254 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5255 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5256 S_IFDIR
| mode
, &index
);
5257 if (IS_ERR(inode
)) {
5258 err
= PTR_ERR(inode
);
5264 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5268 inode
->i_op
= &btrfs_dir_inode_operations
;
5269 inode
->i_fop
= &btrfs_dir_file_operations
;
5271 btrfs_i_size_write(inode
, 0);
5272 err
= btrfs_update_inode(trans
, root
, inode
);
5276 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
5277 dentry
->d_name
.len
, 0, index
);
5281 d_instantiate(dentry
, inode
);
5285 btrfs_end_transaction(trans
, root
);
5288 btrfs_btree_balance_dirty(root
);
5292 /* helper for btfs_get_extent. Given an existing extent in the tree,
5293 * and an extent that you want to insert, deal with overlap and insert
5294 * the new extent into the tree.
5296 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
5297 struct extent_map
*existing
,
5298 struct extent_map
*em
,
5299 u64 map_start
, u64 map_len
)
5303 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
5304 start_diff
= map_start
- em
->start
;
5305 em
->start
= map_start
;
5307 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
5308 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
5309 em
->block_start
+= start_diff
;
5310 em
->block_len
-= start_diff
;
5312 return add_extent_mapping(em_tree
, em
);
5315 static noinline
int uncompress_inline(struct btrfs_path
*path
,
5316 struct inode
*inode
, struct page
*page
,
5317 size_t pg_offset
, u64 extent_offset
,
5318 struct btrfs_file_extent_item
*item
)
5321 struct extent_buffer
*leaf
= path
->nodes
[0];
5324 unsigned long inline_size
;
5328 WARN_ON(pg_offset
!= 0);
5329 compress_type
= btrfs_file_extent_compression(leaf
, item
);
5330 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
5331 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
5332 btrfs_item_nr(leaf
, path
->slots
[0]));
5333 tmp
= kmalloc(inline_size
, GFP_NOFS
);
5336 ptr
= btrfs_file_extent_inline_start(item
);
5338 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
5340 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
5341 ret
= btrfs_decompress(compress_type
, tmp
, page
,
5342 extent_offset
, inline_size
, max_size
);
5344 char *kaddr
= kmap_atomic(page
);
5345 unsigned long copy_size
= min_t(u64
,
5346 PAGE_CACHE_SIZE
- pg_offset
,
5347 max_size
- extent_offset
);
5348 memset(kaddr
+ pg_offset
, 0, copy_size
);
5349 kunmap_atomic(kaddr
);
5356 * a bit scary, this does extent mapping from logical file offset to the disk.
5357 * the ugly parts come from merging extents from the disk with the in-ram
5358 * representation. This gets more complex because of the data=ordered code,
5359 * where the in-ram extents might be locked pending data=ordered completion.
5361 * This also copies inline extents directly into the page.
5364 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
5365 size_t pg_offset
, u64 start
, u64 len
,
5371 u64 extent_start
= 0;
5373 u64 objectid
= btrfs_ino(inode
);
5375 struct btrfs_path
*path
= NULL
;
5376 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5377 struct btrfs_file_extent_item
*item
;
5378 struct extent_buffer
*leaf
;
5379 struct btrfs_key found_key
;
5380 struct extent_map
*em
= NULL
;
5381 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
5382 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
5383 struct btrfs_trans_handle
*trans
= NULL
;
5387 read_lock(&em_tree
->lock
);
5388 em
= lookup_extent_mapping(em_tree
, start
, len
);
5390 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5391 read_unlock(&em_tree
->lock
);
5394 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
5395 free_extent_map(em
);
5396 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
5397 free_extent_map(em
);
5401 em
= alloc_extent_map();
5406 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5407 em
->start
= EXTENT_MAP_HOLE
;
5408 em
->orig_start
= EXTENT_MAP_HOLE
;
5410 em
->block_len
= (u64
)-1;
5413 path
= btrfs_alloc_path();
5419 * Chances are we'll be called again, so go ahead and do
5425 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
5426 objectid
, start
, trans
!= NULL
);
5433 if (path
->slots
[0] == 0)
5438 leaf
= path
->nodes
[0];
5439 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5440 struct btrfs_file_extent_item
);
5441 /* are we inside the extent that was found? */
5442 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5443 found_type
= btrfs_key_type(&found_key
);
5444 if (found_key
.objectid
!= objectid
||
5445 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
5449 found_type
= btrfs_file_extent_type(leaf
, item
);
5450 extent_start
= found_key
.offset
;
5451 compress_type
= btrfs_file_extent_compression(leaf
, item
);
5452 if (found_type
== BTRFS_FILE_EXTENT_REG
||
5453 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5454 extent_end
= extent_start
+
5455 btrfs_file_extent_num_bytes(leaf
, item
);
5456 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
5458 size
= btrfs_file_extent_inline_len(leaf
, item
);
5459 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
5460 ~((u64
)root
->sectorsize
- 1);
5463 if (start
>= extent_end
) {
5465 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
5466 ret
= btrfs_next_leaf(root
, path
);
5473 leaf
= path
->nodes
[0];
5475 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5476 if (found_key
.objectid
!= objectid
||
5477 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
5479 if (start
+ len
<= found_key
.offset
)
5482 em
->orig_start
= start
;
5483 em
->len
= found_key
.offset
- start
;
5487 if (found_type
== BTRFS_FILE_EXTENT_REG
||
5488 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5489 em
->start
= extent_start
;
5490 em
->len
= extent_end
- extent_start
;
5491 em
->orig_start
= extent_start
-
5492 btrfs_file_extent_offset(leaf
, item
);
5493 em
->orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
,
5495 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
5497 em
->block_start
= EXTENT_MAP_HOLE
;
5500 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
5501 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
5502 em
->compress_type
= compress_type
;
5503 em
->block_start
= bytenr
;
5504 em
->block_len
= em
->orig_block_len
;
5506 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
5507 em
->block_start
= bytenr
;
5508 em
->block_len
= em
->len
;
5509 if (found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
5510 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
5513 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
5517 size_t extent_offset
;
5520 em
->block_start
= EXTENT_MAP_INLINE
;
5521 if (!page
|| create
) {
5522 em
->start
= extent_start
;
5523 em
->len
= extent_end
- extent_start
;
5527 size
= btrfs_file_extent_inline_len(leaf
, item
);
5528 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
5529 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
5530 size
- extent_offset
);
5531 em
->start
= extent_start
+ extent_offset
;
5532 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
5533 ~((u64
)root
->sectorsize
- 1);
5534 em
->orig_block_len
= em
->len
;
5535 em
->orig_start
= em
->start
;
5536 if (compress_type
) {
5537 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
5538 em
->compress_type
= compress_type
;
5540 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
5541 if (create
== 0 && !PageUptodate(page
)) {
5542 if (btrfs_file_extent_compression(leaf
, item
) !=
5543 BTRFS_COMPRESS_NONE
) {
5544 ret
= uncompress_inline(path
, inode
, page
,
5546 extent_offset
, item
);
5547 BUG_ON(ret
); /* -ENOMEM */
5550 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
5552 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
5553 memset(map
+ pg_offset
+ copy_size
, 0,
5554 PAGE_CACHE_SIZE
- pg_offset
-
5559 flush_dcache_page(page
);
5560 } else if (create
&& PageUptodate(page
)) {
5564 free_extent_map(em
);
5567 btrfs_release_path(path
);
5568 trans
= btrfs_join_transaction(root
);
5571 return ERR_CAST(trans
);
5575 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
5578 btrfs_mark_buffer_dirty(leaf
);
5580 set_extent_uptodate(io_tree
, em
->start
,
5581 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
5584 WARN(1, KERN_ERR
"btrfs unknown found_type %d\n", found_type
);
5588 em
->orig_start
= start
;
5591 em
->block_start
= EXTENT_MAP_HOLE
;
5592 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
5594 btrfs_release_path(path
);
5595 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
5596 printk(KERN_ERR
"Btrfs: bad extent! em: [%llu %llu] passed "
5597 "[%llu %llu]\n", (unsigned long long)em
->start
,
5598 (unsigned long long)em
->len
,
5599 (unsigned long long)start
,
5600 (unsigned long long)len
);
5606 write_lock(&em_tree
->lock
);
5607 ret
= add_extent_mapping(em_tree
, em
);
5608 /* it is possible that someone inserted the extent into the tree
5609 * while we had the lock dropped. It is also possible that
5610 * an overlapping map exists in the tree
5612 if (ret
== -EEXIST
) {
5613 struct extent_map
*existing
;
5617 existing
= lookup_extent_mapping(em_tree
, start
, len
);
5618 if (existing
&& (existing
->start
> start
||
5619 existing
->start
+ existing
->len
<= start
)) {
5620 free_extent_map(existing
);
5624 existing
= lookup_extent_mapping(em_tree
, em
->start
,
5627 err
= merge_extent_mapping(em_tree
, existing
,
5630 free_extent_map(existing
);
5632 free_extent_map(em
);
5637 free_extent_map(em
);
5641 free_extent_map(em
);
5646 write_unlock(&em_tree
->lock
);
5650 trace_btrfs_get_extent(root
, em
);
5653 btrfs_free_path(path
);
5655 ret
= btrfs_end_transaction(trans
, root
);
5660 free_extent_map(em
);
5661 return ERR_PTR(err
);
5663 BUG_ON(!em
); /* Error is always set */
5667 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
5668 size_t pg_offset
, u64 start
, u64 len
,
5671 struct extent_map
*em
;
5672 struct extent_map
*hole_em
= NULL
;
5673 u64 range_start
= start
;
5679 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
5686 * - a pre-alloc extent,
5687 * there might actually be delalloc bytes behind it.
5689 if (em
->block_start
!= EXTENT_MAP_HOLE
&&
5690 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
5696 /* check to see if we've wrapped (len == -1 or similar) */
5705 /* ok, we didn't find anything, lets look for delalloc */
5706 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
5707 end
, len
, EXTENT_DELALLOC
, 1);
5708 found_end
= range_start
+ found
;
5709 if (found_end
< range_start
)
5710 found_end
= (u64
)-1;
5713 * we didn't find anything useful, return
5714 * the original results from get_extent()
5716 if (range_start
> end
|| found_end
<= start
) {
5722 /* adjust the range_start to make sure it doesn't
5723 * go backwards from the start they passed in
5725 range_start
= max(start
,range_start
);
5726 found
= found_end
- range_start
;
5729 u64 hole_start
= start
;
5732 em
= alloc_extent_map();
5738 * when btrfs_get_extent can't find anything it
5739 * returns one huge hole
5741 * make sure what it found really fits our range, and
5742 * adjust to make sure it is based on the start from
5746 u64 calc_end
= extent_map_end(hole_em
);
5748 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
5749 free_extent_map(hole_em
);
5752 hole_start
= max(hole_em
->start
, start
);
5753 hole_len
= calc_end
- hole_start
;
5757 if (hole_em
&& range_start
> hole_start
) {
5758 /* our hole starts before our delalloc, so we
5759 * have to return just the parts of the hole
5760 * that go until the delalloc starts
5762 em
->len
= min(hole_len
,
5763 range_start
- hole_start
);
5764 em
->start
= hole_start
;
5765 em
->orig_start
= hole_start
;
5767 * don't adjust block start at all,
5768 * it is fixed at EXTENT_MAP_HOLE
5770 em
->block_start
= hole_em
->block_start
;
5771 em
->block_len
= hole_len
;
5772 if (test_bit(EXTENT_FLAG_PREALLOC
, &hole_em
->flags
))
5773 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
5775 em
->start
= range_start
;
5777 em
->orig_start
= range_start
;
5778 em
->block_start
= EXTENT_MAP_DELALLOC
;
5779 em
->block_len
= found
;
5781 } else if (hole_em
) {
5786 free_extent_map(hole_em
);
5788 free_extent_map(em
);
5789 return ERR_PTR(err
);
5794 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
5797 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5798 struct btrfs_trans_handle
*trans
;
5799 struct extent_map
*em
;
5800 struct btrfs_key ins
;
5804 trans
= btrfs_join_transaction(root
);
5806 return ERR_CAST(trans
);
5808 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
5810 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
5811 ret
= btrfs_reserve_extent(trans
, root
, len
, root
->sectorsize
, 0,
5812 alloc_hint
, &ins
, 1);
5818 em
= create_pinned_em(inode
, start
, ins
.offset
, start
, ins
.objectid
,
5819 ins
.offset
, ins
.offset
, 0);
5823 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
5824 ins
.offset
, ins
.offset
, 0);
5826 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
5830 btrfs_end_transaction(trans
, root
);
5835 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5836 * block must be cow'd
5838 static noinline
int can_nocow_odirect(struct btrfs_trans_handle
*trans
,
5839 struct inode
*inode
, u64 offset
, u64 len
)
5841 struct btrfs_path
*path
;
5843 struct extent_buffer
*leaf
;
5844 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5845 struct btrfs_file_extent_item
*fi
;
5846 struct btrfs_key key
;
5854 path
= btrfs_alloc_path();
5858 ret
= btrfs_lookup_file_extent(trans
, root
, path
, btrfs_ino(inode
),
5863 slot
= path
->slots
[0];
5866 /* can't find the item, must cow */
5873 leaf
= path
->nodes
[0];
5874 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
5875 if (key
.objectid
!= btrfs_ino(inode
) ||
5876 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
5877 /* not our file or wrong item type, must cow */
5881 if (key
.offset
> offset
) {
5882 /* Wrong offset, must cow */
5886 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
5887 found_type
= btrfs_file_extent_type(leaf
, fi
);
5888 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
5889 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
5890 /* not a regular extent, must cow */
5893 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
5894 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
5896 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
5897 if (extent_end
< offset
+ len
) {
5898 /* extent doesn't include our full range, must cow */
5902 if (btrfs_extent_readonly(root
, disk_bytenr
))
5906 * look for other files referencing this extent, if we
5907 * find any we must cow
5909 if (btrfs_cross_ref_exist(trans
, root
, btrfs_ino(inode
),
5910 key
.offset
- backref_offset
, disk_bytenr
))
5914 * adjust disk_bytenr and num_bytes to cover just the bytes
5915 * in this extent we are about to write. If there
5916 * are any csums in that range we have to cow in order
5917 * to keep the csums correct
5919 disk_bytenr
+= backref_offset
;
5920 disk_bytenr
+= offset
- key
.offset
;
5921 num_bytes
= min(offset
+ len
, extent_end
) - offset
;
5922 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
5925 * all of the above have passed, it is safe to overwrite this extent
5930 btrfs_free_path(path
);
5934 static int lock_extent_direct(struct inode
*inode
, u64 lockstart
, u64 lockend
,
5935 struct extent_state
**cached_state
, int writing
)
5937 struct btrfs_ordered_extent
*ordered
;
5941 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
5944 * We're concerned with the entire range that we're going to be
5945 * doing DIO to, so we need to make sure theres no ordered
5946 * extents in this range.
5948 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
5949 lockend
- lockstart
+ 1);
5952 * We need to make sure there are no buffered pages in this
5953 * range either, we could have raced between the invalidate in
5954 * generic_file_direct_write and locking the extent. The
5955 * invalidate needs to happen so that reads after a write do not
5958 if (!ordered
&& (!writing
||
5959 !test_range_bit(&BTRFS_I(inode
)->io_tree
,
5960 lockstart
, lockend
, EXTENT_UPTODATE
, 0,
5964 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
5965 cached_state
, GFP_NOFS
);
5968 btrfs_start_ordered_extent(inode
, ordered
, 1);
5969 btrfs_put_ordered_extent(ordered
);
5971 /* Screw you mmap */
5972 ret
= filemap_write_and_wait_range(inode
->i_mapping
,
5979 * If we found a page that couldn't be invalidated just
5980 * fall back to buffered.
5982 ret
= invalidate_inode_pages2_range(inode
->i_mapping
,
5983 lockstart
>> PAGE_CACHE_SHIFT
,
5984 lockend
>> PAGE_CACHE_SHIFT
);
5995 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
5996 u64 len
, u64 orig_start
,
5997 u64 block_start
, u64 block_len
,
5998 u64 orig_block_len
, int type
)
6000 struct extent_map_tree
*em_tree
;
6001 struct extent_map
*em
;
6002 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6005 em_tree
= &BTRFS_I(inode
)->extent_tree
;
6006 em
= alloc_extent_map();
6008 return ERR_PTR(-ENOMEM
);
6011 em
->orig_start
= orig_start
;
6012 em
->mod_start
= start
;
6015 em
->block_len
= block_len
;
6016 em
->block_start
= block_start
;
6017 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6018 em
->orig_block_len
= orig_block_len
;
6019 em
->generation
= -1;
6020 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
6021 if (type
== BTRFS_ORDERED_PREALLOC
)
6022 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
6025 btrfs_drop_extent_cache(inode
, em
->start
,
6026 em
->start
+ em
->len
- 1, 0);
6027 write_lock(&em_tree
->lock
);
6028 ret
= add_extent_mapping(em_tree
, em
);
6030 list_move(&em
->list
,
6031 &em_tree
->modified_extents
);
6032 write_unlock(&em_tree
->lock
);
6033 } while (ret
== -EEXIST
);
6036 free_extent_map(em
);
6037 return ERR_PTR(ret
);
6044 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
6045 struct buffer_head
*bh_result
, int create
)
6047 struct extent_map
*em
;
6048 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6049 struct extent_state
*cached_state
= NULL
;
6050 u64 start
= iblock
<< inode
->i_blkbits
;
6051 u64 lockstart
, lockend
;
6052 u64 len
= bh_result
->b_size
;
6053 struct btrfs_trans_handle
*trans
;
6054 int unlock_bits
= EXTENT_LOCKED
;
6058 spin_lock(&BTRFS_I(inode
)->lock
);
6059 BTRFS_I(inode
)->outstanding_extents
++;
6060 spin_unlock(&BTRFS_I(inode
)->lock
);
6061 unlock_bits
|= EXTENT_DELALLOC
| EXTENT_DIRTY
;
6063 len
= min_t(u64
, len
, root
->sectorsize
);
6066 lockend
= start
+ len
- 1;
6069 * If this errors out it's because we couldn't invalidate pagecache for
6070 * this range and we need to fallback to buffered.
6072 if (lock_extent_direct(inode
, lockstart
, lockend
, &cached_state
, create
))
6075 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
6082 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6083 * io. INLINE is special, and we could probably kludge it in here, but
6084 * it's still buffered so for safety lets just fall back to the generic
6087 * For COMPRESSED we _have_ to read the entire extent in so we can
6088 * decompress it, so there will be buffering required no matter what we
6089 * do, so go ahead and fallback to buffered.
6091 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6092 * to buffered IO. Don't blame me, this is the price we pay for using
6095 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
6096 em
->block_start
== EXTENT_MAP_INLINE
) {
6097 free_extent_map(em
);
6102 /* Just a good old fashioned hole, return */
6103 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
6104 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
6105 free_extent_map(em
);
6110 * We don't allocate a new extent in the following cases
6112 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6114 * 2) The extent is marked as PREALLOC. We're good to go here and can
6115 * just use the extent.
6119 len
= min(len
, em
->len
- (start
- em
->start
));
6120 lockstart
= start
+ len
;
6124 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
6125 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
6126 em
->block_start
!= EXTENT_MAP_HOLE
)) {
6131 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6132 type
= BTRFS_ORDERED_PREALLOC
;
6134 type
= BTRFS_ORDERED_NOCOW
;
6135 len
= min(len
, em
->len
- (start
- em
->start
));
6136 block_start
= em
->block_start
+ (start
- em
->start
);
6139 * we're not going to log anything, but we do need
6140 * to make sure the current transaction stays open
6141 * while we look for nocow cross refs
6143 trans
= btrfs_join_transaction(root
);
6147 if (can_nocow_odirect(trans
, inode
, start
, len
) == 1) {
6148 u64 orig_start
= em
->orig_start
;
6149 u64 orig_block_len
= em
->orig_block_len
;
6151 if (type
== BTRFS_ORDERED_PREALLOC
) {
6152 free_extent_map(em
);
6153 em
= create_pinned_em(inode
, start
, len
,
6156 orig_block_len
, type
);
6158 btrfs_end_transaction(trans
, root
);
6163 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
6164 block_start
, len
, len
, type
);
6165 btrfs_end_transaction(trans
, root
);
6167 free_extent_map(em
);
6172 btrfs_end_transaction(trans
, root
);
6176 * this will cow the extent, reset the len in case we changed
6179 len
= bh_result
->b_size
;
6180 free_extent_map(em
);
6181 em
= btrfs_new_extent_direct(inode
, start
, len
);
6186 len
= min(len
, em
->len
- (start
- em
->start
));
6188 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
6190 bh_result
->b_size
= len
;
6191 bh_result
->b_bdev
= em
->bdev
;
6192 set_buffer_mapped(bh_result
);
6194 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6195 set_buffer_new(bh_result
);
6198 * Need to update the i_size under the extent lock so buffered
6199 * readers will get the updated i_size when we unlock.
6201 if (start
+ len
> i_size_read(inode
))
6202 i_size_write(inode
, start
+ len
);
6204 ret
= set_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6205 lockstart
+ len
- 1, EXTENT_DELALLOC
, NULL
,
6206 &cached_state
, GFP_NOFS
);
6211 * In the case of write we need to clear and unlock the entire range,
6212 * in the case of read we need to unlock only the end area that we
6213 * aren't using if there is any left over space.
6215 if (lockstart
< lockend
) {
6216 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6217 lockend
, unlock_bits
, 1, 0,
6218 &cached_state
, GFP_NOFS
);
6220 free_extent_state(cached_state
);
6223 free_extent_map(em
);
6228 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6229 unlock_bits
, 1, 0, &cached_state
, GFP_NOFS
);
6233 struct btrfs_dio_private
{
6234 struct inode
*inode
;
6240 /* number of bios pending for this dio */
6241 atomic_t pending_bios
;
6246 struct bio
*orig_bio
;
6249 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
6251 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6252 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
6253 struct bio_vec
*bvec
= bio
->bi_io_vec
;
6254 struct inode
*inode
= dip
->inode
;
6255 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6258 start
= dip
->logical_offset
;
6260 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
6261 struct page
*page
= bvec
->bv_page
;
6264 u64
private = ~(u32
)0;
6265 unsigned long flags
;
6267 if (get_state_private(&BTRFS_I(inode
)->io_tree
,
6270 local_irq_save(flags
);
6271 kaddr
= kmap_atomic(page
);
6272 csum
= btrfs_csum_data(root
, kaddr
+ bvec
->bv_offset
,
6273 csum
, bvec
->bv_len
);
6274 btrfs_csum_final(csum
, (char *)&csum
);
6275 kunmap_atomic(kaddr
);
6276 local_irq_restore(flags
);
6278 flush_dcache_page(bvec
->bv_page
);
6279 if (csum
!= private) {
6281 printk(KERN_ERR
"btrfs csum failed ino %llu off"
6282 " %llu csum %u private %u\n",
6283 (unsigned long long)btrfs_ino(inode
),
6284 (unsigned long long)start
,
6285 csum
, (unsigned)private);
6290 start
+= bvec
->bv_len
;
6292 } while (bvec
<= bvec_end
);
6294 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
6295 dip
->logical_offset
+ dip
->bytes
- 1);
6296 bio
->bi_private
= dip
->private;
6300 /* If we had a csum failure make sure to clear the uptodate flag */
6302 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
6303 dio_end_io(bio
, err
);
6306 static void btrfs_endio_direct_write(struct bio
*bio
, int err
)
6308 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6309 struct inode
*inode
= dip
->inode
;
6310 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6311 struct btrfs_ordered_extent
*ordered
= NULL
;
6312 u64 ordered_offset
= dip
->logical_offset
;
6313 u64 ordered_bytes
= dip
->bytes
;
6319 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
6321 ordered_bytes
, !err
);
6325 ordered
->work
.func
= finish_ordered_fn
;
6326 ordered
->work
.flags
= 0;
6327 btrfs_queue_worker(&root
->fs_info
->endio_write_workers
,
6331 * our bio might span multiple ordered extents. If we haven't
6332 * completed the accounting for the whole dio, go back and try again
6334 if (ordered_offset
< dip
->logical_offset
+ dip
->bytes
) {
6335 ordered_bytes
= dip
->logical_offset
+ dip
->bytes
-
6341 bio
->bi_private
= dip
->private;
6345 /* If we had an error make sure to clear the uptodate flag */
6347 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
6348 dio_end_io(bio
, err
);
6351 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
6352 struct bio
*bio
, int mirror_num
,
6353 unsigned long bio_flags
, u64 offset
)
6356 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6357 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
6358 BUG_ON(ret
); /* -ENOMEM */
6362 static void btrfs_end_dio_bio(struct bio
*bio
, int err
)
6364 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6367 printk(KERN_ERR
"btrfs direct IO failed ino %llu rw %lu "
6368 "sector %#Lx len %u err no %d\n",
6369 (unsigned long long)btrfs_ino(dip
->inode
), bio
->bi_rw
,
6370 (unsigned long long)bio
->bi_sector
, bio
->bi_size
, err
);
6374 * before atomic variable goto zero, we must make sure
6375 * dip->errors is perceived to be set.
6377 smp_mb__before_atomic_dec();
6380 /* if there are more bios still pending for this dio, just exit */
6381 if (!atomic_dec_and_test(&dip
->pending_bios
))
6385 bio_io_error(dip
->orig_bio
);
6387 set_bit(BIO_UPTODATE
, &dip
->orig_bio
->bi_flags
);
6388 bio_endio(dip
->orig_bio
, 0);
6394 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
6395 u64 first_sector
, gfp_t gfp_flags
)
6397 int nr_vecs
= bio_get_nr_vecs(bdev
);
6398 return btrfs_bio_alloc(bdev
, first_sector
, nr_vecs
, gfp_flags
);
6401 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
6402 int rw
, u64 file_offset
, int skip_sum
,
6405 int write
= rw
& REQ_WRITE
;
6406 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6410 async_submit
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
6415 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
6423 if (write
&& async_submit
) {
6424 ret
= btrfs_wq_submit_bio(root
->fs_info
,
6425 inode
, rw
, bio
, 0, 0,
6427 __btrfs_submit_bio_start_direct_io
,
6428 __btrfs_submit_bio_done
);
6432 * If we aren't doing async submit, calculate the csum of the
6435 ret
= btrfs_csum_one_bio(root
, inode
, bio
, file_offset
, 1);
6438 } else if (!skip_sum
) {
6439 ret
= btrfs_lookup_bio_sums_dio(root
, inode
, bio
, file_offset
);
6445 ret
= btrfs_map_bio(root
, rw
, bio
, 0, async_submit
);
6451 static int btrfs_submit_direct_hook(int rw
, struct btrfs_dio_private
*dip
,
6454 struct inode
*inode
= dip
->inode
;
6455 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6457 struct bio
*orig_bio
= dip
->orig_bio
;
6458 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
6459 u64 start_sector
= orig_bio
->bi_sector
;
6460 u64 file_offset
= dip
->logical_offset
;
6465 int async_submit
= 0;
6467 map_length
= orig_bio
->bi_size
;
6468 ret
= btrfs_map_block(root
->fs_info
, rw
, start_sector
<< 9,
6469 &map_length
, NULL
, 0);
6474 if (map_length
>= orig_bio
->bi_size
) {
6479 /* async crcs make it difficult to collect full stripe writes. */
6480 if (btrfs_get_alloc_profile(root
, 1) &
6481 (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
))
6486 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
6489 bio
->bi_private
= dip
;
6490 bio
->bi_end_io
= btrfs_end_dio_bio
;
6491 atomic_inc(&dip
->pending_bios
);
6493 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
6494 if (unlikely(map_length
< submit_len
+ bvec
->bv_len
||
6495 bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
6496 bvec
->bv_offset
) < bvec
->bv_len
)) {
6498 * inc the count before we submit the bio so
6499 * we know the end IO handler won't happen before
6500 * we inc the count. Otherwise, the dip might get freed
6501 * before we're done setting it up
6503 atomic_inc(&dip
->pending_bios
);
6504 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
,
6505 file_offset
, skip_sum
,
6509 atomic_dec(&dip
->pending_bios
);
6513 start_sector
+= submit_len
>> 9;
6514 file_offset
+= submit_len
;
6519 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
6520 start_sector
, GFP_NOFS
);
6523 bio
->bi_private
= dip
;
6524 bio
->bi_end_io
= btrfs_end_dio_bio
;
6526 map_length
= orig_bio
->bi_size
;
6527 ret
= btrfs_map_block(root
->fs_info
, rw
,
6529 &map_length
, NULL
, 0);
6535 submit_len
+= bvec
->bv_len
;
6542 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
, file_offset
, skip_sum
,
6551 * before atomic variable goto zero, we must
6552 * make sure dip->errors is perceived to be set.
6554 smp_mb__before_atomic_dec();
6555 if (atomic_dec_and_test(&dip
->pending_bios
))
6556 bio_io_error(dip
->orig_bio
);
6558 /* bio_end_io() will handle error, so we needn't return it */
6562 static void btrfs_submit_direct(int rw
, struct bio
*bio
, struct inode
*inode
,
6565 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6566 struct btrfs_dio_private
*dip
;
6567 struct bio_vec
*bvec
= bio
->bi_io_vec
;
6569 int write
= rw
& REQ_WRITE
;
6572 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
6574 dip
= kmalloc(sizeof(*dip
), GFP_NOFS
);
6580 dip
->private = bio
->bi_private
;
6582 dip
->logical_offset
= file_offset
;
6586 dip
->bytes
+= bvec
->bv_len
;
6588 } while (bvec
<= (bio
->bi_io_vec
+ bio
->bi_vcnt
- 1));
6590 dip
->disk_bytenr
= (u64
)bio
->bi_sector
<< 9;
6591 bio
->bi_private
= dip
;
6593 dip
->orig_bio
= bio
;
6594 atomic_set(&dip
->pending_bios
, 0);
6597 bio
->bi_end_io
= btrfs_endio_direct_write
;
6599 bio
->bi_end_io
= btrfs_endio_direct_read
;
6601 ret
= btrfs_submit_direct_hook(rw
, dip
, skip_sum
);
6606 * If this is a write, we need to clean up the reserved space and kill
6607 * the ordered extent.
6610 struct btrfs_ordered_extent
*ordered
;
6611 ordered
= btrfs_lookup_ordered_extent(inode
, file_offset
);
6612 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
6613 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
6614 btrfs_free_reserved_extent(root
, ordered
->start
,
6616 btrfs_put_ordered_extent(ordered
);
6617 btrfs_put_ordered_extent(ordered
);
6619 bio_endio(bio
, ret
);
6622 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
6623 const struct iovec
*iov
, loff_t offset
,
6624 unsigned long nr_segs
)
6630 unsigned blocksize_mask
= root
->sectorsize
- 1;
6631 ssize_t retval
= -EINVAL
;
6632 loff_t end
= offset
;
6634 if (offset
& blocksize_mask
)
6637 /* Check the memory alignment. Blocks cannot straddle pages */
6638 for (seg
= 0; seg
< nr_segs
; seg
++) {
6639 addr
= (unsigned long)iov
[seg
].iov_base
;
6640 size
= iov
[seg
].iov_len
;
6642 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
6645 /* If this is a write we don't need to check anymore */
6650 * Check to make sure we don't have duplicate iov_base's in this
6651 * iovec, if so return EINVAL, otherwise we'll get csum errors
6652 * when reading back.
6654 for (i
= seg
+ 1; i
< nr_segs
; i
++) {
6655 if (iov
[seg
].iov_base
== iov
[i
].iov_base
)
6664 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
6665 const struct iovec
*iov
, loff_t offset
,
6666 unsigned long nr_segs
)
6668 struct file
*file
= iocb
->ki_filp
;
6669 struct inode
*inode
= file
->f_mapping
->host
;
6673 bool relock
= false;
6676 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iov
,
6680 atomic_inc(&inode
->i_dio_count
);
6681 smp_mb__after_atomic_inc();
6684 count
= iov_length(iov
, nr_segs
);
6686 * If the write DIO is beyond the EOF, we need update
6687 * the isize, but it is protected by i_mutex. So we can
6688 * not unlock the i_mutex at this case.
6690 if (offset
+ count
<= inode
->i_size
) {
6691 mutex_unlock(&inode
->i_mutex
);
6694 ret
= btrfs_delalloc_reserve_space(inode
, count
);
6697 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK
,
6698 &BTRFS_I(inode
)->runtime_flags
))) {
6699 inode_dio_done(inode
);
6700 flags
= DIO_LOCKING
| DIO_SKIP_HOLES
;
6704 ret
= __blockdev_direct_IO(rw
, iocb
, inode
,
6705 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
6706 iov
, offset
, nr_segs
, btrfs_get_blocks_direct
, NULL
,
6707 btrfs_submit_direct
, flags
);
6709 if (ret
< 0 && ret
!= -EIOCBQUEUED
)
6710 btrfs_delalloc_release_space(inode
, count
);
6711 else if (ret
> 0 && (size_t)ret
< count
) {
6712 spin_lock(&BTRFS_I(inode
)->lock
);
6713 BTRFS_I(inode
)->outstanding_extents
++;
6714 spin_unlock(&BTRFS_I(inode
)->lock
);
6715 btrfs_delalloc_release_space(inode
,
6716 count
- (size_t)ret
);
6718 btrfs_delalloc_release_metadata(inode
, 0);
6722 inode_dio_done(inode
);
6724 mutex_lock(&inode
->i_mutex
);
6729 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
6731 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
6732 __u64 start
, __u64 len
)
6736 ret
= fiemap_check_flags(fieinfo
, BTRFS_FIEMAP_FLAGS
);
6740 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
6743 int btrfs_readpage(struct file
*file
, struct page
*page
)
6745 struct extent_io_tree
*tree
;
6746 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6747 return extent_read_full_page(tree
, page
, btrfs_get_extent
, 0);
6750 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
6752 struct extent_io_tree
*tree
;
6755 if (current
->flags
& PF_MEMALLOC
) {
6756 redirty_page_for_writepage(wbc
, page
);
6760 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6761 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
6764 int btrfs_writepages(struct address_space
*mapping
,
6765 struct writeback_control
*wbc
)
6767 struct extent_io_tree
*tree
;
6769 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
6770 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
6774 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
6775 struct list_head
*pages
, unsigned nr_pages
)
6777 struct extent_io_tree
*tree
;
6778 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
6779 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
6782 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
6784 struct extent_io_tree
*tree
;
6785 struct extent_map_tree
*map
;
6788 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6789 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
6790 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
6792 ClearPagePrivate(page
);
6793 set_page_private(page
, 0);
6794 page_cache_release(page
);
6799 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
6801 if (PageWriteback(page
) || PageDirty(page
))
6803 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
6806 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
6808 struct inode
*inode
= page
->mapping
->host
;
6809 struct extent_io_tree
*tree
;
6810 struct btrfs_ordered_extent
*ordered
;
6811 struct extent_state
*cached_state
= NULL
;
6812 u64 page_start
= page_offset(page
);
6813 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6816 * we have the page locked, so new writeback can't start,
6817 * and the dirty bit won't be cleared while we are here.
6819 * Wait for IO on this page so that we can safely clear
6820 * the PagePrivate2 bit and do ordered accounting
6822 wait_on_page_writeback(page
);
6824 tree
= &BTRFS_I(inode
)->io_tree
;
6826 btrfs_releasepage(page
, GFP_NOFS
);
6829 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
6830 ordered
= btrfs_lookup_ordered_extent(inode
, page_offset(page
));
6833 * IO on this page will never be started, so we need
6834 * to account for any ordered extents now
6836 clear_extent_bit(tree
, page_start
, page_end
,
6837 EXTENT_DIRTY
| EXTENT_DELALLOC
|
6838 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
6839 EXTENT_DEFRAG
, 1, 0, &cached_state
, GFP_NOFS
);
6841 * whoever cleared the private bit is responsible
6842 * for the finish_ordered_io
6844 if (TestClearPagePrivate2(page
) &&
6845 btrfs_dec_test_ordered_pending(inode
, &ordered
, page_start
,
6846 PAGE_CACHE_SIZE
, 1)) {
6847 btrfs_finish_ordered_io(ordered
);
6849 btrfs_put_ordered_extent(ordered
);
6850 cached_state
= NULL
;
6851 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
6853 clear_extent_bit(tree
, page_start
, page_end
,
6854 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
6855 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 1, 1,
6856 &cached_state
, GFP_NOFS
);
6857 __btrfs_releasepage(page
, GFP_NOFS
);
6859 ClearPageChecked(page
);
6860 if (PagePrivate(page
)) {
6861 ClearPagePrivate(page
);
6862 set_page_private(page
, 0);
6863 page_cache_release(page
);
6868 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6869 * called from a page fault handler when a page is first dirtied. Hence we must
6870 * be careful to check for EOF conditions here. We set the page up correctly
6871 * for a written page which means we get ENOSPC checking when writing into
6872 * holes and correct delalloc and unwritten extent mapping on filesystems that
6873 * support these features.
6875 * We are not allowed to take the i_mutex here so we have to play games to
6876 * protect against truncate races as the page could now be beyond EOF. Because
6877 * vmtruncate() writes the inode size before removing pages, once we have the
6878 * page lock we can determine safely if the page is beyond EOF. If it is not
6879 * beyond EOF, then the page is guaranteed safe against truncation until we
6882 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
6884 struct page
*page
= vmf
->page
;
6885 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
6886 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6887 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6888 struct btrfs_ordered_extent
*ordered
;
6889 struct extent_state
*cached_state
= NULL
;
6891 unsigned long zero_start
;
6898 sb_start_pagefault(inode
->i_sb
);
6899 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
6901 ret
= file_update_time(vma
->vm_file
);
6907 else /* -ENOSPC, -EIO, etc */
6908 ret
= VM_FAULT_SIGBUS
;
6914 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
6917 size
= i_size_read(inode
);
6918 page_start
= page_offset(page
);
6919 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6921 if ((page
->mapping
!= inode
->i_mapping
) ||
6922 (page_start
>= size
)) {
6923 /* page got truncated out from underneath us */
6926 wait_on_page_writeback(page
);
6928 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
6929 set_page_extent_mapped(page
);
6932 * we can't set the delalloc bits if there are pending ordered
6933 * extents. Drop our locks and wait for them to finish
6935 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
6937 unlock_extent_cached(io_tree
, page_start
, page_end
,
6938 &cached_state
, GFP_NOFS
);
6940 btrfs_start_ordered_extent(inode
, ordered
, 1);
6941 btrfs_put_ordered_extent(ordered
);
6946 * XXX - page_mkwrite gets called every time the page is dirtied, even
6947 * if it was already dirty, so for space accounting reasons we need to
6948 * clear any delalloc bits for the range we are fixing to save. There
6949 * is probably a better way to do this, but for now keep consistent with
6950 * prepare_pages in the normal write path.
6952 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
6953 EXTENT_DIRTY
| EXTENT_DELALLOC
|
6954 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
6955 0, 0, &cached_state
, GFP_NOFS
);
6957 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
6960 unlock_extent_cached(io_tree
, page_start
, page_end
,
6961 &cached_state
, GFP_NOFS
);
6962 ret
= VM_FAULT_SIGBUS
;
6967 /* page is wholly or partially inside EOF */
6968 if (page_start
+ PAGE_CACHE_SIZE
> size
)
6969 zero_start
= size
& ~PAGE_CACHE_MASK
;
6971 zero_start
= PAGE_CACHE_SIZE
;
6973 if (zero_start
!= PAGE_CACHE_SIZE
) {
6975 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
6976 flush_dcache_page(page
);
6979 ClearPageChecked(page
);
6980 set_page_dirty(page
);
6981 SetPageUptodate(page
);
6983 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
6984 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
6985 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->root
->last_log_commit
;
6987 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
6991 sb_end_pagefault(inode
->i_sb
);
6992 return VM_FAULT_LOCKED
;
6996 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
6998 sb_end_pagefault(inode
->i_sb
);
7002 static int btrfs_truncate(struct inode
*inode
)
7004 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7005 struct btrfs_block_rsv
*rsv
;
7008 struct btrfs_trans_handle
*trans
;
7009 u64 mask
= root
->sectorsize
- 1;
7010 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
7012 ret
= btrfs_truncate_page(inode
, inode
->i_size
, 0, 0);
7016 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
7017 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
7020 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7021 * 3 things going on here
7023 * 1) We need to reserve space for our orphan item and the space to
7024 * delete our orphan item. Lord knows we don't want to have a dangling
7025 * orphan item because we didn't reserve space to remove it.
7027 * 2) We need to reserve space to update our inode.
7029 * 3) We need to have something to cache all the space that is going to
7030 * be free'd up by the truncate operation, but also have some slack
7031 * space reserved in case it uses space during the truncate (thank you
7032 * very much snapshotting).
7034 * And we need these to all be seperate. The fact is we can use alot of
7035 * space doing the truncate, and we have no earthly idea how much space
7036 * we will use, so we need the truncate reservation to be seperate so it
7037 * doesn't end up using space reserved for updating the inode or
7038 * removing the orphan item. We also need to be able to stop the
7039 * transaction and start a new one, which means we need to be able to
7040 * update the inode several times, and we have no idea of knowing how
7041 * many times that will be, so we can't just reserve 1 item for the
7042 * entirety of the opration, so that has to be done seperately as well.
7043 * Then there is the orphan item, which does indeed need to be held on
7044 * to for the whole operation, and we need nobody to touch this reserved
7045 * space except the orphan code.
7047 * So that leaves us with
7049 * 1) root->orphan_block_rsv - for the orphan deletion.
7050 * 2) rsv - for the truncate reservation, which we will steal from the
7051 * transaction reservation.
7052 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7053 * updating the inode.
7055 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
7058 rsv
->size
= min_size
;
7062 * 1 for the truncate slack space
7063 * 1 for updating the inode.
7065 trans
= btrfs_start_transaction(root
, 2);
7066 if (IS_ERR(trans
)) {
7067 err
= PTR_ERR(trans
);
7071 /* Migrate the slack space for the truncate to our reserve */
7072 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
, rsv
,
7077 * setattr is responsible for setting the ordered_data_close flag,
7078 * but that is only tested during the last file release. That
7079 * could happen well after the next commit, leaving a great big
7080 * window where new writes may get lost if someone chooses to write
7081 * to this file after truncating to zero
7083 * The inode doesn't have any dirty data here, and so if we commit
7084 * this is a noop. If someone immediately starts writing to the inode
7085 * it is very likely we'll catch some of their writes in this
7086 * transaction, and the commit will find this file on the ordered
7087 * data list with good things to send down.
7089 * This is a best effort solution, there is still a window where
7090 * using truncate to replace the contents of the file will
7091 * end up with a zero length file after a crash.
7093 if (inode
->i_size
== 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
7094 &BTRFS_I(inode
)->runtime_flags
))
7095 btrfs_add_ordered_operation(trans
, root
, inode
);
7098 * So if we truncate and then write and fsync we normally would just
7099 * write the extents that changed, which is a problem if we need to
7100 * first truncate that entire inode. So set this flag so we write out
7101 * all of the extents in the inode to the sync log so we're completely
7104 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
7105 trans
->block_rsv
= rsv
;
7108 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
7110 BTRFS_EXTENT_DATA_KEY
);
7111 if (ret
!= -ENOSPC
) {
7116 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7117 ret
= btrfs_update_inode(trans
, root
, inode
);
7123 btrfs_end_transaction(trans
, root
);
7124 btrfs_btree_balance_dirty(root
);
7126 trans
= btrfs_start_transaction(root
, 2);
7127 if (IS_ERR(trans
)) {
7128 ret
= err
= PTR_ERR(trans
);
7133 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
,
7135 BUG_ON(ret
); /* shouldn't happen */
7136 trans
->block_rsv
= rsv
;
7139 if (ret
== 0 && inode
->i_nlink
> 0) {
7140 trans
->block_rsv
= root
->orphan_block_rsv
;
7141 ret
= btrfs_orphan_del(trans
, inode
);
7147 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7148 ret
= btrfs_update_inode(trans
, root
, inode
);
7152 ret
= btrfs_end_transaction(trans
, root
);
7153 btrfs_btree_balance_dirty(root
);
7157 btrfs_free_block_rsv(root
, rsv
);
7166 * create a new subvolume directory/inode (helper for the ioctl).
7168 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
7169 struct btrfs_root
*new_root
, u64 new_dirid
)
7171 struct inode
*inode
;
7175 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
7176 new_dirid
, new_dirid
,
7177 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
7180 return PTR_ERR(inode
);
7181 inode
->i_op
= &btrfs_dir_inode_operations
;
7182 inode
->i_fop
= &btrfs_dir_file_operations
;
7184 set_nlink(inode
, 1);
7185 btrfs_i_size_write(inode
, 0);
7187 err
= btrfs_update_inode(trans
, new_root
, inode
);
7193 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
7195 struct btrfs_inode
*ei
;
7196 struct inode
*inode
;
7198 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
7205 ei
->last_sub_trans
= 0;
7206 ei
->logged_trans
= 0;
7207 ei
->delalloc_bytes
= 0;
7208 ei
->disk_i_size
= 0;
7211 ei
->index_cnt
= (u64
)-1;
7212 ei
->last_unlink_trans
= 0;
7213 ei
->last_log_commit
= 0;
7215 spin_lock_init(&ei
->lock
);
7216 ei
->outstanding_extents
= 0;
7217 ei
->reserved_extents
= 0;
7219 ei
->runtime_flags
= 0;
7220 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
7222 ei
->delayed_node
= NULL
;
7224 inode
= &ei
->vfs_inode
;
7225 extent_map_tree_init(&ei
->extent_tree
);
7226 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
);
7227 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
);
7228 ei
->io_tree
.track_uptodate
= 1;
7229 ei
->io_failure_tree
.track_uptodate
= 1;
7230 atomic_set(&ei
->sync_writers
, 0);
7231 mutex_init(&ei
->log_mutex
);
7232 mutex_init(&ei
->delalloc_mutex
);
7233 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
7234 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
7235 INIT_LIST_HEAD(&ei
->ordered_operations
);
7236 RB_CLEAR_NODE(&ei
->rb_node
);
7241 static void btrfs_i_callback(struct rcu_head
*head
)
7243 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
7244 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
7247 void btrfs_destroy_inode(struct inode
*inode
)
7249 struct btrfs_ordered_extent
*ordered
;
7250 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7252 WARN_ON(!hlist_empty(&inode
->i_dentry
));
7253 WARN_ON(inode
->i_data
.nrpages
);
7254 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
7255 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
7256 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
7257 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
7260 * This can happen where we create an inode, but somebody else also
7261 * created the same inode and we need to destroy the one we already
7268 * Make sure we're properly removed from the ordered operation
7272 if (!list_empty(&BTRFS_I(inode
)->ordered_operations
)) {
7273 spin_lock(&root
->fs_info
->ordered_extent_lock
);
7274 list_del_init(&BTRFS_I(inode
)->ordered_operations
);
7275 spin_unlock(&root
->fs_info
->ordered_extent_lock
);
7278 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
7279 &BTRFS_I(inode
)->runtime_flags
)) {
7280 printk(KERN_INFO
"BTRFS: inode %llu still on the orphan list\n",
7281 (unsigned long long)btrfs_ino(inode
));
7282 atomic_dec(&root
->orphan_inodes
);
7286 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
7290 printk(KERN_ERR
"btrfs found ordered "
7291 "extent %llu %llu on inode cleanup\n",
7292 (unsigned long long)ordered
->file_offset
,
7293 (unsigned long long)ordered
->len
);
7294 btrfs_remove_ordered_extent(inode
, ordered
);
7295 btrfs_put_ordered_extent(ordered
);
7296 btrfs_put_ordered_extent(ordered
);
7299 inode_tree_del(inode
);
7300 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
7302 btrfs_remove_delayed_node(inode
);
7303 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
7306 int btrfs_drop_inode(struct inode
*inode
)
7308 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7310 /* the snap/subvol tree is on deleting */
7311 if (btrfs_root_refs(&root
->root_item
) == 0 &&
7312 root
!= root
->fs_info
->tree_root
)
7315 return generic_drop_inode(inode
);
7318 static void init_once(void *foo
)
7320 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
7322 inode_init_once(&ei
->vfs_inode
);
7325 void btrfs_destroy_cachep(void)
7328 * Make sure all delayed rcu free inodes are flushed before we
7332 if (btrfs_inode_cachep
)
7333 kmem_cache_destroy(btrfs_inode_cachep
);
7334 if (btrfs_trans_handle_cachep
)
7335 kmem_cache_destroy(btrfs_trans_handle_cachep
);
7336 if (btrfs_transaction_cachep
)
7337 kmem_cache_destroy(btrfs_transaction_cachep
);
7338 if (btrfs_path_cachep
)
7339 kmem_cache_destroy(btrfs_path_cachep
);
7340 if (btrfs_free_space_cachep
)
7341 kmem_cache_destroy(btrfs_free_space_cachep
);
7342 if (btrfs_delalloc_work_cachep
)
7343 kmem_cache_destroy(btrfs_delalloc_work_cachep
);
7346 int btrfs_init_cachep(void)
7348 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode",
7349 sizeof(struct btrfs_inode
), 0,
7350 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
7351 if (!btrfs_inode_cachep
)
7354 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle",
7355 sizeof(struct btrfs_trans_handle
), 0,
7356 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7357 if (!btrfs_trans_handle_cachep
)
7360 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction",
7361 sizeof(struct btrfs_transaction
), 0,
7362 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7363 if (!btrfs_transaction_cachep
)
7366 btrfs_path_cachep
= kmem_cache_create("btrfs_path",
7367 sizeof(struct btrfs_path
), 0,
7368 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7369 if (!btrfs_path_cachep
)
7372 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space",
7373 sizeof(struct btrfs_free_space
), 0,
7374 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7375 if (!btrfs_free_space_cachep
)
7378 btrfs_delalloc_work_cachep
= kmem_cache_create("btrfs_delalloc_work",
7379 sizeof(struct btrfs_delalloc_work
), 0,
7380 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
,
7382 if (!btrfs_delalloc_work_cachep
)
7387 btrfs_destroy_cachep();
7391 static int btrfs_getattr(struct vfsmount
*mnt
,
7392 struct dentry
*dentry
, struct kstat
*stat
)
7395 struct inode
*inode
= dentry
->d_inode
;
7396 u32 blocksize
= inode
->i_sb
->s_blocksize
;
7398 generic_fillattr(inode
, stat
);
7399 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
7400 stat
->blksize
= PAGE_CACHE_SIZE
;
7402 spin_lock(&BTRFS_I(inode
)->lock
);
7403 delalloc_bytes
= BTRFS_I(inode
)->delalloc_bytes
;
7404 spin_unlock(&BTRFS_I(inode
)->lock
);
7405 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
7406 ALIGN(delalloc_bytes
, blocksize
)) >> 9;
7411 * If a file is moved, it will inherit the cow and compression flags of the new
7414 static void fixup_inode_flags(struct inode
*dir
, struct inode
*inode
)
7416 struct btrfs_inode
*b_dir
= BTRFS_I(dir
);
7417 struct btrfs_inode
*b_inode
= BTRFS_I(inode
);
7419 if (b_dir
->flags
& BTRFS_INODE_NODATACOW
)
7420 b_inode
->flags
|= BTRFS_INODE_NODATACOW
;
7422 b_inode
->flags
&= ~BTRFS_INODE_NODATACOW
;
7424 if (b_dir
->flags
& BTRFS_INODE_COMPRESS
) {
7425 b_inode
->flags
|= BTRFS_INODE_COMPRESS
;
7426 b_inode
->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
7428 b_inode
->flags
&= ~(BTRFS_INODE_COMPRESS
|
7429 BTRFS_INODE_NOCOMPRESS
);
7433 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
7434 struct inode
*new_dir
, struct dentry
*new_dentry
)
7436 struct btrfs_trans_handle
*trans
;
7437 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
7438 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
7439 struct inode
*new_inode
= new_dentry
->d_inode
;
7440 struct inode
*old_inode
= old_dentry
->d_inode
;
7441 struct timespec ctime
= CURRENT_TIME
;
7445 u64 old_ino
= btrfs_ino(old_inode
);
7447 if (btrfs_ino(new_dir
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
7450 /* we only allow rename subvolume link between subvolumes */
7451 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
7454 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
7455 (new_inode
&& btrfs_ino(new_inode
) == BTRFS_FIRST_FREE_OBJECTID
))
7458 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
7459 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
7463 /* check for collisions, even if the name isn't there */
7464 ret
= btrfs_check_dir_item_collision(root
, new_dir
->i_ino
,
7465 new_dentry
->d_name
.name
,
7466 new_dentry
->d_name
.len
);
7469 if (ret
== -EEXIST
) {
7471 * eexist without a new_inode */
7477 /* maybe -EOVERFLOW */
7484 * we're using rename to replace one file with another.
7485 * and the replacement file is large. Start IO on it now so
7486 * we don't add too much work to the end of the transaction
7488 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
&&
7489 old_inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
7490 filemap_flush(old_inode
->i_mapping
);
7492 /* close the racy window with snapshot create/destroy ioctl */
7493 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
7494 down_read(&root
->fs_info
->subvol_sem
);
7496 * We want to reserve the absolute worst case amount of items. So if
7497 * both inodes are subvols and we need to unlink them then that would
7498 * require 4 item modifications, but if they are both normal inodes it
7499 * would require 5 item modifications, so we'll assume their normal
7500 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7501 * should cover the worst case number of items we'll modify.
7503 trans
= btrfs_start_transaction(root
, 20);
7504 if (IS_ERR(trans
)) {
7505 ret
= PTR_ERR(trans
);
7510 btrfs_record_root_in_trans(trans
, dest
);
7512 ret
= btrfs_set_inode_index(new_dir
, &index
);
7516 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
7517 /* force full log commit if subvolume involved. */
7518 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
7520 ret
= btrfs_insert_inode_ref(trans
, dest
,
7521 new_dentry
->d_name
.name
,
7522 new_dentry
->d_name
.len
,
7524 btrfs_ino(new_dir
), index
);
7528 * this is an ugly little race, but the rename is required
7529 * to make sure that if we crash, the inode is either at the
7530 * old name or the new one. pinning the log transaction lets
7531 * us make sure we don't allow a log commit to come in after
7532 * we unlink the name but before we add the new name back in.
7534 btrfs_pin_log_trans(root
);
7537 * make sure the inode gets flushed if it is replacing
7540 if (new_inode
&& new_inode
->i_size
&& S_ISREG(old_inode
->i_mode
))
7541 btrfs_add_ordered_operation(trans
, root
, old_inode
);
7543 inode_inc_iversion(old_dir
);
7544 inode_inc_iversion(new_dir
);
7545 inode_inc_iversion(old_inode
);
7546 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
7547 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
7548 old_inode
->i_ctime
= ctime
;
7550 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
7551 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
7553 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
7554 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
7555 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
7556 old_dentry
->d_name
.name
,
7557 old_dentry
->d_name
.len
);
7559 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
7560 old_dentry
->d_inode
,
7561 old_dentry
->d_name
.name
,
7562 old_dentry
->d_name
.len
);
7564 ret
= btrfs_update_inode(trans
, root
, old_inode
);
7567 btrfs_abort_transaction(trans
, root
, ret
);
7572 inode_inc_iversion(new_inode
);
7573 new_inode
->i_ctime
= CURRENT_TIME
;
7574 if (unlikely(btrfs_ino(new_inode
) ==
7575 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
7576 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
7577 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
7579 new_dentry
->d_name
.name
,
7580 new_dentry
->d_name
.len
);
7581 BUG_ON(new_inode
->i_nlink
== 0);
7583 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
7584 new_dentry
->d_inode
,
7585 new_dentry
->d_name
.name
,
7586 new_dentry
->d_name
.len
);
7588 if (!ret
&& new_inode
->i_nlink
== 0) {
7589 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
7593 btrfs_abort_transaction(trans
, root
, ret
);
7598 fixup_inode_flags(new_dir
, old_inode
);
7600 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
7601 new_dentry
->d_name
.name
,
7602 new_dentry
->d_name
.len
, 0, index
);
7604 btrfs_abort_transaction(trans
, root
, ret
);
7608 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
7609 struct dentry
*parent
= new_dentry
->d_parent
;
7610 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
7611 btrfs_end_log_trans(root
);
7614 btrfs_end_transaction(trans
, root
);
7616 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
7617 up_read(&root
->fs_info
->subvol_sem
);
7622 static void btrfs_run_delalloc_work(struct btrfs_work
*work
)
7624 struct btrfs_delalloc_work
*delalloc_work
;
7626 delalloc_work
= container_of(work
, struct btrfs_delalloc_work
,
7628 if (delalloc_work
->wait
)
7629 btrfs_wait_ordered_range(delalloc_work
->inode
, 0, (u64
)-1);
7631 filemap_flush(delalloc_work
->inode
->i_mapping
);
7633 if (delalloc_work
->delay_iput
)
7634 btrfs_add_delayed_iput(delalloc_work
->inode
);
7636 iput(delalloc_work
->inode
);
7637 complete(&delalloc_work
->completion
);
7640 struct btrfs_delalloc_work
*btrfs_alloc_delalloc_work(struct inode
*inode
,
7641 int wait
, int delay_iput
)
7643 struct btrfs_delalloc_work
*work
;
7645 work
= kmem_cache_zalloc(btrfs_delalloc_work_cachep
, GFP_NOFS
);
7649 init_completion(&work
->completion
);
7650 INIT_LIST_HEAD(&work
->list
);
7651 work
->inode
= inode
;
7653 work
->delay_iput
= delay_iput
;
7654 work
->work
.func
= btrfs_run_delalloc_work
;
7659 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work
*work
)
7661 wait_for_completion(&work
->completion
);
7662 kmem_cache_free(btrfs_delalloc_work_cachep
, work
);
7666 * some fairly slow code that needs optimization. This walks the list
7667 * of all the inodes with pending delalloc and forces them to disk.
7669 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
7671 struct btrfs_inode
*binode
;
7672 struct inode
*inode
;
7673 struct btrfs_delalloc_work
*work
, *next
;
7674 struct list_head works
;
7675 struct list_head splice
;
7678 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
7681 INIT_LIST_HEAD(&works
);
7682 INIT_LIST_HEAD(&splice
);
7684 spin_lock(&root
->fs_info
->delalloc_lock
);
7685 list_splice_init(&root
->fs_info
->delalloc_inodes
, &splice
);
7686 while (!list_empty(&splice
)) {
7687 binode
= list_entry(splice
.next
, struct btrfs_inode
,
7690 list_del_init(&binode
->delalloc_inodes
);
7692 inode
= igrab(&binode
->vfs_inode
);
7694 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
7695 &binode
->runtime_flags
);
7699 list_add_tail(&binode
->delalloc_inodes
,
7700 &root
->fs_info
->delalloc_inodes
);
7701 spin_unlock(&root
->fs_info
->delalloc_lock
);
7703 work
= btrfs_alloc_delalloc_work(inode
, 0, delay_iput
);
7704 if (unlikely(!work
)) {
7708 list_add_tail(&work
->list
, &works
);
7709 btrfs_queue_worker(&root
->fs_info
->flush_workers
,
7713 spin_lock(&root
->fs_info
->delalloc_lock
);
7715 spin_unlock(&root
->fs_info
->delalloc_lock
);
7717 list_for_each_entry_safe(work
, next
, &works
, list
) {
7718 list_del_init(&work
->list
);
7719 btrfs_wait_and_free_delalloc_work(work
);
7722 /* the filemap_flush will queue IO into the worker threads, but
7723 * we have to make sure the IO is actually started and that
7724 * ordered extents get created before we return
7726 atomic_inc(&root
->fs_info
->async_submit_draining
);
7727 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
7728 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
7729 wait_event(root
->fs_info
->async_submit_wait
,
7730 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
7731 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
7733 atomic_dec(&root
->fs_info
->async_submit_draining
);
7736 list_for_each_entry_safe(work
, next
, &works
, list
) {
7737 list_del_init(&work
->list
);
7738 btrfs_wait_and_free_delalloc_work(work
);
7741 if (!list_empty_careful(&splice
)) {
7742 spin_lock(&root
->fs_info
->delalloc_lock
);
7743 list_splice_tail(&splice
, &root
->fs_info
->delalloc_inodes
);
7744 spin_unlock(&root
->fs_info
->delalloc_lock
);
7749 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
7750 const char *symname
)
7752 struct btrfs_trans_handle
*trans
;
7753 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
7754 struct btrfs_path
*path
;
7755 struct btrfs_key key
;
7756 struct inode
*inode
= NULL
;
7764 struct btrfs_file_extent_item
*ei
;
7765 struct extent_buffer
*leaf
;
7767 name_len
= strlen(symname
) + 1;
7768 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
7769 return -ENAMETOOLONG
;
7772 * 2 items for inode item and ref
7773 * 2 items for dir items
7774 * 1 item for xattr if selinux is on
7776 trans
= btrfs_start_transaction(root
, 5);
7778 return PTR_ERR(trans
);
7780 err
= btrfs_find_free_ino(root
, &objectid
);
7784 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
7785 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
7786 S_IFLNK
|S_IRWXUGO
, &index
);
7787 if (IS_ERR(inode
)) {
7788 err
= PTR_ERR(inode
);
7792 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
7799 * If the active LSM wants to access the inode during
7800 * d_instantiate it needs these. Smack checks to see
7801 * if the filesystem supports xattrs by looking at the
7804 inode
->i_fop
= &btrfs_file_operations
;
7805 inode
->i_op
= &btrfs_file_inode_operations
;
7807 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
7811 inode
->i_mapping
->a_ops
= &btrfs_aops
;
7812 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
7813 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
7818 path
= btrfs_alloc_path();
7824 key
.objectid
= btrfs_ino(inode
);
7826 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
7827 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
7828 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
7832 btrfs_free_path(path
);
7835 leaf
= path
->nodes
[0];
7836 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
7837 struct btrfs_file_extent_item
);
7838 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
7839 btrfs_set_file_extent_type(leaf
, ei
,
7840 BTRFS_FILE_EXTENT_INLINE
);
7841 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
7842 btrfs_set_file_extent_compression(leaf
, ei
, 0);
7843 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
7844 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
7846 ptr
= btrfs_file_extent_inline_start(ei
);
7847 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
7848 btrfs_mark_buffer_dirty(leaf
);
7849 btrfs_free_path(path
);
7851 inode
->i_op
= &btrfs_symlink_inode_operations
;
7852 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
7853 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
7854 inode_set_bytes(inode
, name_len
);
7855 btrfs_i_size_write(inode
, name_len
- 1);
7856 err
= btrfs_update_inode(trans
, root
, inode
);
7862 d_instantiate(dentry
, inode
);
7863 btrfs_end_transaction(trans
, root
);
7865 inode_dec_link_count(inode
);
7868 btrfs_btree_balance_dirty(root
);
7872 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
7873 u64 start
, u64 num_bytes
, u64 min_size
,
7874 loff_t actual_len
, u64
*alloc_hint
,
7875 struct btrfs_trans_handle
*trans
)
7877 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
7878 struct extent_map
*em
;
7879 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7880 struct btrfs_key ins
;
7881 u64 cur_offset
= start
;
7884 bool own_trans
= true;
7888 while (num_bytes
> 0) {
7890 trans
= btrfs_start_transaction(root
, 3);
7891 if (IS_ERR(trans
)) {
7892 ret
= PTR_ERR(trans
);
7897 ret
= btrfs_reserve_extent(trans
, root
, num_bytes
, min_size
,
7898 0, *alloc_hint
, &ins
, 1);
7901 btrfs_end_transaction(trans
, root
);
7905 ret
= insert_reserved_file_extent(trans
, inode
,
7906 cur_offset
, ins
.objectid
,
7907 ins
.offset
, ins
.offset
,
7908 ins
.offset
, 0, 0, 0,
7909 BTRFS_FILE_EXTENT_PREALLOC
);
7911 btrfs_abort_transaction(trans
, root
, ret
);
7913 btrfs_end_transaction(trans
, root
);
7916 btrfs_drop_extent_cache(inode
, cur_offset
,
7917 cur_offset
+ ins
.offset
-1, 0);
7919 em
= alloc_extent_map();
7921 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
7922 &BTRFS_I(inode
)->runtime_flags
);
7926 em
->start
= cur_offset
;
7927 em
->orig_start
= cur_offset
;
7928 em
->len
= ins
.offset
;
7929 em
->block_start
= ins
.objectid
;
7930 em
->block_len
= ins
.offset
;
7931 em
->orig_block_len
= ins
.offset
;
7932 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
7933 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
7934 em
->generation
= trans
->transid
;
7937 write_lock(&em_tree
->lock
);
7938 ret
= add_extent_mapping(em_tree
, em
);
7940 list_move(&em
->list
,
7941 &em_tree
->modified_extents
);
7942 write_unlock(&em_tree
->lock
);
7945 btrfs_drop_extent_cache(inode
, cur_offset
,
7946 cur_offset
+ ins
.offset
- 1,
7949 free_extent_map(em
);
7951 num_bytes
-= ins
.offset
;
7952 cur_offset
+= ins
.offset
;
7953 *alloc_hint
= ins
.objectid
+ ins
.offset
;
7955 inode_inc_iversion(inode
);
7956 inode
->i_ctime
= CURRENT_TIME
;
7957 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
7958 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
7959 (actual_len
> inode
->i_size
) &&
7960 (cur_offset
> inode
->i_size
)) {
7961 if (cur_offset
> actual_len
)
7962 i_size
= actual_len
;
7964 i_size
= cur_offset
;
7965 i_size_write(inode
, i_size
);
7966 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
7969 ret
= btrfs_update_inode(trans
, root
, inode
);
7972 btrfs_abort_transaction(trans
, root
, ret
);
7974 btrfs_end_transaction(trans
, root
);
7979 btrfs_end_transaction(trans
, root
);
7984 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
7985 u64 start
, u64 num_bytes
, u64 min_size
,
7986 loff_t actual_len
, u64
*alloc_hint
)
7988 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
7989 min_size
, actual_len
, alloc_hint
,
7993 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
7994 struct btrfs_trans_handle
*trans
, int mode
,
7995 u64 start
, u64 num_bytes
, u64 min_size
,
7996 loff_t actual_len
, u64
*alloc_hint
)
7998 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
7999 min_size
, actual_len
, alloc_hint
, trans
);
8002 static int btrfs_set_page_dirty(struct page
*page
)
8004 return __set_page_dirty_nobuffers(page
);
8007 static int btrfs_permission(struct inode
*inode
, int mask
)
8009 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8010 umode_t mode
= inode
->i_mode
;
8012 if (mask
& MAY_WRITE
&&
8013 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
8014 if (btrfs_root_readonly(root
))
8016 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
8019 return generic_permission(inode
, mask
);
8022 static const struct inode_operations btrfs_dir_inode_operations
= {
8023 .getattr
= btrfs_getattr
,
8024 .lookup
= btrfs_lookup
,
8025 .create
= btrfs_create
,
8026 .unlink
= btrfs_unlink
,
8028 .mkdir
= btrfs_mkdir
,
8029 .rmdir
= btrfs_rmdir
,
8030 .rename
= btrfs_rename
,
8031 .symlink
= btrfs_symlink
,
8032 .setattr
= btrfs_setattr
,
8033 .mknod
= btrfs_mknod
,
8034 .setxattr
= btrfs_setxattr
,
8035 .getxattr
= btrfs_getxattr
,
8036 .listxattr
= btrfs_listxattr
,
8037 .removexattr
= btrfs_removexattr
,
8038 .permission
= btrfs_permission
,
8039 .get_acl
= btrfs_get_acl
,
8041 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
8042 .lookup
= btrfs_lookup
,
8043 .permission
= btrfs_permission
,
8044 .get_acl
= btrfs_get_acl
,
8047 static const struct file_operations btrfs_dir_file_operations
= {
8048 .llseek
= generic_file_llseek
,
8049 .read
= generic_read_dir
,
8050 .readdir
= btrfs_real_readdir
,
8051 .unlocked_ioctl
= btrfs_ioctl
,
8052 #ifdef CONFIG_COMPAT
8053 .compat_ioctl
= btrfs_ioctl
,
8055 .release
= btrfs_release_file
,
8056 .fsync
= btrfs_sync_file
,
8059 static struct extent_io_ops btrfs_extent_io_ops
= {
8060 .fill_delalloc
= run_delalloc_range
,
8061 .submit_bio_hook
= btrfs_submit_bio_hook
,
8062 .merge_bio_hook
= btrfs_merge_bio_hook
,
8063 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
8064 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
8065 .writepage_start_hook
= btrfs_writepage_start_hook
,
8066 .set_bit_hook
= btrfs_set_bit_hook
,
8067 .clear_bit_hook
= btrfs_clear_bit_hook
,
8068 .merge_extent_hook
= btrfs_merge_extent_hook
,
8069 .split_extent_hook
= btrfs_split_extent_hook
,
8073 * btrfs doesn't support the bmap operation because swapfiles
8074 * use bmap to make a mapping of extents in the file. They assume
8075 * these extents won't change over the life of the file and they
8076 * use the bmap result to do IO directly to the drive.
8078 * the btrfs bmap call would return logical addresses that aren't
8079 * suitable for IO and they also will change frequently as COW
8080 * operations happen. So, swapfile + btrfs == corruption.
8082 * For now we're avoiding this by dropping bmap.
8084 static const struct address_space_operations btrfs_aops
= {
8085 .readpage
= btrfs_readpage
,
8086 .writepage
= btrfs_writepage
,
8087 .writepages
= btrfs_writepages
,
8088 .readpages
= btrfs_readpages
,
8089 .direct_IO
= btrfs_direct_IO
,
8090 .invalidatepage
= btrfs_invalidatepage
,
8091 .releasepage
= btrfs_releasepage
,
8092 .set_page_dirty
= btrfs_set_page_dirty
,
8093 .error_remove_page
= generic_error_remove_page
,
8096 static const struct address_space_operations btrfs_symlink_aops
= {
8097 .readpage
= btrfs_readpage
,
8098 .writepage
= btrfs_writepage
,
8099 .invalidatepage
= btrfs_invalidatepage
,
8100 .releasepage
= btrfs_releasepage
,
8103 static const struct inode_operations btrfs_file_inode_operations
= {
8104 .getattr
= btrfs_getattr
,
8105 .setattr
= btrfs_setattr
,
8106 .setxattr
= btrfs_setxattr
,
8107 .getxattr
= btrfs_getxattr
,
8108 .listxattr
= btrfs_listxattr
,
8109 .removexattr
= btrfs_removexattr
,
8110 .permission
= btrfs_permission
,
8111 .fiemap
= btrfs_fiemap
,
8112 .get_acl
= btrfs_get_acl
,
8113 .update_time
= btrfs_update_time
,
8115 static const struct inode_operations btrfs_special_inode_operations
= {
8116 .getattr
= btrfs_getattr
,
8117 .setattr
= btrfs_setattr
,
8118 .permission
= btrfs_permission
,
8119 .setxattr
= btrfs_setxattr
,
8120 .getxattr
= btrfs_getxattr
,
8121 .listxattr
= btrfs_listxattr
,
8122 .removexattr
= btrfs_removexattr
,
8123 .get_acl
= btrfs_get_acl
,
8124 .update_time
= btrfs_update_time
,
8126 static const struct inode_operations btrfs_symlink_inode_operations
= {
8127 .readlink
= generic_readlink
,
8128 .follow_link
= page_follow_link_light
,
8129 .put_link
= page_put_link
,
8130 .getattr
= btrfs_getattr
,
8131 .setattr
= btrfs_setattr
,
8132 .permission
= btrfs_permission
,
8133 .setxattr
= btrfs_setxattr
,
8134 .getxattr
= btrfs_getxattr
,
8135 .listxattr
= btrfs_listxattr
,
8136 .removexattr
= btrfs_removexattr
,
8137 .get_acl
= btrfs_get_acl
,
8138 .update_time
= btrfs_update_time
,
8141 const struct dentry_operations btrfs_dentry_operations
= {
8142 .d_delete
= btrfs_dentry_delete
,
8143 .d_release
= btrfs_dentry_release
,