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>
46 #include "transaction.h"
47 #include "btrfs_inode.h"
48 #include "print-tree.h"
49 #include "ordered-data.h"
53 #include "compression.h"
55 #include "free-space-cache.h"
56 #include "inode-map.h"
58 struct btrfs_iget_args
{
60 struct btrfs_root
*root
;
63 static const struct inode_operations btrfs_dir_inode_operations
;
64 static const struct inode_operations btrfs_symlink_inode_operations
;
65 static const struct inode_operations btrfs_dir_ro_inode_operations
;
66 static const struct inode_operations btrfs_special_inode_operations
;
67 static const struct inode_operations btrfs_file_inode_operations
;
68 static const struct address_space_operations btrfs_aops
;
69 static const struct address_space_operations btrfs_symlink_aops
;
70 static const struct file_operations btrfs_dir_file_operations
;
71 static struct extent_io_ops btrfs_extent_io_ops
;
73 static struct kmem_cache
*btrfs_inode_cachep
;
74 static struct kmem_cache
*btrfs_delalloc_work_cachep
;
75 struct kmem_cache
*btrfs_trans_handle_cachep
;
76 struct kmem_cache
*btrfs_transaction_cachep
;
77 struct kmem_cache
*btrfs_path_cachep
;
78 struct kmem_cache
*btrfs_free_space_cachep
;
81 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
82 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
83 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
84 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
85 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
86 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
87 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
88 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
91 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
);
92 static int btrfs_truncate(struct inode
*inode
);
93 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
);
94 static noinline
int cow_file_range(struct inode
*inode
,
95 struct page
*locked_page
,
96 u64 start
, u64 end
, int *page_started
,
97 unsigned long *nr_written
, int unlock
);
98 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
99 u64 len
, u64 orig_start
,
100 u64 block_start
, u64 block_len
,
101 u64 orig_block_len
, int type
);
103 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
104 struct inode
*inode
, struct inode
*dir
,
105 const struct qstr
*qstr
)
109 err
= btrfs_init_acl(trans
, inode
, dir
);
111 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
116 * this does all the hard work for inserting an inline extent into
117 * the btree. The caller should have done a btrfs_drop_extents so that
118 * no overlapping inline items exist in the btree
120 static noinline
int insert_inline_extent(struct btrfs_trans_handle
*trans
,
121 struct btrfs_root
*root
, struct inode
*inode
,
122 u64 start
, size_t size
, size_t compressed_size
,
124 struct page
**compressed_pages
)
126 struct btrfs_key key
;
127 struct btrfs_path
*path
;
128 struct extent_buffer
*leaf
;
129 struct page
*page
= NULL
;
132 struct btrfs_file_extent_item
*ei
;
135 size_t cur_size
= size
;
137 unsigned long offset
;
139 if (compressed_size
&& compressed_pages
)
140 cur_size
= compressed_size
;
142 path
= btrfs_alloc_path();
146 path
->leave_spinning
= 1;
148 key
.objectid
= btrfs_ino(inode
);
150 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
151 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
153 inode_add_bytes(inode
, size
);
154 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
160 leaf
= path
->nodes
[0];
161 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
162 struct btrfs_file_extent_item
);
163 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
164 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
165 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
166 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
167 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
168 ptr
= btrfs_file_extent_inline_start(ei
);
170 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
173 while (compressed_size
> 0) {
174 cpage
= compressed_pages
[i
];
175 cur_size
= min_t(unsigned long, compressed_size
,
178 kaddr
= kmap_atomic(cpage
);
179 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
180 kunmap_atomic(kaddr
);
184 compressed_size
-= cur_size
;
186 btrfs_set_file_extent_compression(leaf
, ei
,
189 page
= find_get_page(inode
->i_mapping
,
190 start
>> PAGE_CACHE_SHIFT
);
191 btrfs_set_file_extent_compression(leaf
, ei
, 0);
192 kaddr
= kmap_atomic(page
);
193 offset
= start
& (PAGE_CACHE_SIZE
- 1);
194 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
195 kunmap_atomic(kaddr
);
196 page_cache_release(page
);
198 btrfs_mark_buffer_dirty(leaf
);
199 btrfs_free_path(path
);
202 * we're an inline extent, so nobody can
203 * extend the file past i_size without locking
204 * a page we already have locked.
206 * We must do any isize and inode updates
207 * before we unlock the pages. Otherwise we
208 * could end up racing with unlink.
210 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
211 ret
= btrfs_update_inode(trans
, root
, inode
);
215 btrfs_free_path(path
);
221 * conditionally insert an inline extent into the file. This
222 * does the checks required to make sure the data is small enough
223 * to fit as an inline extent.
225 static noinline
int cow_file_range_inline(struct btrfs_trans_handle
*trans
,
226 struct btrfs_root
*root
,
227 struct inode
*inode
, u64 start
, u64 end
,
228 size_t compressed_size
, int compress_type
,
229 struct page
**compressed_pages
)
231 u64 isize
= i_size_read(inode
);
232 u64 actual_end
= min(end
+ 1, isize
);
233 u64 inline_len
= actual_end
- start
;
234 u64 aligned_end
= (end
+ root
->sectorsize
- 1) &
235 ~((u64
)root
->sectorsize
- 1);
236 u64 data_len
= inline_len
;
240 data_len
= compressed_size
;
243 actual_end
>= PAGE_CACHE_SIZE
||
244 data_len
>= BTRFS_MAX_INLINE_DATA_SIZE(root
) ||
246 (actual_end
& (root
->sectorsize
- 1)) == 0) ||
248 data_len
> root
->fs_info
->max_inline
) {
252 ret
= btrfs_drop_extents(trans
, root
, inode
, start
, aligned_end
, 1);
256 if (isize
> actual_end
)
257 inline_len
= min_t(u64
, isize
, actual_end
);
258 ret
= insert_inline_extent(trans
, root
, inode
, start
,
259 inline_len
, compressed_size
,
260 compress_type
, compressed_pages
);
261 if (ret
&& ret
!= -ENOSPC
) {
262 btrfs_abort_transaction(trans
, root
, ret
);
264 } else if (ret
== -ENOSPC
) {
268 btrfs_delalloc_release_metadata(inode
, end
+ 1 - start
);
269 btrfs_drop_extent_cache(inode
, start
, aligned_end
- 1, 0);
273 struct async_extent
{
278 unsigned long nr_pages
;
280 struct list_head list
;
285 struct btrfs_root
*root
;
286 struct page
*locked_page
;
289 struct list_head extents
;
290 struct btrfs_work work
;
293 static noinline
int add_async_extent(struct async_cow
*cow
,
294 u64 start
, u64 ram_size
,
297 unsigned long nr_pages
,
300 struct async_extent
*async_extent
;
302 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
303 BUG_ON(!async_extent
); /* -ENOMEM */
304 async_extent
->start
= start
;
305 async_extent
->ram_size
= ram_size
;
306 async_extent
->compressed_size
= compressed_size
;
307 async_extent
->pages
= pages
;
308 async_extent
->nr_pages
= nr_pages
;
309 async_extent
->compress_type
= compress_type
;
310 list_add_tail(&async_extent
->list
, &cow
->extents
);
315 * we create compressed extents in two phases. The first
316 * phase compresses a range of pages that have already been
317 * locked (both pages and state bits are locked).
319 * This is done inside an ordered work queue, and the compression
320 * is spread across many cpus. The actual IO submission is step
321 * two, and the ordered work queue takes care of making sure that
322 * happens in the same order things were put onto the queue by
323 * writepages and friends.
325 * If this code finds it can't get good compression, it puts an
326 * entry onto the work queue to write the uncompressed bytes. This
327 * makes sure that both compressed inodes and uncompressed inodes
328 * are written in the same order that the flusher thread sent them
331 static noinline
int compress_file_range(struct inode
*inode
,
332 struct page
*locked_page
,
334 struct async_cow
*async_cow
,
337 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
338 struct btrfs_trans_handle
*trans
;
340 u64 blocksize
= root
->sectorsize
;
342 u64 isize
= i_size_read(inode
);
344 struct page
**pages
= NULL
;
345 unsigned long nr_pages
;
346 unsigned long nr_pages_ret
= 0;
347 unsigned long total_compressed
= 0;
348 unsigned long total_in
= 0;
349 unsigned long max_compressed
= 128 * 1024;
350 unsigned long max_uncompressed
= 128 * 1024;
353 int compress_type
= root
->fs_info
->compress_type
;
355 /* if this is a small write inside eof, kick off a defrag */
356 if ((end
- start
+ 1) < 16 * 1024 &&
357 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
358 btrfs_add_inode_defrag(NULL
, inode
);
360 actual_end
= min_t(u64
, isize
, end
+ 1);
363 nr_pages
= (end
>> PAGE_CACHE_SHIFT
) - (start
>> PAGE_CACHE_SHIFT
) + 1;
364 nr_pages
= min(nr_pages
, (128 * 1024UL) / PAGE_CACHE_SIZE
);
367 * we don't want to send crud past the end of i_size through
368 * compression, that's just a waste of CPU time. So, if the
369 * end of the file is before the start of our current
370 * requested range of bytes, we bail out to the uncompressed
371 * cleanup code that can deal with all of this.
373 * It isn't really the fastest way to fix things, but this is a
374 * very uncommon corner.
376 if (actual_end
<= start
)
377 goto cleanup_and_bail_uncompressed
;
379 total_compressed
= actual_end
- start
;
381 /* we want to make sure that amount of ram required to uncompress
382 * an extent is reasonable, so we limit the total size in ram
383 * of a compressed extent to 128k. This is a crucial number
384 * because it also controls how easily we can spread reads across
385 * cpus for decompression.
387 * We also want to make sure the amount of IO required to do
388 * a random read is reasonably small, so we limit the size of
389 * a compressed extent to 128k.
391 total_compressed
= min(total_compressed
, max_uncompressed
);
392 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
393 num_bytes
= max(blocksize
, num_bytes
);
398 * we do compression for mount -o compress and when the
399 * inode has not been flagged as nocompress. This flag can
400 * change at any time if we discover bad compression ratios.
402 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
) &&
403 (btrfs_test_opt(root
, COMPRESS
) ||
404 (BTRFS_I(inode
)->force_compress
) ||
405 (BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
))) {
407 pages
= kzalloc(sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
409 /* just bail out to the uncompressed code */
413 if (BTRFS_I(inode
)->force_compress
)
414 compress_type
= BTRFS_I(inode
)->force_compress
;
416 ret
= btrfs_compress_pages(compress_type
,
417 inode
->i_mapping
, start
,
418 total_compressed
, pages
,
419 nr_pages
, &nr_pages_ret
,
425 unsigned long offset
= total_compressed
&
426 (PAGE_CACHE_SIZE
- 1);
427 struct page
*page
= pages
[nr_pages_ret
- 1];
430 /* zero the tail end of the last page, we might be
431 * sending it down to disk
434 kaddr
= kmap_atomic(page
);
435 memset(kaddr
+ offset
, 0,
436 PAGE_CACHE_SIZE
- offset
);
437 kunmap_atomic(kaddr
);
444 trans
= btrfs_join_transaction(root
);
446 ret
= PTR_ERR(trans
);
448 goto cleanup_and_out
;
450 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
452 /* lets try to make an inline extent */
453 if (ret
|| total_in
< (actual_end
- start
)) {
454 /* we didn't compress the entire range, try
455 * to make an uncompressed inline extent.
457 ret
= cow_file_range_inline(trans
, root
, inode
,
458 start
, end
, 0, 0, NULL
);
460 /* try making a compressed inline extent */
461 ret
= cow_file_range_inline(trans
, root
, inode
,
464 compress_type
, pages
);
468 * inline extent creation worked or returned error,
469 * we don't need to create any more async work items.
470 * Unlock and free up our temp pages.
472 extent_clear_unlock_delalloc(inode
,
473 &BTRFS_I(inode
)->io_tree
,
475 EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
476 EXTENT_CLEAR_DELALLOC
|
477 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
);
479 btrfs_end_transaction(trans
, root
);
482 btrfs_end_transaction(trans
, root
);
487 * we aren't doing an inline extent round the compressed size
488 * up to a block size boundary so the allocator does sane
491 total_compressed
= (total_compressed
+ blocksize
- 1) &
495 * one last check to make sure the compression is really a
496 * win, compare the page count read with the blocks on disk
498 total_in
= (total_in
+ PAGE_CACHE_SIZE
- 1) &
499 ~(PAGE_CACHE_SIZE
- 1);
500 if (total_compressed
>= total_in
) {
503 num_bytes
= total_in
;
506 if (!will_compress
&& pages
) {
508 * the compression code ran but failed to make things smaller,
509 * free any pages it allocated and our page pointer array
511 for (i
= 0; i
< nr_pages_ret
; i
++) {
512 WARN_ON(pages
[i
]->mapping
);
513 page_cache_release(pages
[i
]);
517 total_compressed
= 0;
520 /* flag the file so we don't compress in the future */
521 if (!btrfs_test_opt(root
, FORCE_COMPRESS
) &&
522 !(BTRFS_I(inode
)->force_compress
)) {
523 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
529 /* the async work queues will take care of doing actual
530 * allocation on disk for these compressed pages,
531 * and will submit them to the elevator.
533 add_async_extent(async_cow
, start
, num_bytes
,
534 total_compressed
, pages
, nr_pages_ret
,
537 if (start
+ num_bytes
< end
) {
544 cleanup_and_bail_uncompressed
:
546 * No compression, but we still need to write the pages in
547 * the file we've been given so far. redirty the locked
548 * page if it corresponds to our extent and set things up
549 * for the async work queue to run cow_file_range to do
550 * the normal delalloc dance
552 if (page_offset(locked_page
) >= start
&&
553 page_offset(locked_page
) <= end
) {
554 __set_page_dirty_nobuffers(locked_page
);
555 /* unlocked later on in the async handlers */
557 add_async_extent(async_cow
, start
, end
- start
+ 1,
558 0, NULL
, 0, BTRFS_COMPRESS_NONE
);
566 for (i
= 0; i
< nr_pages_ret
; i
++) {
567 WARN_ON(pages
[i
]->mapping
);
568 page_cache_release(pages
[i
]);
575 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
577 EXTENT_CLEAR_UNLOCK_PAGE
|
579 EXTENT_CLEAR_DELALLOC
|
580 EXTENT_SET_WRITEBACK
|
581 EXTENT_END_WRITEBACK
);
582 if (!trans
|| IS_ERR(trans
))
583 btrfs_error(root
->fs_info
, ret
, "Failed to join transaction");
585 btrfs_abort_transaction(trans
, root
, ret
);
590 * phase two of compressed writeback. This is the ordered portion
591 * of the code, which only gets called in the order the work was
592 * queued. We walk all the async extents created by compress_file_range
593 * and send them down to the disk.
595 static noinline
int submit_compressed_extents(struct inode
*inode
,
596 struct async_cow
*async_cow
)
598 struct async_extent
*async_extent
;
600 struct btrfs_trans_handle
*trans
;
601 struct btrfs_key ins
;
602 struct extent_map
*em
;
603 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
604 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
605 struct extent_io_tree
*io_tree
;
608 if (list_empty(&async_cow
->extents
))
612 while (!list_empty(&async_cow
->extents
)) {
613 async_extent
= list_entry(async_cow
->extents
.next
,
614 struct async_extent
, list
);
615 list_del(&async_extent
->list
);
617 io_tree
= &BTRFS_I(inode
)->io_tree
;
620 /* did the compression code fall back to uncompressed IO? */
621 if (!async_extent
->pages
) {
622 int page_started
= 0;
623 unsigned long nr_written
= 0;
625 lock_extent(io_tree
, async_extent
->start
,
626 async_extent
->start
+
627 async_extent
->ram_size
- 1);
629 /* allocate blocks */
630 ret
= cow_file_range(inode
, async_cow
->locked_page
,
632 async_extent
->start
+
633 async_extent
->ram_size
- 1,
634 &page_started
, &nr_written
, 0);
639 * if page_started, cow_file_range inserted an
640 * inline extent and took care of all the unlocking
641 * and IO for us. Otherwise, we need to submit
642 * all those pages down to the drive.
644 if (!page_started
&& !ret
)
645 extent_write_locked_range(io_tree
,
646 inode
, async_extent
->start
,
647 async_extent
->start
+
648 async_extent
->ram_size
- 1,
652 unlock_page(async_cow
->locked_page
);
658 lock_extent(io_tree
, async_extent
->start
,
659 async_extent
->start
+ async_extent
->ram_size
- 1);
661 trans
= btrfs_join_transaction(root
);
663 ret
= PTR_ERR(trans
);
665 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
666 ret
= btrfs_reserve_extent(trans
, root
,
667 async_extent
->compressed_size
,
668 async_extent
->compressed_size
,
669 0, alloc_hint
, &ins
, 1);
670 if (ret
&& ret
!= -ENOSPC
)
671 btrfs_abort_transaction(trans
, root
, ret
);
672 btrfs_end_transaction(trans
, root
);
678 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
679 WARN_ON(async_extent
->pages
[i
]->mapping
);
680 page_cache_release(async_extent
->pages
[i
]);
682 kfree(async_extent
->pages
);
683 async_extent
->nr_pages
= 0;
684 async_extent
->pages
= NULL
;
692 * here we're doing allocation and writeback of the
695 btrfs_drop_extent_cache(inode
, async_extent
->start
,
696 async_extent
->start
+
697 async_extent
->ram_size
- 1, 0);
699 em
= alloc_extent_map();
701 goto out_free_reserve
;
702 em
->start
= async_extent
->start
;
703 em
->len
= async_extent
->ram_size
;
704 em
->orig_start
= em
->start
;
705 em
->mod_start
= em
->start
;
706 em
->mod_len
= em
->len
;
708 em
->block_start
= ins
.objectid
;
709 em
->block_len
= ins
.offset
;
710 em
->orig_block_len
= ins
.offset
;
711 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
712 em
->compress_type
= async_extent
->compress_type
;
713 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
714 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
718 write_lock(&em_tree
->lock
);
719 ret
= add_extent_mapping(em_tree
, em
);
722 &em_tree
->modified_extents
);
723 write_unlock(&em_tree
->lock
);
724 if (ret
!= -EEXIST
) {
728 btrfs_drop_extent_cache(inode
, async_extent
->start
,
729 async_extent
->start
+
730 async_extent
->ram_size
- 1, 0);
734 goto out_free_reserve
;
736 ret
= btrfs_add_ordered_extent_compress(inode
,
739 async_extent
->ram_size
,
741 BTRFS_ORDERED_COMPRESSED
,
742 async_extent
->compress_type
);
744 goto out_free_reserve
;
747 * clear dirty, set writeback and unlock the pages.
749 extent_clear_unlock_delalloc(inode
,
750 &BTRFS_I(inode
)->io_tree
,
752 async_extent
->start
+
753 async_extent
->ram_size
- 1,
754 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
755 EXTENT_CLEAR_UNLOCK
|
756 EXTENT_CLEAR_DELALLOC
|
757 EXTENT_CLEAR_DIRTY
| EXTENT_SET_WRITEBACK
);
759 ret
= btrfs_submit_compressed_write(inode
,
761 async_extent
->ram_size
,
763 ins
.offset
, async_extent
->pages
,
764 async_extent
->nr_pages
);
765 alloc_hint
= ins
.objectid
+ ins
.offset
;
775 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
777 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
779 async_extent
->start
+
780 async_extent
->ram_size
- 1,
781 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
782 EXTENT_CLEAR_UNLOCK
|
783 EXTENT_CLEAR_DELALLOC
|
785 EXTENT_SET_WRITEBACK
|
786 EXTENT_END_WRITEBACK
);
791 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
794 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
795 struct extent_map
*em
;
798 read_lock(&em_tree
->lock
);
799 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
802 * if block start isn't an actual block number then find the
803 * first block in this inode and use that as a hint. If that
804 * block is also bogus then just don't worry about it.
806 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
808 em
= search_extent_mapping(em_tree
, 0, 0);
809 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
810 alloc_hint
= em
->block_start
;
814 alloc_hint
= em
->block_start
;
818 read_unlock(&em_tree
->lock
);
824 * when extent_io.c finds a delayed allocation range in the file,
825 * the call backs end up in this code. The basic idea is to
826 * allocate extents on disk for the range, and create ordered data structs
827 * in ram to track those extents.
829 * locked_page is the page that writepage had locked already. We use
830 * it to make sure we don't do extra locks or unlocks.
832 * *page_started is set to one if we unlock locked_page and do everything
833 * required to start IO on it. It may be clean and already done with
836 static noinline
int __cow_file_range(struct btrfs_trans_handle
*trans
,
838 struct btrfs_root
*root
,
839 struct page
*locked_page
,
840 u64 start
, u64 end
, int *page_started
,
841 unsigned long *nr_written
,
846 unsigned long ram_size
;
849 u64 blocksize
= root
->sectorsize
;
850 struct btrfs_key ins
;
851 struct extent_map
*em
;
852 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
855 BUG_ON(btrfs_is_free_space_inode(inode
));
857 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
858 num_bytes
= max(blocksize
, num_bytes
);
859 disk_num_bytes
= num_bytes
;
861 /* if this is a small write inside eof, kick off defrag */
862 if (num_bytes
< 64 * 1024 &&
863 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
864 btrfs_add_inode_defrag(trans
, inode
);
867 /* lets try to make an inline extent */
868 ret
= cow_file_range_inline(trans
, root
, inode
,
869 start
, end
, 0, 0, NULL
);
871 extent_clear_unlock_delalloc(inode
,
872 &BTRFS_I(inode
)->io_tree
,
874 EXTENT_CLEAR_UNLOCK_PAGE
|
875 EXTENT_CLEAR_UNLOCK
|
876 EXTENT_CLEAR_DELALLOC
|
878 EXTENT_SET_WRITEBACK
|
879 EXTENT_END_WRITEBACK
);
881 *nr_written
= *nr_written
+
882 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
885 } else if (ret
< 0) {
886 btrfs_abort_transaction(trans
, root
, ret
);
891 BUG_ON(disk_num_bytes
>
892 btrfs_super_total_bytes(root
->fs_info
->super_copy
));
894 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
895 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
897 while (disk_num_bytes
> 0) {
900 cur_alloc_size
= disk_num_bytes
;
901 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
902 root
->sectorsize
, 0, alloc_hint
,
905 btrfs_abort_transaction(trans
, root
, ret
);
909 em
= alloc_extent_map();
910 BUG_ON(!em
); /* -ENOMEM */
912 em
->orig_start
= em
->start
;
913 ram_size
= ins
.offset
;
914 em
->len
= ins
.offset
;
915 em
->mod_start
= em
->start
;
916 em
->mod_len
= em
->len
;
918 em
->block_start
= ins
.objectid
;
919 em
->block_len
= ins
.offset
;
920 em
->orig_block_len
= ins
.offset
;
921 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
922 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
926 write_lock(&em_tree
->lock
);
927 ret
= add_extent_mapping(em_tree
, em
);
930 &em_tree
->modified_extents
);
931 write_unlock(&em_tree
->lock
);
932 if (ret
!= -EEXIST
) {
936 btrfs_drop_extent_cache(inode
, start
,
937 start
+ ram_size
- 1, 0);
940 cur_alloc_size
= ins
.offset
;
941 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
942 ram_size
, cur_alloc_size
, 0);
943 BUG_ON(ret
); /* -ENOMEM */
945 if (root
->root_key
.objectid
==
946 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
947 ret
= btrfs_reloc_clone_csums(inode
, start
,
950 btrfs_abort_transaction(trans
, root
, ret
);
955 if (disk_num_bytes
< cur_alloc_size
)
958 /* we're not doing compressed IO, don't unlock the first
959 * page (which the caller expects to stay locked), don't
960 * clear any dirty bits and don't set any writeback bits
962 * Do set the Private2 bit so we know this page was properly
963 * setup for writepage
965 op
= unlock
? EXTENT_CLEAR_UNLOCK_PAGE
: 0;
966 op
|= EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
969 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
970 start
, start
+ ram_size
- 1,
972 disk_num_bytes
-= cur_alloc_size
;
973 num_bytes
-= cur_alloc_size
;
974 alloc_hint
= ins
.objectid
+ ins
.offset
;
975 start
+= cur_alloc_size
;
981 extent_clear_unlock_delalloc(inode
,
982 &BTRFS_I(inode
)->io_tree
,
983 start
, end
, locked_page
,
984 EXTENT_CLEAR_UNLOCK_PAGE
|
985 EXTENT_CLEAR_UNLOCK
|
986 EXTENT_CLEAR_DELALLOC
|
988 EXTENT_SET_WRITEBACK
|
989 EXTENT_END_WRITEBACK
);
994 static noinline
int cow_file_range(struct inode
*inode
,
995 struct page
*locked_page
,
996 u64 start
, u64 end
, int *page_started
,
997 unsigned long *nr_written
,
1000 struct btrfs_trans_handle
*trans
;
1001 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1004 trans
= btrfs_join_transaction(root
);
1005 if (IS_ERR(trans
)) {
1006 extent_clear_unlock_delalloc(inode
,
1007 &BTRFS_I(inode
)->io_tree
,
1008 start
, end
, locked_page
,
1009 EXTENT_CLEAR_UNLOCK_PAGE
|
1010 EXTENT_CLEAR_UNLOCK
|
1011 EXTENT_CLEAR_DELALLOC
|
1012 EXTENT_CLEAR_DIRTY
|
1013 EXTENT_SET_WRITEBACK
|
1014 EXTENT_END_WRITEBACK
);
1015 return PTR_ERR(trans
);
1017 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1019 ret
= __cow_file_range(trans
, inode
, root
, locked_page
, start
, end
,
1020 page_started
, nr_written
, unlock
);
1022 btrfs_end_transaction(trans
, root
);
1028 * work queue call back to started compression on a file and pages
1030 static noinline
void async_cow_start(struct btrfs_work
*work
)
1032 struct async_cow
*async_cow
;
1034 async_cow
= container_of(work
, struct async_cow
, work
);
1036 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
1037 async_cow
->start
, async_cow
->end
, async_cow
,
1039 if (num_added
== 0) {
1040 btrfs_add_delayed_iput(async_cow
->inode
);
1041 async_cow
->inode
= NULL
;
1046 * work queue call back to submit previously compressed pages
1048 static noinline
void async_cow_submit(struct btrfs_work
*work
)
1050 struct async_cow
*async_cow
;
1051 struct btrfs_root
*root
;
1052 unsigned long nr_pages
;
1054 async_cow
= container_of(work
, struct async_cow
, work
);
1056 root
= async_cow
->root
;
1057 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
1060 if (atomic_sub_return(nr_pages
, &root
->fs_info
->async_delalloc_pages
) <
1062 waitqueue_active(&root
->fs_info
->async_submit_wait
))
1063 wake_up(&root
->fs_info
->async_submit_wait
);
1065 if (async_cow
->inode
)
1066 submit_compressed_extents(async_cow
->inode
, async_cow
);
1069 static noinline
void async_cow_free(struct btrfs_work
*work
)
1071 struct async_cow
*async_cow
;
1072 async_cow
= container_of(work
, struct async_cow
, work
);
1073 if (async_cow
->inode
)
1074 btrfs_add_delayed_iput(async_cow
->inode
);
1078 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
1079 u64 start
, u64 end
, int *page_started
,
1080 unsigned long *nr_written
)
1082 struct async_cow
*async_cow
;
1083 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1084 unsigned long nr_pages
;
1086 int limit
= 10 * 1024 * 1024;
1088 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
1089 1, 0, NULL
, GFP_NOFS
);
1090 while (start
< end
) {
1091 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
1092 BUG_ON(!async_cow
); /* -ENOMEM */
1093 async_cow
->inode
= igrab(inode
);
1094 async_cow
->root
= root
;
1095 async_cow
->locked_page
= locked_page
;
1096 async_cow
->start
= start
;
1098 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
1101 cur_end
= min(end
, start
+ 512 * 1024 - 1);
1103 async_cow
->end
= cur_end
;
1104 INIT_LIST_HEAD(&async_cow
->extents
);
1106 async_cow
->work
.func
= async_cow_start
;
1107 async_cow
->work
.ordered_func
= async_cow_submit
;
1108 async_cow
->work
.ordered_free
= async_cow_free
;
1109 async_cow
->work
.flags
= 0;
1111 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
1113 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
1115 btrfs_queue_worker(&root
->fs_info
->delalloc_workers
,
1118 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
1119 wait_event(root
->fs_info
->async_submit_wait
,
1120 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
1124 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
1125 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1126 wait_event(root
->fs_info
->async_submit_wait
,
1127 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
1131 *nr_written
+= nr_pages
;
1132 start
= cur_end
+ 1;
1138 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
1139 u64 bytenr
, u64 num_bytes
)
1142 struct btrfs_ordered_sum
*sums
;
1145 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
1146 bytenr
+ num_bytes
- 1, &list
, 0);
1147 if (ret
== 0 && list_empty(&list
))
1150 while (!list_empty(&list
)) {
1151 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1152 list_del(&sums
->list
);
1159 * when nowcow writeback call back. This checks for snapshots or COW copies
1160 * of the extents that exist in the file, and COWs the file as required.
1162 * If no cow copies or snapshots exist, we write directly to the existing
1165 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1166 struct page
*locked_page
,
1167 u64 start
, u64 end
, int *page_started
, int force
,
1168 unsigned long *nr_written
)
1170 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1171 struct btrfs_trans_handle
*trans
;
1172 struct extent_buffer
*leaf
;
1173 struct btrfs_path
*path
;
1174 struct btrfs_file_extent_item
*fi
;
1175 struct btrfs_key found_key
;
1189 u64 ino
= btrfs_ino(inode
);
1191 path
= btrfs_alloc_path();
1193 extent_clear_unlock_delalloc(inode
,
1194 &BTRFS_I(inode
)->io_tree
,
1195 start
, end
, locked_page
,
1196 EXTENT_CLEAR_UNLOCK_PAGE
|
1197 EXTENT_CLEAR_UNLOCK
|
1198 EXTENT_CLEAR_DELALLOC
|
1199 EXTENT_CLEAR_DIRTY
|
1200 EXTENT_SET_WRITEBACK
|
1201 EXTENT_END_WRITEBACK
);
1205 nolock
= btrfs_is_free_space_inode(inode
);
1208 trans
= btrfs_join_transaction_nolock(root
);
1210 trans
= btrfs_join_transaction(root
);
1212 if (IS_ERR(trans
)) {
1213 extent_clear_unlock_delalloc(inode
,
1214 &BTRFS_I(inode
)->io_tree
,
1215 start
, end
, locked_page
,
1216 EXTENT_CLEAR_UNLOCK_PAGE
|
1217 EXTENT_CLEAR_UNLOCK
|
1218 EXTENT_CLEAR_DELALLOC
|
1219 EXTENT_CLEAR_DIRTY
|
1220 EXTENT_SET_WRITEBACK
|
1221 EXTENT_END_WRITEBACK
);
1222 btrfs_free_path(path
);
1223 return PTR_ERR(trans
);
1226 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1228 cow_start
= (u64
)-1;
1231 ret
= btrfs_lookup_file_extent(trans
, root
, path
, ino
,
1234 btrfs_abort_transaction(trans
, root
, ret
);
1237 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1238 leaf
= path
->nodes
[0];
1239 btrfs_item_key_to_cpu(leaf
, &found_key
,
1240 path
->slots
[0] - 1);
1241 if (found_key
.objectid
== ino
&&
1242 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1247 leaf
= path
->nodes
[0];
1248 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1249 ret
= btrfs_next_leaf(root
, path
);
1251 btrfs_abort_transaction(trans
, root
, ret
);
1256 leaf
= path
->nodes
[0];
1262 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1264 if (found_key
.objectid
> ino
||
1265 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1266 found_key
.offset
> end
)
1269 if (found_key
.offset
> cur_offset
) {
1270 extent_end
= found_key
.offset
;
1275 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1276 struct btrfs_file_extent_item
);
1277 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1279 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1280 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1281 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1282 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1283 extent_end
= found_key
.offset
+
1284 btrfs_file_extent_num_bytes(leaf
, fi
);
1286 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1287 if (extent_end
<= start
) {
1291 if (disk_bytenr
== 0)
1293 if (btrfs_file_extent_compression(leaf
, fi
) ||
1294 btrfs_file_extent_encryption(leaf
, fi
) ||
1295 btrfs_file_extent_other_encoding(leaf
, fi
))
1297 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1299 if (btrfs_extent_readonly(root
, disk_bytenr
))
1301 if (btrfs_cross_ref_exist(trans
, root
, ino
,
1303 extent_offset
, disk_bytenr
))
1305 disk_bytenr
+= extent_offset
;
1306 disk_bytenr
+= cur_offset
- found_key
.offset
;
1307 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1309 * force cow if csum exists in the range.
1310 * this ensure that csum for a given extent are
1311 * either valid or do not exist.
1313 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1316 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1317 extent_end
= found_key
.offset
+
1318 btrfs_file_extent_inline_len(leaf
, fi
);
1319 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1324 if (extent_end
<= start
) {
1329 if (cow_start
== (u64
)-1)
1330 cow_start
= cur_offset
;
1331 cur_offset
= extent_end
;
1332 if (cur_offset
> end
)
1338 btrfs_release_path(path
);
1339 if (cow_start
!= (u64
)-1) {
1340 ret
= __cow_file_range(trans
, inode
, root
, locked_page
,
1341 cow_start
, found_key
.offset
- 1,
1342 page_started
, nr_written
, 1);
1344 btrfs_abort_transaction(trans
, root
, ret
);
1347 cow_start
= (u64
)-1;
1350 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1351 struct extent_map
*em
;
1352 struct extent_map_tree
*em_tree
;
1353 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1354 em
= alloc_extent_map();
1355 BUG_ON(!em
); /* -ENOMEM */
1356 em
->start
= cur_offset
;
1357 em
->orig_start
= found_key
.offset
- extent_offset
;
1358 em
->len
= num_bytes
;
1359 em
->block_len
= num_bytes
;
1360 em
->block_start
= disk_bytenr
;
1361 em
->orig_block_len
= disk_num_bytes
;
1362 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1363 em
->mod_start
= em
->start
;
1364 em
->mod_len
= em
->len
;
1365 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1366 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
1367 em
->generation
= -1;
1369 write_lock(&em_tree
->lock
);
1370 ret
= add_extent_mapping(em_tree
, em
);
1372 list_move(&em
->list
,
1373 &em_tree
->modified_extents
);
1374 write_unlock(&em_tree
->lock
);
1375 if (ret
!= -EEXIST
) {
1376 free_extent_map(em
);
1379 btrfs_drop_extent_cache(inode
, em
->start
,
1380 em
->start
+ em
->len
- 1, 0);
1382 type
= BTRFS_ORDERED_PREALLOC
;
1384 type
= BTRFS_ORDERED_NOCOW
;
1387 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1388 num_bytes
, num_bytes
, type
);
1389 BUG_ON(ret
); /* -ENOMEM */
1391 if (root
->root_key
.objectid
==
1392 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1393 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1396 btrfs_abort_transaction(trans
, root
, ret
);
1401 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
1402 cur_offset
, cur_offset
+ num_bytes
- 1,
1403 locked_page
, EXTENT_CLEAR_UNLOCK_PAGE
|
1404 EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
1405 EXTENT_SET_PRIVATE2
);
1406 cur_offset
= extent_end
;
1407 if (cur_offset
> end
)
1410 btrfs_release_path(path
);
1412 if (cur_offset
<= end
&& cow_start
== (u64
)-1) {
1413 cow_start
= cur_offset
;
1417 if (cow_start
!= (u64
)-1) {
1418 ret
= __cow_file_range(trans
, inode
, root
, locked_page
,
1420 page_started
, nr_written
, 1);
1422 btrfs_abort_transaction(trans
, root
, ret
);
1428 err
= btrfs_end_transaction(trans
, root
);
1432 if (ret
&& cur_offset
< end
)
1433 extent_clear_unlock_delalloc(inode
,
1434 &BTRFS_I(inode
)->io_tree
,
1435 cur_offset
, end
, locked_page
,
1436 EXTENT_CLEAR_UNLOCK_PAGE
|
1437 EXTENT_CLEAR_UNLOCK
|
1438 EXTENT_CLEAR_DELALLOC
|
1439 EXTENT_CLEAR_DIRTY
|
1440 EXTENT_SET_WRITEBACK
|
1441 EXTENT_END_WRITEBACK
);
1443 btrfs_free_path(path
);
1448 * extent_io.c call back to do delayed allocation processing
1450 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1451 u64 start
, u64 end
, int *page_started
,
1452 unsigned long *nr_written
)
1455 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1457 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) {
1458 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1459 page_started
, 1, nr_written
);
1460 } else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
) {
1461 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1462 page_started
, 0, nr_written
);
1463 } else if (!btrfs_test_opt(root
, COMPRESS
) &&
1464 !(BTRFS_I(inode
)->force_compress
) &&
1465 !(BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
)) {
1466 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1467 page_started
, nr_written
, 1);
1469 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1470 &BTRFS_I(inode
)->runtime_flags
);
1471 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1472 page_started
, nr_written
);
1477 static void btrfs_split_extent_hook(struct inode
*inode
,
1478 struct extent_state
*orig
, u64 split
)
1480 /* not delalloc, ignore it */
1481 if (!(orig
->state
& EXTENT_DELALLOC
))
1484 spin_lock(&BTRFS_I(inode
)->lock
);
1485 BTRFS_I(inode
)->outstanding_extents
++;
1486 spin_unlock(&BTRFS_I(inode
)->lock
);
1490 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1491 * extents so we can keep track of new extents that are just merged onto old
1492 * extents, such as when we are doing sequential writes, so we can properly
1493 * account for the metadata space we'll need.
1495 static void btrfs_merge_extent_hook(struct inode
*inode
,
1496 struct extent_state
*new,
1497 struct extent_state
*other
)
1499 /* not delalloc, ignore it */
1500 if (!(other
->state
& EXTENT_DELALLOC
))
1503 spin_lock(&BTRFS_I(inode
)->lock
);
1504 BTRFS_I(inode
)->outstanding_extents
--;
1505 spin_unlock(&BTRFS_I(inode
)->lock
);
1509 * extent_io.c set_bit_hook, used to track delayed allocation
1510 * bytes in this file, and to maintain the list of inodes that
1511 * have pending delalloc work to be done.
1513 static void btrfs_set_bit_hook(struct inode
*inode
,
1514 struct extent_state
*state
, int *bits
)
1518 * set_bit and clear bit hooks normally require _irqsave/restore
1519 * but in this case, we are only testing for the DELALLOC
1520 * bit, which is only set or cleared with irqs on
1522 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1523 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1524 u64 len
= state
->end
+ 1 - state
->start
;
1525 bool do_list
= !btrfs_is_free_space_inode(inode
);
1527 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1528 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1530 spin_lock(&BTRFS_I(inode
)->lock
);
1531 BTRFS_I(inode
)->outstanding_extents
++;
1532 spin_unlock(&BTRFS_I(inode
)->lock
);
1535 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, len
,
1536 root
->fs_info
->delalloc_batch
);
1537 spin_lock(&BTRFS_I(inode
)->lock
);
1538 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1539 if (do_list
&& !test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1540 &BTRFS_I(inode
)->runtime_flags
)) {
1541 spin_lock(&root
->fs_info
->delalloc_lock
);
1542 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1543 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1544 &root
->fs_info
->delalloc_inodes
);
1545 set_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1546 &BTRFS_I(inode
)->runtime_flags
);
1548 spin_unlock(&root
->fs_info
->delalloc_lock
);
1550 spin_unlock(&BTRFS_I(inode
)->lock
);
1555 * extent_io.c clear_bit_hook, see set_bit_hook for why
1557 static void btrfs_clear_bit_hook(struct inode
*inode
,
1558 struct extent_state
*state
, int *bits
)
1561 * set_bit and clear bit hooks normally require _irqsave/restore
1562 * but in this case, we are only testing for the DELALLOC
1563 * bit, which is only set or cleared with irqs on
1565 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1566 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1567 u64 len
= state
->end
+ 1 - state
->start
;
1568 bool do_list
= !btrfs_is_free_space_inode(inode
);
1570 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1571 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1572 } else if (!(*bits
& EXTENT_DO_ACCOUNTING
)) {
1573 spin_lock(&BTRFS_I(inode
)->lock
);
1574 BTRFS_I(inode
)->outstanding_extents
--;
1575 spin_unlock(&BTRFS_I(inode
)->lock
);
1578 if (*bits
& EXTENT_DO_ACCOUNTING
)
1579 btrfs_delalloc_release_metadata(inode
, len
);
1581 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1583 btrfs_free_reserved_data_space(inode
, len
);
1585 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, -len
,
1586 root
->fs_info
->delalloc_batch
);
1587 spin_lock(&BTRFS_I(inode
)->lock
);
1588 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1589 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1590 test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1591 &BTRFS_I(inode
)->runtime_flags
)) {
1592 spin_lock(&root
->fs_info
->delalloc_lock
);
1593 if (!list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1594 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1595 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1596 &BTRFS_I(inode
)->runtime_flags
);
1598 spin_unlock(&root
->fs_info
->delalloc_lock
);
1600 spin_unlock(&BTRFS_I(inode
)->lock
);
1605 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1606 * we don't create bios that span stripes or chunks
1608 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
1609 size_t size
, struct bio
*bio
,
1610 unsigned long bio_flags
)
1612 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1613 u64 logical
= (u64
)bio
->bi_sector
<< 9;
1618 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1621 length
= bio
->bi_size
;
1622 map_length
= length
;
1623 ret
= btrfs_map_block(root
->fs_info
, READ
, logical
,
1624 &map_length
, NULL
, 0);
1625 /* Will always return 0 with map_multi == NULL */
1627 if (map_length
< length
+ size
)
1633 * in order to insert checksums into the metadata in large chunks,
1634 * we wait until bio submission time. All the pages in the bio are
1635 * checksummed and sums are attached onto the ordered extent record.
1637 * At IO completion time the cums attached on the ordered extent record
1638 * are inserted into the btree
1640 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1641 struct bio
*bio
, int mirror_num
,
1642 unsigned long bio_flags
,
1645 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1648 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1649 BUG_ON(ret
); /* -ENOMEM */
1654 * in order to insert checksums into the metadata in large chunks,
1655 * we wait until bio submission time. All the pages in the bio are
1656 * checksummed and sums are attached onto the ordered extent record.
1658 * At IO completion time the cums attached on the ordered extent record
1659 * are inserted into the btree
1661 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1662 int mirror_num
, unsigned long bio_flags
,
1665 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1668 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1670 bio_endio(bio
, ret
);
1675 * extent_io.c submission hook. This does the right thing for csum calculation
1676 * on write, or reading the csums from the tree before a read
1678 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1679 int mirror_num
, unsigned long bio_flags
,
1682 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1686 int async
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
1688 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1690 if (btrfs_is_free_space_inode(inode
))
1693 if (!(rw
& REQ_WRITE
)) {
1694 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, metadata
);
1698 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1699 ret
= btrfs_submit_compressed_read(inode
, bio
,
1703 } else if (!skip_sum
) {
1704 ret
= btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1709 } else if (async
&& !skip_sum
) {
1710 /* csum items have already been cloned */
1711 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1713 /* we're doing a write, do the async checksumming */
1714 ret
= btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1715 inode
, rw
, bio
, mirror_num
,
1716 bio_flags
, bio_offset
,
1717 __btrfs_submit_bio_start
,
1718 __btrfs_submit_bio_done
);
1720 } else if (!skip_sum
) {
1721 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1727 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1731 bio_endio(bio
, ret
);
1736 * given a list of ordered sums record them in the inode. This happens
1737 * at IO completion time based on sums calculated at bio submission time.
1739 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1740 struct inode
*inode
, u64 file_offset
,
1741 struct list_head
*list
)
1743 struct btrfs_ordered_sum
*sum
;
1745 list_for_each_entry(sum
, list
, list
) {
1746 btrfs_csum_file_blocks(trans
,
1747 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1752 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1753 struct extent_state
**cached_state
)
1755 WARN_ON((end
& (PAGE_CACHE_SIZE
- 1)) == 0);
1756 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1757 cached_state
, GFP_NOFS
);
1760 /* see btrfs_writepage_start_hook for details on why this is required */
1761 struct btrfs_writepage_fixup
{
1763 struct btrfs_work work
;
1766 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1768 struct btrfs_writepage_fixup
*fixup
;
1769 struct btrfs_ordered_extent
*ordered
;
1770 struct extent_state
*cached_state
= NULL
;
1772 struct inode
*inode
;
1777 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1781 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1782 ClearPageChecked(page
);
1786 inode
= page
->mapping
->host
;
1787 page_start
= page_offset(page
);
1788 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1790 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1793 /* already ordered? We're done */
1794 if (PagePrivate2(page
))
1797 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1799 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1800 page_end
, &cached_state
, GFP_NOFS
);
1802 btrfs_start_ordered_extent(inode
, ordered
, 1);
1803 btrfs_put_ordered_extent(ordered
);
1807 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
1809 mapping_set_error(page
->mapping
, ret
);
1810 end_extent_writepage(page
, ret
, page_start
, page_end
);
1811 ClearPageChecked(page
);
1815 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1816 ClearPageChecked(page
);
1817 set_page_dirty(page
);
1819 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1820 &cached_state
, GFP_NOFS
);
1823 page_cache_release(page
);
1828 * There are a few paths in the higher layers of the kernel that directly
1829 * set the page dirty bit without asking the filesystem if it is a
1830 * good idea. This causes problems because we want to make sure COW
1831 * properly happens and the data=ordered rules are followed.
1833 * In our case any range that doesn't have the ORDERED bit set
1834 * hasn't been properly setup for IO. We kick off an async process
1835 * to fix it up. The async helper will wait for ordered extents, set
1836 * the delalloc bit and make it safe to write the page.
1838 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1840 struct inode
*inode
= page
->mapping
->host
;
1841 struct btrfs_writepage_fixup
*fixup
;
1842 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1844 /* this page is properly in the ordered list */
1845 if (TestClearPagePrivate2(page
))
1848 if (PageChecked(page
))
1851 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1855 SetPageChecked(page
);
1856 page_cache_get(page
);
1857 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
1859 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
1863 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1864 struct inode
*inode
, u64 file_pos
,
1865 u64 disk_bytenr
, u64 disk_num_bytes
,
1866 u64 num_bytes
, u64 ram_bytes
,
1867 u8 compression
, u8 encryption
,
1868 u16 other_encoding
, int extent_type
)
1870 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1871 struct btrfs_file_extent_item
*fi
;
1872 struct btrfs_path
*path
;
1873 struct extent_buffer
*leaf
;
1874 struct btrfs_key ins
;
1877 path
= btrfs_alloc_path();
1881 path
->leave_spinning
= 1;
1884 * we may be replacing one extent in the tree with another.
1885 * The new extent is pinned in the extent map, and we don't want
1886 * to drop it from the cache until it is completely in the btree.
1888 * So, tell btrfs_drop_extents to leave this extent in the cache.
1889 * the caller is expected to unpin it and allow it to be merged
1892 ret
= btrfs_drop_extents(trans
, root
, inode
, file_pos
,
1893 file_pos
+ num_bytes
, 0);
1897 ins
.objectid
= btrfs_ino(inode
);
1898 ins
.offset
= file_pos
;
1899 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1900 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
, sizeof(*fi
));
1903 leaf
= path
->nodes
[0];
1904 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1905 struct btrfs_file_extent_item
);
1906 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1907 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1908 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1909 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1910 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1911 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1912 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1913 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1914 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1915 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1917 btrfs_mark_buffer_dirty(leaf
);
1918 btrfs_release_path(path
);
1920 inode_add_bytes(inode
, num_bytes
);
1922 ins
.objectid
= disk_bytenr
;
1923 ins
.offset
= disk_num_bytes
;
1924 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1925 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
1926 root
->root_key
.objectid
,
1927 btrfs_ino(inode
), file_pos
, &ins
);
1929 btrfs_free_path(path
);
1935 * helper function for btrfs_finish_ordered_io, this
1936 * just reads in some of the csum leaves to prime them into ram
1937 * before we start the transaction. It limits the amount of btree
1938 * reads required while inside the transaction.
1940 /* as ordered data IO finishes, this gets called so we can finish
1941 * an ordered extent if the range of bytes in the file it covers are
1944 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
)
1946 struct inode
*inode
= ordered_extent
->inode
;
1947 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1948 struct btrfs_trans_handle
*trans
= NULL
;
1949 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1950 struct extent_state
*cached_state
= NULL
;
1951 int compress_type
= 0;
1955 nolock
= btrfs_is_free_space_inode(inode
);
1957 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered_extent
->flags
)) {
1962 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
1963 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
1964 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
1966 trans
= btrfs_join_transaction_nolock(root
);
1968 trans
= btrfs_join_transaction(root
);
1969 if (IS_ERR(trans
)) {
1970 ret
= PTR_ERR(trans
);
1974 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1975 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
1976 if (ret
) /* -ENOMEM or corruption */
1977 btrfs_abort_transaction(trans
, root
, ret
);
1981 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
1982 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
1986 trans
= btrfs_join_transaction_nolock(root
);
1988 trans
= btrfs_join_transaction(root
);
1989 if (IS_ERR(trans
)) {
1990 ret
= PTR_ERR(trans
);
1994 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1996 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
1997 compress_type
= ordered_extent
->compress_type
;
1998 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
1999 BUG_ON(compress_type
);
2000 ret
= btrfs_mark_extent_written(trans
, inode
,
2001 ordered_extent
->file_offset
,
2002 ordered_extent
->file_offset
+
2003 ordered_extent
->len
);
2005 BUG_ON(root
== root
->fs_info
->tree_root
);
2006 ret
= insert_reserved_file_extent(trans
, inode
,
2007 ordered_extent
->file_offset
,
2008 ordered_extent
->start
,
2009 ordered_extent
->disk_len
,
2010 ordered_extent
->len
,
2011 ordered_extent
->len
,
2012 compress_type
, 0, 0,
2013 BTRFS_FILE_EXTENT_REG
);
2015 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
2016 ordered_extent
->file_offset
, ordered_extent
->len
,
2019 btrfs_abort_transaction(trans
, root
, ret
);
2023 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
2024 &ordered_extent
->list
);
2026 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2027 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2028 if (ret
) { /* -ENOMEM or corruption */
2029 btrfs_abort_transaction(trans
, root
, ret
);
2034 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
2035 ordered_extent
->file_offset
+
2036 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
2038 if (root
!= root
->fs_info
->tree_root
)
2039 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
2041 btrfs_end_transaction(trans
, root
);
2044 clear_extent_uptodate(io_tree
, ordered_extent
->file_offset
,
2045 ordered_extent
->file_offset
+
2046 ordered_extent
->len
- 1, NULL
, GFP_NOFS
);
2049 * If the ordered extent had an IOERR or something else went
2050 * wrong we need to return the space for this ordered extent
2051 * back to the allocator.
2053 if (!test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
2054 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
))
2055 btrfs_free_reserved_extent(root
, ordered_extent
->start
,
2056 ordered_extent
->disk_len
);
2061 * This needs to be done to make sure anybody waiting knows we are done
2062 * updating everything for this ordered extent.
2064 btrfs_remove_ordered_extent(inode
, ordered_extent
);
2067 btrfs_put_ordered_extent(ordered_extent
);
2068 /* once for the tree */
2069 btrfs_put_ordered_extent(ordered_extent
);
2074 static void finish_ordered_fn(struct btrfs_work
*work
)
2076 struct btrfs_ordered_extent
*ordered_extent
;
2077 ordered_extent
= container_of(work
, struct btrfs_ordered_extent
, work
);
2078 btrfs_finish_ordered_io(ordered_extent
);
2081 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2082 struct extent_state
*state
, int uptodate
)
2084 struct inode
*inode
= page
->mapping
->host
;
2085 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2086 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
2087 struct btrfs_workers
*workers
;
2089 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
2091 ClearPagePrivate2(page
);
2092 if (!btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
2093 end
- start
+ 1, uptodate
))
2096 ordered_extent
->work
.func
= finish_ordered_fn
;
2097 ordered_extent
->work
.flags
= 0;
2099 if (btrfs_is_free_space_inode(inode
))
2100 workers
= &root
->fs_info
->endio_freespace_worker
;
2102 workers
= &root
->fs_info
->endio_write_workers
;
2103 btrfs_queue_worker(workers
, &ordered_extent
->work
);
2109 * when reads are done, we need to check csums to verify the data is correct
2110 * if there's a match, we allow the bio to finish. If not, the code in
2111 * extent_io.c will try to find good copies for us.
2113 static int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2114 struct extent_state
*state
, int mirror
)
2116 size_t offset
= start
- page_offset(page
);
2117 struct inode
*inode
= page
->mapping
->host
;
2118 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2120 u64
private = ~(u32
)0;
2122 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2125 if (PageChecked(page
)) {
2126 ClearPageChecked(page
);
2130 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
2133 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
2134 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
2135 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
2140 if (state
&& state
->start
== start
) {
2141 private = state
->private;
2144 ret
= get_state_private(io_tree
, start
, &private);
2146 kaddr
= kmap_atomic(page
);
2150 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
2151 btrfs_csum_final(csum
, (char *)&csum
);
2152 if (csum
!= private)
2155 kunmap_atomic(kaddr
);
2160 printk_ratelimited(KERN_INFO
"btrfs csum failed ino %llu off %llu csum %u "
2162 (unsigned long long)btrfs_ino(page
->mapping
->host
),
2163 (unsigned long long)start
, csum
,
2164 (unsigned long long)private);
2165 memset(kaddr
+ offset
, 1, end
- start
+ 1);
2166 flush_dcache_page(page
);
2167 kunmap_atomic(kaddr
);
2173 struct delayed_iput
{
2174 struct list_head list
;
2175 struct inode
*inode
;
2178 /* JDM: If this is fs-wide, why can't we add a pointer to
2179 * btrfs_inode instead and avoid the allocation? */
2180 void btrfs_add_delayed_iput(struct inode
*inode
)
2182 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2183 struct delayed_iput
*delayed
;
2185 if (atomic_add_unless(&inode
->i_count
, -1, 1))
2188 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
2189 delayed
->inode
= inode
;
2191 spin_lock(&fs_info
->delayed_iput_lock
);
2192 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
2193 spin_unlock(&fs_info
->delayed_iput_lock
);
2196 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
2199 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2200 struct delayed_iput
*delayed
;
2203 spin_lock(&fs_info
->delayed_iput_lock
);
2204 empty
= list_empty(&fs_info
->delayed_iputs
);
2205 spin_unlock(&fs_info
->delayed_iput_lock
);
2209 spin_lock(&fs_info
->delayed_iput_lock
);
2210 list_splice_init(&fs_info
->delayed_iputs
, &list
);
2211 spin_unlock(&fs_info
->delayed_iput_lock
);
2213 while (!list_empty(&list
)) {
2214 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
2215 list_del(&delayed
->list
);
2216 iput(delayed
->inode
);
2222 * This is called in transaction commit time. If there are no orphan
2223 * files in the subvolume, it removes orphan item and frees block_rsv
2226 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
2227 struct btrfs_root
*root
)
2229 struct btrfs_block_rsv
*block_rsv
;
2232 if (atomic_read(&root
->orphan_inodes
) ||
2233 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
2236 spin_lock(&root
->orphan_lock
);
2237 if (atomic_read(&root
->orphan_inodes
)) {
2238 spin_unlock(&root
->orphan_lock
);
2242 if (root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
) {
2243 spin_unlock(&root
->orphan_lock
);
2247 block_rsv
= root
->orphan_block_rsv
;
2248 root
->orphan_block_rsv
= NULL
;
2249 spin_unlock(&root
->orphan_lock
);
2251 if (root
->orphan_item_inserted
&&
2252 btrfs_root_refs(&root
->root_item
) > 0) {
2253 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
2254 root
->root_key
.objectid
);
2256 root
->orphan_item_inserted
= 0;
2260 WARN_ON(block_rsv
->size
> 0);
2261 btrfs_free_block_rsv(root
, block_rsv
);
2266 * This creates an orphan entry for the given inode in case something goes
2267 * wrong in the middle of an unlink/truncate.
2269 * NOTE: caller of this function should reserve 5 units of metadata for
2272 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2274 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2275 struct btrfs_block_rsv
*block_rsv
= NULL
;
2280 if (!root
->orphan_block_rsv
) {
2281 block_rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
2286 spin_lock(&root
->orphan_lock
);
2287 if (!root
->orphan_block_rsv
) {
2288 root
->orphan_block_rsv
= block_rsv
;
2289 } else if (block_rsv
) {
2290 btrfs_free_block_rsv(root
, block_rsv
);
2294 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2295 &BTRFS_I(inode
)->runtime_flags
)) {
2298 * For proper ENOSPC handling, we should do orphan
2299 * cleanup when mounting. But this introduces backward
2300 * compatibility issue.
2302 if (!xchg(&root
->orphan_item_inserted
, 1))
2308 atomic_inc(&root
->orphan_inodes
);
2311 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
2312 &BTRFS_I(inode
)->runtime_flags
))
2314 spin_unlock(&root
->orphan_lock
);
2316 /* grab metadata reservation from transaction handle */
2318 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
2319 BUG_ON(ret
); /* -ENOSPC in reservation; Logic error? JDM */
2322 /* insert an orphan item to track this unlinked/truncated file */
2324 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
2325 if (ret
&& ret
!= -EEXIST
) {
2326 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2327 &BTRFS_I(inode
)->runtime_flags
);
2328 btrfs_abort_transaction(trans
, root
, ret
);
2334 /* insert an orphan item to track subvolume contains orphan files */
2336 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
2337 root
->root_key
.objectid
);
2338 if (ret
&& ret
!= -EEXIST
) {
2339 btrfs_abort_transaction(trans
, root
, ret
);
2347 * We have done the truncate/delete so we can go ahead and remove the orphan
2348 * item for this particular inode.
2350 int btrfs_orphan_del(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2352 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2353 int delete_item
= 0;
2354 int release_rsv
= 0;
2357 spin_lock(&root
->orphan_lock
);
2358 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2359 &BTRFS_I(inode
)->runtime_flags
))
2362 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
2363 &BTRFS_I(inode
)->runtime_flags
))
2365 spin_unlock(&root
->orphan_lock
);
2367 if (trans
&& delete_item
) {
2368 ret
= btrfs_del_orphan_item(trans
, root
, btrfs_ino(inode
));
2369 BUG_ON(ret
); /* -ENOMEM or corruption (JDM: Recheck) */
2373 btrfs_orphan_release_metadata(inode
);
2374 atomic_dec(&root
->orphan_inodes
);
2381 * this cleans up any orphans that may be left on the list from the last use
2384 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
2386 struct btrfs_path
*path
;
2387 struct extent_buffer
*leaf
;
2388 struct btrfs_key key
, found_key
;
2389 struct btrfs_trans_handle
*trans
;
2390 struct inode
*inode
;
2391 u64 last_objectid
= 0;
2392 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
2394 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
2397 path
= btrfs_alloc_path();
2404 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
2405 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
2406 key
.offset
= (u64
)-1;
2409 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2414 * if ret == 0 means we found what we were searching for, which
2415 * is weird, but possible, so only screw with path if we didn't
2416 * find the key and see if we have stuff that matches
2420 if (path
->slots
[0] == 0)
2425 /* pull out the item */
2426 leaf
= path
->nodes
[0];
2427 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2429 /* make sure the item matches what we want */
2430 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
2432 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
2435 /* release the path since we're done with it */
2436 btrfs_release_path(path
);
2439 * this is where we are basically btrfs_lookup, without the
2440 * crossing root thing. we store the inode number in the
2441 * offset of the orphan item.
2444 if (found_key
.offset
== last_objectid
) {
2445 printk(KERN_ERR
"btrfs: Error removing orphan entry, "
2446 "stopping orphan cleanup\n");
2451 last_objectid
= found_key
.offset
;
2453 found_key
.objectid
= found_key
.offset
;
2454 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
2455 found_key
.offset
= 0;
2456 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
2457 ret
= PTR_RET(inode
);
2458 if (ret
&& ret
!= -ESTALE
)
2461 if (ret
== -ESTALE
&& root
== root
->fs_info
->tree_root
) {
2462 struct btrfs_root
*dead_root
;
2463 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2464 int is_dead_root
= 0;
2467 * this is an orphan in the tree root. Currently these
2468 * could come from 2 sources:
2469 * a) a snapshot deletion in progress
2470 * b) a free space cache inode
2471 * We need to distinguish those two, as the snapshot
2472 * orphan must not get deleted.
2473 * find_dead_roots already ran before us, so if this
2474 * is a snapshot deletion, we should find the root
2475 * in the dead_roots list
2477 spin_lock(&fs_info
->trans_lock
);
2478 list_for_each_entry(dead_root
, &fs_info
->dead_roots
,
2480 if (dead_root
->root_key
.objectid
==
2481 found_key
.objectid
) {
2486 spin_unlock(&fs_info
->trans_lock
);
2488 /* prevent this orphan from being found again */
2489 key
.offset
= found_key
.objectid
- 1;
2494 * Inode is already gone but the orphan item is still there,
2495 * kill the orphan item.
2497 if (ret
== -ESTALE
) {
2498 trans
= btrfs_start_transaction(root
, 1);
2499 if (IS_ERR(trans
)) {
2500 ret
= PTR_ERR(trans
);
2503 printk(KERN_ERR
"auto deleting %Lu\n",
2504 found_key
.objectid
);
2505 ret
= btrfs_del_orphan_item(trans
, root
,
2506 found_key
.objectid
);
2507 BUG_ON(ret
); /* -ENOMEM or corruption (JDM: Recheck) */
2508 btrfs_end_transaction(trans
, root
);
2513 * add this inode to the orphan list so btrfs_orphan_del does
2514 * the proper thing when we hit it
2516 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2517 &BTRFS_I(inode
)->runtime_flags
);
2518 atomic_inc(&root
->orphan_inodes
);
2520 /* if we have links, this was a truncate, lets do that */
2521 if (inode
->i_nlink
) {
2522 if (!S_ISREG(inode
->i_mode
)) {
2529 /* 1 for the orphan item deletion. */
2530 trans
= btrfs_start_transaction(root
, 1);
2531 if (IS_ERR(trans
)) {
2532 ret
= PTR_ERR(trans
);
2535 ret
= btrfs_orphan_add(trans
, inode
);
2536 btrfs_end_transaction(trans
, root
);
2540 ret
= btrfs_truncate(inode
);
2545 /* this will do delete_inode and everything for us */
2550 /* release the path since we're done with it */
2551 btrfs_release_path(path
);
2553 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
2555 if (root
->orphan_block_rsv
)
2556 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
2559 if (root
->orphan_block_rsv
|| root
->orphan_item_inserted
) {
2560 trans
= btrfs_join_transaction(root
);
2562 btrfs_end_transaction(trans
, root
);
2566 printk(KERN_INFO
"btrfs: unlinked %d orphans\n", nr_unlink
);
2568 printk(KERN_INFO
"btrfs: truncated %d orphans\n", nr_truncate
);
2572 printk(KERN_CRIT
"btrfs: could not do orphan cleanup %d\n", ret
);
2573 btrfs_free_path(path
);
2578 * very simple check to peek ahead in the leaf looking for xattrs. If we
2579 * don't find any xattrs, we know there can't be any acls.
2581 * slot is the slot the inode is in, objectid is the objectid of the inode
2583 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
2584 int slot
, u64 objectid
)
2586 u32 nritems
= btrfs_header_nritems(leaf
);
2587 struct btrfs_key found_key
;
2591 while (slot
< nritems
) {
2592 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2594 /* we found a different objectid, there must not be acls */
2595 if (found_key
.objectid
!= objectid
)
2598 /* we found an xattr, assume we've got an acl */
2599 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
)
2603 * we found a key greater than an xattr key, there can't
2604 * be any acls later on
2606 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
2613 * it goes inode, inode backrefs, xattrs, extents,
2614 * so if there are a ton of hard links to an inode there can
2615 * be a lot of backrefs. Don't waste time searching too hard,
2616 * this is just an optimization
2621 /* we hit the end of the leaf before we found an xattr or
2622 * something larger than an xattr. We have to assume the inode
2629 * read an inode from the btree into the in-memory inode
2631 static void btrfs_read_locked_inode(struct inode
*inode
)
2633 struct btrfs_path
*path
;
2634 struct extent_buffer
*leaf
;
2635 struct btrfs_inode_item
*inode_item
;
2636 struct btrfs_timespec
*tspec
;
2637 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2638 struct btrfs_key location
;
2642 bool filled
= false;
2644 ret
= btrfs_fill_inode(inode
, &rdev
);
2648 path
= btrfs_alloc_path();
2652 path
->leave_spinning
= 1;
2653 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
2655 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
2659 leaf
= path
->nodes
[0];
2664 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2665 struct btrfs_inode_item
);
2666 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
2667 set_nlink(inode
, btrfs_inode_nlink(leaf
, inode_item
));
2668 i_uid_write(inode
, btrfs_inode_uid(leaf
, inode_item
));
2669 i_gid_write(inode
, btrfs_inode_gid(leaf
, inode_item
));
2670 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
2672 tspec
= btrfs_inode_atime(inode_item
);
2673 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2674 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2676 tspec
= btrfs_inode_mtime(inode_item
);
2677 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2678 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2680 tspec
= btrfs_inode_ctime(inode_item
);
2681 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2682 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2684 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
2685 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
2686 BTRFS_I(inode
)->last_trans
= btrfs_inode_transid(leaf
, inode_item
);
2689 * If we were modified in the current generation and evicted from memory
2690 * and then re-read we need to do a full sync since we don't have any
2691 * idea about which extents were modified before we were evicted from
2694 if (BTRFS_I(inode
)->last_trans
== root
->fs_info
->generation
)
2695 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
2696 &BTRFS_I(inode
)->runtime_flags
);
2698 inode
->i_version
= btrfs_inode_sequence(leaf
, inode_item
);
2699 inode
->i_generation
= BTRFS_I(inode
)->generation
;
2701 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
2703 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
2704 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
2707 * try to precache a NULL acl entry for files that don't have
2708 * any xattrs or acls
2710 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
2713 cache_no_acl(inode
);
2715 btrfs_free_path(path
);
2717 switch (inode
->i_mode
& S_IFMT
) {
2719 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2720 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2721 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2722 inode
->i_fop
= &btrfs_file_operations
;
2723 inode
->i_op
= &btrfs_file_inode_operations
;
2726 inode
->i_fop
= &btrfs_dir_file_operations
;
2727 if (root
== root
->fs_info
->tree_root
)
2728 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
2730 inode
->i_op
= &btrfs_dir_inode_operations
;
2733 inode
->i_op
= &btrfs_symlink_inode_operations
;
2734 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2735 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2738 inode
->i_op
= &btrfs_special_inode_operations
;
2739 init_special_inode(inode
, inode
->i_mode
, rdev
);
2743 btrfs_update_iflags(inode
);
2747 btrfs_free_path(path
);
2748 make_bad_inode(inode
);
2752 * given a leaf and an inode, copy the inode fields into the leaf
2754 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
2755 struct extent_buffer
*leaf
,
2756 struct btrfs_inode_item
*item
,
2757 struct inode
*inode
)
2759 struct btrfs_map_token token
;
2761 btrfs_init_map_token(&token
);
2763 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
2764 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
2765 btrfs_set_token_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
,
2767 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
2768 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
2770 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_atime(item
),
2771 inode
->i_atime
.tv_sec
, &token
);
2772 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_atime(item
),
2773 inode
->i_atime
.tv_nsec
, &token
);
2775 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_mtime(item
),
2776 inode
->i_mtime
.tv_sec
, &token
);
2777 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
2778 inode
->i_mtime
.tv_nsec
, &token
);
2780 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_ctime(item
),
2781 inode
->i_ctime
.tv_sec
, &token
);
2782 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
2783 inode
->i_ctime
.tv_nsec
, &token
);
2785 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
2787 btrfs_set_token_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
,
2789 btrfs_set_token_inode_sequence(leaf
, item
, inode
->i_version
, &token
);
2790 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
2791 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
2792 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
2793 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
2797 * copy everything in the in-memory inode into the btree.
2799 static noinline
int btrfs_update_inode_item(struct btrfs_trans_handle
*trans
,
2800 struct btrfs_root
*root
, struct inode
*inode
)
2802 struct btrfs_inode_item
*inode_item
;
2803 struct btrfs_path
*path
;
2804 struct extent_buffer
*leaf
;
2807 path
= btrfs_alloc_path();
2811 path
->leave_spinning
= 1;
2812 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
2820 btrfs_unlock_up_safe(path
, 1);
2821 leaf
= path
->nodes
[0];
2822 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2823 struct btrfs_inode_item
);
2825 fill_inode_item(trans
, leaf
, inode_item
, inode
);
2826 btrfs_mark_buffer_dirty(leaf
);
2827 btrfs_set_inode_last_trans(trans
, inode
);
2830 btrfs_free_path(path
);
2835 * copy everything in the in-memory inode into the btree.
2837 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
2838 struct btrfs_root
*root
, struct inode
*inode
)
2843 * If the inode is a free space inode, we can deadlock during commit
2844 * if we put it into the delayed code.
2846 * The data relocation inode should also be directly updated
2849 if (!btrfs_is_free_space_inode(inode
)
2850 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
) {
2851 btrfs_update_root_times(trans
, root
);
2853 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
2855 btrfs_set_inode_last_trans(trans
, inode
);
2859 return btrfs_update_inode_item(trans
, root
, inode
);
2862 noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
2863 struct btrfs_root
*root
,
2864 struct inode
*inode
)
2868 ret
= btrfs_update_inode(trans
, root
, inode
);
2870 return btrfs_update_inode_item(trans
, root
, inode
);
2875 * unlink helper that gets used here in inode.c and in the tree logging
2876 * recovery code. It remove a link in a directory with a given name, and
2877 * also drops the back refs in the inode to the directory
2879 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
2880 struct btrfs_root
*root
,
2881 struct inode
*dir
, struct inode
*inode
,
2882 const char *name
, int name_len
)
2884 struct btrfs_path
*path
;
2886 struct extent_buffer
*leaf
;
2887 struct btrfs_dir_item
*di
;
2888 struct btrfs_key key
;
2890 u64 ino
= btrfs_ino(inode
);
2891 u64 dir_ino
= btrfs_ino(dir
);
2893 path
= btrfs_alloc_path();
2899 path
->leave_spinning
= 1;
2900 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
2901 name
, name_len
, -1);
2910 leaf
= path
->nodes
[0];
2911 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2912 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2915 btrfs_release_path(path
);
2917 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
2920 printk(KERN_INFO
"btrfs failed to delete reference to %.*s, "
2921 "inode %llu parent %llu\n", name_len
, name
,
2922 (unsigned long long)ino
, (unsigned long long)dir_ino
);
2923 btrfs_abort_transaction(trans
, root
, ret
);
2927 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
2929 btrfs_abort_transaction(trans
, root
, ret
);
2933 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
2935 if (ret
!= 0 && ret
!= -ENOENT
) {
2936 btrfs_abort_transaction(trans
, root
, ret
);
2940 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
2945 btrfs_free_path(path
);
2949 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
2950 inode_inc_iversion(inode
);
2951 inode_inc_iversion(dir
);
2952 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
2953 ret
= btrfs_update_inode(trans
, root
, dir
);
2958 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
2959 struct btrfs_root
*root
,
2960 struct inode
*dir
, struct inode
*inode
,
2961 const char *name
, int name_len
)
2964 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
2966 btrfs_drop_nlink(inode
);
2967 ret
= btrfs_update_inode(trans
, root
, inode
);
2973 /* helper to check if there is any shared block in the path */
2974 static int check_path_shared(struct btrfs_root
*root
,
2975 struct btrfs_path
*path
)
2977 struct extent_buffer
*eb
;
2981 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
2984 if (!path
->nodes
[level
])
2986 eb
= path
->nodes
[level
];
2987 if (!btrfs_block_can_be_shared(root
, eb
))
2989 ret
= btrfs_lookup_extent_info(NULL
, root
, eb
->start
, eb
->len
,
2998 * helper to start transaction for unlink and rmdir.
3000 * unlink and rmdir are special in btrfs, they do not always free space.
3001 * so in enospc case, we should make sure they will free space before
3002 * allowing them to use the global metadata reservation.
3004 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
,
3005 struct dentry
*dentry
)
3007 struct btrfs_trans_handle
*trans
;
3008 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3009 struct btrfs_path
*path
;
3010 struct btrfs_dir_item
*di
;
3011 struct inode
*inode
= dentry
->d_inode
;
3016 u64 ino
= btrfs_ino(inode
);
3017 u64 dir_ino
= btrfs_ino(dir
);
3020 * 1 for the possible orphan item
3021 * 1 for the dir item
3022 * 1 for the dir index
3023 * 1 for the inode ref
3024 * 1 for the inode ref in the tree log
3025 * 2 for the dir entries in the log
3028 trans
= btrfs_start_transaction(root
, 8);
3029 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
3032 if (ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
3033 return ERR_PTR(-ENOSPC
);
3035 /* check if there is someone else holds reference */
3036 if (S_ISDIR(inode
->i_mode
) && atomic_read(&inode
->i_count
) > 1)
3037 return ERR_PTR(-ENOSPC
);
3039 if (atomic_read(&inode
->i_count
) > 2)
3040 return ERR_PTR(-ENOSPC
);
3042 if (xchg(&root
->fs_info
->enospc_unlink
, 1))
3043 return ERR_PTR(-ENOSPC
);
3045 path
= btrfs_alloc_path();
3047 root
->fs_info
->enospc_unlink
= 0;
3048 return ERR_PTR(-ENOMEM
);
3051 /* 1 for the orphan item */
3052 trans
= btrfs_start_transaction(root
, 1);
3053 if (IS_ERR(trans
)) {
3054 btrfs_free_path(path
);
3055 root
->fs_info
->enospc_unlink
= 0;
3059 path
->skip_locking
= 1;
3060 path
->search_commit_root
= 1;
3062 ret
= btrfs_lookup_inode(trans
, root
, path
,
3063 &BTRFS_I(dir
)->location
, 0);
3069 if (check_path_shared(root
, path
))
3074 btrfs_release_path(path
);
3076 ret
= btrfs_lookup_inode(trans
, root
, path
,
3077 &BTRFS_I(inode
)->location
, 0);
3083 if (check_path_shared(root
, path
))
3088 btrfs_release_path(path
);
3090 if (ret
== 0 && S_ISREG(inode
->i_mode
)) {
3091 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
3097 BUG_ON(ret
== 0); /* Corruption */
3098 if (check_path_shared(root
, path
))
3100 btrfs_release_path(path
);
3108 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3109 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
3115 if (check_path_shared(root
, path
))
3121 btrfs_release_path(path
);
3123 ret
= btrfs_get_inode_ref_index(trans
, root
, path
, dentry
->d_name
.name
,
3124 dentry
->d_name
.len
, ino
, dir_ino
, 0,
3131 if (check_path_shared(root
, path
))
3134 btrfs_release_path(path
);
3137 * This is a commit root search, if we can lookup inode item and other
3138 * relative items in the commit root, it means the transaction of
3139 * dir/file creation has been committed, and the dir index item that we
3140 * delay to insert has also been inserted into the commit root. So
3141 * we needn't worry about the delayed insertion of the dir index item
3144 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir_ino
, index
,
3145 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
3150 BUG_ON(ret
== -ENOENT
);
3151 if (check_path_shared(root
, path
))
3156 btrfs_free_path(path
);
3157 /* Migrate the orphan reservation over */
3159 err
= btrfs_block_rsv_migrate(trans
->block_rsv
,
3160 &root
->fs_info
->global_block_rsv
,
3161 trans
->bytes_reserved
);
3164 btrfs_end_transaction(trans
, root
);
3165 root
->fs_info
->enospc_unlink
= 0;
3166 return ERR_PTR(err
);
3169 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
3173 static void __unlink_end_trans(struct btrfs_trans_handle
*trans
,
3174 struct btrfs_root
*root
)
3176 if (trans
->block_rsv
->type
== BTRFS_BLOCK_RSV_GLOBAL
) {
3177 btrfs_block_rsv_release(root
, trans
->block_rsv
,
3178 trans
->bytes_reserved
);
3179 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
3180 BUG_ON(!root
->fs_info
->enospc_unlink
);
3181 root
->fs_info
->enospc_unlink
= 0;
3183 btrfs_end_transaction(trans
, root
);
3186 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
3188 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3189 struct btrfs_trans_handle
*trans
;
3190 struct inode
*inode
= dentry
->d_inode
;
3193 trans
= __unlink_start_trans(dir
, dentry
);
3195 return PTR_ERR(trans
);
3197 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
3199 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3200 dentry
->d_name
.name
, dentry
->d_name
.len
);
3204 if (inode
->i_nlink
== 0) {
3205 ret
= btrfs_orphan_add(trans
, inode
);
3211 __unlink_end_trans(trans
, root
);
3212 btrfs_btree_balance_dirty(root
);
3216 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
3217 struct btrfs_root
*root
,
3218 struct inode
*dir
, u64 objectid
,
3219 const char *name
, int name_len
)
3221 struct btrfs_path
*path
;
3222 struct extent_buffer
*leaf
;
3223 struct btrfs_dir_item
*di
;
3224 struct btrfs_key key
;
3227 u64 dir_ino
= btrfs_ino(dir
);
3229 path
= btrfs_alloc_path();
3233 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3234 name
, name_len
, -1);
3235 if (IS_ERR_OR_NULL(di
)) {
3243 leaf
= path
->nodes
[0];
3244 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3245 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
3246 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3248 btrfs_abort_transaction(trans
, root
, ret
);
3251 btrfs_release_path(path
);
3253 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
3254 objectid
, root
->root_key
.objectid
,
3255 dir_ino
, &index
, name
, name_len
);
3257 if (ret
!= -ENOENT
) {
3258 btrfs_abort_transaction(trans
, root
, ret
);
3261 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
3263 if (IS_ERR_OR_NULL(di
)) {
3268 btrfs_abort_transaction(trans
, root
, ret
);
3272 leaf
= path
->nodes
[0];
3273 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3274 btrfs_release_path(path
);
3277 btrfs_release_path(path
);
3279 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3281 btrfs_abort_transaction(trans
, root
, ret
);
3285 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3286 inode_inc_iversion(dir
);
3287 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3288 ret
= btrfs_update_inode_fallback(trans
, root
, dir
);
3290 btrfs_abort_transaction(trans
, root
, ret
);
3292 btrfs_free_path(path
);
3296 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3298 struct inode
*inode
= dentry
->d_inode
;
3300 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3301 struct btrfs_trans_handle
*trans
;
3303 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
3305 if (btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
)
3308 trans
= __unlink_start_trans(dir
, dentry
);
3310 return PTR_ERR(trans
);
3312 if (unlikely(btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
3313 err
= btrfs_unlink_subvol(trans
, root
, dir
,
3314 BTRFS_I(inode
)->location
.objectid
,
3315 dentry
->d_name
.name
,
3316 dentry
->d_name
.len
);
3320 err
= btrfs_orphan_add(trans
, inode
);
3324 /* now the directory is empty */
3325 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3326 dentry
->d_name
.name
, dentry
->d_name
.len
);
3328 btrfs_i_size_write(inode
, 0);
3330 __unlink_end_trans(trans
, root
);
3331 btrfs_btree_balance_dirty(root
);
3337 * this can truncate away extent items, csum items and directory items.
3338 * It starts at a high offset and removes keys until it can't find
3339 * any higher than new_size
3341 * csum items that cross the new i_size are truncated to the new size
3344 * min_type is the minimum key type to truncate down to. If set to 0, this
3345 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3347 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
3348 struct btrfs_root
*root
,
3349 struct inode
*inode
,
3350 u64 new_size
, u32 min_type
)
3352 struct btrfs_path
*path
;
3353 struct extent_buffer
*leaf
;
3354 struct btrfs_file_extent_item
*fi
;
3355 struct btrfs_key key
;
3356 struct btrfs_key found_key
;
3357 u64 extent_start
= 0;
3358 u64 extent_num_bytes
= 0;
3359 u64 extent_offset
= 0;
3361 u64 mask
= root
->sectorsize
- 1;
3362 u32 found_type
= (u8
)-1;
3365 int pending_del_nr
= 0;
3366 int pending_del_slot
= 0;
3367 int extent_type
= -1;
3370 u64 ino
= btrfs_ino(inode
);
3372 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
3374 path
= btrfs_alloc_path();
3380 * We want to drop from the next block forward in case this new size is
3381 * not block aligned since we will be keeping the last block of the
3382 * extent just the way it is.
3384 if (root
->ref_cows
|| root
== root
->fs_info
->tree_root
)
3385 btrfs_drop_extent_cache(inode
, (new_size
+ mask
) & (~mask
), (u64
)-1, 0);
3388 * This function is also used to drop the items in the log tree before
3389 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3390 * it is used to drop the loged items. So we shouldn't kill the delayed
3393 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
3394 btrfs_kill_delayed_inode_items(inode
);
3397 key
.offset
= (u64
)-1;
3401 path
->leave_spinning
= 1;
3402 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3409 /* there are no items in the tree for us to truncate, we're
3412 if (path
->slots
[0] == 0)
3419 leaf
= path
->nodes
[0];
3420 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3421 found_type
= btrfs_key_type(&found_key
);
3423 if (found_key
.objectid
!= ino
)
3426 if (found_type
< min_type
)
3429 item_end
= found_key
.offset
;
3430 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
3431 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3432 struct btrfs_file_extent_item
);
3433 extent_type
= btrfs_file_extent_type(leaf
, fi
);
3434 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3436 btrfs_file_extent_num_bytes(leaf
, fi
);
3437 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3438 item_end
+= btrfs_file_extent_inline_len(leaf
,
3443 if (found_type
> min_type
) {
3446 if (item_end
< new_size
)
3448 if (found_key
.offset
>= new_size
)
3454 /* FIXME, shrink the extent if the ref count is only 1 */
3455 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
3458 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3460 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
3462 u64 orig_num_bytes
=
3463 btrfs_file_extent_num_bytes(leaf
, fi
);
3464 extent_num_bytes
= new_size
-
3465 found_key
.offset
+ root
->sectorsize
- 1;
3466 extent_num_bytes
= extent_num_bytes
&
3467 ~((u64
)root
->sectorsize
- 1);
3468 btrfs_set_file_extent_num_bytes(leaf
, fi
,
3470 num_dec
= (orig_num_bytes
-
3472 if (root
->ref_cows
&& extent_start
!= 0)
3473 inode_sub_bytes(inode
, num_dec
);
3474 btrfs_mark_buffer_dirty(leaf
);
3477 btrfs_file_extent_disk_num_bytes(leaf
,
3479 extent_offset
= found_key
.offset
-
3480 btrfs_file_extent_offset(leaf
, fi
);
3482 /* FIXME blocksize != 4096 */
3483 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
3484 if (extent_start
!= 0) {
3487 inode_sub_bytes(inode
, num_dec
);
3490 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3492 * we can't truncate inline items that have had
3496 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
3497 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
3498 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
3499 u32 size
= new_size
- found_key
.offset
;
3501 if (root
->ref_cows
) {
3502 inode_sub_bytes(inode
, item_end
+ 1 -
3506 btrfs_file_extent_calc_inline_size(size
);
3507 btrfs_truncate_item(trans
, root
, path
,
3509 } else if (root
->ref_cows
) {
3510 inode_sub_bytes(inode
, item_end
+ 1 -
3516 if (!pending_del_nr
) {
3517 /* no pending yet, add ourselves */
3518 pending_del_slot
= path
->slots
[0];
3520 } else if (pending_del_nr
&&
3521 path
->slots
[0] + 1 == pending_del_slot
) {
3522 /* hop on the pending chunk */
3524 pending_del_slot
= path
->slots
[0];
3531 if (found_extent
&& (root
->ref_cows
||
3532 root
== root
->fs_info
->tree_root
)) {
3533 btrfs_set_path_blocking(path
);
3534 ret
= btrfs_free_extent(trans
, root
, extent_start
,
3535 extent_num_bytes
, 0,
3536 btrfs_header_owner(leaf
),
3537 ino
, extent_offset
, 0);
3541 if (found_type
== BTRFS_INODE_ITEM_KEY
)
3544 if (path
->slots
[0] == 0 ||
3545 path
->slots
[0] != pending_del_slot
) {
3546 if (pending_del_nr
) {
3547 ret
= btrfs_del_items(trans
, root
, path
,
3551 btrfs_abort_transaction(trans
,
3557 btrfs_release_path(path
);
3564 if (pending_del_nr
) {
3565 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
3568 btrfs_abort_transaction(trans
, root
, ret
);
3571 btrfs_free_path(path
);
3576 * btrfs_truncate_page - read, zero a chunk and write a page
3577 * @inode - inode that we're zeroing
3578 * @from - the offset to start zeroing
3579 * @len - the length to zero, 0 to zero the entire range respective to the
3581 * @front - zero up to the offset instead of from the offset on
3583 * This will find the page for the "from" offset and cow the page and zero the
3584 * part we want to zero. This is used with truncate and hole punching.
3586 int btrfs_truncate_page(struct inode
*inode
, loff_t from
, loff_t len
,
3589 struct address_space
*mapping
= inode
->i_mapping
;
3590 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3591 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3592 struct btrfs_ordered_extent
*ordered
;
3593 struct extent_state
*cached_state
= NULL
;
3595 u32 blocksize
= root
->sectorsize
;
3596 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
3597 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
3599 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
3604 if ((offset
& (blocksize
- 1)) == 0 &&
3605 (!len
|| ((len
& (blocksize
- 1)) == 0)))
3607 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
3612 page
= find_or_create_page(mapping
, index
, mask
);
3614 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
3619 page_start
= page_offset(page
);
3620 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3622 if (!PageUptodate(page
)) {
3623 ret
= btrfs_readpage(NULL
, page
);
3625 if (page
->mapping
!= mapping
) {
3627 page_cache_release(page
);
3630 if (!PageUptodate(page
)) {
3635 wait_on_page_writeback(page
);
3637 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
3638 set_page_extent_mapped(page
);
3640 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
3642 unlock_extent_cached(io_tree
, page_start
, page_end
,
3643 &cached_state
, GFP_NOFS
);
3645 page_cache_release(page
);
3646 btrfs_start_ordered_extent(inode
, ordered
, 1);
3647 btrfs_put_ordered_extent(ordered
);
3651 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
3652 EXTENT_DIRTY
| EXTENT_DELALLOC
|
3653 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
3654 0, 0, &cached_state
, GFP_NOFS
);
3656 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
3659 unlock_extent_cached(io_tree
, page_start
, page_end
,
3660 &cached_state
, GFP_NOFS
);
3664 if (offset
!= PAGE_CACHE_SIZE
) {
3666 len
= PAGE_CACHE_SIZE
- offset
;
3669 memset(kaddr
, 0, offset
);
3671 memset(kaddr
+ offset
, 0, len
);
3672 flush_dcache_page(page
);
3675 ClearPageChecked(page
);
3676 set_page_dirty(page
);
3677 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
3682 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
3684 page_cache_release(page
);
3690 * This function puts in dummy file extents for the area we're creating a hole
3691 * for. So if we are truncating this file to a larger size we need to insert
3692 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3693 * the range between oldsize and size
3695 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
3697 struct btrfs_trans_handle
*trans
;
3698 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3699 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3700 struct extent_map
*em
= NULL
;
3701 struct extent_state
*cached_state
= NULL
;
3702 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
3703 u64 mask
= root
->sectorsize
- 1;
3704 u64 hole_start
= (oldsize
+ mask
) & ~mask
;
3705 u64 block_end
= (size
+ mask
) & ~mask
;
3711 if (size
<= hole_start
)
3715 struct btrfs_ordered_extent
*ordered
;
3716 btrfs_wait_ordered_range(inode
, hole_start
,
3717 block_end
- hole_start
);
3718 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
3720 ordered
= btrfs_lookup_ordered_extent(inode
, hole_start
);
3723 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
3724 &cached_state
, GFP_NOFS
);
3725 btrfs_put_ordered_extent(ordered
);
3728 cur_offset
= hole_start
;
3730 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
3731 block_end
- cur_offset
, 0);
3737 last_byte
= min(extent_map_end(em
), block_end
);
3738 last_byte
= (last_byte
+ mask
) & ~mask
;
3739 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
3740 struct extent_map
*hole_em
;
3741 hole_size
= last_byte
- cur_offset
;
3743 trans
= btrfs_start_transaction(root
, 3);
3744 if (IS_ERR(trans
)) {
3745 err
= PTR_ERR(trans
);
3749 err
= btrfs_drop_extents(trans
, root
, inode
,
3751 cur_offset
+ hole_size
, 1);
3753 btrfs_abort_transaction(trans
, root
, err
);
3754 btrfs_end_transaction(trans
, root
);
3758 err
= btrfs_insert_file_extent(trans
, root
,
3759 btrfs_ino(inode
), cur_offset
, 0,
3760 0, hole_size
, 0, hole_size
,
3763 btrfs_abort_transaction(trans
, root
, err
);
3764 btrfs_end_transaction(trans
, root
);
3768 btrfs_drop_extent_cache(inode
, cur_offset
,
3769 cur_offset
+ hole_size
- 1, 0);
3770 hole_em
= alloc_extent_map();
3772 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3773 &BTRFS_I(inode
)->runtime_flags
);
3776 hole_em
->start
= cur_offset
;
3777 hole_em
->len
= hole_size
;
3778 hole_em
->orig_start
= cur_offset
;
3780 hole_em
->block_start
= EXTENT_MAP_HOLE
;
3781 hole_em
->block_len
= 0;
3782 hole_em
->orig_block_len
= 0;
3783 hole_em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
3784 hole_em
->compress_type
= BTRFS_COMPRESS_NONE
;
3785 hole_em
->generation
= trans
->transid
;
3788 write_lock(&em_tree
->lock
);
3789 err
= add_extent_mapping(em_tree
, hole_em
);
3791 list_move(&hole_em
->list
,
3792 &em_tree
->modified_extents
);
3793 write_unlock(&em_tree
->lock
);
3796 btrfs_drop_extent_cache(inode
, cur_offset
,
3800 free_extent_map(hole_em
);
3802 btrfs_update_inode(trans
, root
, inode
);
3803 btrfs_end_transaction(trans
, root
);
3805 free_extent_map(em
);
3807 cur_offset
= last_byte
;
3808 if (cur_offset
>= block_end
)
3812 free_extent_map(em
);
3813 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
3818 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
)
3820 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3821 struct btrfs_trans_handle
*trans
;
3822 loff_t oldsize
= i_size_read(inode
);
3823 loff_t newsize
= attr
->ia_size
;
3824 int mask
= attr
->ia_valid
;
3827 if (newsize
== oldsize
)
3831 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
3832 * special case where we need to update the times despite not having
3833 * these flags set. For all other operations the VFS set these flags
3834 * explicitly if it wants a timestamp update.
3836 if (newsize
!= oldsize
&& (!(mask
& (ATTR_CTIME
| ATTR_MTIME
))))
3837 inode
->i_ctime
= inode
->i_mtime
= current_fs_time(inode
->i_sb
);
3839 if (newsize
> oldsize
) {
3840 truncate_pagecache(inode
, oldsize
, newsize
);
3841 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
3845 trans
= btrfs_start_transaction(root
, 1);
3847 return PTR_ERR(trans
);
3849 i_size_write(inode
, newsize
);
3850 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
3851 ret
= btrfs_update_inode(trans
, root
, inode
);
3852 btrfs_end_transaction(trans
, root
);
3856 * We're truncating a file that used to have good data down to
3857 * zero. Make sure it gets into the ordered flush list so that
3858 * any new writes get down to disk quickly.
3861 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
3862 &BTRFS_I(inode
)->runtime_flags
);
3865 * 1 for the orphan item we're going to add
3866 * 1 for the orphan item deletion.
3868 trans
= btrfs_start_transaction(root
, 2);
3870 return PTR_ERR(trans
);
3873 * We need to do this in case we fail at _any_ point during the
3874 * actual truncate. Once we do the truncate_setsize we could
3875 * invalidate pages which forces any outstanding ordered io to
3876 * be instantly completed which will give us extents that need
3877 * to be truncated. If we fail to get an orphan inode down we
3878 * could have left over extents that were never meant to live,
3879 * so we need to garuntee from this point on that everything
3880 * will be consistent.
3882 ret
= btrfs_orphan_add(trans
, inode
);
3883 btrfs_end_transaction(trans
, root
);
3887 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3888 truncate_setsize(inode
, newsize
);
3889 ret
= btrfs_truncate(inode
);
3890 if (ret
&& inode
->i_nlink
)
3891 btrfs_orphan_del(NULL
, inode
);
3897 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
3899 struct inode
*inode
= dentry
->d_inode
;
3900 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3903 if (btrfs_root_readonly(root
))
3906 err
= inode_change_ok(inode
, attr
);
3910 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
3911 err
= btrfs_setsize(inode
, attr
);
3916 if (attr
->ia_valid
) {
3917 setattr_copy(inode
, attr
);
3918 inode_inc_iversion(inode
);
3919 err
= btrfs_dirty_inode(inode
);
3921 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
3922 err
= btrfs_acl_chmod(inode
);
3928 void btrfs_evict_inode(struct inode
*inode
)
3930 struct btrfs_trans_handle
*trans
;
3931 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3932 struct btrfs_block_rsv
*rsv
, *global_rsv
;
3933 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
3936 trace_btrfs_inode_evict(inode
);
3938 truncate_inode_pages(&inode
->i_data
, 0);
3939 if (inode
->i_nlink
&& (btrfs_root_refs(&root
->root_item
) != 0 ||
3940 btrfs_is_free_space_inode(inode
)))
3943 if (is_bad_inode(inode
)) {
3944 btrfs_orphan_del(NULL
, inode
);
3947 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
3948 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
3950 if (root
->fs_info
->log_root_recovering
) {
3951 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3952 &BTRFS_I(inode
)->runtime_flags
));
3956 if (inode
->i_nlink
> 0) {
3957 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0);
3961 ret
= btrfs_commit_inode_delayed_inode(inode
);
3963 btrfs_orphan_del(NULL
, inode
);
3967 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
3969 btrfs_orphan_del(NULL
, inode
);
3972 rsv
->size
= min_size
;
3974 global_rsv
= &root
->fs_info
->global_block_rsv
;
3976 btrfs_i_size_write(inode
, 0);
3979 * This is a bit simpler than btrfs_truncate since we've already
3980 * reserved our space for our orphan item in the unlink, so we just
3981 * need to reserve some slack space in case we add bytes and update
3982 * inode item when doing the truncate.
3985 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
,
3986 BTRFS_RESERVE_FLUSH_LIMIT
);
3989 * Try and steal from the global reserve since we will
3990 * likely not use this space anyway, we want to try as
3991 * hard as possible to get this to work.
3994 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
, min_size
);
3997 printk(KERN_WARNING
"Could not get space for a "
3998 "delete, will truncate on mount %d\n", ret
);
3999 btrfs_orphan_del(NULL
, inode
);
4000 btrfs_free_block_rsv(root
, rsv
);
4004 trans
= btrfs_join_transaction(root
);
4005 if (IS_ERR(trans
)) {
4006 btrfs_orphan_del(NULL
, inode
);
4007 btrfs_free_block_rsv(root
, rsv
);
4011 trans
->block_rsv
= rsv
;
4013 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
4017 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4018 btrfs_end_transaction(trans
, root
);
4020 btrfs_btree_balance_dirty(root
);
4023 btrfs_free_block_rsv(root
, rsv
);
4026 trans
->block_rsv
= root
->orphan_block_rsv
;
4027 ret
= btrfs_orphan_del(trans
, inode
);
4031 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4032 if (!(root
== root
->fs_info
->tree_root
||
4033 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
4034 btrfs_return_ino(root
, btrfs_ino(inode
));
4036 btrfs_end_transaction(trans
, root
);
4037 btrfs_btree_balance_dirty(root
);
4044 * this returns the key found in the dir entry in the location pointer.
4045 * If no dir entries were found, location->objectid is 0.
4047 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
4048 struct btrfs_key
*location
)
4050 const char *name
= dentry
->d_name
.name
;
4051 int namelen
= dentry
->d_name
.len
;
4052 struct btrfs_dir_item
*di
;
4053 struct btrfs_path
*path
;
4054 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4057 path
= btrfs_alloc_path();
4061 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(dir
), name
,
4066 if (IS_ERR_OR_NULL(di
))
4069 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
4071 btrfs_free_path(path
);
4074 location
->objectid
= 0;
4079 * when we hit a tree root in a directory, the btrfs part of the inode
4080 * needs to be changed to reflect the root directory of the tree root. This
4081 * is kind of like crossing a mount point.
4083 static int fixup_tree_root_location(struct btrfs_root
*root
,
4085 struct dentry
*dentry
,
4086 struct btrfs_key
*location
,
4087 struct btrfs_root
**sub_root
)
4089 struct btrfs_path
*path
;
4090 struct btrfs_root
*new_root
;
4091 struct btrfs_root_ref
*ref
;
4092 struct extent_buffer
*leaf
;
4096 path
= btrfs_alloc_path();
4103 ret
= btrfs_find_root_ref(root
->fs_info
->tree_root
, path
,
4104 BTRFS_I(dir
)->root
->root_key
.objectid
,
4105 location
->objectid
);
4112 leaf
= path
->nodes
[0];
4113 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
4114 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(dir
) ||
4115 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
4118 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
4119 (unsigned long)(ref
+ 1),
4120 dentry
->d_name
.len
);
4124 btrfs_release_path(path
);
4126 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
4127 if (IS_ERR(new_root
)) {
4128 err
= PTR_ERR(new_root
);
4132 if (btrfs_root_refs(&new_root
->root_item
) == 0) {
4137 *sub_root
= new_root
;
4138 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
4139 location
->type
= BTRFS_INODE_ITEM_KEY
;
4140 location
->offset
= 0;
4143 btrfs_free_path(path
);
4147 static void inode_tree_add(struct inode
*inode
)
4149 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4150 struct btrfs_inode
*entry
;
4152 struct rb_node
*parent
;
4153 u64 ino
= btrfs_ino(inode
);
4155 p
= &root
->inode_tree
.rb_node
;
4158 if (inode_unhashed(inode
))
4161 spin_lock(&root
->inode_lock
);
4164 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
4166 if (ino
< btrfs_ino(&entry
->vfs_inode
))
4167 p
= &parent
->rb_left
;
4168 else if (ino
> btrfs_ino(&entry
->vfs_inode
))
4169 p
= &parent
->rb_right
;
4171 WARN_ON(!(entry
->vfs_inode
.i_state
&
4172 (I_WILL_FREE
| I_FREEING
)));
4173 rb_erase(parent
, &root
->inode_tree
);
4174 RB_CLEAR_NODE(parent
);
4175 spin_unlock(&root
->inode_lock
);
4179 rb_link_node(&BTRFS_I(inode
)->rb_node
, parent
, p
);
4180 rb_insert_color(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4181 spin_unlock(&root
->inode_lock
);
4184 static void inode_tree_del(struct inode
*inode
)
4186 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4189 spin_lock(&root
->inode_lock
);
4190 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
4191 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4192 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
4193 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4195 spin_unlock(&root
->inode_lock
);
4198 * Free space cache has inodes in the tree root, but the tree root has a
4199 * root_refs of 0, so this could end up dropping the tree root as a
4200 * snapshot, so we need the extra !root->fs_info->tree_root check to
4201 * make sure we don't drop it.
4203 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0 &&
4204 root
!= root
->fs_info
->tree_root
) {
4205 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
4206 spin_lock(&root
->inode_lock
);
4207 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4208 spin_unlock(&root
->inode_lock
);
4210 btrfs_add_dead_root(root
);
4214 void btrfs_invalidate_inodes(struct btrfs_root
*root
)
4216 struct rb_node
*node
;
4217 struct rb_node
*prev
;
4218 struct btrfs_inode
*entry
;
4219 struct inode
*inode
;
4222 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
4224 spin_lock(&root
->inode_lock
);
4226 node
= root
->inode_tree
.rb_node
;
4230 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4232 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
4233 node
= node
->rb_left
;
4234 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
4235 node
= node
->rb_right
;
4241 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
4242 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
4246 prev
= rb_next(prev
);
4250 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4251 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
4252 inode
= igrab(&entry
->vfs_inode
);
4254 spin_unlock(&root
->inode_lock
);
4255 if (atomic_read(&inode
->i_count
) > 1)
4256 d_prune_aliases(inode
);
4258 * btrfs_drop_inode will have it removed from
4259 * the inode cache when its usage count
4264 spin_lock(&root
->inode_lock
);
4268 if (cond_resched_lock(&root
->inode_lock
))
4271 node
= rb_next(node
);
4273 spin_unlock(&root
->inode_lock
);
4276 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
4278 struct btrfs_iget_args
*args
= p
;
4279 inode
->i_ino
= args
->ino
;
4280 BTRFS_I(inode
)->root
= args
->root
;
4284 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
4286 struct btrfs_iget_args
*args
= opaque
;
4287 return args
->ino
== btrfs_ino(inode
) &&
4288 args
->root
== BTRFS_I(inode
)->root
;
4291 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
4293 struct btrfs_root
*root
)
4295 struct inode
*inode
;
4296 struct btrfs_iget_args args
;
4297 args
.ino
= objectid
;
4300 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
4301 btrfs_init_locked_inode
,
4306 /* Get an inode object given its location and corresponding root.
4307 * Returns in *is_new if the inode was read from disk
4309 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
4310 struct btrfs_root
*root
, int *new)
4312 struct inode
*inode
;
4314 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
4316 return ERR_PTR(-ENOMEM
);
4318 if (inode
->i_state
& I_NEW
) {
4319 BTRFS_I(inode
)->root
= root
;
4320 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
4321 btrfs_read_locked_inode(inode
);
4322 if (!is_bad_inode(inode
)) {
4323 inode_tree_add(inode
);
4324 unlock_new_inode(inode
);
4328 unlock_new_inode(inode
);
4330 inode
= ERR_PTR(-ESTALE
);
4337 static struct inode
*new_simple_dir(struct super_block
*s
,
4338 struct btrfs_key
*key
,
4339 struct btrfs_root
*root
)
4341 struct inode
*inode
= new_inode(s
);
4344 return ERR_PTR(-ENOMEM
);
4346 BTRFS_I(inode
)->root
= root
;
4347 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
4348 set_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
);
4350 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
4351 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
4352 inode
->i_fop
= &simple_dir_operations
;
4353 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
4354 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4359 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
4361 struct inode
*inode
;
4362 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4363 struct btrfs_root
*sub_root
= root
;
4364 struct btrfs_key location
;
4368 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
4369 return ERR_PTR(-ENAMETOOLONG
);
4371 if (unlikely(d_need_lookup(dentry
))) {
4372 memcpy(&location
, dentry
->d_fsdata
, sizeof(struct btrfs_key
));
4373 kfree(dentry
->d_fsdata
);
4374 dentry
->d_fsdata
= NULL
;
4375 /* This thing is hashed, drop it for now */
4378 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
);
4450 if (unlikely(d_need_lookup(dentry
))) {
4451 spin_lock(&dentry
->d_lock
);
4452 dentry
->d_flags
&= ~DCACHE_NEED_LOOKUP
;
4453 spin_unlock(&dentry
->d_lock
);
4458 unsigned char btrfs_filetype_table
[] = {
4459 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
4462 static int btrfs_real_readdir(struct file
*filp
, void *dirent
,
4465 struct inode
*inode
= filp
->f_dentry
->d_inode
;
4466 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4467 struct btrfs_item
*item
;
4468 struct btrfs_dir_item
*di
;
4469 struct btrfs_key key
;
4470 struct btrfs_key found_key
;
4471 struct btrfs_path
*path
;
4472 struct list_head ins_list
;
4473 struct list_head del_list
;
4475 struct extent_buffer
*leaf
;
4477 unsigned char d_type
;
4482 int key_type
= BTRFS_DIR_INDEX_KEY
;
4486 int is_curr
= 0; /* filp->f_pos points to the current index? */
4488 /* FIXME, use a real flag for deciding about the key type */
4489 if (root
->fs_info
->tree_root
== root
)
4490 key_type
= BTRFS_DIR_ITEM_KEY
;
4492 /* special case for "." */
4493 if (filp
->f_pos
== 0) {
4494 over
= filldir(dirent
, ".", 1,
4495 filp
->f_pos
, btrfs_ino(inode
), DT_DIR
);
4500 /* special case for .., just use the back ref */
4501 if (filp
->f_pos
== 1) {
4502 u64 pino
= parent_ino(filp
->f_path
.dentry
);
4503 over
= filldir(dirent
, "..", 2,
4504 filp
->f_pos
, pino
, DT_DIR
);
4509 path
= btrfs_alloc_path();
4515 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
4516 INIT_LIST_HEAD(&ins_list
);
4517 INIT_LIST_HEAD(&del_list
);
4518 btrfs_get_delayed_items(inode
, &ins_list
, &del_list
);
4521 btrfs_set_key_type(&key
, key_type
);
4522 key
.offset
= filp
->f_pos
;
4523 key
.objectid
= btrfs_ino(inode
);
4525 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4530 leaf
= path
->nodes
[0];
4531 slot
= path
->slots
[0];
4532 if (slot
>= btrfs_header_nritems(leaf
)) {
4533 ret
= btrfs_next_leaf(root
, path
);
4541 item
= btrfs_item_nr(leaf
, slot
);
4542 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
4544 if (found_key
.objectid
!= key
.objectid
)
4546 if (btrfs_key_type(&found_key
) != key_type
)
4548 if (found_key
.offset
< filp
->f_pos
)
4550 if (key_type
== BTRFS_DIR_INDEX_KEY
&&
4551 btrfs_should_delete_dir_index(&del_list
,
4555 filp
->f_pos
= found_key
.offset
;
4558 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
4560 di_total
= btrfs_item_size(leaf
, item
);
4562 while (di_cur
< di_total
) {
4563 struct btrfs_key location
;
4565 if (verify_dir_item(root
, leaf
, di
))
4568 name_len
= btrfs_dir_name_len(leaf
, di
);
4569 if (name_len
<= sizeof(tmp_name
)) {
4570 name_ptr
= tmp_name
;
4572 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
4578 read_extent_buffer(leaf
, name_ptr
,
4579 (unsigned long)(di
+ 1), name_len
);
4581 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
4582 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
4585 /* is this a reference to our own snapshot? If so
4588 * In contrast to old kernels, we insert the snapshot's
4589 * dir item and dir index after it has been created, so
4590 * we won't find a reference to our own snapshot. We
4591 * still keep the following code for backward
4594 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
4595 location
.objectid
== root
->root_key
.objectid
) {
4599 over
= filldir(dirent
, name_ptr
, name_len
,
4600 found_key
.offset
, location
.objectid
,
4604 if (name_ptr
!= tmp_name
)
4609 di_len
= btrfs_dir_name_len(leaf
, di
) +
4610 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
4612 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
4618 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
4621 ret
= btrfs_readdir_delayed_dir_index(filp
, dirent
, filldir
,
4627 /* Reached end of directory/root. Bump pos past the last item. */
4628 if (key_type
== BTRFS_DIR_INDEX_KEY
)
4630 * 32-bit glibc will use getdents64, but then strtol -
4631 * so the last number we can serve is this.
4633 filp
->f_pos
= 0x7fffffff;
4639 if (key_type
== BTRFS_DIR_INDEX_KEY
)
4640 btrfs_put_delayed_items(&ins_list
, &del_list
);
4641 btrfs_free_path(path
);
4645 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
4647 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4648 struct btrfs_trans_handle
*trans
;
4650 bool nolock
= false;
4652 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
4655 if (btrfs_fs_closing(root
->fs_info
) && btrfs_is_free_space_inode(inode
))
4658 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
4660 trans
= btrfs_join_transaction_nolock(root
);
4662 trans
= btrfs_join_transaction(root
);
4664 return PTR_ERR(trans
);
4665 ret
= btrfs_commit_transaction(trans
, root
);
4671 * This is somewhat expensive, updating the tree every time the
4672 * inode changes. But, it is most likely to find the inode in cache.
4673 * FIXME, needs more benchmarking...there are no reasons other than performance
4674 * to keep or drop this code.
4676 int btrfs_dirty_inode(struct inode
*inode
)
4678 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4679 struct btrfs_trans_handle
*trans
;
4682 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
4685 trans
= btrfs_join_transaction(root
);
4687 return PTR_ERR(trans
);
4689 ret
= btrfs_update_inode(trans
, root
, inode
);
4690 if (ret
&& ret
== -ENOSPC
) {
4691 /* whoops, lets try again with the full transaction */
4692 btrfs_end_transaction(trans
, root
);
4693 trans
= btrfs_start_transaction(root
, 1);
4695 return PTR_ERR(trans
);
4697 ret
= btrfs_update_inode(trans
, root
, inode
);
4699 btrfs_end_transaction(trans
, root
);
4700 if (BTRFS_I(inode
)->delayed_node
)
4701 btrfs_balance_delayed_items(root
);
4707 * This is a copy of file_update_time. We need this so we can return error on
4708 * ENOSPC for updating the inode in the case of file write and mmap writes.
4710 static int btrfs_update_time(struct inode
*inode
, struct timespec
*now
,
4713 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4715 if (btrfs_root_readonly(root
))
4718 if (flags
& S_VERSION
)
4719 inode_inc_iversion(inode
);
4720 if (flags
& S_CTIME
)
4721 inode
->i_ctime
= *now
;
4722 if (flags
& S_MTIME
)
4723 inode
->i_mtime
= *now
;
4724 if (flags
& S_ATIME
)
4725 inode
->i_atime
= *now
;
4726 return btrfs_dirty_inode(inode
);
4730 * find the highest existing sequence number in a directory
4731 * and then set the in-memory index_cnt variable to reflect
4732 * free sequence numbers
4734 static int btrfs_set_inode_index_count(struct inode
*inode
)
4736 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4737 struct btrfs_key key
, found_key
;
4738 struct btrfs_path
*path
;
4739 struct extent_buffer
*leaf
;
4742 key
.objectid
= btrfs_ino(inode
);
4743 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
4744 key
.offset
= (u64
)-1;
4746 path
= btrfs_alloc_path();
4750 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4753 /* FIXME: we should be able to handle this */
4759 * MAGIC NUMBER EXPLANATION:
4760 * since we search a directory based on f_pos we have to start at 2
4761 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4762 * else has to start at 2
4764 if (path
->slots
[0] == 0) {
4765 BTRFS_I(inode
)->index_cnt
= 2;
4771 leaf
= path
->nodes
[0];
4772 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4774 if (found_key
.objectid
!= btrfs_ino(inode
) ||
4775 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
4776 BTRFS_I(inode
)->index_cnt
= 2;
4780 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
4782 btrfs_free_path(path
);
4787 * helper to find a free sequence number in a given directory. This current
4788 * code is very simple, later versions will do smarter things in the btree
4790 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
4794 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
4795 ret
= btrfs_inode_delayed_dir_index_count(dir
);
4797 ret
= btrfs_set_inode_index_count(dir
);
4803 *index
= BTRFS_I(dir
)->index_cnt
;
4804 BTRFS_I(dir
)->index_cnt
++;
4809 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
4810 struct btrfs_root
*root
,
4812 const char *name
, int name_len
,
4813 u64 ref_objectid
, u64 objectid
,
4814 umode_t mode
, u64
*index
)
4816 struct inode
*inode
;
4817 struct btrfs_inode_item
*inode_item
;
4818 struct btrfs_key
*location
;
4819 struct btrfs_path
*path
;
4820 struct btrfs_inode_ref
*ref
;
4821 struct btrfs_key key
[2];
4827 path
= btrfs_alloc_path();
4829 return ERR_PTR(-ENOMEM
);
4831 inode
= new_inode(root
->fs_info
->sb
);
4833 btrfs_free_path(path
);
4834 return ERR_PTR(-ENOMEM
);
4838 * we have to initialize this early, so we can reclaim the inode
4839 * number if we fail afterwards in this function.
4841 inode
->i_ino
= objectid
;
4844 trace_btrfs_inode_request(dir
);
4846 ret
= btrfs_set_inode_index(dir
, index
);
4848 btrfs_free_path(path
);
4850 return ERR_PTR(ret
);
4854 * index_cnt is ignored for everything but a dir,
4855 * btrfs_get_inode_index_count has an explanation for the magic
4858 BTRFS_I(inode
)->index_cnt
= 2;
4859 BTRFS_I(inode
)->root
= root
;
4860 BTRFS_I(inode
)->generation
= trans
->transid
;
4861 inode
->i_generation
= BTRFS_I(inode
)->generation
;
4864 * We could have gotten an inode number from somebody who was fsynced
4865 * and then removed in this same transaction, so let's just set full
4866 * sync since it will be a full sync anyway and this will blow away the
4867 * old info in the log.
4869 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
4876 key
[0].objectid
= objectid
;
4877 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
4881 * Start new inodes with an inode_ref. This is slightly more
4882 * efficient for small numbers of hard links since they will
4883 * be packed into one item. Extended refs will kick in if we
4884 * add more hard links than can fit in the ref item.
4886 key
[1].objectid
= objectid
;
4887 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
4888 key
[1].offset
= ref_objectid
;
4890 sizes
[0] = sizeof(struct btrfs_inode_item
);
4891 sizes
[1] = name_len
+ sizeof(*ref
);
4893 path
->leave_spinning
= 1;
4894 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
4898 inode_init_owner(inode
, dir
, mode
);
4899 inode_set_bytes(inode
, 0);
4900 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4901 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4902 struct btrfs_inode_item
);
4903 memset_extent_buffer(path
->nodes
[0], 0, (unsigned long)inode_item
,
4904 sizeof(*inode_item
));
4905 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
4907 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
4908 struct btrfs_inode_ref
);
4909 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
4910 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
4911 ptr
= (unsigned long)(ref
+ 1);
4912 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
4914 btrfs_mark_buffer_dirty(path
->nodes
[0]);
4915 btrfs_free_path(path
);
4917 location
= &BTRFS_I(inode
)->location
;
4918 location
->objectid
= objectid
;
4919 location
->offset
= 0;
4920 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
4922 btrfs_inherit_iflags(inode
, dir
);
4924 if (S_ISREG(mode
)) {
4925 if (btrfs_test_opt(root
, NODATASUM
))
4926 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
4927 if (btrfs_test_opt(root
, NODATACOW
))
4928 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
;
4931 insert_inode_hash(inode
);
4932 inode_tree_add(inode
);
4934 trace_btrfs_inode_new(inode
);
4935 btrfs_set_inode_last_trans(trans
, inode
);
4937 btrfs_update_root_times(trans
, root
);
4942 BTRFS_I(dir
)->index_cnt
--;
4943 btrfs_free_path(path
);
4945 return ERR_PTR(ret
);
4948 static inline u8
btrfs_inode_type(struct inode
*inode
)
4950 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
4954 * utility function to add 'inode' into 'parent_inode' with
4955 * a give name and a given sequence number.
4956 * if 'add_backref' is true, also insert a backref from the
4957 * inode to the parent directory.
4959 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
4960 struct inode
*parent_inode
, struct inode
*inode
,
4961 const char *name
, int name_len
, int add_backref
, u64 index
)
4964 struct btrfs_key key
;
4965 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
4966 u64 ino
= btrfs_ino(inode
);
4967 u64 parent_ino
= btrfs_ino(parent_inode
);
4969 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4970 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
4973 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
4977 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4978 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
4979 key
.objectid
, root
->root_key
.objectid
,
4980 parent_ino
, index
, name
, name_len
);
4981 } else if (add_backref
) {
4982 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
4986 /* Nothing to clean up yet */
4990 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
4992 btrfs_inode_type(inode
), index
);
4993 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
)
4996 btrfs_abort_transaction(trans
, root
, ret
);
5000 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
5002 inode_inc_iversion(parent_inode
);
5003 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
5004 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
5006 btrfs_abort_transaction(trans
, root
, ret
);
5010 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5013 err
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
5014 key
.objectid
, root
->root_key
.objectid
,
5015 parent_ino
, &local_index
, name
, name_len
);
5017 } else if (add_backref
) {
5021 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
5022 ino
, parent_ino
, &local_index
);
5027 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
5028 struct inode
*dir
, struct dentry
*dentry
,
5029 struct inode
*inode
, int backref
, u64 index
)
5031 int err
= btrfs_add_link(trans
, dir
, inode
,
5032 dentry
->d_name
.name
, dentry
->d_name
.len
,
5039 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
5040 umode_t mode
, dev_t rdev
)
5042 struct btrfs_trans_handle
*trans
;
5043 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5044 struct inode
*inode
= NULL
;
5050 if (!new_valid_dev(rdev
))
5054 * 2 for inode item and ref
5056 * 1 for xattr if selinux is on
5058 trans
= btrfs_start_transaction(root
, 5);
5060 return PTR_ERR(trans
);
5062 err
= btrfs_find_free_ino(root
, &objectid
);
5066 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5067 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5069 if (IS_ERR(inode
)) {
5070 err
= PTR_ERR(inode
);
5074 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5081 * If the active LSM wants to access the inode during
5082 * d_instantiate it needs these. Smack checks to see
5083 * if the filesystem supports xattrs by looking at the
5087 inode
->i_op
= &btrfs_special_inode_operations
;
5088 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5092 init_special_inode(inode
, inode
->i_mode
, rdev
);
5093 btrfs_update_inode(trans
, root
, inode
);
5094 d_instantiate(dentry
, inode
);
5097 btrfs_end_transaction(trans
, root
);
5098 btrfs_btree_balance_dirty(root
);
5100 inode_dec_link_count(inode
);
5106 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
5107 umode_t mode
, bool excl
)
5109 struct btrfs_trans_handle
*trans
;
5110 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5111 struct inode
*inode
= NULL
;
5112 int drop_inode_on_err
= 0;
5118 * 2 for inode item and ref
5120 * 1 for xattr if selinux is on
5122 trans
= btrfs_start_transaction(root
, 5);
5124 return PTR_ERR(trans
);
5126 err
= btrfs_find_free_ino(root
, &objectid
);
5130 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5131 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5133 if (IS_ERR(inode
)) {
5134 err
= PTR_ERR(inode
);
5137 drop_inode_on_err
= 1;
5139 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5143 err
= btrfs_update_inode(trans
, root
, inode
);
5148 * If the active LSM wants to access the inode during
5149 * d_instantiate it needs these. Smack checks to see
5150 * if the filesystem supports xattrs by looking at the
5153 inode
->i_fop
= &btrfs_file_operations
;
5154 inode
->i_op
= &btrfs_file_inode_operations
;
5156 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5160 inode
->i_mapping
->a_ops
= &btrfs_aops
;
5161 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
5162 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
5163 d_instantiate(dentry
, inode
);
5166 btrfs_end_transaction(trans
, root
);
5167 if (err
&& drop_inode_on_err
) {
5168 inode_dec_link_count(inode
);
5171 btrfs_btree_balance_dirty(root
);
5175 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
5176 struct dentry
*dentry
)
5178 struct btrfs_trans_handle
*trans
;
5179 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5180 struct inode
*inode
= old_dentry
->d_inode
;
5185 /* do not allow sys_link's with other subvols of the same device */
5186 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
5189 if (inode
->i_nlink
>= BTRFS_LINK_MAX
)
5192 err
= btrfs_set_inode_index(dir
, &index
);
5197 * 2 items for inode and inode ref
5198 * 2 items for dir items
5199 * 1 item for parent inode
5201 trans
= btrfs_start_transaction(root
, 5);
5202 if (IS_ERR(trans
)) {
5203 err
= PTR_ERR(trans
);
5207 btrfs_inc_nlink(inode
);
5208 inode_inc_iversion(inode
);
5209 inode
->i_ctime
= CURRENT_TIME
;
5211 set_bit(BTRFS_INODE_COPY_EVERYTHING
, &BTRFS_I(inode
)->runtime_flags
);
5213 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
5218 struct dentry
*parent
= dentry
->d_parent
;
5219 err
= btrfs_update_inode(trans
, root
, inode
);
5222 d_instantiate(dentry
, inode
);
5223 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
5226 btrfs_end_transaction(trans
, root
);
5229 inode_dec_link_count(inode
);
5232 btrfs_btree_balance_dirty(root
);
5236 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
5238 struct inode
*inode
= NULL
;
5239 struct btrfs_trans_handle
*trans
;
5240 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5242 int drop_on_err
= 0;
5247 * 2 items for inode and ref
5248 * 2 items for dir items
5249 * 1 for xattr if selinux is on
5251 trans
= btrfs_start_transaction(root
, 5);
5253 return PTR_ERR(trans
);
5255 err
= btrfs_find_free_ino(root
, &objectid
);
5259 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5260 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5261 S_IFDIR
| mode
, &index
);
5262 if (IS_ERR(inode
)) {
5263 err
= PTR_ERR(inode
);
5269 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5273 inode
->i_op
= &btrfs_dir_inode_operations
;
5274 inode
->i_fop
= &btrfs_dir_file_operations
;
5276 btrfs_i_size_write(inode
, 0);
5277 err
= btrfs_update_inode(trans
, root
, inode
);
5281 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
5282 dentry
->d_name
.len
, 0, index
);
5286 d_instantiate(dentry
, inode
);
5290 btrfs_end_transaction(trans
, root
);
5293 btrfs_btree_balance_dirty(root
);
5297 /* helper for btfs_get_extent. Given an existing extent in the tree,
5298 * and an extent that you want to insert, deal with overlap and insert
5299 * the new extent into the tree.
5301 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
5302 struct extent_map
*existing
,
5303 struct extent_map
*em
,
5304 u64 map_start
, u64 map_len
)
5308 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
5309 start_diff
= map_start
- em
->start
;
5310 em
->start
= map_start
;
5312 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
5313 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
5314 em
->block_start
+= start_diff
;
5315 em
->block_len
-= start_diff
;
5317 return add_extent_mapping(em_tree
, em
);
5320 static noinline
int uncompress_inline(struct btrfs_path
*path
,
5321 struct inode
*inode
, struct page
*page
,
5322 size_t pg_offset
, u64 extent_offset
,
5323 struct btrfs_file_extent_item
*item
)
5326 struct extent_buffer
*leaf
= path
->nodes
[0];
5329 unsigned long inline_size
;
5333 WARN_ON(pg_offset
!= 0);
5334 compress_type
= btrfs_file_extent_compression(leaf
, item
);
5335 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
5336 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
5337 btrfs_item_nr(leaf
, path
->slots
[0]));
5338 tmp
= kmalloc(inline_size
, GFP_NOFS
);
5341 ptr
= btrfs_file_extent_inline_start(item
);
5343 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
5345 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
5346 ret
= btrfs_decompress(compress_type
, tmp
, page
,
5347 extent_offset
, inline_size
, max_size
);
5349 char *kaddr
= kmap_atomic(page
);
5350 unsigned long copy_size
= min_t(u64
,
5351 PAGE_CACHE_SIZE
- pg_offset
,
5352 max_size
- extent_offset
);
5353 memset(kaddr
+ pg_offset
, 0, copy_size
);
5354 kunmap_atomic(kaddr
);
5361 * a bit scary, this does extent mapping from logical file offset to the disk.
5362 * the ugly parts come from merging extents from the disk with the in-ram
5363 * representation. This gets more complex because of the data=ordered code,
5364 * where the in-ram extents might be locked pending data=ordered completion.
5366 * This also copies inline extents directly into the page.
5369 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
5370 size_t pg_offset
, u64 start
, u64 len
,
5376 u64 extent_start
= 0;
5378 u64 objectid
= btrfs_ino(inode
);
5380 struct btrfs_path
*path
= NULL
;
5381 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5382 struct btrfs_file_extent_item
*item
;
5383 struct extent_buffer
*leaf
;
5384 struct btrfs_key found_key
;
5385 struct extent_map
*em
= NULL
;
5386 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
5387 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
5388 struct btrfs_trans_handle
*trans
= NULL
;
5392 read_lock(&em_tree
->lock
);
5393 em
= lookup_extent_mapping(em_tree
, start
, len
);
5395 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5396 read_unlock(&em_tree
->lock
);
5399 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
5400 free_extent_map(em
);
5401 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
5402 free_extent_map(em
);
5406 em
= alloc_extent_map();
5411 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5412 em
->start
= EXTENT_MAP_HOLE
;
5413 em
->orig_start
= EXTENT_MAP_HOLE
;
5415 em
->block_len
= (u64
)-1;
5418 path
= btrfs_alloc_path();
5424 * Chances are we'll be called again, so go ahead and do
5430 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
5431 objectid
, start
, trans
!= NULL
);
5438 if (path
->slots
[0] == 0)
5443 leaf
= path
->nodes
[0];
5444 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5445 struct btrfs_file_extent_item
);
5446 /* are we inside the extent that was found? */
5447 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5448 found_type
= btrfs_key_type(&found_key
);
5449 if (found_key
.objectid
!= objectid
||
5450 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
5454 found_type
= btrfs_file_extent_type(leaf
, item
);
5455 extent_start
= found_key
.offset
;
5456 compress_type
= btrfs_file_extent_compression(leaf
, item
);
5457 if (found_type
== BTRFS_FILE_EXTENT_REG
||
5458 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5459 extent_end
= extent_start
+
5460 btrfs_file_extent_num_bytes(leaf
, item
);
5461 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
5463 size
= btrfs_file_extent_inline_len(leaf
, item
);
5464 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
5465 ~((u64
)root
->sectorsize
- 1);
5468 if (start
>= extent_end
) {
5470 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
5471 ret
= btrfs_next_leaf(root
, path
);
5478 leaf
= path
->nodes
[0];
5480 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5481 if (found_key
.objectid
!= objectid
||
5482 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
5484 if (start
+ len
<= found_key
.offset
)
5487 em
->orig_start
= start
;
5488 em
->len
= found_key
.offset
- start
;
5492 if (found_type
== BTRFS_FILE_EXTENT_REG
||
5493 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5494 em
->start
= extent_start
;
5495 em
->len
= extent_end
- extent_start
;
5496 em
->orig_start
= extent_start
-
5497 btrfs_file_extent_offset(leaf
, item
);
5498 em
->orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
,
5500 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
5502 em
->block_start
= EXTENT_MAP_HOLE
;
5505 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
5506 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
5507 em
->compress_type
= compress_type
;
5508 em
->block_start
= bytenr
;
5509 em
->block_len
= em
->orig_block_len
;
5511 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
5512 em
->block_start
= bytenr
;
5513 em
->block_len
= em
->len
;
5514 if (found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
5515 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
5518 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
5522 size_t extent_offset
;
5525 em
->block_start
= EXTENT_MAP_INLINE
;
5526 if (!page
|| create
) {
5527 em
->start
= extent_start
;
5528 em
->len
= extent_end
- extent_start
;
5532 size
= btrfs_file_extent_inline_len(leaf
, item
);
5533 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
5534 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
5535 size
- extent_offset
);
5536 em
->start
= extent_start
+ extent_offset
;
5537 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
5538 ~((u64
)root
->sectorsize
- 1);
5539 em
->orig_block_len
= em
->len
;
5540 em
->orig_start
= em
->start
;
5541 if (compress_type
) {
5542 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
5543 em
->compress_type
= compress_type
;
5545 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
5546 if (create
== 0 && !PageUptodate(page
)) {
5547 if (btrfs_file_extent_compression(leaf
, item
) !=
5548 BTRFS_COMPRESS_NONE
) {
5549 ret
= uncompress_inline(path
, inode
, page
,
5551 extent_offset
, item
);
5552 BUG_ON(ret
); /* -ENOMEM */
5555 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
5557 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
5558 memset(map
+ pg_offset
+ copy_size
, 0,
5559 PAGE_CACHE_SIZE
- pg_offset
-
5564 flush_dcache_page(page
);
5565 } else if (create
&& PageUptodate(page
)) {
5569 free_extent_map(em
);
5572 btrfs_release_path(path
);
5573 trans
= btrfs_join_transaction(root
);
5576 return ERR_CAST(trans
);
5580 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
5583 btrfs_mark_buffer_dirty(leaf
);
5585 set_extent_uptodate(io_tree
, em
->start
,
5586 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
5589 WARN(1, KERN_ERR
"btrfs unknown found_type %d\n", found_type
);
5593 em
->orig_start
= start
;
5596 em
->block_start
= EXTENT_MAP_HOLE
;
5597 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
5599 btrfs_release_path(path
);
5600 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
5601 printk(KERN_ERR
"Btrfs: bad extent! em: [%llu %llu] passed "
5602 "[%llu %llu]\n", (unsigned long long)em
->start
,
5603 (unsigned long long)em
->len
,
5604 (unsigned long long)start
,
5605 (unsigned long long)len
);
5611 write_lock(&em_tree
->lock
);
5612 ret
= add_extent_mapping(em_tree
, em
);
5613 /* it is possible that someone inserted the extent into the tree
5614 * while we had the lock dropped. It is also possible that
5615 * an overlapping map exists in the tree
5617 if (ret
== -EEXIST
) {
5618 struct extent_map
*existing
;
5622 existing
= lookup_extent_mapping(em_tree
, start
, len
);
5623 if (existing
&& (existing
->start
> start
||
5624 existing
->start
+ existing
->len
<= start
)) {
5625 free_extent_map(existing
);
5629 existing
= lookup_extent_mapping(em_tree
, em
->start
,
5632 err
= merge_extent_mapping(em_tree
, existing
,
5635 free_extent_map(existing
);
5637 free_extent_map(em
);
5642 free_extent_map(em
);
5646 free_extent_map(em
);
5651 write_unlock(&em_tree
->lock
);
5655 trace_btrfs_get_extent(root
, em
);
5658 btrfs_free_path(path
);
5660 ret
= btrfs_end_transaction(trans
, root
);
5665 free_extent_map(em
);
5666 return ERR_PTR(err
);
5668 BUG_ON(!em
); /* Error is always set */
5672 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
5673 size_t pg_offset
, u64 start
, u64 len
,
5676 struct extent_map
*em
;
5677 struct extent_map
*hole_em
= NULL
;
5678 u64 range_start
= start
;
5684 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
5691 * - a pre-alloc extent,
5692 * there might actually be delalloc bytes behind it.
5694 if (em
->block_start
!= EXTENT_MAP_HOLE
&&
5695 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
5701 /* check to see if we've wrapped (len == -1 or similar) */
5710 /* ok, we didn't find anything, lets look for delalloc */
5711 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
5712 end
, len
, EXTENT_DELALLOC
, 1);
5713 found_end
= range_start
+ found
;
5714 if (found_end
< range_start
)
5715 found_end
= (u64
)-1;
5718 * we didn't find anything useful, return
5719 * the original results from get_extent()
5721 if (range_start
> end
|| found_end
<= start
) {
5727 /* adjust the range_start to make sure it doesn't
5728 * go backwards from the start they passed in
5730 range_start
= max(start
,range_start
);
5731 found
= found_end
- range_start
;
5734 u64 hole_start
= start
;
5737 em
= alloc_extent_map();
5743 * when btrfs_get_extent can't find anything it
5744 * returns one huge hole
5746 * make sure what it found really fits our range, and
5747 * adjust to make sure it is based on the start from
5751 u64 calc_end
= extent_map_end(hole_em
);
5753 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
5754 free_extent_map(hole_em
);
5757 hole_start
= max(hole_em
->start
, start
);
5758 hole_len
= calc_end
- hole_start
;
5762 if (hole_em
&& range_start
> hole_start
) {
5763 /* our hole starts before our delalloc, so we
5764 * have to return just the parts of the hole
5765 * that go until the delalloc starts
5767 em
->len
= min(hole_len
,
5768 range_start
- hole_start
);
5769 em
->start
= hole_start
;
5770 em
->orig_start
= hole_start
;
5772 * don't adjust block start at all,
5773 * it is fixed at EXTENT_MAP_HOLE
5775 em
->block_start
= hole_em
->block_start
;
5776 em
->block_len
= hole_len
;
5777 if (test_bit(EXTENT_FLAG_PREALLOC
, &hole_em
->flags
))
5778 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
5780 em
->start
= range_start
;
5782 em
->orig_start
= range_start
;
5783 em
->block_start
= EXTENT_MAP_DELALLOC
;
5784 em
->block_len
= found
;
5786 } else if (hole_em
) {
5791 free_extent_map(hole_em
);
5793 free_extent_map(em
);
5794 return ERR_PTR(err
);
5799 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
5802 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5803 struct btrfs_trans_handle
*trans
;
5804 struct extent_map
*em
;
5805 struct btrfs_key ins
;
5809 trans
= btrfs_join_transaction(root
);
5811 return ERR_CAST(trans
);
5813 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
5815 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
5816 ret
= btrfs_reserve_extent(trans
, root
, len
, root
->sectorsize
, 0,
5817 alloc_hint
, &ins
, 1);
5823 em
= create_pinned_em(inode
, start
, ins
.offset
, start
, ins
.objectid
,
5824 ins
.offset
, ins
.offset
, 0);
5828 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
5829 ins
.offset
, ins
.offset
, 0);
5831 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
5835 btrfs_end_transaction(trans
, root
);
5840 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5841 * block must be cow'd
5843 static noinline
int can_nocow_odirect(struct btrfs_trans_handle
*trans
,
5844 struct inode
*inode
, u64 offset
, u64 len
)
5846 struct btrfs_path
*path
;
5848 struct extent_buffer
*leaf
;
5849 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5850 struct btrfs_file_extent_item
*fi
;
5851 struct btrfs_key key
;
5859 path
= btrfs_alloc_path();
5863 ret
= btrfs_lookup_file_extent(trans
, root
, path
, btrfs_ino(inode
),
5868 slot
= path
->slots
[0];
5871 /* can't find the item, must cow */
5878 leaf
= path
->nodes
[0];
5879 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
5880 if (key
.objectid
!= btrfs_ino(inode
) ||
5881 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
5882 /* not our file or wrong item type, must cow */
5886 if (key
.offset
> offset
) {
5887 /* Wrong offset, must cow */
5891 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
5892 found_type
= btrfs_file_extent_type(leaf
, fi
);
5893 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
5894 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
5895 /* not a regular extent, must cow */
5898 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
5899 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
5901 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
5902 if (extent_end
< offset
+ len
) {
5903 /* extent doesn't include our full range, must cow */
5907 if (btrfs_extent_readonly(root
, disk_bytenr
))
5911 * look for other files referencing this extent, if we
5912 * find any we must cow
5914 if (btrfs_cross_ref_exist(trans
, root
, btrfs_ino(inode
),
5915 key
.offset
- backref_offset
, disk_bytenr
))
5919 * adjust disk_bytenr and num_bytes to cover just the bytes
5920 * in this extent we are about to write. If there
5921 * are any csums in that range we have to cow in order
5922 * to keep the csums correct
5924 disk_bytenr
+= backref_offset
;
5925 disk_bytenr
+= offset
- key
.offset
;
5926 num_bytes
= min(offset
+ len
, extent_end
) - offset
;
5927 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
5930 * all of the above have passed, it is safe to overwrite this extent
5935 btrfs_free_path(path
);
5939 static int lock_extent_direct(struct inode
*inode
, u64 lockstart
, u64 lockend
,
5940 struct extent_state
**cached_state
, int writing
)
5942 struct btrfs_ordered_extent
*ordered
;
5946 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
5949 * We're concerned with the entire range that we're going to be
5950 * doing DIO to, so we need to make sure theres no ordered
5951 * extents in this range.
5953 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
5954 lockend
- lockstart
+ 1);
5957 * We need to make sure there are no buffered pages in this
5958 * range either, we could have raced between the invalidate in
5959 * generic_file_direct_write and locking the extent. The
5960 * invalidate needs to happen so that reads after a write do not
5963 if (!ordered
&& (!writing
||
5964 !test_range_bit(&BTRFS_I(inode
)->io_tree
,
5965 lockstart
, lockend
, EXTENT_UPTODATE
, 0,
5969 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
5970 cached_state
, GFP_NOFS
);
5973 btrfs_start_ordered_extent(inode
, ordered
, 1);
5974 btrfs_put_ordered_extent(ordered
);
5976 /* Screw you mmap */
5977 ret
= filemap_write_and_wait_range(inode
->i_mapping
,
5984 * If we found a page that couldn't be invalidated just
5985 * fall back to buffered.
5987 ret
= invalidate_inode_pages2_range(inode
->i_mapping
,
5988 lockstart
>> PAGE_CACHE_SHIFT
,
5989 lockend
>> PAGE_CACHE_SHIFT
);
6000 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
6001 u64 len
, u64 orig_start
,
6002 u64 block_start
, u64 block_len
,
6003 u64 orig_block_len
, int type
)
6005 struct extent_map_tree
*em_tree
;
6006 struct extent_map
*em
;
6007 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6010 em_tree
= &BTRFS_I(inode
)->extent_tree
;
6011 em
= alloc_extent_map();
6013 return ERR_PTR(-ENOMEM
);
6016 em
->orig_start
= orig_start
;
6017 em
->mod_start
= start
;
6020 em
->block_len
= block_len
;
6021 em
->block_start
= block_start
;
6022 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6023 em
->orig_block_len
= orig_block_len
;
6024 em
->generation
= -1;
6025 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
6026 if (type
== BTRFS_ORDERED_PREALLOC
)
6027 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
6030 btrfs_drop_extent_cache(inode
, em
->start
,
6031 em
->start
+ em
->len
- 1, 0);
6032 write_lock(&em_tree
->lock
);
6033 ret
= add_extent_mapping(em_tree
, em
);
6035 list_move(&em
->list
,
6036 &em_tree
->modified_extents
);
6037 write_unlock(&em_tree
->lock
);
6038 } while (ret
== -EEXIST
);
6041 free_extent_map(em
);
6042 return ERR_PTR(ret
);
6049 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
6050 struct buffer_head
*bh_result
, int create
)
6052 struct extent_map
*em
;
6053 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6054 struct extent_state
*cached_state
= NULL
;
6055 u64 start
= iblock
<< inode
->i_blkbits
;
6056 u64 lockstart
, lockend
;
6057 u64 len
= bh_result
->b_size
;
6058 struct btrfs_trans_handle
*trans
;
6059 int unlock_bits
= EXTENT_LOCKED
;
6063 ret
= btrfs_delalloc_reserve_space(inode
, len
);
6066 unlock_bits
|= EXTENT_DELALLOC
| EXTENT_DIRTY
;
6068 len
= min_t(u64
, len
, root
->sectorsize
);
6072 lockend
= start
+ len
- 1;
6075 * If this errors out it's because we couldn't invalidate pagecache for
6076 * this range and we need to fallback to buffered.
6078 if (lock_extent_direct(inode
, lockstart
, lockend
, &cached_state
, create
))
6082 ret
= set_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6083 lockend
, EXTENT_DELALLOC
, NULL
,
6084 &cached_state
, GFP_NOFS
);
6089 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
6096 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6097 * io. INLINE is special, and we could probably kludge it in here, but
6098 * it's still buffered so for safety lets just fall back to the generic
6101 * For COMPRESSED we _have_ to read the entire extent in so we can
6102 * decompress it, so there will be buffering required no matter what we
6103 * do, so go ahead and fallback to buffered.
6105 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6106 * to buffered IO. Don't blame me, this is the price we pay for using
6109 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
6110 em
->block_start
== EXTENT_MAP_INLINE
) {
6111 free_extent_map(em
);
6116 /* Just a good old fashioned hole, return */
6117 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
6118 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
6119 free_extent_map(em
);
6125 * We don't allocate a new extent in the following cases
6127 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6129 * 2) The extent is marked as PREALLOC. We're good to go here and can
6130 * just use the extent.
6134 len
= min(len
, em
->len
- (start
- em
->start
));
6135 lockstart
= start
+ len
;
6139 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
6140 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
6141 em
->block_start
!= EXTENT_MAP_HOLE
)) {
6146 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6147 type
= BTRFS_ORDERED_PREALLOC
;
6149 type
= BTRFS_ORDERED_NOCOW
;
6150 len
= min(len
, em
->len
- (start
- em
->start
));
6151 block_start
= em
->block_start
+ (start
- em
->start
);
6154 * we're not going to log anything, but we do need
6155 * to make sure the current transaction stays open
6156 * while we look for nocow cross refs
6158 trans
= btrfs_join_transaction(root
);
6162 if (can_nocow_odirect(trans
, inode
, start
, len
) == 1) {
6163 u64 orig_start
= em
->orig_start
;
6164 u64 orig_block_len
= em
->orig_block_len
;
6166 if (type
== BTRFS_ORDERED_PREALLOC
) {
6167 free_extent_map(em
);
6168 em
= create_pinned_em(inode
, start
, len
,
6171 orig_block_len
, type
);
6173 btrfs_end_transaction(trans
, root
);
6178 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
6179 block_start
, len
, len
, type
);
6180 btrfs_end_transaction(trans
, root
);
6182 free_extent_map(em
);
6187 btrfs_end_transaction(trans
, root
);
6191 * this will cow the extent, reset the len in case we changed
6194 len
= bh_result
->b_size
;
6195 free_extent_map(em
);
6196 em
= btrfs_new_extent_direct(inode
, start
, len
);
6201 len
= min(len
, em
->len
- (start
- em
->start
));
6203 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
6205 bh_result
->b_size
= len
;
6206 bh_result
->b_bdev
= em
->bdev
;
6207 set_buffer_mapped(bh_result
);
6209 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6210 set_buffer_new(bh_result
);
6213 * Need to update the i_size under the extent lock so buffered
6214 * readers will get the updated i_size when we unlock.
6216 if (start
+ len
> i_size_read(inode
))
6217 i_size_write(inode
, start
+ len
);
6221 * In the case of write we need to clear and unlock the entire range,
6222 * in the case of read we need to unlock only the end area that we
6223 * aren't using if there is any left over space.
6225 if (lockstart
< lockend
) {
6226 if (create
&& len
< lockend
- lockstart
) {
6227 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6228 lockstart
+ len
- 1,
6229 unlock_bits
| EXTENT_DEFRAG
, 1, 0,
6230 &cached_state
, GFP_NOFS
);
6232 * Beside unlock, we also need to cleanup reserved space
6233 * for the left range by attaching EXTENT_DO_ACCOUNTING.
6235 clear_extent_bit(&BTRFS_I(inode
)->io_tree
,
6236 lockstart
+ len
, lockend
,
6237 unlock_bits
| EXTENT_DO_ACCOUNTING
|
6238 EXTENT_DEFRAG
, 1, 0, NULL
, GFP_NOFS
);
6240 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6241 lockend
, unlock_bits
, 1, 0,
6242 &cached_state
, GFP_NOFS
);
6245 free_extent_state(cached_state
);
6248 free_extent_map(em
);
6254 unlock_bits
|= EXTENT_DO_ACCOUNTING
;
6256 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6257 unlock_bits
, 1, 0, &cached_state
, GFP_NOFS
);
6261 struct btrfs_dio_private
{
6262 struct inode
*inode
;
6268 /* number of bios pending for this dio */
6269 atomic_t pending_bios
;
6274 struct bio
*orig_bio
;
6277 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
6279 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6280 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
6281 struct bio_vec
*bvec
= bio
->bi_io_vec
;
6282 struct inode
*inode
= dip
->inode
;
6283 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6286 start
= dip
->logical_offset
;
6288 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
6289 struct page
*page
= bvec
->bv_page
;
6292 u64
private = ~(u32
)0;
6293 unsigned long flags
;
6295 if (get_state_private(&BTRFS_I(inode
)->io_tree
,
6298 local_irq_save(flags
);
6299 kaddr
= kmap_atomic(page
);
6300 csum
= btrfs_csum_data(root
, kaddr
+ bvec
->bv_offset
,
6301 csum
, bvec
->bv_len
);
6302 btrfs_csum_final(csum
, (char *)&csum
);
6303 kunmap_atomic(kaddr
);
6304 local_irq_restore(flags
);
6306 flush_dcache_page(bvec
->bv_page
);
6307 if (csum
!= private) {
6309 printk(KERN_ERR
"btrfs csum failed ino %llu off"
6310 " %llu csum %u private %u\n",
6311 (unsigned long long)btrfs_ino(inode
),
6312 (unsigned long long)start
,
6313 csum
, (unsigned)private);
6318 start
+= bvec
->bv_len
;
6320 } while (bvec
<= bvec_end
);
6322 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
6323 dip
->logical_offset
+ dip
->bytes
- 1);
6324 bio
->bi_private
= dip
->private;
6328 /* If we had a csum failure make sure to clear the uptodate flag */
6330 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
6331 dio_end_io(bio
, err
);
6334 static void btrfs_endio_direct_write(struct bio
*bio
, int err
)
6336 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6337 struct inode
*inode
= dip
->inode
;
6338 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6339 struct btrfs_ordered_extent
*ordered
= NULL
;
6340 u64 ordered_offset
= dip
->logical_offset
;
6341 u64 ordered_bytes
= dip
->bytes
;
6347 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
6349 ordered_bytes
, !err
);
6353 ordered
->work
.func
= finish_ordered_fn
;
6354 ordered
->work
.flags
= 0;
6355 btrfs_queue_worker(&root
->fs_info
->endio_write_workers
,
6359 * our bio might span multiple ordered extents. If we haven't
6360 * completed the accounting for the whole dio, go back and try again
6362 if (ordered_offset
< dip
->logical_offset
+ dip
->bytes
) {
6363 ordered_bytes
= dip
->logical_offset
+ dip
->bytes
-
6369 bio
->bi_private
= dip
->private;
6373 /* If we had an error make sure to clear the uptodate flag */
6375 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
6376 dio_end_io(bio
, err
);
6379 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
6380 struct bio
*bio
, int mirror_num
,
6381 unsigned long bio_flags
, u64 offset
)
6384 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6385 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
6386 BUG_ON(ret
); /* -ENOMEM */
6390 static void btrfs_end_dio_bio(struct bio
*bio
, int err
)
6392 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6395 printk(KERN_ERR
"btrfs direct IO failed ino %llu rw %lu "
6396 "sector %#Lx len %u err no %d\n",
6397 (unsigned long long)btrfs_ino(dip
->inode
), bio
->bi_rw
,
6398 (unsigned long long)bio
->bi_sector
, bio
->bi_size
, err
);
6402 * before atomic variable goto zero, we must make sure
6403 * dip->errors is perceived to be set.
6405 smp_mb__before_atomic_dec();
6408 /* if there are more bios still pending for this dio, just exit */
6409 if (!atomic_dec_and_test(&dip
->pending_bios
))
6413 bio_io_error(dip
->orig_bio
);
6415 set_bit(BIO_UPTODATE
, &dip
->orig_bio
->bi_flags
);
6416 bio_endio(dip
->orig_bio
, 0);
6422 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
6423 u64 first_sector
, gfp_t gfp_flags
)
6425 int nr_vecs
= bio_get_nr_vecs(bdev
);
6426 return btrfs_bio_alloc(bdev
, first_sector
, nr_vecs
, gfp_flags
);
6429 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
6430 int rw
, u64 file_offset
, int skip_sum
,
6433 int write
= rw
& REQ_WRITE
;
6434 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6438 async_submit
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
6443 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
6451 if (write
&& async_submit
) {
6452 ret
= btrfs_wq_submit_bio(root
->fs_info
,
6453 inode
, rw
, bio
, 0, 0,
6455 __btrfs_submit_bio_start_direct_io
,
6456 __btrfs_submit_bio_done
);
6460 * If we aren't doing async submit, calculate the csum of the
6463 ret
= btrfs_csum_one_bio(root
, inode
, bio
, file_offset
, 1);
6466 } else if (!skip_sum
) {
6467 ret
= btrfs_lookup_bio_sums_dio(root
, inode
, bio
, file_offset
);
6473 ret
= btrfs_map_bio(root
, rw
, bio
, 0, async_submit
);
6479 static int btrfs_submit_direct_hook(int rw
, struct btrfs_dio_private
*dip
,
6482 struct inode
*inode
= dip
->inode
;
6483 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6485 struct bio
*orig_bio
= dip
->orig_bio
;
6486 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
6487 u64 start_sector
= orig_bio
->bi_sector
;
6488 u64 file_offset
= dip
->logical_offset
;
6493 int async_submit
= 0;
6495 map_length
= orig_bio
->bi_size
;
6496 ret
= btrfs_map_block(root
->fs_info
, READ
, start_sector
<< 9,
6497 &map_length
, NULL
, 0);
6503 if (map_length
>= orig_bio
->bi_size
) {
6509 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
6512 bio
->bi_private
= dip
;
6513 bio
->bi_end_io
= btrfs_end_dio_bio
;
6514 atomic_inc(&dip
->pending_bios
);
6516 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
6517 if (unlikely(map_length
< submit_len
+ bvec
->bv_len
||
6518 bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
6519 bvec
->bv_offset
) < bvec
->bv_len
)) {
6521 * inc the count before we submit the bio so
6522 * we know the end IO handler won't happen before
6523 * we inc the count. Otherwise, the dip might get freed
6524 * before we're done setting it up
6526 atomic_inc(&dip
->pending_bios
);
6527 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
,
6528 file_offset
, skip_sum
,
6532 atomic_dec(&dip
->pending_bios
);
6536 start_sector
+= submit_len
>> 9;
6537 file_offset
+= submit_len
;
6542 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
6543 start_sector
, GFP_NOFS
);
6546 bio
->bi_private
= dip
;
6547 bio
->bi_end_io
= btrfs_end_dio_bio
;
6549 map_length
= orig_bio
->bi_size
;
6550 ret
= btrfs_map_block(root
->fs_info
, READ
,
6552 &map_length
, NULL
, 0);
6558 submit_len
+= bvec
->bv_len
;
6565 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
, file_offset
, skip_sum
,
6574 * before atomic variable goto zero, we must
6575 * make sure dip->errors is perceived to be set.
6577 smp_mb__before_atomic_dec();
6578 if (atomic_dec_and_test(&dip
->pending_bios
))
6579 bio_io_error(dip
->orig_bio
);
6581 /* bio_end_io() will handle error, so we needn't return it */
6585 static void btrfs_submit_direct(int rw
, struct bio
*bio
, struct inode
*inode
,
6588 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6589 struct btrfs_dio_private
*dip
;
6590 struct bio_vec
*bvec
= bio
->bi_io_vec
;
6592 int write
= rw
& REQ_WRITE
;
6595 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
6597 dip
= kmalloc(sizeof(*dip
), GFP_NOFS
);
6603 dip
->private = bio
->bi_private
;
6605 dip
->logical_offset
= file_offset
;
6609 dip
->bytes
+= bvec
->bv_len
;
6611 } while (bvec
<= (bio
->bi_io_vec
+ bio
->bi_vcnt
- 1));
6613 dip
->disk_bytenr
= (u64
)bio
->bi_sector
<< 9;
6614 bio
->bi_private
= dip
;
6616 dip
->orig_bio
= bio
;
6617 atomic_set(&dip
->pending_bios
, 0);
6620 bio
->bi_end_io
= btrfs_endio_direct_write
;
6622 bio
->bi_end_io
= btrfs_endio_direct_read
;
6624 ret
= btrfs_submit_direct_hook(rw
, dip
, skip_sum
);
6629 * If this is a write, we need to clean up the reserved space and kill
6630 * the ordered extent.
6633 struct btrfs_ordered_extent
*ordered
;
6634 ordered
= btrfs_lookup_ordered_extent(inode
, file_offset
);
6635 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
6636 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
6637 btrfs_free_reserved_extent(root
, ordered
->start
,
6639 btrfs_put_ordered_extent(ordered
);
6640 btrfs_put_ordered_extent(ordered
);
6642 bio_endio(bio
, ret
);
6645 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
6646 const struct iovec
*iov
, loff_t offset
,
6647 unsigned long nr_segs
)
6653 unsigned blocksize_mask
= root
->sectorsize
- 1;
6654 ssize_t retval
= -EINVAL
;
6655 loff_t end
= offset
;
6657 if (offset
& blocksize_mask
)
6660 /* Check the memory alignment. Blocks cannot straddle pages */
6661 for (seg
= 0; seg
< nr_segs
; seg
++) {
6662 addr
= (unsigned long)iov
[seg
].iov_base
;
6663 size
= iov
[seg
].iov_len
;
6665 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
6668 /* If this is a write we don't need to check anymore */
6673 * Check to make sure we don't have duplicate iov_base's in this
6674 * iovec, if so return EINVAL, otherwise we'll get csum errors
6675 * when reading back.
6677 for (i
= seg
+ 1; i
< nr_segs
; i
++) {
6678 if (iov
[seg
].iov_base
== iov
[i
].iov_base
)
6687 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
6688 const struct iovec
*iov
, loff_t offset
,
6689 unsigned long nr_segs
)
6691 struct file
*file
= iocb
->ki_filp
;
6692 struct inode
*inode
= file
->f_mapping
->host
;
6694 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iov
,
6698 return __blockdev_direct_IO(rw
, iocb
, inode
,
6699 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
6700 iov
, offset
, nr_segs
, btrfs_get_blocks_direct
, NULL
,
6701 btrfs_submit_direct
, 0);
6704 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
6706 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
6707 __u64 start
, __u64 len
)
6711 ret
= fiemap_check_flags(fieinfo
, BTRFS_FIEMAP_FLAGS
);
6715 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
6718 int btrfs_readpage(struct file
*file
, struct page
*page
)
6720 struct extent_io_tree
*tree
;
6721 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6722 return extent_read_full_page(tree
, page
, btrfs_get_extent
, 0);
6725 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
6727 struct extent_io_tree
*tree
;
6730 if (current
->flags
& PF_MEMALLOC
) {
6731 redirty_page_for_writepage(wbc
, page
);
6735 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6736 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
6739 int btrfs_writepages(struct address_space
*mapping
,
6740 struct writeback_control
*wbc
)
6742 struct extent_io_tree
*tree
;
6744 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
6745 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
6749 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
6750 struct list_head
*pages
, unsigned nr_pages
)
6752 struct extent_io_tree
*tree
;
6753 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
6754 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
6757 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
6759 struct extent_io_tree
*tree
;
6760 struct extent_map_tree
*map
;
6763 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6764 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
6765 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
6767 ClearPagePrivate(page
);
6768 set_page_private(page
, 0);
6769 page_cache_release(page
);
6774 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
6776 if (PageWriteback(page
) || PageDirty(page
))
6778 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
6781 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
6783 struct inode
*inode
= page
->mapping
->host
;
6784 struct extent_io_tree
*tree
;
6785 struct btrfs_ordered_extent
*ordered
;
6786 struct extent_state
*cached_state
= NULL
;
6787 u64 page_start
= page_offset(page
);
6788 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6791 * we have the page locked, so new writeback can't start,
6792 * and the dirty bit won't be cleared while we are here.
6794 * Wait for IO on this page so that we can safely clear
6795 * the PagePrivate2 bit and do ordered accounting
6797 wait_on_page_writeback(page
);
6799 tree
= &BTRFS_I(inode
)->io_tree
;
6801 btrfs_releasepage(page
, GFP_NOFS
);
6804 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
6805 ordered
= btrfs_lookup_ordered_extent(inode
, page_offset(page
));
6808 * IO on this page will never be started, so we need
6809 * to account for any ordered extents now
6811 clear_extent_bit(tree
, page_start
, page_end
,
6812 EXTENT_DIRTY
| EXTENT_DELALLOC
|
6813 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
6814 EXTENT_DEFRAG
, 1, 0, &cached_state
, GFP_NOFS
);
6816 * whoever cleared the private bit is responsible
6817 * for the finish_ordered_io
6819 if (TestClearPagePrivate2(page
) &&
6820 btrfs_dec_test_ordered_pending(inode
, &ordered
, page_start
,
6821 PAGE_CACHE_SIZE
, 1)) {
6822 btrfs_finish_ordered_io(ordered
);
6824 btrfs_put_ordered_extent(ordered
);
6825 cached_state
= NULL
;
6826 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
6828 clear_extent_bit(tree
, page_start
, page_end
,
6829 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
6830 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 1, 1,
6831 &cached_state
, GFP_NOFS
);
6832 __btrfs_releasepage(page
, GFP_NOFS
);
6834 ClearPageChecked(page
);
6835 if (PagePrivate(page
)) {
6836 ClearPagePrivate(page
);
6837 set_page_private(page
, 0);
6838 page_cache_release(page
);
6843 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6844 * called from a page fault handler when a page is first dirtied. Hence we must
6845 * be careful to check for EOF conditions here. We set the page up correctly
6846 * for a written page which means we get ENOSPC checking when writing into
6847 * holes and correct delalloc and unwritten extent mapping on filesystems that
6848 * support these features.
6850 * We are not allowed to take the i_mutex here so we have to play games to
6851 * protect against truncate races as the page could now be beyond EOF. Because
6852 * vmtruncate() writes the inode size before removing pages, once we have the
6853 * page lock we can determine safely if the page is beyond EOF. If it is not
6854 * beyond EOF, then the page is guaranteed safe against truncation until we
6857 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
6859 struct page
*page
= vmf
->page
;
6860 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
6861 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6862 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6863 struct btrfs_ordered_extent
*ordered
;
6864 struct extent_state
*cached_state
= NULL
;
6866 unsigned long zero_start
;
6873 sb_start_pagefault(inode
->i_sb
);
6874 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
6876 ret
= file_update_time(vma
->vm_file
);
6882 else /* -ENOSPC, -EIO, etc */
6883 ret
= VM_FAULT_SIGBUS
;
6889 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
6892 size
= i_size_read(inode
);
6893 page_start
= page_offset(page
);
6894 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6896 if ((page
->mapping
!= inode
->i_mapping
) ||
6897 (page_start
>= size
)) {
6898 /* page got truncated out from underneath us */
6901 wait_on_page_writeback(page
);
6903 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
6904 set_page_extent_mapped(page
);
6907 * we can't set the delalloc bits if there are pending ordered
6908 * extents. Drop our locks and wait for them to finish
6910 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
6912 unlock_extent_cached(io_tree
, page_start
, page_end
,
6913 &cached_state
, GFP_NOFS
);
6915 btrfs_start_ordered_extent(inode
, ordered
, 1);
6916 btrfs_put_ordered_extent(ordered
);
6921 * XXX - page_mkwrite gets called every time the page is dirtied, even
6922 * if it was already dirty, so for space accounting reasons we need to
6923 * clear any delalloc bits for the range we are fixing to save. There
6924 * is probably a better way to do this, but for now keep consistent with
6925 * prepare_pages in the normal write path.
6927 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
6928 EXTENT_DIRTY
| EXTENT_DELALLOC
|
6929 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
6930 0, 0, &cached_state
, GFP_NOFS
);
6932 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
6935 unlock_extent_cached(io_tree
, page_start
, page_end
,
6936 &cached_state
, GFP_NOFS
);
6937 ret
= VM_FAULT_SIGBUS
;
6942 /* page is wholly or partially inside EOF */
6943 if (page_start
+ PAGE_CACHE_SIZE
> size
)
6944 zero_start
= size
& ~PAGE_CACHE_MASK
;
6946 zero_start
= PAGE_CACHE_SIZE
;
6948 if (zero_start
!= PAGE_CACHE_SIZE
) {
6950 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
6951 flush_dcache_page(page
);
6954 ClearPageChecked(page
);
6955 set_page_dirty(page
);
6956 SetPageUptodate(page
);
6958 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
6959 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
6960 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->root
->last_log_commit
;
6962 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
6966 sb_end_pagefault(inode
->i_sb
);
6967 return VM_FAULT_LOCKED
;
6971 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
6973 sb_end_pagefault(inode
->i_sb
);
6977 static int btrfs_truncate(struct inode
*inode
)
6979 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6980 struct btrfs_block_rsv
*rsv
;
6983 struct btrfs_trans_handle
*trans
;
6984 u64 mask
= root
->sectorsize
- 1;
6985 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
6987 ret
= btrfs_truncate_page(inode
, inode
->i_size
, 0, 0);
6991 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
6992 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
6995 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6996 * 3 things going on here
6998 * 1) We need to reserve space for our orphan item and the space to
6999 * delete our orphan item. Lord knows we don't want to have a dangling
7000 * orphan item because we didn't reserve space to remove it.
7002 * 2) We need to reserve space to update our inode.
7004 * 3) We need to have something to cache all the space that is going to
7005 * be free'd up by the truncate operation, but also have some slack
7006 * space reserved in case it uses space during the truncate (thank you
7007 * very much snapshotting).
7009 * And we need these to all be seperate. The fact is we can use alot of
7010 * space doing the truncate, and we have no earthly idea how much space
7011 * we will use, so we need the truncate reservation to be seperate so it
7012 * doesn't end up using space reserved for updating the inode or
7013 * removing the orphan item. We also need to be able to stop the
7014 * transaction and start a new one, which means we need to be able to
7015 * update the inode several times, and we have no idea of knowing how
7016 * many times that will be, so we can't just reserve 1 item for the
7017 * entirety of the opration, so that has to be done seperately as well.
7018 * Then there is the orphan item, which does indeed need to be held on
7019 * to for the whole operation, and we need nobody to touch this reserved
7020 * space except the orphan code.
7022 * So that leaves us with
7024 * 1) root->orphan_block_rsv - for the orphan deletion.
7025 * 2) rsv - for the truncate reservation, which we will steal from the
7026 * transaction reservation.
7027 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7028 * updating the inode.
7030 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
7033 rsv
->size
= min_size
;
7037 * 1 for the truncate slack space
7038 * 1 for updating the inode.
7040 trans
= btrfs_start_transaction(root
, 2);
7041 if (IS_ERR(trans
)) {
7042 err
= PTR_ERR(trans
);
7046 /* Migrate the slack space for the truncate to our reserve */
7047 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
, rsv
,
7052 * setattr is responsible for setting the ordered_data_close flag,
7053 * but that is only tested during the last file release. That
7054 * could happen well after the next commit, leaving a great big
7055 * window where new writes may get lost if someone chooses to write
7056 * to this file after truncating to zero
7058 * The inode doesn't have any dirty data here, and so if we commit
7059 * this is a noop. If someone immediately starts writing to the inode
7060 * it is very likely we'll catch some of their writes in this
7061 * transaction, and the commit will find this file on the ordered
7062 * data list with good things to send down.
7064 * This is a best effort solution, there is still a window where
7065 * using truncate to replace the contents of the file will
7066 * end up with a zero length file after a crash.
7068 if (inode
->i_size
== 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
7069 &BTRFS_I(inode
)->runtime_flags
))
7070 btrfs_add_ordered_operation(trans
, root
, inode
);
7073 * So if we truncate and then write and fsync we normally would just
7074 * write the extents that changed, which is a problem if we need to
7075 * first truncate that entire inode. So set this flag so we write out
7076 * all of the extents in the inode to the sync log so we're completely
7079 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
7080 trans
->block_rsv
= rsv
;
7083 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
7085 BTRFS_EXTENT_DATA_KEY
);
7086 if (ret
!= -ENOSPC
) {
7091 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7092 ret
= btrfs_update_inode(trans
, root
, inode
);
7098 btrfs_end_transaction(trans
, root
);
7099 btrfs_btree_balance_dirty(root
);
7101 trans
= btrfs_start_transaction(root
, 2);
7102 if (IS_ERR(trans
)) {
7103 ret
= err
= PTR_ERR(trans
);
7108 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
,
7110 BUG_ON(ret
); /* shouldn't happen */
7111 trans
->block_rsv
= rsv
;
7114 if (ret
== 0 && inode
->i_nlink
> 0) {
7115 trans
->block_rsv
= root
->orphan_block_rsv
;
7116 ret
= btrfs_orphan_del(trans
, inode
);
7122 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7123 ret
= btrfs_update_inode(trans
, root
, inode
);
7127 ret
= btrfs_end_transaction(trans
, root
);
7128 btrfs_btree_balance_dirty(root
);
7132 btrfs_free_block_rsv(root
, rsv
);
7141 * create a new subvolume directory/inode (helper for the ioctl).
7143 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
7144 struct btrfs_root
*new_root
, u64 new_dirid
)
7146 struct inode
*inode
;
7150 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
7151 new_dirid
, new_dirid
,
7152 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
7155 return PTR_ERR(inode
);
7156 inode
->i_op
= &btrfs_dir_inode_operations
;
7157 inode
->i_fop
= &btrfs_dir_file_operations
;
7159 set_nlink(inode
, 1);
7160 btrfs_i_size_write(inode
, 0);
7162 err
= btrfs_update_inode(trans
, new_root
, inode
);
7168 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
7170 struct btrfs_inode
*ei
;
7171 struct inode
*inode
;
7173 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
7180 ei
->last_sub_trans
= 0;
7181 ei
->logged_trans
= 0;
7182 ei
->delalloc_bytes
= 0;
7183 ei
->disk_i_size
= 0;
7186 ei
->index_cnt
= (u64
)-1;
7187 ei
->last_unlink_trans
= 0;
7188 ei
->last_log_commit
= 0;
7190 spin_lock_init(&ei
->lock
);
7191 ei
->outstanding_extents
= 0;
7192 ei
->reserved_extents
= 0;
7194 ei
->runtime_flags
= 0;
7195 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
7197 ei
->delayed_node
= NULL
;
7199 inode
= &ei
->vfs_inode
;
7200 extent_map_tree_init(&ei
->extent_tree
);
7201 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
);
7202 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
);
7203 ei
->io_tree
.track_uptodate
= 1;
7204 ei
->io_failure_tree
.track_uptodate
= 1;
7205 atomic_set(&ei
->sync_writers
, 0);
7206 mutex_init(&ei
->log_mutex
);
7207 mutex_init(&ei
->delalloc_mutex
);
7208 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
7209 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
7210 INIT_LIST_HEAD(&ei
->ordered_operations
);
7211 RB_CLEAR_NODE(&ei
->rb_node
);
7216 static void btrfs_i_callback(struct rcu_head
*head
)
7218 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
7219 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
7222 void btrfs_destroy_inode(struct inode
*inode
)
7224 struct btrfs_ordered_extent
*ordered
;
7225 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7227 WARN_ON(!hlist_empty(&inode
->i_dentry
));
7228 WARN_ON(inode
->i_data
.nrpages
);
7229 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
7230 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
7231 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
7232 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
7235 * This can happen where we create an inode, but somebody else also
7236 * created the same inode and we need to destroy the one we already
7243 * Make sure we're properly removed from the ordered operation
7247 if (!list_empty(&BTRFS_I(inode
)->ordered_operations
)) {
7248 spin_lock(&root
->fs_info
->ordered_extent_lock
);
7249 list_del_init(&BTRFS_I(inode
)->ordered_operations
);
7250 spin_unlock(&root
->fs_info
->ordered_extent_lock
);
7253 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
7254 &BTRFS_I(inode
)->runtime_flags
)) {
7255 printk(KERN_INFO
"BTRFS: inode %llu still on the orphan list\n",
7256 (unsigned long long)btrfs_ino(inode
));
7257 atomic_dec(&root
->orphan_inodes
);
7261 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
7265 printk(KERN_ERR
"btrfs found ordered "
7266 "extent %llu %llu on inode cleanup\n",
7267 (unsigned long long)ordered
->file_offset
,
7268 (unsigned long long)ordered
->len
);
7269 btrfs_remove_ordered_extent(inode
, ordered
);
7270 btrfs_put_ordered_extent(ordered
);
7271 btrfs_put_ordered_extent(ordered
);
7274 inode_tree_del(inode
);
7275 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
7277 btrfs_remove_delayed_node(inode
);
7278 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
7281 int btrfs_drop_inode(struct inode
*inode
)
7283 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7285 if (btrfs_root_refs(&root
->root_item
) == 0 &&
7286 !btrfs_is_free_space_inode(inode
))
7289 return generic_drop_inode(inode
);
7292 static void init_once(void *foo
)
7294 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
7296 inode_init_once(&ei
->vfs_inode
);
7299 void btrfs_destroy_cachep(void)
7302 * Make sure all delayed rcu free inodes are flushed before we
7306 if (btrfs_inode_cachep
)
7307 kmem_cache_destroy(btrfs_inode_cachep
);
7308 if (btrfs_trans_handle_cachep
)
7309 kmem_cache_destroy(btrfs_trans_handle_cachep
);
7310 if (btrfs_transaction_cachep
)
7311 kmem_cache_destroy(btrfs_transaction_cachep
);
7312 if (btrfs_path_cachep
)
7313 kmem_cache_destroy(btrfs_path_cachep
);
7314 if (btrfs_free_space_cachep
)
7315 kmem_cache_destroy(btrfs_free_space_cachep
);
7316 if (btrfs_delalloc_work_cachep
)
7317 kmem_cache_destroy(btrfs_delalloc_work_cachep
);
7320 int btrfs_init_cachep(void)
7322 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode",
7323 sizeof(struct btrfs_inode
), 0,
7324 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
7325 if (!btrfs_inode_cachep
)
7328 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle",
7329 sizeof(struct btrfs_trans_handle
), 0,
7330 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7331 if (!btrfs_trans_handle_cachep
)
7334 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction",
7335 sizeof(struct btrfs_transaction
), 0,
7336 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7337 if (!btrfs_transaction_cachep
)
7340 btrfs_path_cachep
= kmem_cache_create("btrfs_path",
7341 sizeof(struct btrfs_path
), 0,
7342 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7343 if (!btrfs_path_cachep
)
7346 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space",
7347 sizeof(struct btrfs_free_space
), 0,
7348 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7349 if (!btrfs_free_space_cachep
)
7352 btrfs_delalloc_work_cachep
= kmem_cache_create("btrfs_delalloc_work",
7353 sizeof(struct btrfs_delalloc_work
), 0,
7354 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
,
7356 if (!btrfs_delalloc_work_cachep
)
7361 btrfs_destroy_cachep();
7365 static int btrfs_getattr(struct vfsmount
*mnt
,
7366 struct dentry
*dentry
, struct kstat
*stat
)
7369 struct inode
*inode
= dentry
->d_inode
;
7370 u32 blocksize
= inode
->i_sb
->s_blocksize
;
7372 generic_fillattr(inode
, stat
);
7373 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
7374 stat
->blksize
= PAGE_CACHE_SIZE
;
7376 spin_lock(&BTRFS_I(inode
)->lock
);
7377 delalloc_bytes
= BTRFS_I(inode
)->delalloc_bytes
;
7378 spin_unlock(&BTRFS_I(inode
)->lock
);
7379 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
7380 ALIGN(delalloc_bytes
, blocksize
)) >> 9;
7385 * If a file is moved, it will inherit the cow and compression flags of the new
7388 static void fixup_inode_flags(struct inode
*dir
, struct inode
*inode
)
7390 struct btrfs_inode
*b_dir
= BTRFS_I(dir
);
7391 struct btrfs_inode
*b_inode
= BTRFS_I(inode
);
7393 if (b_dir
->flags
& BTRFS_INODE_NODATACOW
)
7394 b_inode
->flags
|= BTRFS_INODE_NODATACOW
;
7396 b_inode
->flags
&= ~BTRFS_INODE_NODATACOW
;
7398 if (b_dir
->flags
& BTRFS_INODE_COMPRESS
) {
7399 b_inode
->flags
|= BTRFS_INODE_COMPRESS
;
7400 b_inode
->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
7402 b_inode
->flags
&= ~(BTRFS_INODE_COMPRESS
|
7403 BTRFS_INODE_NOCOMPRESS
);
7407 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
7408 struct inode
*new_dir
, struct dentry
*new_dentry
)
7410 struct btrfs_trans_handle
*trans
;
7411 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
7412 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
7413 struct inode
*new_inode
= new_dentry
->d_inode
;
7414 struct inode
*old_inode
= old_dentry
->d_inode
;
7415 struct timespec ctime
= CURRENT_TIME
;
7419 u64 old_ino
= btrfs_ino(old_inode
);
7421 if (btrfs_ino(new_dir
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
7424 /* we only allow rename subvolume link between subvolumes */
7425 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
7428 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
7429 (new_inode
&& btrfs_ino(new_inode
) == BTRFS_FIRST_FREE_OBJECTID
))
7432 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
7433 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
7437 /* check for collisions, even if the name isn't there */
7438 ret
= btrfs_check_dir_item_collision(root
, new_dir
->i_ino
,
7439 new_dentry
->d_name
.name
,
7440 new_dentry
->d_name
.len
);
7443 if (ret
== -EEXIST
) {
7445 * eexist without a new_inode */
7451 /* maybe -EOVERFLOW */
7458 * we're using rename to replace one file with another.
7459 * and the replacement file is large. Start IO on it now so
7460 * we don't add too much work to the end of the transaction
7462 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
&&
7463 old_inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
7464 filemap_flush(old_inode
->i_mapping
);
7466 /* close the racy window with snapshot create/destroy ioctl */
7467 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
7468 down_read(&root
->fs_info
->subvol_sem
);
7470 * We want to reserve the absolute worst case amount of items. So if
7471 * both inodes are subvols and we need to unlink them then that would
7472 * require 4 item modifications, but if they are both normal inodes it
7473 * would require 5 item modifications, so we'll assume their normal
7474 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7475 * should cover the worst case number of items we'll modify.
7477 trans
= btrfs_start_transaction(root
, 20);
7478 if (IS_ERR(trans
)) {
7479 ret
= PTR_ERR(trans
);
7484 btrfs_record_root_in_trans(trans
, dest
);
7486 ret
= btrfs_set_inode_index(new_dir
, &index
);
7490 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
7491 /* force full log commit if subvolume involved. */
7492 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
7494 ret
= btrfs_insert_inode_ref(trans
, dest
,
7495 new_dentry
->d_name
.name
,
7496 new_dentry
->d_name
.len
,
7498 btrfs_ino(new_dir
), index
);
7502 * this is an ugly little race, but the rename is required
7503 * to make sure that if we crash, the inode is either at the
7504 * old name or the new one. pinning the log transaction lets
7505 * us make sure we don't allow a log commit to come in after
7506 * we unlink the name but before we add the new name back in.
7508 btrfs_pin_log_trans(root
);
7511 * make sure the inode gets flushed if it is replacing
7514 if (new_inode
&& new_inode
->i_size
&& S_ISREG(old_inode
->i_mode
))
7515 btrfs_add_ordered_operation(trans
, root
, old_inode
);
7517 inode_inc_iversion(old_dir
);
7518 inode_inc_iversion(new_dir
);
7519 inode_inc_iversion(old_inode
);
7520 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
7521 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
7522 old_inode
->i_ctime
= ctime
;
7524 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
7525 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
7527 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
7528 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
7529 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
7530 old_dentry
->d_name
.name
,
7531 old_dentry
->d_name
.len
);
7533 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
7534 old_dentry
->d_inode
,
7535 old_dentry
->d_name
.name
,
7536 old_dentry
->d_name
.len
);
7538 ret
= btrfs_update_inode(trans
, root
, old_inode
);
7541 btrfs_abort_transaction(trans
, root
, ret
);
7546 inode_inc_iversion(new_inode
);
7547 new_inode
->i_ctime
= CURRENT_TIME
;
7548 if (unlikely(btrfs_ino(new_inode
) ==
7549 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
7550 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
7551 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
7553 new_dentry
->d_name
.name
,
7554 new_dentry
->d_name
.len
);
7555 BUG_ON(new_inode
->i_nlink
== 0);
7557 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
7558 new_dentry
->d_inode
,
7559 new_dentry
->d_name
.name
,
7560 new_dentry
->d_name
.len
);
7562 if (!ret
&& new_inode
->i_nlink
== 0) {
7563 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
7567 btrfs_abort_transaction(trans
, root
, ret
);
7572 fixup_inode_flags(new_dir
, old_inode
);
7574 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
7575 new_dentry
->d_name
.name
,
7576 new_dentry
->d_name
.len
, 0, index
);
7578 btrfs_abort_transaction(trans
, root
, ret
);
7582 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
7583 struct dentry
*parent
= new_dentry
->d_parent
;
7584 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
7585 btrfs_end_log_trans(root
);
7588 btrfs_end_transaction(trans
, root
);
7590 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
7591 up_read(&root
->fs_info
->subvol_sem
);
7596 static void btrfs_run_delalloc_work(struct btrfs_work
*work
)
7598 struct btrfs_delalloc_work
*delalloc_work
;
7600 delalloc_work
= container_of(work
, struct btrfs_delalloc_work
,
7602 if (delalloc_work
->wait
)
7603 btrfs_wait_ordered_range(delalloc_work
->inode
, 0, (u64
)-1);
7605 filemap_flush(delalloc_work
->inode
->i_mapping
);
7607 if (delalloc_work
->delay_iput
)
7608 btrfs_add_delayed_iput(delalloc_work
->inode
);
7610 iput(delalloc_work
->inode
);
7611 complete(&delalloc_work
->completion
);
7614 struct btrfs_delalloc_work
*btrfs_alloc_delalloc_work(struct inode
*inode
,
7615 int wait
, int delay_iput
)
7617 struct btrfs_delalloc_work
*work
;
7619 work
= kmem_cache_zalloc(btrfs_delalloc_work_cachep
, GFP_NOFS
);
7623 init_completion(&work
->completion
);
7624 INIT_LIST_HEAD(&work
->list
);
7625 work
->inode
= inode
;
7627 work
->delay_iput
= delay_iput
;
7628 work
->work
.func
= btrfs_run_delalloc_work
;
7633 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work
*work
)
7635 wait_for_completion(&work
->completion
);
7636 kmem_cache_free(btrfs_delalloc_work_cachep
, work
);
7640 * some fairly slow code that needs optimization. This walks the list
7641 * of all the inodes with pending delalloc and forces them to disk.
7643 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
7645 struct btrfs_inode
*binode
;
7646 struct inode
*inode
;
7647 struct btrfs_delalloc_work
*work
, *next
;
7648 struct list_head works
;
7649 struct list_head splice
;
7652 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
7655 INIT_LIST_HEAD(&works
);
7656 INIT_LIST_HEAD(&splice
);
7658 spin_lock(&root
->fs_info
->delalloc_lock
);
7659 list_splice_init(&root
->fs_info
->delalloc_inodes
, &splice
);
7660 while (!list_empty(&splice
)) {
7661 binode
= list_entry(splice
.next
, struct btrfs_inode
,
7664 list_del_init(&binode
->delalloc_inodes
);
7666 inode
= igrab(&binode
->vfs_inode
);
7668 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
7669 &binode
->runtime_flags
);
7673 list_add_tail(&binode
->delalloc_inodes
,
7674 &root
->fs_info
->delalloc_inodes
);
7675 spin_unlock(&root
->fs_info
->delalloc_lock
);
7677 work
= btrfs_alloc_delalloc_work(inode
, 0, delay_iput
);
7678 if (unlikely(!work
)) {
7682 list_add_tail(&work
->list
, &works
);
7683 btrfs_queue_worker(&root
->fs_info
->flush_workers
,
7687 spin_lock(&root
->fs_info
->delalloc_lock
);
7689 spin_unlock(&root
->fs_info
->delalloc_lock
);
7691 list_for_each_entry_safe(work
, next
, &works
, list
) {
7692 list_del_init(&work
->list
);
7693 btrfs_wait_and_free_delalloc_work(work
);
7696 /* the filemap_flush will queue IO into the worker threads, but
7697 * we have to make sure the IO is actually started and that
7698 * ordered extents get created before we return
7700 atomic_inc(&root
->fs_info
->async_submit_draining
);
7701 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
7702 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
7703 wait_event(root
->fs_info
->async_submit_wait
,
7704 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
7705 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
7707 atomic_dec(&root
->fs_info
->async_submit_draining
);
7710 list_for_each_entry_safe(work
, next
, &works
, list
) {
7711 list_del_init(&work
->list
);
7712 btrfs_wait_and_free_delalloc_work(work
);
7715 if (!list_empty_careful(&splice
)) {
7716 spin_lock(&root
->fs_info
->delalloc_lock
);
7717 list_splice_tail(&splice
, &root
->fs_info
->delalloc_inodes
);
7718 spin_unlock(&root
->fs_info
->delalloc_lock
);
7723 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
7724 const char *symname
)
7726 struct btrfs_trans_handle
*trans
;
7727 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
7728 struct btrfs_path
*path
;
7729 struct btrfs_key key
;
7730 struct inode
*inode
= NULL
;
7738 struct btrfs_file_extent_item
*ei
;
7739 struct extent_buffer
*leaf
;
7741 name_len
= strlen(symname
) + 1;
7742 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
7743 return -ENAMETOOLONG
;
7746 * 2 items for inode item and ref
7747 * 2 items for dir items
7748 * 1 item for xattr if selinux is on
7750 trans
= btrfs_start_transaction(root
, 5);
7752 return PTR_ERR(trans
);
7754 err
= btrfs_find_free_ino(root
, &objectid
);
7758 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
7759 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
7760 S_IFLNK
|S_IRWXUGO
, &index
);
7761 if (IS_ERR(inode
)) {
7762 err
= PTR_ERR(inode
);
7766 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
7773 * If the active LSM wants to access the inode during
7774 * d_instantiate it needs these. Smack checks to see
7775 * if the filesystem supports xattrs by looking at the
7778 inode
->i_fop
= &btrfs_file_operations
;
7779 inode
->i_op
= &btrfs_file_inode_operations
;
7781 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
7785 inode
->i_mapping
->a_ops
= &btrfs_aops
;
7786 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
7787 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
7792 path
= btrfs_alloc_path();
7798 key
.objectid
= btrfs_ino(inode
);
7800 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
7801 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
7802 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
7806 btrfs_free_path(path
);
7809 leaf
= path
->nodes
[0];
7810 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
7811 struct btrfs_file_extent_item
);
7812 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
7813 btrfs_set_file_extent_type(leaf
, ei
,
7814 BTRFS_FILE_EXTENT_INLINE
);
7815 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
7816 btrfs_set_file_extent_compression(leaf
, ei
, 0);
7817 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
7818 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
7820 ptr
= btrfs_file_extent_inline_start(ei
);
7821 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
7822 btrfs_mark_buffer_dirty(leaf
);
7823 btrfs_free_path(path
);
7825 inode
->i_op
= &btrfs_symlink_inode_operations
;
7826 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
7827 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
7828 inode_set_bytes(inode
, name_len
);
7829 btrfs_i_size_write(inode
, name_len
- 1);
7830 err
= btrfs_update_inode(trans
, root
, inode
);
7836 d_instantiate(dentry
, inode
);
7837 btrfs_end_transaction(trans
, root
);
7839 inode_dec_link_count(inode
);
7842 btrfs_btree_balance_dirty(root
);
7846 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
7847 u64 start
, u64 num_bytes
, u64 min_size
,
7848 loff_t actual_len
, u64
*alloc_hint
,
7849 struct btrfs_trans_handle
*trans
)
7851 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
7852 struct extent_map
*em
;
7853 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7854 struct btrfs_key ins
;
7855 u64 cur_offset
= start
;
7858 bool own_trans
= true;
7862 while (num_bytes
> 0) {
7864 trans
= btrfs_start_transaction(root
, 3);
7865 if (IS_ERR(trans
)) {
7866 ret
= PTR_ERR(trans
);
7871 ret
= btrfs_reserve_extent(trans
, root
, num_bytes
, min_size
,
7872 0, *alloc_hint
, &ins
, 1);
7875 btrfs_end_transaction(trans
, root
);
7879 ret
= insert_reserved_file_extent(trans
, inode
,
7880 cur_offset
, ins
.objectid
,
7881 ins
.offset
, ins
.offset
,
7882 ins
.offset
, 0, 0, 0,
7883 BTRFS_FILE_EXTENT_PREALLOC
);
7885 btrfs_abort_transaction(trans
, root
, ret
);
7887 btrfs_end_transaction(trans
, root
);
7890 btrfs_drop_extent_cache(inode
, cur_offset
,
7891 cur_offset
+ ins
.offset
-1, 0);
7893 em
= alloc_extent_map();
7895 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
7896 &BTRFS_I(inode
)->runtime_flags
);
7900 em
->start
= cur_offset
;
7901 em
->orig_start
= cur_offset
;
7902 em
->len
= ins
.offset
;
7903 em
->block_start
= ins
.objectid
;
7904 em
->block_len
= ins
.offset
;
7905 em
->orig_block_len
= ins
.offset
;
7906 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
7907 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
7908 em
->generation
= trans
->transid
;
7911 write_lock(&em_tree
->lock
);
7912 ret
= add_extent_mapping(em_tree
, em
);
7914 list_move(&em
->list
,
7915 &em_tree
->modified_extents
);
7916 write_unlock(&em_tree
->lock
);
7919 btrfs_drop_extent_cache(inode
, cur_offset
,
7920 cur_offset
+ ins
.offset
- 1,
7923 free_extent_map(em
);
7925 num_bytes
-= ins
.offset
;
7926 cur_offset
+= ins
.offset
;
7927 *alloc_hint
= ins
.objectid
+ ins
.offset
;
7929 inode_inc_iversion(inode
);
7930 inode
->i_ctime
= CURRENT_TIME
;
7931 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
7932 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
7933 (actual_len
> inode
->i_size
) &&
7934 (cur_offset
> inode
->i_size
)) {
7935 if (cur_offset
> actual_len
)
7936 i_size
= actual_len
;
7938 i_size
= cur_offset
;
7939 i_size_write(inode
, i_size
);
7940 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
7943 ret
= btrfs_update_inode(trans
, root
, inode
);
7946 btrfs_abort_transaction(trans
, root
, ret
);
7948 btrfs_end_transaction(trans
, root
);
7953 btrfs_end_transaction(trans
, root
);
7958 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
7959 u64 start
, u64 num_bytes
, u64 min_size
,
7960 loff_t actual_len
, u64
*alloc_hint
)
7962 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
7963 min_size
, actual_len
, alloc_hint
,
7967 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
7968 struct btrfs_trans_handle
*trans
, int mode
,
7969 u64 start
, u64 num_bytes
, u64 min_size
,
7970 loff_t actual_len
, u64
*alloc_hint
)
7972 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
7973 min_size
, actual_len
, alloc_hint
, trans
);
7976 static int btrfs_set_page_dirty(struct page
*page
)
7978 return __set_page_dirty_nobuffers(page
);
7981 static int btrfs_permission(struct inode
*inode
, int mask
)
7983 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7984 umode_t mode
= inode
->i_mode
;
7986 if (mask
& MAY_WRITE
&&
7987 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
7988 if (btrfs_root_readonly(root
))
7990 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
7993 return generic_permission(inode
, mask
);
7996 static const struct inode_operations btrfs_dir_inode_operations
= {
7997 .getattr
= btrfs_getattr
,
7998 .lookup
= btrfs_lookup
,
7999 .create
= btrfs_create
,
8000 .unlink
= btrfs_unlink
,
8002 .mkdir
= btrfs_mkdir
,
8003 .rmdir
= btrfs_rmdir
,
8004 .rename
= btrfs_rename
,
8005 .symlink
= btrfs_symlink
,
8006 .setattr
= btrfs_setattr
,
8007 .mknod
= btrfs_mknod
,
8008 .setxattr
= btrfs_setxattr
,
8009 .getxattr
= btrfs_getxattr
,
8010 .listxattr
= btrfs_listxattr
,
8011 .removexattr
= btrfs_removexattr
,
8012 .permission
= btrfs_permission
,
8013 .get_acl
= btrfs_get_acl
,
8015 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
8016 .lookup
= btrfs_lookup
,
8017 .permission
= btrfs_permission
,
8018 .get_acl
= btrfs_get_acl
,
8021 static const struct file_operations btrfs_dir_file_operations
= {
8022 .llseek
= generic_file_llseek
,
8023 .read
= generic_read_dir
,
8024 .readdir
= btrfs_real_readdir
,
8025 .unlocked_ioctl
= btrfs_ioctl
,
8026 #ifdef CONFIG_COMPAT
8027 .compat_ioctl
= btrfs_ioctl
,
8029 .release
= btrfs_release_file
,
8030 .fsync
= btrfs_sync_file
,
8033 static struct extent_io_ops btrfs_extent_io_ops
= {
8034 .fill_delalloc
= run_delalloc_range
,
8035 .submit_bio_hook
= btrfs_submit_bio_hook
,
8036 .merge_bio_hook
= btrfs_merge_bio_hook
,
8037 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
8038 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
8039 .writepage_start_hook
= btrfs_writepage_start_hook
,
8040 .set_bit_hook
= btrfs_set_bit_hook
,
8041 .clear_bit_hook
= btrfs_clear_bit_hook
,
8042 .merge_extent_hook
= btrfs_merge_extent_hook
,
8043 .split_extent_hook
= btrfs_split_extent_hook
,
8047 * btrfs doesn't support the bmap operation because swapfiles
8048 * use bmap to make a mapping of extents in the file. They assume
8049 * these extents won't change over the life of the file and they
8050 * use the bmap result to do IO directly to the drive.
8052 * the btrfs bmap call would return logical addresses that aren't
8053 * suitable for IO and they also will change frequently as COW
8054 * operations happen. So, swapfile + btrfs == corruption.
8056 * For now we're avoiding this by dropping bmap.
8058 static const struct address_space_operations btrfs_aops
= {
8059 .readpage
= btrfs_readpage
,
8060 .writepage
= btrfs_writepage
,
8061 .writepages
= btrfs_writepages
,
8062 .readpages
= btrfs_readpages
,
8063 .direct_IO
= btrfs_direct_IO
,
8064 .invalidatepage
= btrfs_invalidatepage
,
8065 .releasepage
= btrfs_releasepage
,
8066 .set_page_dirty
= btrfs_set_page_dirty
,
8067 .error_remove_page
= generic_error_remove_page
,
8070 static const struct address_space_operations btrfs_symlink_aops
= {
8071 .readpage
= btrfs_readpage
,
8072 .writepage
= btrfs_writepage
,
8073 .invalidatepage
= btrfs_invalidatepage
,
8074 .releasepage
= btrfs_releasepage
,
8077 static const struct inode_operations btrfs_file_inode_operations
= {
8078 .getattr
= btrfs_getattr
,
8079 .setattr
= btrfs_setattr
,
8080 .setxattr
= btrfs_setxattr
,
8081 .getxattr
= btrfs_getxattr
,
8082 .listxattr
= btrfs_listxattr
,
8083 .removexattr
= btrfs_removexattr
,
8084 .permission
= btrfs_permission
,
8085 .fiemap
= btrfs_fiemap
,
8086 .get_acl
= btrfs_get_acl
,
8087 .update_time
= btrfs_update_time
,
8089 static const struct inode_operations btrfs_special_inode_operations
= {
8090 .getattr
= btrfs_getattr
,
8091 .setattr
= btrfs_setattr
,
8092 .permission
= btrfs_permission
,
8093 .setxattr
= btrfs_setxattr
,
8094 .getxattr
= btrfs_getxattr
,
8095 .listxattr
= btrfs_listxattr
,
8096 .removexattr
= btrfs_removexattr
,
8097 .get_acl
= btrfs_get_acl
,
8098 .update_time
= btrfs_update_time
,
8100 static const struct inode_operations btrfs_symlink_inode_operations
= {
8101 .readlink
= generic_readlink
,
8102 .follow_link
= page_follow_link_light
,
8103 .put_link
= page_put_link
,
8104 .getattr
= btrfs_getattr
,
8105 .setattr
= btrfs_setattr
,
8106 .permission
= btrfs_permission
,
8107 .setxattr
= btrfs_setxattr
,
8108 .getxattr
= btrfs_getxattr
,
8109 .listxattr
= btrfs_listxattr
,
8110 .removexattr
= btrfs_removexattr
,
8111 .get_acl
= btrfs_get_acl
,
8112 .update_time
= btrfs_update_time
,
8115 const struct dentry_operations btrfs_dentry_operations
= {
8116 .d_delete
= btrfs_dentry_delete
,
8117 .d_release
= btrfs_dentry_release
,