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>
44 #include "transaction.h"
45 #include "btrfs_inode.h"
47 #include "print-tree.h"
49 #include "ordered-data.h"
52 #include "compression.h"
54 #include "free-space-cache.h"
55 #include "inode-map.h"
57 struct btrfs_iget_args
{
59 struct btrfs_root
*root
;
62 static const struct inode_operations btrfs_dir_inode_operations
;
63 static const struct inode_operations btrfs_symlink_inode_operations
;
64 static const struct inode_operations btrfs_dir_ro_inode_operations
;
65 static const struct inode_operations btrfs_special_inode_operations
;
66 static const struct inode_operations btrfs_file_inode_operations
;
67 static const struct address_space_operations btrfs_aops
;
68 static const struct address_space_operations btrfs_symlink_aops
;
69 static const struct file_operations btrfs_dir_file_operations
;
70 static struct extent_io_ops btrfs_extent_io_ops
;
72 static struct kmem_cache
*btrfs_inode_cachep
;
73 struct kmem_cache
*btrfs_trans_handle_cachep
;
74 struct kmem_cache
*btrfs_transaction_cachep
;
75 struct kmem_cache
*btrfs_path_cachep
;
76 struct kmem_cache
*btrfs_free_space_cachep
;
79 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
80 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
81 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
82 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
83 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
84 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
85 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
86 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
89 static int btrfs_setsize(struct inode
*inode
, loff_t newsize
);
90 static int btrfs_truncate(struct inode
*inode
);
91 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
);
92 static noinline
int cow_file_range(struct inode
*inode
,
93 struct page
*locked_page
,
94 u64 start
, u64 end
, int *page_started
,
95 unsigned long *nr_written
, int unlock
);
97 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
98 struct inode
*inode
, struct inode
*dir
,
99 const struct qstr
*qstr
)
103 err
= btrfs_init_acl(trans
, inode
, dir
);
105 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
110 * this does all the hard work for inserting an inline extent into
111 * the btree. The caller should have done a btrfs_drop_extents so that
112 * no overlapping inline items exist in the btree
114 static noinline
int insert_inline_extent(struct btrfs_trans_handle
*trans
,
115 struct btrfs_root
*root
, struct inode
*inode
,
116 u64 start
, size_t size
, size_t compressed_size
,
118 struct page
**compressed_pages
)
120 struct btrfs_key key
;
121 struct btrfs_path
*path
;
122 struct extent_buffer
*leaf
;
123 struct page
*page
= NULL
;
126 struct btrfs_file_extent_item
*ei
;
129 size_t cur_size
= size
;
131 unsigned long offset
;
133 if (compressed_size
&& compressed_pages
)
134 cur_size
= compressed_size
;
136 path
= btrfs_alloc_path();
140 path
->leave_spinning
= 1;
142 key
.objectid
= btrfs_ino(inode
);
144 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
145 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
147 inode_add_bytes(inode
, size
);
148 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
155 leaf
= path
->nodes
[0];
156 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
157 struct btrfs_file_extent_item
);
158 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
159 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
160 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
161 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
162 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
163 ptr
= btrfs_file_extent_inline_start(ei
);
165 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
168 while (compressed_size
> 0) {
169 cpage
= compressed_pages
[i
];
170 cur_size
= min_t(unsigned long, compressed_size
,
173 kaddr
= kmap_atomic(cpage
, KM_USER0
);
174 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
175 kunmap_atomic(kaddr
, KM_USER0
);
179 compressed_size
-= cur_size
;
181 btrfs_set_file_extent_compression(leaf
, ei
,
184 page
= find_get_page(inode
->i_mapping
,
185 start
>> PAGE_CACHE_SHIFT
);
186 btrfs_set_file_extent_compression(leaf
, ei
, 0);
187 kaddr
= kmap_atomic(page
, KM_USER0
);
188 offset
= start
& (PAGE_CACHE_SIZE
- 1);
189 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
190 kunmap_atomic(kaddr
, KM_USER0
);
191 page_cache_release(page
);
193 btrfs_mark_buffer_dirty(leaf
);
194 btrfs_free_path(path
);
197 * we're an inline extent, so nobody can
198 * extend the file past i_size without locking
199 * a page we already have locked.
201 * We must do any isize and inode updates
202 * before we unlock the pages. Otherwise we
203 * could end up racing with unlink.
205 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
206 btrfs_update_inode(trans
, root
, inode
);
210 btrfs_free_path(path
);
216 * conditionally insert an inline extent into the file. This
217 * does the checks required to make sure the data is small enough
218 * to fit as an inline extent.
220 static noinline
int cow_file_range_inline(struct btrfs_trans_handle
*trans
,
221 struct btrfs_root
*root
,
222 struct inode
*inode
, u64 start
, u64 end
,
223 size_t compressed_size
, int compress_type
,
224 struct page
**compressed_pages
)
226 u64 isize
= i_size_read(inode
);
227 u64 actual_end
= min(end
+ 1, isize
);
228 u64 inline_len
= actual_end
- start
;
229 u64 aligned_end
= (end
+ root
->sectorsize
- 1) &
230 ~((u64
)root
->sectorsize
- 1);
232 u64 data_len
= inline_len
;
236 data_len
= compressed_size
;
239 actual_end
>= PAGE_CACHE_SIZE
||
240 data_len
>= BTRFS_MAX_INLINE_DATA_SIZE(root
) ||
242 (actual_end
& (root
->sectorsize
- 1)) == 0) ||
244 data_len
> root
->fs_info
->max_inline
) {
248 ret
= btrfs_drop_extents(trans
, inode
, start
, aligned_end
,
252 if (isize
> actual_end
)
253 inline_len
= min_t(u64
, isize
, actual_end
);
254 ret
= insert_inline_extent(trans
, root
, inode
, start
,
255 inline_len
, compressed_size
,
256 compress_type
, compressed_pages
);
258 btrfs_delalloc_release_metadata(inode
, end
+ 1 - start
);
259 btrfs_drop_extent_cache(inode
, start
, aligned_end
- 1, 0);
263 struct async_extent
{
268 unsigned long nr_pages
;
270 struct list_head list
;
275 struct btrfs_root
*root
;
276 struct page
*locked_page
;
279 struct list_head extents
;
280 struct btrfs_work work
;
283 static noinline
int add_async_extent(struct async_cow
*cow
,
284 u64 start
, u64 ram_size
,
287 unsigned long nr_pages
,
290 struct async_extent
*async_extent
;
292 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
293 BUG_ON(!async_extent
);
294 async_extent
->start
= start
;
295 async_extent
->ram_size
= ram_size
;
296 async_extent
->compressed_size
= compressed_size
;
297 async_extent
->pages
= pages
;
298 async_extent
->nr_pages
= nr_pages
;
299 async_extent
->compress_type
= compress_type
;
300 list_add_tail(&async_extent
->list
, &cow
->extents
);
305 * we create compressed extents in two phases. The first
306 * phase compresses a range of pages that have already been
307 * locked (both pages and state bits are locked).
309 * This is done inside an ordered work queue, and the compression
310 * is spread across many cpus. The actual IO submission is step
311 * two, and the ordered work queue takes care of making sure that
312 * happens in the same order things were put onto the queue by
313 * writepages and friends.
315 * If this code finds it can't get good compression, it puts an
316 * entry onto the work queue to write the uncompressed bytes. This
317 * makes sure that both compressed inodes and uncompressed inodes
318 * are written in the same order that pdflush sent them down.
320 static noinline
int compress_file_range(struct inode
*inode
,
321 struct page
*locked_page
,
323 struct async_cow
*async_cow
,
326 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
327 struct btrfs_trans_handle
*trans
;
329 u64 blocksize
= root
->sectorsize
;
331 u64 isize
= i_size_read(inode
);
333 struct page
**pages
= NULL
;
334 unsigned long nr_pages
;
335 unsigned long nr_pages_ret
= 0;
336 unsigned long total_compressed
= 0;
337 unsigned long total_in
= 0;
338 unsigned long max_compressed
= 128 * 1024;
339 unsigned long max_uncompressed
= 128 * 1024;
342 int compress_type
= root
->fs_info
->compress_type
;
344 /* if this is a small write inside eof, kick off a defragbot */
345 if (end
<= BTRFS_I(inode
)->disk_i_size
&& (end
- start
+ 1) < 16 * 1024)
346 btrfs_add_inode_defrag(NULL
, inode
);
348 actual_end
= min_t(u64
, isize
, end
+ 1);
351 nr_pages
= (end
>> PAGE_CACHE_SHIFT
) - (start
>> PAGE_CACHE_SHIFT
) + 1;
352 nr_pages
= min(nr_pages
, (128 * 1024UL) / PAGE_CACHE_SIZE
);
355 * we don't want to send crud past the end of i_size through
356 * compression, that's just a waste of CPU time. So, if the
357 * end of the file is before the start of our current
358 * requested range of bytes, we bail out to the uncompressed
359 * cleanup code that can deal with all of this.
361 * It isn't really the fastest way to fix things, but this is a
362 * very uncommon corner.
364 if (actual_end
<= start
)
365 goto cleanup_and_bail_uncompressed
;
367 total_compressed
= actual_end
- start
;
369 /* we want to make sure that amount of ram required to uncompress
370 * an extent is reasonable, so we limit the total size in ram
371 * of a compressed extent to 128k. This is a crucial number
372 * because it also controls how easily we can spread reads across
373 * cpus for decompression.
375 * We also want to make sure the amount of IO required to do
376 * a random read is reasonably small, so we limit the size of
377 * a compressed extent to 128k.
379 total_compressed
= min(total_compressed
, max_uncompressed
);
380 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
381 num_bytes
= max(blocksize
, num_bytes
);
386 * we do compression for mount -o compress and when the
387 * inode has not been flagged as nocompress. This flag can
388 * change at any time if we discover bad compression ratios.
390 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
) &&
391 (btrfs_test_opt(root
, COMPRESS
) ||
392 (BTRFS_I(inode
)->force_compress
) ||
393 (BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
))) {
395 pages
= kzalloc(sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
397 /* just bail out to the uncompressed code */
401 if (BTRFS_I(inode
)->force_compress
)
402 compress_type
= BTRFS_I(inode
)->force_compress
;
404 ret
= btrfs_compress_pages(compress_type
,
405 inode
->i_mapping
, start
,
406 total_compressed
, pages
,
407 nr_pages
, &nr_pages_ret
,
413 unsigned long offset
= total_compressed
&
414 (PAGE_CACHE_SIZE
- 1);
415 struct page
*page
= pages
[nr_pages_ret
- 1];
418 /* zero the tail end of the last page, we might be
419 * sending it down to disk
422 kaddr
= kmap_atomic(page
, KM_USER0
);
423 memset(kaddr
+ offset
, 0,
424 PAGE_CACHE_SIZE
- offset
);
425 kunmap_atomic(kaddr
, KM_USER0
);
432 trans
= btrfs_join_transaction(root
);
433 BUG_ON(IS_ERR(trans
));
434 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
436 /* lets try to make an inline extent */
437 if (ret
|| total_in
< (actual_end
- start
)) {
438 /* we didn't compress the entire range, try
439 * to make an uncompressed inline extent.
441 ret
= cow_file_range_inline(trans
, root
, inode
,
442 start
, end
, 0, 0, NULL
);
444 /* try making a compressed inline extent */
445 ret
= cow_file_range_inline(trans
, root
, inode
,
448 compress_type
, pages
);
452 * inline extent creation worked, we don't need
453 * to create any more async work items. Unlock
454 * and free up our temp pages.
456 extent_clear_unlock_delalloc(inode
,
457 &BTRFS_I(inode
)->io_tree
,
459 EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
460 EXTENT_CLEAR_DELALLOC
|
461 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
);
463 btrfs_end_transaction(trans
, root
);
466 btrfs_end_transaction(trans
, root
);
471 * we aren't doing an inline extent round the compressed size
472 * up to a block size boundary so the allocator does sane
475 total_compressed
= (total_compressed
+ blocksize
- 1) &
479 * one last check to make sure the compression is really a
480 * win, compare the page count read with the blocks on disk
482 total_in
= (total_in
+ PAGE_CACHE_SIZE
- 1) &
483 ~(PAGE_CACHE_SIZE
- 1);
484 if (total_compressed
>= total_in
) {
487 num_bytes
= total_in
;
490 if (!will_compress
&& pages
) {
492 * the compression code ran but failed to make things smaller,
493 * free any pages it allocated and our page pointer array
495 for (i
= 0; i
< nr_pages_ret
; i
++) {
496 WARN_ON(pages
[i
]->mapping
);
497 page_cache_release(pages
[i
]);
501 total_compressed
= 0;
504 /* flag the file so we don't compress in the future */
505 if (!btrfs_test_opt(root
, FORCE_COMPRESS
) &&
506 !(BTRFS_I(inode
)->force_compress
)) {
507 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
513 /* the async work queues will take care of doing actual
514 * allocation on disk for these compressed pages,
515 * and will submit them to the elevator.
517 add_async_extent(async_cow
, start
, num_bytes
,
518 total_compressed
, pages
, nr_pages_ret
,
521 if (start
+ num_bytes
< end
) {
528 cleanup_and_bail_uncompressed
:
530 * No compression, but we still need to write the pages in
531 * the file we've been given so far. redirty the locked
532 * page if it corresponds to our extent and set things up
533 * for the async work queue to run cow_file_range to do
534 * the normal delalloc dance
536 if (page_offset(locked_page
) >= start
&&
537 page_offset(locked_page
) <= end
) {
538 __set_page_dirty_nobuffers(locked_page
);
539 /* unlocked later on in the async handlers */
541 add_async_extent(async_cow
, start
, end
- start
+ 1,
542 0, NULL
, 0, BTRFS_COMPRESS_NONE
);
550 for (i
= 0; i
< nr_pages_ret
; i
++) {
551 WARN_ON(pages
[i
]->mapping
);
552 page_cache_release(pages
[i
]);
560 * phase two of compressed writeback. This is the ordered portion
561 * of the code, which only gets called in the order the work was
562 * queued. We walk all the async extents created by compress_file_range
563 * and send them down to the disk.
565 static noinline
int submit_compressed_extents(struct inode
*inode
,
566 struct async_cow
*async_cow
)
568 struct async_extent
*async_extent
;
570 struct btrfs_trans_handle
*trans
;
571 struct btrfs_key ins
;
572 struct extent_map
*em
;
573 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
574 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
575 struct extent_io_tree
*io_tree
;
578 if (list_empty(&async_cow
->extents
))
582 while (!list_empty(&async_cow
->extents
)) {
583 async_extent
= list_entry(async_cow
->extents
.next
,
584 struct async_extent
, list
);
585 list_del(&async_extent
->list
);
587 io_tree
= &BTRFS_I(inode
)->io_tree
;
590 /* did the compression code fall back to uncompressed IO? */
591 if (!async_extent
->pages
) {
592 int page_started
= 0;
593 unsigned long nr_written
= 0;
595 lock_extent(io_tree
, async_extent
->start
,
596 async_extent
->start
+
597 async_extent
->ram_size
- 1, GFP_NOFS
);
599 /* allocate blocks */
600 ret
= cow_file_range(inode
, async_cow
->locked_page
,
602 async_extent
->start
+
603 async_extent
->ram_size
- 1,
604 &page_started
, &nr_written
, 0);
607 * if page_started, cow_file_range inserted an
608 * inline extent and took care of all the unlocking
609 * and IO for us. Otherwise, we need to submit
610 * all those pages down to the drive.
612 if (!page_started
&& !ret
)
613 extent_write_locked_range(io_tree
,
614 inode
, async_extent
->start
,
615 async_extent
->start
+
616 async_extent
->ram_size
- 1,
624 lock_extent(io_tree
, async_extent
->start
,
625 async_extent
->start
+ async_extent
->ram_size
- 1,
628 trans
= btrfs_join_transaction(root
);
629 BUG_ON(IS_ERR(trans
));
630 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
631 ret
= btrfs_reserve_extent(trans
, root
,
632 async_extent
->compressed_size
,
633 async_extent
->compressed_size
,
636 btrfs_end_transaction(trans
, root
);
640 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
641 WARN_ON(async_extent
->pages
[i
]->mapping
);
642 page_cache_release(async_extent
->pages
[i
]);
644 kfree(async_extent
->pages
);
645 async_extent
->nr_pages
= 0;
646 async_extent
->pages
= NULL
;
647 unlock_extent(io_tree
, async_extent
->start
,
648 async_extent
->start
+
649 async_extent
->ram_size
- 1, GFP_NOFS
);
654 * here we're doing allocation and writeback of the
657 btrfs_drop_extent_cache(inode
, async_extent
->start
,
658 async_extent
->start
+
659 async_extent
->ram_size
- 1, 0);
661 em
= alloc_extent_map();
663 em
->start
= async_extent
->start
;
664 em
->len
= async_extent
->ram_size
;
665 em
->orig_start
= em
->start
;
667 em
->block_start
= ins
.objectid
;
668 em
->block_len
= ins
.offset
;
669 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
670 em
->compress_type
= async_extent
->compress_type
;
671 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
672 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
675 write_lock(&em_tree
->lock
);
676 ret
= add_extent_mapping(em_tree
, em
);
677 write_unlock(&em_tree
->lock
);
678 if (ret
!= -EEXIST
) {
682 btrfs_drop_extent_cache(inode
, async_extent
->start
,
683 async_extent
->start
+
684 async_extent
->ram_size
- 1, 0);
687 ret
= btrfs_add_ordered_extent_compress(inode
,
690 async_extent
->ram_size
,
692 BTRFS_ORDERED_COMPRESSED
,
693 async_extent
->compress_type
);
697 * clear dirty, set writeback and unlock the pages.
699 extent_clear_unlock_delalloc(inode
,
700 &BTRFS_I(inode
)->io_tree
,
702 async_extent
->start
+
703 async_extent
->ram_size
- 1,
704 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
705 EXTENT_CLEAR_UNLOCK
|
706 EXTENT_CLEAR_DELALLOC
|
707 EXTENT_CLEAR_DIRTY
| EXTENT_SET_WRITEBACK
);
709 ret
= btrfs_submit_compressed_write(inode
,
711 async_extent
->ram_size
,
713 ins
.offset
, async_extent
->pages
,
714 async_extent
->nr_pages
);
717 alloc_hint
= ins
.objectid
+ ins
.offset
;
725 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
728 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
729 struct extent_map
*em
;
732 read_lock(&em_tree
->lock
);
733 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
736 * if block start isn't an actual block number then find the
737 * first block in this inode and use that as a hint. If that
738 * block is also bogus then just don't worry about it.
740 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
742 em
= search_extent_mapping(em_tree
, 0, 0);
743 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
744 alloc_hint
= em
->block_start
;
748 alloc_hint
= em
->block_start
;
752 read_unlock(&em_tree
->lock
);
758 * when extent_io.c finds a delayed allocation range in the file,
759 * the call backs end up in this code. The basic idea is to
760 * allocate extents on disk for the range, and create ordered data structs
761 * in ram to track those extents.
763 * locked_page is the page that writepage had locked already. We use
764 * it to make sure we don't do extra locks or unlocks.
766 * *page_started is set to one if we unlock locked_page and do everything
767 * required to start IO on it. It may be clean and already done with
770 static noinline
int cow_file_range(struct inode
*inode
,
771 struct page
*locked_page
,
772 u64 start
, u64 end
, int *page_started
,
773 unsigned long *nr_written
,
776 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
777 struct btrfs_trans_handle
*trans
;
780 unsigned long ram_size
;
783 u64 blocksize
= root
->sectorsize
;
784 struct btrfs_key ins
;
785 struct extent_map
*em
;
786 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
789 BUG_ON(btrfs_is_free_space_inode(root
, inode
));
790 trans
= btrfs_join_transaction(root
);
791 BUG_ON(IS_ERR(trans
));
792 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
794 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
795 num_bytes
= max(blocksize
, num_bytes
);
796 disk_num_bytes
= num_bytes
;
799 /* if this is a small write inside eof, kick off defrag */
800 if (end
<= BTRFS_I(inode
)->disk_i_size
&& num_bytes
< 64 * 1024)
801 btrfs_add_inode_defrag(trans
, inode
);
804 /* lets try to make an inline extent */
805 ret
= cow_file_range_inline(trans
, root
, inode
,
806 start
, end
, 0, 0, NULL
);
808 extent_clear_unlock_delalloc(inode
,
809 &BTRFS_I(inode
)->io_tree
,
811 EXTENT_CLEAR_UNLOCK_PAGE
|
812 EXTENT_CLEAR_UNLOCK
|
813 EXTENT_CLEAR_DELALLOC
|
815 EXTENT_SET_WRITEBACK
|
816 EXTENT_END_WRITEBACK
);
818 *nr_written
= *nr_written
+
819 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
826 BUG_ON(disk_num_bytes
>
827 btrfs_super_total_bytes(&root
->fs_info
->super_copy
));
829 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
830 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
832 while (disk_num_bytes
> 0) {
835 cur_alloc_size
= disk_num_bytes
;
836 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
837 root
->sectorsize
, 0, alloc_hint
,
841 em
= alloc_extent_map();
844 em
->orig_start
= em
->start
;
845 ram_size
= ins
.offset
;
846 em
->len
= ins
.offset
;
848 em
->block_start
= ins
.objectid
;
849 em
->block_len
= ins
.offset
;
850 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
851 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
854 write_lock(&em_tree
->lock
);
855 ret
= add_extent_mapping(em_tree
, em
);
856 write_unlock(&em_tree
->lock
);
857 if (ret
!= -EEXIST
) {
861 btrfs_drop_extent_cache(inode
, start
,
862 start
+ ram_size
- 1, 0);
865 cur_alloc_size
= ins
.offset
;
866 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
867 ram_size
, cur_alloc_size
, 0);
870 if (root
->root_key
.objectid
==
871 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
872 ret
= btrfs_reloc_clone_csums(inode
, start
,
877 if (disk_num_bytes
< cur_alloc_size
)
880 /* we're not doing compressed IO, don't unlock the first
881 * page (which the caller expects to stay locked), don't
882 * clear any dirty bits and don't set any writeback bits
884 * Do set the Private2 bit so we know this page was properly
885 * setup for writepage
887 op
= unlock
? EXTENT_CLEAR_UNLOCK_PAGE
: 0;
888 op
|= EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
891 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
892 start
, start
+ ram_size
- 1,
894 disk_num_bytes
-= cur_alloc_size
;
895 num_bytes
-= cur_alloc_size
;
896 alloc_hint
= ins
.objectid
+ ins
.offset
;
897 start
+= cur_alloc_size
;
901 btrfs_end_transaction(trans
, root
);
907 * work queue call back to started compression on a file and pages
909 static noinline
void async_cow_start(struct btrfs_work
*work
)
911 struct async_cow
*async_cow
;
913 async_cow
= container_of(work
, struct async_cow
, work
);
915 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
916 async_cow
->start
, async_cow
->end
, async_cow
,
919 async_cow
->inode
= NULL
;
923 * work queue call back to submit previously compressed pages
925 static noinline
void async_cow_submit(struct btrfs_work
*work
)
927 struct async_cow
*async_cow
;
928 struct btrfs_root
*root
;
929 unsigned long nr_pages
;
931 async_cow
= container_of(work
, struct async_cow
, work
);
933 root
= async_cow
->root
;
934 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
937 atomic_sub(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
939 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
941 waitqueue_active(&root
->fs_info
->async_submit_wait
))
942 wake_up(&root
->fs_info
->async_submit_wait
);
944 if (async_cow
->inode
)
945 submit_compressed_extents(async_cow
->inode
, async_cow
);
948 static noinline
void async_cow_free(struct btrfs_work
*work
)
950 struct async_cow
*async_cow
;
951 async_cow
= container_of(work
, struct async_cow
, work
);
955 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
956 u64 start
, u64 end
, int *page_started
,
957 unsigned long *nr_written
)
959 struct async_cow
*async_cow
;
960 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
961 unsigned long nr_pages
;
963 int limit
= 10 * 1024 * 1042;
965 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
966 1, 0, NULL
, GFP_NOFS
);
967 while (start
< end
) {
968 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
970 async_cow
->inode
= inode
;
971 async_cow
->root
= root
;
972 async_cow
->locked_page
= locked_page
;
973 async_cow
->start
= start
;
975 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
978 cur_end
= min(end
, start
+ 512 * 1024 - 1);
980 async_cow
->end
= cur_end
;
981 INIT_LIST_HEAD(&async_cow
->extents
);
983 async_cow
->work
.func
= async_cow_start
;
984 async_cow
->work
.ordered_func
= async_cow_submit
;
985 async_cow
->work
.ordered_free
= async_cow_free
;
986 async_cow
->work
.flags
= 0;
988 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
990 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
992 btrfs_queue_worker(&root
->fs_info
->delalloc_workers
,
995 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
996 wait_event(root
->fs_info
->async_submit_wait
,
997 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
1001 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
1002 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1003 wait_event(root
->fs_info
->async_submit_wait
,
1004 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
1008 *nr_written
+= nr_pages
;
1009 start
= cur_end
+ 1;
1015 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
1016 u64 bytenr
, u64 num_bytes
)
1019 struct btrfs_ordered_sum
*sums
;
1022 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
1023 bytenr
+ num_bytes
- 1, &list
, 0);
1024 if (ret
== 0 && list_empty(&list
))
1027 while (!list_empty(&list
)) {
1028 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1029 list_del(&sums
->list
);
1036 * when nowcow writeback call back. This checks for snapshots or COW copies
1037 * of the extents that exist in the file, and COWs the file as required.
1039 * If no cow copies or snapshots exist, we write directly to the existing
1042 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1043 struct page
*locked_page
,
1044 u64 start
, u64 end
, int *page_started
, int force
,
1045 unsigned long *nr_written
)
1047 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1048 struct btrfs_trans_handle
*trans
;
1049 struct extent_buffer
*leaf
;
1050 struct btrfs_path
*path
;
1051 struct btrfs_file_extent_item
*fi
;
1052 struct btrfs_key found_key
;
1065 u64 ino
= btrfs_ino(inode
);
1067 path
= btrfs_alloc_path();
1071 nolock
= btrfs_is_free_space_inode(root
, inode
);
1074 trans
= btrfs_join_transaction_nolock(root
);
1076 trans
= btrfs_join_transaction(root
);
1078 BUG_ON(IS_ERR(trans
));
1079 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1081 cow_start
= (u64
)-1;
1084 ret
= btrfs_lookup_file_extent(trans
, root
, path
, ino
,
1087 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1088 leaf
= path
->nodes
[0];
1089 btrfs_item_key_to_cpu(leaf
, &found_key
,
1090 path
->slots
[0] - 1);
1091 if (found_key
.objectid
== ino
&&
1092 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1097 leaf
= path
->nodes
[0];
1098 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1099 ret
= btrfs_next_leaf(root
, path
);
1104 leaf
= path
->nodes
[0];
1110 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1112 if (found_key
.objectid
> ino
||
1113 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1114 found_key
.offset
> end
)
1117 if (found_key
.offset
> cur_offset
) {
1118 extent_end
= found_key
.offset
;
1123 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1124 struct btrfs_file_extent_item
);
1125 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1127 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1128 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1129 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1130 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1131 extent_end
= found_key
.offset
+
1132 btrfs_file_extent_num_bytes(leaf
, fi
);
1133 if (extent_end
<= start
) {
1137 if (disk_bytenr
== 0)
1139 if (btrfs_file_extent_compression(leaf
, fi
) ||
1140 btrfs_file_extent_encryption(leaf
, fi
) ||
1141 btrfs_file_extent_other_encoding(leaf
, fi
))
1143 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1145 if (btrfs_extent_readonly(root
, disk_bytenr
))
1147 if (btrfs_cross_ref_exist(trans
, root
, ino
,
1149 extent_offset
, disk_bytenr
))
1151 disk_bytenr
+= extent_offset
;
1152 disk_bytenr
+= cur_offset
- found_key
.offset
;
1153 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1155 * force cow if csum exists in the range.
1156 * this ensure that csum for a given extent are
1157 * either valid or do not exist.
1159 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1162 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1163 extent_end
= found_key
.offset
+
1164 btrfs_file_extent_inline_len(leaf
, fi
);
1165 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1170 if (extent_end
<= start
) {
1175 if (cow_start
== (u64
)-1)
1176 cow_start
= cur_offset
;
1177 cur_offset
= extent_end
;
1178 if (cur_offset
> end
)
1184 btrfs_release_path(path
);
1185 if (cow_start
!= (u64
)-1) {
1186 ret
= cow_file_range(inode
, locked_page
, cow_start
,
1187 found_key
.offset
- 1, page_started
,
1190 cow_start
= (u64
)-1;
1193 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1194 struct extent_map
*em
;
1195 struct extent_map_tree
*em_tree
;
1196 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1197 em
= alloc_extent_map();
1199 em
->start
= cur_offset
;
1200 em
->orig_start
= em
->start
;
1201 em
->len
= num_bytes
;
1202 em
->block_len
= num_bytes
;
1203 em
->block_start
= disk_bytenr
;
1204 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1205 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1207 write_lock(&em_tree
->lock
);
1208 ret
= add_extent_mapping(em_tree
, em
);
1209 write_unlock(&em_tree
->lock
);
1210 if (ret
!= -EEXIST
) {
1211 free_extent_map(em
);
1214 btrfs_drop_extent_cache(inode
, em
->start
,
1215 em
->start
+ em
->len
- 1, 0);
1217 type
= BTRFS_ORDERED_PREALLOC
;
1219 type
= BTRFS_ORDERED_NOCOW
;
1222 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1223 num_bytes
, num_bytes
, type
);
1226 if (root
->root_key
.objectid
==
1227 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1228 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1233 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
1234 cur_offset
, cur_offset
+ num_bytes
- 1,
1235 locked_page
, EXTENT_CLEAR_UNLOCK_PAGE
|
1236 EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
1237 EXTENT_SET_PRIVATE2
);
1238 cur_offset
= extent_end
;
1239 if (cur_offset
> end
)
1242 btrfs_release_path(path
);
1244 if (cur_offset
<= end
&& cow_start
== (u64
)-1)
1245 cow_start
= cur_offset
;
1246 if (cow_start
!= (u64
)-1) {
1247 ret
= cow_file_range(inode
, locked_page
, cow_start
, end
,
1248 page_started
, nr_written
, 1);
1253 ret
= btrfs_end_transaction_nolock(trans
, root
);
1256 ret
= btrfs_end_transaction(trans
, root
);
1259 btrfs_free_path(path
);
1264 * extent_io.c call back to do delayed allocation processing
1266 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1267 u64 start
, u64 end
, int *page_started
,
1268 unsigned long *nr_written
)
1271 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1273 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
)
1274 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1275 page_started
, 1, nr_written
);
1276 else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
)
1277 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1278 page_started
, 0, nr_written
);
1279 else if (!btrfs_test_opt(root
, COMPRESS
) &&
1280 !(BTRFS_I(inode
)->force_compress
) &&
1281 !(BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
))
1282 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1283 page_started
, nr_written
, 1);
1285 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1286 page_started
, nr_written
);
1290 static void btrfs_split_extent_hook(struct inode
*inode
,
1291 struct extent_state
*orig
, u64 split
)
1293 /* not delalloc, ignore it */
1294 if (!(orig
->state
& EXTENT_DELALLOC
))
1297 spin_lock(&BTRFS_I(inode
)->lock
);
1298 BTRFS_I(inode
)->outstanding_extents
++;
1299 spin_unlock(&BTRFS_I(inode
)->lock
);
1303 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1304 * extents so we can keep track of new extents that are just merged onto old
1305 * extents, such as when we are doing sequential writes, so we can properly
1306 * account for the metadata space we'll need.
1308 static void btrfs_merge_extent_hook(struct inode
*inode
,
1309 struct extent_state
*new,
1310 struct extent_state
*other
)
1312 /* not delalloc, ignore it */
1313 if (!(other
->state
& EXTENT_DELALLOC
))
1316 spin_lock(&BTRFS_I(inode
)->lock
);
1317 BTRFS_I(inode
)->outstanding_extents
--;
1318 spin_unlock(&BTRFS_I(inode
)->lock
);
1322 * extent_io.c set_bit_hook, used to track delayed allocation
1323 * bytes in this file, and to maintain the list of inodes that
1324 * have pending delalloc work to be done.
1326 static void btrfs_set_bit_hook(struct inode
*inode
,
1327 struct extent_state
*state
, int *bits
)
1331 * set_bit and clear bit hooks normally require _irqsave/restore
1332 * but in this case, we are only testing for the DELALLOC
1333 * bit, which is only set or cleared with irqs on
1335 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1336 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1337 u64 len
= state
->end
+ 1 - state
->start
;
1338 bool do_list
= !btrfs_is_free_space_inode(root
, inode
);
1340 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1341 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1343 spin_lock(&BTRFS_I(inode
)->lock
);
1344 BTRFS_I(inode
)->outstanding_extents
++;
1345 spin_unlock(&BTRFS_I(inode
)->lock
);
1348 spin_lock(&root
->fs_info
->delalloc_lock
);
1349 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1350 root
->fs_info
->delalloc_bytes
+= len
;
1351 if (do_list
&& list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1352 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1353 &root
->fs_info
->delalloc_inodes
);
1355 spin_unlock(&root
->fs_info
->delalloc_lock
);
1360 * extent_io.c clear_bit_hook, see set_bit_hook for why
1362 static void btrfs_clear_bit_hook(struct inode
*inode
,
1363 struct extent_state
*state
, int *bits
)
1366 * set_bit and clear bit hooks normally require _irqsave/restore
1367 * but in this case, we are only testing for the DELALLOC
1368 * bit, which is only set or cleared with irqs on
1370 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1371 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1372 u64 len
= state
->end
+ 1 - state
->start
;
1373 bool do_list
= !btrfs_is_free_space_inode(root
, inode
);
1375 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1376 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1377 } else if (!(*bits
& EXTENT_DO_ACCOUNTING
)) {
1378 spin_lock(&BTRFS_I(inode
)->lock
);
1379 BTRFS_I(inode
)->outstanding_extents
--;
1380 spin_unlock(&BTRFS_I(inode
)->lock
);
1383 if (*bits
& EXTENT_DO_ACCOUNTING
)
1384 btrfs_delalloc_release_metadata(inode
, len
);
1386 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1388 btrfs_free_reserved_data_space(inode
, len
);
1390 spin_lock(&root
->fs_info
->delalloc_lock
);
1391 root
->fs_info
->delalloc_bytes
-= len
;
1392 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1394 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1395 !list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1396 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1398 spin_unlock(&root
->fs_info
->delalloc_lock
);
1403 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1404 * we don't create bios that span stripes or chunks
1406 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
1407 size_t size
, struct bio
*bio
,
1408 unsigned long bio_flags
)
1410 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1411 struct btrfs_mapping_tree
*map_tree
;
1412 u64 logical
= (u64
)bio
->bi_sector
<< 9;
1417 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1420 length
= bio
->bi_size
;
1421 map_tree
= &root
->fs_info
->mapping_tree
;
1422 map_length
= length
;
1423 ret
= btrfs_map_block(map_tree
, READ
, logical
,
1424 &map_length
, NULL
, 0);
1426 if (map_length
< length
+ size
)
1432 * in order to insert checksums into the metadata in large chunks,
1433 * we wait until bio submission time. All the pages in the bio are
1434 * checksummed and sums are attached onto the ordered extent record.
1436 * At IO completion time the cums attached on the ordered extent record
1437 * are inserted into the btree
1439 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1440 struct bio
*bio
, int mirror_num
,
1441 unsigned long bio_flags
,
1444 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1447 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1453 * in order to insert checksums into the metadata in large chunks,
1454 * we wait until bio submission time. All the pages in the bio are
1455 * checksummed and sums are attached onto the ordered extent record.
1457 * At IO completion time the cums attached on the ordered extent record
1458 * are inserted into the btree
1460 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1461 int mirror_num
, unsigned long bio_flags
,
1464 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1465 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1469 * extent_io.c submission hook. This does the right thing for csum calculation
1470 * on write, or reading the csums from the tree before a read
1472 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1473 int mirror_num
, unsigned long bio_flags
,
1476 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1480 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1482 if (btrfs_is_free_space_inode(root
, inode
))
1483 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 2);
1485 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
1488 if (!(rw
& REQ_WRITE
)) {
1489 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1490 return btrfs_submit_compressed_read(inode
, bio
,
1491 mirror_num
, bio_flags
);
1492 } else if (!skip_sum
) {
1493 ret
= btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1498 } else if (!skip_sum
) {
1499 /* csum items have already been cloned */
1500 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1502 /* we're doing a write, do the async checksumming */
1503 return btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1504 inode
, rw
, bio
, mirror_num
,
1505 bio_flags
, bio_offset
,
1506 __btrfs_submit_bio_start
,
1507 __btrfs_submit_bio_done
);
1511 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1515 * given a list of ordered sums record them in the inode. This happens
1516 * at IO completion time based on sums calculated at bio submission time.
1518 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1519 struct inode
*inode
, u64 file_offset
,
1520 struct list_head
*list
)
1522 struct btrfs_ordered_sum
*sum
;
1524 list_for_each_entry(sum
, list
, list
) {
1525 btrfs_csum_file_blocks(trans
,
1526 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1531 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1532 struct extent_state
**cached_state
)
1534 if ((end
& (PAGE_CACHE_SIZE
- 1)) == 0)
1536 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1537 cached_state
, GFP_NOFS
);
1540 /* see btrfs_writepage_start_hook for details on why this is required */
1541 struct btrfs_writepage_fixup
{
1543 struct btrfs_work work
;
1546 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1548 struct btrfs_writepage_fixup
*fixup
;
1549 struct btrfs_ordered_extent
*ordered
;
1550 struct extent_state
*cached_state
= NULL
;
1552 struct inode
*inode
;
1556 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1560 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1561 ClearPageChecked(page
);
1565 inode
= page
->mapping
->host
;
1566 page_start
= page_offset(page
);
1567 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1569 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1570 &cached_state
, GFP_NOFS
);
1572 /* already ordered? We're done */
1573 if (PagePrivate2(page
))
1576 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1578 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1579 page_end
, &cached_state
, GFP_NOFS
);
1581 btrfs_start_ordered_extent(inode
, ordered
, 1);
1586 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1587 ClearPageChecked(page
);
1589 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1590 &cached_state
, GFP_NOFS
);
1593 page_cache_release(page
);
1598 * There are a few paths in the higher layers of the kernel that directly
1599 * set the page dirty bit without asking the filesystem if it is a
1600 * good idea. This causes problems because we want to make sure COW
1601 * properly happens and the data=ordered rules are followed.
1603 * In our case any range that doesn't have the ORDERED bit set
1604 * hasn't been properly setup for IO. We kick off an async process
1605 * to fix it up. The async helper will wait for ordered extents, set
1606 * the delalloc bit and make it safe to write the page.
1608 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1610 struct inode
*inode
= page
->mapping
->host
;
1611 struct btrfs_writepage_fixup
*fixup
;
1612 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1614 /* this page is properly in the ordered list */
1615 if (TestClearPagePrivate2(page
))
1618 if (PageChecked(page
))
1621 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1625 SetPageChecked(page
);
1626 page_cache_get(page
);
1627 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
1629 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
1633 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1634 struct inode
*inode
, u64 file_pos
,
1635 u64 disk_bytenr
, u64 disk_num_bytes
,
1636 u64 num_bytes
, u64 ram_bytes
,
1637 u8 compression
, u8 encryption
,
1638 u16 other_encoding
, int extent_type
)
1640 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1641 struct btrfs_file_extent_item
*fi
;
1642 struct btrfs_path
*path
;
1643 struct extent_buffer
*leaf
;
1644 struct btrfs_key ins
;
1648 path
= btrfs_alloc_path();
1652 path
->leave_spinning
= 1;
1655 * we may be replacing one extent in the tree with another.
1656 * The new extent is pinned in the extent map, and we don't want
1657 * to drop it from the cache until it is completely in the btree.
1659 * So, tell btrfs_drop_extents to leave this extent in the cache.
1660 * the caller is expected to unpin it and allow it to be merged
1663 ret
= btrfs_drop_extents(trans
, inode
, file_pos
, file_pos
+ num_bytes
,
1667 ins
.objectid
= btrfs_ino(inode
);
1668 ins
.offset
= file_pos
;
1669 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1670 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
, sizeof(*fi
));
1672 leaf
= path
->nodes
[0];
1673 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1674 struct btrfs_file_extent_item
);
1675 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1676 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1677 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1678 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1679 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1680 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1681 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1682 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1683 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1684 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1686 btrfs_unlock_up_safe(path
, 1);
1687 btrfs_set_lock_blocking(leaf
);
1689 btrfs_mark_buffer_dirty(leaf
);
1691 inode_add_bytes(inode
, num_bytes
);
1693 ins
.objectid
= disk_bytenr
;
1694 ins
.offset
= disk_num_bytes
;
1695 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1696 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
1697 root
->root_key
.objectid
,
1698 btrfs_ino(inode
), file_pos
, &ins
);
1700 btrfs_free_path(path
);
1706 * helper function for btrfs_finish_ordered_io, this
1707 * just reads in some of the csum leaves to prime them into ram
1708 * before we start the transaction. It limits the amount of btree
1709 * reads required while inside the transaction.
1711 /* as ordered data IO finishes, this gets called so we can finish
1712 * an ordered extent if the range of bytes in the file it covers are
1715 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
)
1717 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1718 struct btrfs_trans_handle
*trans
= NULL
;
1719 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
1720 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1721 struct extent_state
*cached_state
= NULL
;
1722 int compress_type
= 0;
1726 ret
= btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
1730 BUG_ON(!ordered_extent
);
1732 nolock
= btrfs_is_free_space_inode(root
, inode
);
1734 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
1735 BUG_ON(!list_empty(&ordered_extent
->list
));
1736 ret
= btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
1739 trans
= btrfs_join_transaction_nolock(root
);
1741 trans
= btrfs_join_transaction(root
);
1742 BUG_ON(IS_ERR(trans
));
1743 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1744 ret
= btrfs_update_inode(trans
, root
, inode
);
1750 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
1751 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
1752 0, &cached_state
, GFP_NOFS
);
1755 trans
= btrfs_join_transaction_nolock(root
);
1757 trans
= btrfs_join_transaction(root
);
1758 BUG_ON(IS_ERR(trans
));
1759 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1761 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
1762 compress_type
= ordered_extent
->compress_type
;
1763 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
1764 BUG_ON(compress_type
);
1765 ret
= btrfs_mark_extent_written(trans
, inode
,
1766 ordered_extent
->file_offset
,
1767 ordered_extent
->file_offset
+
1768 ordered_extent
->len
);
1771 BUG_ON(root
== root
->fs_info
->tree_root
);
1772 ret
= insert_reserved_file_extent(trans
, inode
,
1773 ordered_extent
->file_offset
,
1774 ordered_extent
->start
,
1775 ordered_extent
->disk_len
,
1776 ordered_extent
->len
,
1777 ordered_extent
->len
,
1778 compress_type
, 0, 0,
1779 BTRFS_FILE_EXTENT_REG
);
1780 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
1781 ordered_extent
->file_offset
,
1782 ordered_extent
->len
);
1785 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
1786 ordered_extent
->file_offset
+
1787 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
1789 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
1790 &ordered_extent
->list
);
1792 ret
= btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
1793 if (!ret
|| !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
1794 ret
= btrfs_update_inode(trans
, root
, inode
);
1799 if (root
!= root
->fs_info
->tree_root
)
1800 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
1803 btrfs_end_transaction_nolock(trans
, root
);
1805 btrfs_end_transaction(trans
, root
);
1809 btrfs_put_ordered_extent(ordered_extent
);
1810 /* once for the tree */
1811 btrfs_put_ordered_extent(ordered_extent
);
1816 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
1817 struct extent_state
*state
, int uptodate
)
1819 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
1821 ClearPagePrivate2(page
);
1822 return btrfs_finish_ordered_io(page
->mapping
->host
, start
, end
);
1826 * When IO fails, either with EIO or csum verification fails, we
1827 * try other mirrors that might have a good copy of the data. This
1828 * io_failure_record is used to record state as we go through all the
1829 * mirrors. If another mirror has good data, the page is set up to date
1830 * and things continue. If a good mirror can't be found, the original
1831 * bio end_io callback is called to indicate things have failed.
1833 struct io_failure_record
{
1838 unsigned long bio_flags
;
1842 static int btrfs_io_failed_hook(struct bio
*failed_bio
,
1843 struct page
*page
, u64 start
, u64 end
,
1844 struct extent_state
*state
)
1846 struct io_failure_record
*failrec
= NULL
;
1848 struct extent_map
*em
;
1849 struct inode
*inode
= page
->mapping
->host
;
1850 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
1851 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1858 ret
= get_state_private(failure_tree
, start
, &private);
1860 failrec
= kmalloc(sizeof(*failrec
), GFP_NOFS
);
1863 failrec
->start
= start
;
1864 failrec
->len
= end
- start
+ 1;
1865 failrec
->last_mirror
= 0;
1866 failrec
->bio_flags
= 0;
1868 read_lock(&em_tree
->lock
);
1869 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
1870 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
1871 free_extent_map(em
);
1874 read_unlock(&em_tree
->lock
);
1876 if (IS_ERR_OR_NULL(em
)) {
1880 logical
= start
- em
->start
;
1881 logical
= em
->block_start
+ logical
;
1882 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
1883 logical
= em
->block_start
;
1884 failrec
->bio_flags
= EXTENT_BIO_COMPRESSED
;
1885 extent_set_compress_type(&failrec
->bio_flags
,
1888 failrec
->logical
= logical
;
1889 free_extent_map(em
);
1890 set_extent_bits(failure_tree
, start
, end
, EXTENT_LOCKED
|
1891 EXTENT_DIRTY
, GFP_NOFS
);
1892 set_state_private(failure_tree
, start
,
1893 (u64
)(unsigned long)failrec
);
1895 failrec
= (struct io_failure_record
*)(unsigned long)private;
1897 num_copies
= btrfs_num_copies(
1898 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
1899 failrec
->logical
, failrec
->len
);
1900 failrec
->last_mirror
++;
1902 spin_lock(&BTRFS_I(inode
)->io_tree
.lock
);
1903 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
1906 if (state
&& state
->start
!= failrec
->start
)
1908 spin_unlock(&BTRFS_I(inode
)->io_tree
.lock
);
1910 if (!state
|| failrec
->last_mirror
> num_copies
) {
1911 set_state_private(failure_tree
, failrec
->start
, 0);
1912 clear_extent_bits(failure_tree
, failrec
->start
,
1913 failrec
->start
+ failrec
->len
- 1,
1914 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
1918 bio
= bio_alloc(GFP_NOFS
, 1);
1919 bio
->bi_private
= state
;
1920 bio
->bi_end_io
= failed_bio
->bi_end_io
;
1921 bio
->bi_sector
= failrec
->logical
>> 9;
1922 bio
->bi_bdev
= failed_bio
->bi_bdev
;
1925 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
1926 if (failed_bio
->bi_rw
& REQ_WRITE
)
1931 ret
= BTRFS_I(inode
)->io_tree
.ops
->submit_bio_hook(inode
, rw
, bio
,
1932 failrec
->last_mirror
,
1933 failrec
->bio_flags
, 0);
1938 * each time an IO finishes, we do a fast check in the IO failure tree
1939 * to see if we need to process or clean up an io_failure_record
1941 static int btrfs_clean_io_failures(struct inode
*inode
, u64 start
)
1944 u64 private_failure
;
1945 struct io_failure_record
*failure
;
1949 if (count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
1950 (u64
)-1, 1, EXTENT_DIRTY
, 0)) {
1951 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
,
1952 start
, &private_failure
);
1954 failure
= (struct io_failure_record
*)(unsigned long)
1956 set_state_private(&BTRFS_I(inode
)->io_failure_tree
,
1958 clear_extent_bits(&BTRFS_I(inode
)->io_failure_tree
,
1960 failure
->start
+ failure
->len
- 1,
1961 EXTENT_DIRTY
| EXTENT_LOCKED
,
1970 * when reads are done, we need to check csums to verify the data is correct
1971 * if there's a match, we allow the bio to finish. If not, we go through
1972 * the io_failure_record routines to find good copies
1974 static int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
1975 struct extent_state
*state
)
1977 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
1978 struct inode
*inode
= page
->mapping
->host
;
1979 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1981 u64
private = ~(u32
)0;
1983 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1986 if (PageChecked(page
)) {
1987 ClearPageChecked(page
);
1991 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
1994 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
1995 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
1996 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
2001 if (state
&& state
->start
== start
) {
2002 private = state
->private;
2005 ret
= get_state_private(io_tree
, start
, &private);
2007 kaddr
= kmap_atomic(page
, KM_USER0
);
2011 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
2012 btrfs_csum_final(csum
, (char *)&csum
);
2013 if (csum
!= private)
2016 kunmap_atomic(kaddr
, KM_USER0
);
2018 /* if the io failure tree for this inode is non-empty,
2019 * check to see if we've recovered from a failed IO
2021 btrfs_clean_io_failures(inode
, start
);
2025 printk_ratelimited(KERN_INFO
"btrfs csum failed ino %llu off %llu csum %u "
2027 (unsigned long long)btrfs_ino(page
->mapping
->host
),
2028 (unsigned long long)start
, csum
,
2029 (unsigned long long)private);
2030 memset(kaddr
+ offset
, 1, end
- start
+ 1);
2031 flush_dcache_page(page
);
2032 kunmap_atomic(kaddr
, KM_USER0
);
2038 struct delayed_iput
{
2039 struct list_head list
;
2040 struct inode
*inode
;
2043 void btrfs_add_delayed_iput(struct inode
*inode
)
2045 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2046 struct delayed_iput
*delayed
;
2048 if (atomic_add_unless(&inode
->i_count
, -1, 1))
2051 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
2052 delayed
->inode
= inode
;
2054 spin_lock(&fs_info
->delayed_iput_lock
);
2055 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
2056 spin_unlock(&fs_info
->delayed_iput_lock
);
2059 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
2062 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2063 struct delayed_iput
*delayed
;
2066 spin_lock(&fs_info
->delayed_iput_lock
);
2067 empty
= list_empty(&fs_info
->delayed_iputs
);
2068 spin_unlock(&fs_info
->delayed_iput_lock
);
2072 down_read(&root
->fs_info
->cleanup_work_sem
);
2073 spin_lock(&fs_info
->delayed_iput_lock
);
2074 list_splice_init(&fs_info
->delayed_iputs
, &list
);
2075 spin_unlock(&fs_info
->delayed_iput_lock
);
2077 while (!list_empty(&list
)) {
2078 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
2079 list_del(&delayed
->list
);
2080 iput(delayed
->inode
);
2083 up_read(&root
->fs_info
->cleanup_work_sem
);
2086 enum btrfs_orphan_cleanup_state
{
2087 ORPHAN_CLEANUP_STARTED
= 1,
2088 ORPHAN_CLEANUP_DONE
= 2,
2092 * This is called in transaction commmit time. If there are no orphan
2093 * files in the subvolume, it removes orphan item and frees block_rsv
2096 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
2097 struct btrfs_root
*root
)
2101 if (!list_empty(&root
->orphan_list
) ||
2102 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
2105 if (root
->orphan_item_inserted
&&
2106 btrfs_root_refs(&root
->root_item
) > 0) {
2107 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
2108 root
->root_key
.objectid
);
2110 root
->orphan_item_inserted
= 0;
2113 if (root
->orphan_block_rsv
) {
2114 WARN_ON(root
->orphan_block_rsv
->size
> 0);
2115 btrfs_free_block_rsv(root
, root
->orphan_block_rsv
);
2116 root
->orphan_block_rsv
= NULL
;
2121 * This creates an orphan entry for the given inode in case something goes
2122 * wrong in the middle of an unlink/truncate.
2124 * NOTE: caller of this function should reserve 5 units of metadata for
2127 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2129 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2130 struct btrfs_block_rsv
*block_rsv
= NULL
;
2135 if (!root
->orphan_block_rsv
) {
2136 block_rsv
= btrfs_alloc_block_rsv(root
);
2141 spin_lock(&root
->orphan_lock
);
2142 if (!root
->orphan_block_rsv
) {
2143 root
->orphan_block_rsv
= block_rsv
;
2144 } else if (block_rsv
) {
2145 btrfs_free_block_rsv(root
, block_rsv
);
2149 if (list_empty(&BTRFS_I(inode
)->i_orphan
)) {
2150 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
2153 * For proper ENOSPC handling, we should do orphan
2154 * cleanup when mounting. But this introduces backward
2155 * compatibility issue.
2157 if (!xchg(&root
->orphan_item_inserted
, 1))
2165 if (!BTRFS_I(inode
)->orphan_meta_reserved
) {
2166 BTRFS_I(inode
)->orphan_meta_reserved
= 1;
2169 spin_unlock(&root
->orphan_lock
);
2171 /* grab metadata reservation from transaction handle */
2173 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
2177 /* insert an orphan item to track this unlinked/truncated file */
2179 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
2183 /* insert an orphan item to track subvolume contains orphan files */
2185 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
2186 root
->root_key
.objectid
);
2193 * We have done the truncate/delete so we can go ahead and remove the orphan
2194 * item for this particular inode.
2196 int btrfs_orphan_del(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2198 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2199 int delete_item
= 0;
2200 int release_rsv
= 0;
2203 spin_lock(&root
->orphan_lock
);
2204 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
2205 list_del_init(&BTRFS_I(inode
)->i_orphan
);
2209 if (BTRFS_I(inode
)->orphan_meta_reserved
) {
2210 BTRFS_I(inode
)->orphan_meta_reserved
= 0;
2213 spin_unlock(&root
->orphan_lock
);
2215 if (trans
&& delete_item
) {
2216 ret
= btrfs_del_orphan_item(trans
, root
, btrfs_ino(inode
));
2221 btrfs_orphan_release_metadata(inode
);
2227 * this cleans up any orphans that may be left on the list from the last use
2230 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
2232 struct btrfs_path
*path
;
2233 struct extent_buffer
*leaf
;
2234 struct btrfs_key key
, found_key
;
2235 struct btrfs_trans_handle
*trans
;
2236 struct inode
*inode
;
2237 u64 last_objectid
= 0;
2238 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
2240 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
2243 path
= btrfs_alloc_path();
2250 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
2251 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
2252 key
.offset
= (u64
)-1;
2255 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2260 * if ret == 0 means we found what we were searching for, which
2261 * is weird, but possible, so only screw with path if we didn't
2262 * find the key and see if we have stuff that matches
2266 if (path
->slots
[0] == 0)
2271 /* pull out the item */
2272 leaf
= path
->nodes
[0];
2273 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2275 /* make sure the item matches what we want */
2276 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
2278 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
2281 /* release the path since we're done with it */
2282 btrfs_release_path(path
);
2285 * this is where we are basically btrfs_lookup, without the
2286 * crossing root thing. we store the inode number in the
2287 * offset of the orphan item.
2290 if (found_key
.offset
== last_objectid
) {
2291 printk(KERN_ERR
"btrfs: Error removing orphan entry, "
2292 "stopping orphan cleanup\n");
2297 last_objectid
= found_key
.offset
;
2299 found_key
.objectid
= found_key
.offset
;
2300 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
2301 found_key
.offset
= 0;
2302 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
2303 ret
= PTR_RET(inode
);
2304 if (ret
&& ret
!= -ESTALE
)
2308 * Inode is already gone but the orphan item is still there,
2309 * kill the orphan item.
2311 if (ret
== -ESTALE
) {
2312 trans
= btrfs_start_transaction(root
, 1);
2313 if (IS_ERR(trans
)) {
2314 ret
= PTR_ERR(trans
);
2317 ret
= btrfs_del_orphan_item(trans
, root
,
2318 found_key
.objectid
);
2320 btrfs_end_transaction(trans
, root
);
2325 * add this inode to the orphan list so btrfs_orphan_del does
2326 * the proper thing when we hit it
2328 spin_lock(&root
->orphan_lock
);
2329 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
2330 spin_unlock(&root
->orphan_lock
);
2332 /* if we have links, this was a truncate, lets do that */
2333 if (inode
->i_nlink
) {
2334 if (!S_ISREG(inode
->i_mode
)) {
2340 ret
= btrfs_truncate(inode
);
2345 /* this will do delete_inode and everything for us */
2350 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
2352 if (root
->orphan_block_rsv
)
2353 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
2356 if (root
->orphan_block_rsv
|| root
->orphan_item_inserted
) {
2357 trans
= btrfs_join_transaction(root
);
2359 btrfs_end_transaction(trans
, root
);
2363 printk(KERN_INFO
"btrfs: unlinked %d orphans\n", nr_unlink
);
2365 printk(KERN_INFO
"btrfs: truncated %d orphans\n", nr_truncate
);
2369 printk(KERN_CRIT
"btrfs: could not do orphan cleanup %d\n", ret
);
2370 btrfs_free_path(path
);
2375 * very simple check to peek ahead in the leaf looking for xattrs. If we
2376 * don't find any xattrs, we know there can't be any acls.
2378 * slot is the slot the inode is in, objectid is the objectid of the inode
2380 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
2381 int slot
, u64 objectid
)
2383 u32 nritems
= btrfs_header_nritems(leaf
);
2384 struct btrfs_key found_key
;
2388 while (slot
< nritems
) {
2389 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2391 /* we found a different objectid, there must not be acls */
2392 if (found_key
.objectid
!= objectid
)
2395 /* we found an xattr, assume we've got an acl */
2396 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
)
2400 * we found a key greater than an xattr key, there can't
2401 * be any acls later on
2403 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
2410 * it goes inode, inode backrefs, xattrs, extents,
2411 * so if there are a ton of hard links to an inode there can
2412 * be a lot of backrefs. Don't waste time searching too hard,
2413 * this is just an optimization
2418 /* we hit the end of the leaf before we found an xattr or
2419 * something larger than an xattr. We have to assume the inode
2426 * read an inode from the btree into the in-memory inode
2428 static void btrfs_read_locked_inode(struct inode
*inode
)
2430 struct btrfs_path
*path
;
2431 struct extent_buffer
*leaf
;
2432 struct btrfs_inode_item
*inode_item
;
2433 struct btrfs_timespec
*tspec
;
2434 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2435 struct btrfs_key location
;
2439 bool filled
= false;
2441 ret
= btrfs_fill_inode(inode
, &rdev
);
2445 path
= btrfs_alloc_path();
2449 path
->leave_spinning
= 1;
2450 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
2452 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
2456 leaf
= path
->nodes
[0];
2461 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2462 struct btrfs_inode_item
);
2463 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
2464 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
2465 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
2466 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
2467 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
2469 tspec
= btrfs_inode_atime(inode_item
);
2470 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2471 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2473 tspec
= btrfs_inode_mtime(inode_item
);
2474 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2475 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2477 tspec
= btrfs_inode_ctime(inode_item
);
2478 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2479 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2481 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
2482 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
2483 BTRFS_I(inode
)->sequence
= btrfs_inode_sequence(leaf
, inode_item
);
2484 inode
->i_generation
= BTRFS_I(inode
)->generation
;
2486 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
2488 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
2489 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
2492 * try to precache a NULL acl entry for files that don't have
2493 * any xattrs or acls
2495 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
2498 cache_no_acl(inode
);
2500 btrfs_free_path(path
);
2502 switch (inode
->i_mode
& S_IFMT
) {
2504 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2505 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2506 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2507 inode
->i_fop
= &btrfs_file_operations
;
2508 inode
->i_op
= &btrfs_file_inode_operations
;
2511 inode
->i_fop
= &btrfs_dir_file_operations
;
2512 if (root
== root
->fs_info
->tree_root
)
2513 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
2515 inode
->i_op
= &btrfs_dir_inode_operations
;
2518 inode
->i_op
= &btrfs_symlink_inode_operations
;
2519 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2520 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2523 inode
->i_op
= &btrfs_special_inode_operations
;
2524 init_special_inode(inode
, inode
->i_mode
, rdev
);
2528 btrfs_update_iflags(inode
);
2532 btrfs_free_path(path
);
2533 make_bad_inode(inode
);
2537 * given a leaf and an inode, copy the inode fields into the leaf
2539 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
2540 struct extent_buffer
*leaf
,
2541 struct btrfs_inode_item
*item
,
2542 struct inode
*inode
)
2544 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
2545 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
2546 btrfs_set_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
);
2547 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
2548 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
2550 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
2551 inode
->i_atime
.tv_sec
);
2552 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
2553 inode
->i_atime
.tv_nsec
);
2555 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
2556 inode
->i_mtime
.tv_sec
);
2557 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
2558 inode
->i_mtime
.tv_nsec
);
2560 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
2561 inode
->i_ctime
.tv_sec
);
2562 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
2563 inode
->i_ctime
.tv_nsec
);
2565 btrfs_set_inode_nbytes(leaf
, item
, inode_get_bytes(inode
));
2566 btrfs_set_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
);
2567 btrfs_set_inode_sequence(leaf
, item
, BTRFS_I(inode
)->sequence
);
2568 btrfs_set_inode_transid(leaf
, item
, trans
->transid
);
2569 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
2570 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
2571 btrfs_set_inode_block_group(leaf
, item
, 0);
2575 * copy everything in the in-memory inode into the btree.
2577 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
2578 struct btrfs_root
*root
, struct inode
*inode
)
2580 struct btrfs_inode_item
*inode_item
;
2581 struct btrfs_path
*path
;
2582 struct extent_buffer
*leaf
;
2586 * If the inode is a free space inode, we can deadlock during commit
2587 * if we put it into the delayed code.
2589 * The data relocation inode should also be directly updated
2592 if (!btrfs_is_free_space_inode(root
, inode
)
2593 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
) {
2594 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
2596 btrfs_set_inode_last_trans(trans
, inode
);
2600 path
= btrfs_alloc_path();
2604 path
->leave_spinning
= 1;
2605 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
2613 btrfs_unlock_up_safe(path
, 1);
2614 leaf
= path
->nodes
[0];
2615 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2616 struct btrfs_inode_item
);
2618 fill_inode_item(trans
, leaf
, inode_item
, inode
);
2619 btrfs_mark_buffer_dirty(leaf
);
2620 btrfs_set_inode_last_trans(trans
, inode
);
2623 btrfs_free_path(path
);
2628 * unlink helper that gets used here in inode.c and in the tree logging
2629 * recovery code. It remove a link in a directory with a given name, and
2630 * also drops the back refs in the inode to the directory
2632 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
2633 struct btrfs_root
*root
,
2634 struct inode
*dir
, struct inode
*inode
,
2635 const char *name
, int name_len
)
2637 struct btrfs_path
*path
;
2639 struct extent_buffer
*leaf
;
2640 struct btrfs_dir_item
*di
;
2641 struct btrfs_key key
;
2643 u64 ino
= btrfs_ino(inode
);
2644 u64 dir_ino
= btrfs_ino(dir
);
2646 path
= btrfs_alloc_path();
2652 path
->leave_spinning
= 1;
2653 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
2654 name
, name_len
, -1);
2663 leaf
= path
->nodes
[0];
2664 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2665 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2668 btrfs_release_path(path
);
2670 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
2673 printk(KERN_INFO
"btrfs failed to delete reference to %.*s, "
2674 "inode %llu parent %llu\n", name_len
, name
,
2675 (unsigned long long)ino
, (unsigned long long)dir_ino
);
2679 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
2683 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
2685 BUG_ON(ret
!= 0 && ret
!= -ENOENT
);
2687 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
2692 btrfs_free_path(path
);
2696 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
2697 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
2698 btrfs_update_inode(trans
, root
, dir
);
2703 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
2704 struct btrfs_root
*root
,
2705 struct inode
*dir
, struct inode
*inode
,
2706 const char *name
, int name_len
)
2709 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
2711 btrfs_drop_nlink(inode
);
2712 ret
= btrfs_update_inode(trans
, root
, inode
);
2718 /* helper to check if there is any shared block in the path */
2719 static int check_path_shared(struct btrfs_root
*root
,
2720 struct btrfs_path
*path
)
2722 struct extent_buffer
*eb
;
2726 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
2729 if (!path
->nodes
[level
])
2731 eb
= path
->nodes
[level
];
2732 if (!btrfs_block_can_be_shared(root
, eb
))
2734 ret
= btrfs_lookup_extent_info(NULL
, root
, eb
->start
, eb
->len
,
2743 * helper to start transaction for unlink and rmdir.
2745 * unlink and rmdir are special in btrfs, they do not always free space.
2746 * so in enospc case, we should make sure they will free space before
2747 * allowing them to use the global metadata reservation.
2749 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
,
2750 struct dentry
*dentry
)
2752 struct btrfs_trans_handle
*trans
;
2753 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2754 struct btrfs_path
*path
;
2755 struct btrfs_inode_ref
*ref
;
2756 struct btrfs_dir_item
*di
;
2757 struct inode
*inode
= dentry
->d_inode
;
2762 u64 ino
= btrfs_ino(inode
);
2763 u64 dir_ino
= btrfs_ino(dir
);
2766 * 1 for the possible orphan item
2767 * 1 for the dir item
2768 * 1 for the dir index
2769 * 1 for the inode ref
2770 * 1 for the inode ref in the tree log
2771 * 2 for the dir entries in the log
2774 trans
= btrfs_start_transaction(root
, 8);
2775 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
2778 if (ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
2779 return ERR_PTR(-ENOSPC
);
2781 /* check if there is someone else holds reference */
2782 if (S_ISDIR(inode
->i_mode
) && atomic_read(&inode
->i_count
) > 1)
2783 return ERR_PTR(-ENOSPC
);
2785 if (atomic_read(&inode
->i_count
) > 2)
2786 return ERR_PTR(-ENOSPC
);
2788 if (xchg(&root
->fs_info
->enospc_unlink
, 1))
2789 return ERR_PTR(-ENOSPC
);
2791 path
= btrfs_alloc_path();
2793 root
->fs_info
->enospc_unlink
= 0;
2794 return ERR_PTR(-ENOMEM
);
2797 /* 1 for the orphan item */
2798 trans
= btrfs_start_transaction(root
, 1);
2799 if (IS_ERR(trans
)) {
2800 btrfs_free_path(path
);
2801 root
->fs_info
->enospc_unlink
= 0;
2805 path
->skip_locking
= 1;
2806 path
->search_commit_root
= 1;
2808 ret
= btrfs_lookup_inode(trans
, root
, path
,
2809 &BTRFS_I(dir
)->location
, 0);
2815 if (check_path_shared(root
, path
))
2820 btrfs_release_path(path
);
2822 ret
= btrfs_lookup_inode(trans
, root
, path
,
2823 &BTRFS_I(inode
)->location
, 0);
2829 if (check_path_shared(root
, path
))
2834 btrfs_release_path(path
);
2836 if (ret
== 0 && S_ISREG(inode
->i_mode
)) {
2837 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
2844 if (check_path_shared(root
, path
))
2846 btrfs_release_path(path
);
2854 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
2855 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
2861 if (check_path_shared(root
, path
))
2867 btrfs_release_path(path
);
2869 ref
= btrfs_lookup_inode_ref(trans
, root
, path
,
2870 dentry
->d_name
.name
, dentry
->d_name
.len
,
2877 if (check_path_shared(root
, path
))
2879 index
= btrfs_inode_ref_index(path
->nodes
[0], ref
);
2880 btrfs_release_path(path
);
2883 * This is a commit root search, if we can lookup inode item and other
2884 * relative items in the commit root, it means the transaction of
2885 * dir/file creation has been committed, and the dir index item that we
2886 * delay to insert has also been inserted into the commit root. So
2887 * we needn't worry about the delayed insertion of the dir index item
2890 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir_ino
, index
,
2891 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
2896 BUG_ON(ret
== -ENOENT
);
2897 if (check_path_shared(root
, path
))
2902 btrfs_free_path(path
);
2903 /* Migrate the orphan reservation over */
2905 err
= btrfs_block_rsv_migrate(trans
->block_rsv
,
2906 &root
->fs_info
->global_block_rsv
,
2907 trans
->bytes_reserved
);
2910 btrfs_end_transaction(trans
, root
);
2911 root
->fs_info
->enospc_unlink
= 0;
2912 return ERR_PTR(err
);
2915 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
2919 static void __unlink_end_trans(struct btrfs_trans_handle
*trans
,
2920 struct btrfs_root
*root
)
2922 if (trans
->block_rsv
== &root
->fs_info
->global_block_rsv
) {
2923 btrfs_block_rsv_release(root
, trans
->block_rsv
,
2924 trans
->bytes_reserved
);
2925 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
2926 BUG_ON(!root
->fs_info
->enospc_unlink
);
2927 root
->fs_info
->enospc_unlink
= 0;
2929 btrfs_end_transaction_throttle(trans
, root
);
2932 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
2934 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2935 struct btrfs_trans_handle
*trans
;
2936 struct inode
*inode
= dentry
->d_inode
;
2938 unsigned long nr
= 0;
2940 trans
= __unlink_start_trans(dir
, dentry
);
2942 return PTR_ERR(trans
);
2944 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
2946 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
2947 dentry
->d_name
.name
, dentry
->d_name
.len
);
2951 if (inode
->i_nlink
== 0) {
2952 ret
= btrfs_orphan_add(trans
, inode
);
2958 nr
= trans
->blocks_used
;
2959 __unlink_end_trans(trans
, root
);
2960 btrfs_btree_balance_dirty(root
, nr
);
2964 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
2965 struct btrfs_root
*root
,
2966 struct inode
*dir
, u64 objectid
,
2967 const char *name
, int name_len
)
2969 struct btrfs_path
*path
;
2970 struct extent_buffer
*leaf
;
2971 struct btrfs_dir_item
*di
;
2972 struct btrfs_key key
;
2975 u64 dir_ino
= btrfs_ino(dir
);
2977 path
= btrfs_alloc_path();
2981 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
2982 name
, name_len
, -1);
2983 BUG_ON(IS_ERR_OR_NULL(di
));
2985 leaf
= path
->nodes
[0];
2986 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2987 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
2988 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2990 btrfs_release_path(path
);
2992 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
2993 objectid
, root
->root_key
.objectid
,
2994 dir_ino
, &index
, name
, name_len
);
2996 BUG_ON(ret
!= -ENOENT
);
2997 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
2999 BUG_ON(IS_ERR_OR_NULL(di
));
3001 leaf
= path
->nodes
[0];
3002 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3003 btrfs_release_path(path
);
3006 btrfs_release_path(path
);
3008 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3011 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3012 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3013 ret
= btrfs_update_inode(trans
, root
, dir
);
3016 btrfs_free_path(path
);
3020 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3022 struct inode
*inode
= dentry
->d_inode
;
3024 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3025 struct btrfs_trans_handle
*trans
;
3026 unsigned long nr
= 0;
3028 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
||
3029 btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
)
3032 trans
= __unlink_start_trans(dir
, dentry
);
3034 return PTR_ERR(trans
);
3036 if (unlikely(btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
3037 err
= btrfs_unlink_subvol(trans
, root
, dir
,
3038 BTRFS_I(inode
)->location
.objectid
,
3039 dentry
->d_name
.name
,
3040 dentry
->d_name
.len
);
3044 err
= btrfs_orphan_add(trans
, inode
);
3048 /* now the directory is empty */
3049 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3050 dentry
->d_name
.name
, dentry
->d_name
.len
);
3052 btrfs_i_size_write(inode
, 0);
3054 nr
= trans
->blocks_used
;
3055 __unlink_end_trans(trans
, root
);
3056 btrfs_btree_balance_dirty(root
, nr
);
3062 * this can truncate away extent items, csum items and directory items.
3063 * It starts at a high offset and removes keys until it can't find
3064 * any higher than new_size
3066 * csum items that cross the new i_size are truncated to the new size
3069 * min_type is the minimum key type to truncate down to. If set to 0, this
3070 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3072 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
3073 struct btrfs_root
*root
,
3074 struct inode
*inode
,
3075 u64 new_size
, u32 min_type
)
3077 struct btrfs_path
*path
;
3078 struct extent_buffer
*leaf
;
3079 struct btrfs_file_extent_item
*fi
;
3080 struct btrfs_key key
;
3081 struct btrfs_key found_key
;
3082 u64 extent_start
= 0;
3083 u64 extent_num_bytes
= 0;
3084 u64 extent_offset
= 0;
3086 u64 mask
= root
->sectorsize
- 1;
3087 u32 found_type
= (u8
)-1;
3090 int pending_del_nr
= 0;
3091 int pending_del_slot
= 0;
3092 int extent_type
= -1;
3096 u64 ino
= btrfs_ino(inode
);
3098 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
3100 path
= btrfs_alloc_path();
3105 if (root
->ref_cows
|| root
== root
->fs_info
->tree_root
)
3106 btrfs_drop_extent_cache(inode
, new_size
& (~mask
), (u64
)-1, 0);
3109 * This function is also used to drop the items in the log tree before
3110 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3111 * it is used to drop the loged items. So we shouldn't kill the delayed
3114 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
3115 btrfs_kill_delayed_inode_items(inode
);
3118 key
.offset
= (u64
)-1;
3122 path
->leave_spinning
= 1;
3123 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3130 /* there are no items in the tree for us to truncate, we're
3133 if (path
->slots
[0] == 0)
3140 leaf
= path
->nodes
[0];
3141 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3142 found_type
= btrfs_key_type(&found_key
);
3145 if (found_key
.objectid
!= ino
)
3148 if (found_type
< min_type
)
3151 item_end
= found_key
.offset
;
3152 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
3153 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3154 struct btrfs_file_extent_item
);
3155 extent_type
= btrfs_file_extent_type(leaf
, fi
);
3156 encoding
= btrfs_file_extent_compression(leaf
, fi
);
3157 encoding
|= btrfs_file_extent_encryption(leaf
, fi
);
3158 encoding
|= btrfs_file_extent_other_encoding(leaf
, fi
);
3160 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3162 btrfs_file_extent_num_bytes(leaf
, fi
);
3163 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3164 item_end
+= btrfs_file_extent_inline_len(leaf
,
3169 if (found_type
> min_type
) {
3172 if (item_end
< new_size
)
3174 if (found_key
.offset
>= new_size
)
3180 /* FIXME, shrink the extent if the ref count is only 1 */
3181 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
3184 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3186 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
3187 if (!del_item
&& !encoding
) {
3188 u64 orig_num_bytes
=
3189 btrfs_file_extent_num_bytes(leaf
, fi
);
3190 extent_num_bytes
= new_size
-
3191 found_key
.offset
+ root
->sectorsize
- 1;
3192 extent_num_bytes
= extent_num_bytes
&
3193 ~((u64
)root
->sectorsize
- 1);
3194 btrfs_set_file_extent_num_bytes(leaf
, fi
,
3196 num_dec
= (orig_num_bytes
-
3198 if (root
->ref_cows
&& extent_start
!= 0)
3199 inode_sub_bytes(inode
, num_dec
);
3200 btrfs_mark_buffer_dirty(leaf
);
3203 btrfs_file_extent_disk_num_bytes(leaf
,
3205 extent_offset
= found_key
.offset
-
3206 btrfs_file_extent_offset(leaf
, fi
);
3208 /* FIXME blocksize != 4096 */
3209 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
3210 if (extent_start
!= 0) {
3213 inode_sub_bytes(inode
, num_dec
);
3216 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3218 * we can't truncate inline items that have had
3222 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
3223 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
3224 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
3225 u32 size
= new_size
- found_key
.offset
;
3227 if (root
->ref_cows
) {
3228 inode_sub_bytes(inode
, item_end
+ 1 -
3232 btrfs_file_extent_calc_inline_size(size
);
3233 ret
= btrfs_truncate_item(trans
, root
, path
,
3235 } else if (root
->ref_cows
) {
3236 inode_sub_bytes(inode
, item_end
+ 1 -
3242 if (!pending_del_nr
) {
3243 /* no pending yet, add ourselves */
3244 pending_del_slot
= path
->slots
[0];
3246 } else if (pending_del_nr
&&
3247 path
->slots
[0] + 1 == pending_del_slot
) {
3248 /* hop on the pending chunk */
3250 pending_del_slot
= path
->slots
[0];
3257 if (found_extent
&& (root
->ref_cows
||
3258 root
== root
->fs_info
->tree_root
)) {
3259 btrfs_set_path_blocking(path
);
3260 ret
= btrfs_free_extent(trans
, root
, extent_start
,
3261 extent_num_bytes
, 0,
3262 btrfs_header_owner(leaf
),
3263 ino
, extent_offset
);
3267 if (found_type
== BTRFS_INODE_ITEM_KEY
)
3270 if (path
->slots
[0] == 0 ||
3271 path
->slots
[0] != pending_del_slot
) {
3272 if (root
->ref_cows
&&
3273 BTRFS_I(inode
)->location
.objectid
!=
3274 BTRFS_FREE_INO_OBJECTID
) {
3278 if (pending_del_nr
) {
3279 ret
= btrfs_del_items(trans
, root
, path
,
3285 btrfs_release_path(path
);
3292 if (pending_del_nr
) {
3293 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
3297 btrfs_free_path(path
);
3302 * taken from block_truncate_page, but does cow as it zeros out
3303 * any bytes left in the last page in the file.
3305 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
3307 struct inode
*inode
= mapping
->host
;
3308 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3309 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3310 struct btrfs_ordered_extent
*ordered
;
3311 struct extent_state
*cached_state
= NULL
;
3313 u32 blocksize
= root
->sectorsize
;
3314 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
3315 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
3317 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
3322 if ((offset
& (blocksize
- 1)) == 0)
3324 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
3330 page
= find_or_create_page(mapping
, index
, mask
);
3332 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
3336 page_start
= page_offset(page
);
3337 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3339 if (!PageUptodate(page
)) {
3340 ret
= btrfs_readpage(NULL
, page
);
3342 if (page
->mapping
!= mapping
) {
3344 page_cache_release(page
);
3347 if (!PageUptodate(page
)) {
3352 wait_on_page_writeback(page
);
3354 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
,
3356 set_page_extent_mapped(page
);
3358 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
3360 unlock_extent_cached(io_tree
, page_start
, page_end
,
3361 &cached_state
, GFP_NOFS
);
3363 page_cache_release(page
);
3364 btrfs_start_ordered_extent(inode
, ordered
, 1);
3365 btrfs_put_ordered_extent(ordered
);
3369 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
3370 EXTENT_DIRTY
| EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
,
3371 0, 0, &cached_state
, GFP_NOFS
);
3373 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
3376 unlock_extent_cached(io_tree
, page_start
, page_end
,
3377 &cached_state
, GFP_NOFS
);
3382 if (offset
!= PAGE_CACHE_SIZE
) {
3384 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
3385 flush_dcache_page(page
);
3388 ClearPageChecked(page
);
3389 set_page_dirty(page
);
3390 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
3395 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
3397 page_cache_release(page
);
3403 * This function puts in dummy file extents for the area we're creating a hole
3404 * for. So if we are truncating this file to a larger size we need to insert
3405 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3406 * the range between oldsize and size
3408 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
3410 struct btrfs_trans_handle
*trans
;
3411 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3412 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3413 struct extent_map
*em
= NULL
;
3414 struct extent_state
*cached_state
= NULL
;
3415 u64 mask
= root
->sectorsize
- 1;
3416 u64 hole_start
= (oldsize
+ mask
) & ~mask
;
3417 u64 block_end
= (size
+ mask
) & ~mask
;
3423 if (size
<= hole_start
)
3427 struct btrfs_ordered_extent
*ordered
;
3428 btrfs_wait_ordered_range(inode
, hole_start
,
3429 block_end
- hole_start
);
3430 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
3431 &cached_state
, GFP_NOFS
);
3432 ordered
= btrfs_lookup_ordered_extent(inode
, hole_start
);
3435 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
3436 &cached_state
, GFP_NOFS
);
3437 btrfs_put_ordered_extent(ordered
);
3440 cur_offset
= hole_start
;
3442 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
3443 block_end
- cur_offset
, 0);
3444 BUG_ON(IS_ERR_OR_NULL(em
));
3445 last_byte
= min(extent_map_end(em
), block_end
);
3446 last_byte
= (last_byte
+ mask
) & ~mask
;
3447 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
3449 hole_size
= last_byte
- cur_offset
;
3451 trans
= btrfs_start_transaction(root
, 2);
3452 if (IS_ERR(trans
)) {
3453 err
= PTR_ERR(trans
);
3457 err
= btrfs_drop_extents(trans
, inode
, cur_offset
,
3458 cur_offset
+ hole_size
,
3461 btrfs_end_transaction(trans
, root
);
3465 err
= btrfs_insert_file_extent(trans
, root
,
3466 btrfs_ino(inode
), cur_offset
, 0,
3467 0, hole_size
, 0, hole_size
,
3470 btrfs_end_transaction(trans
, root
);
3474 btrfs_drop_extent_cache(inode
, hole_start
,
3477 btrfs_end_transaction(trans
, root
);
3479 free_extent_map(em
);
3481 cur_offset
= last_byte
;
3482 if (cur_offset
>= block_end
)
3486 free_extent_map(em
);
3487 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
3492 static int btrfs_setsize(struct inode
*inode
, loff_t newsize
)
3494 loff_t oldsize
= i_size_read(inode
);
3497 if (newsize
== oldsize
)
3500 if (newsize
> oldsize
) {
3501 i_size_write(inode
, newsize
);
3502 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
3503 truncate_pagecache(inode
, oldsize
, newsize
);
3504 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
3506 btrfs_setsize(inode
, oldsize
);
3510 mark_inode_dirty(inode
);
3514 * We're truncating a file that used to have good data down to
3515 * zero. Make sure it gets into the ordered flush list so that
3516 * any new writes get down to disk quickly.
3519 BTRFS_I(inode
)->ordered_data_close
= 1;
3521 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3522 truncate_setsize(inode
, newsize
);
3523 ret
= btrfs_truncate(inode
);
3529 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
3531 struct inode
*inode
= dentry
->d_inode
;
3532 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3535 if (btrfs_root_readonly(root
))
3538 err
= inode_change_ok(inode
, attr
);
3542 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
3543 err
= btrfs_setsize(inode
, attr
->ia_size
);
3548 if (attr
->ia_valid
) {
3549 setattr_copy(inode
, attr
);
3550 mark_inode_dirty(inode
);
3552 if (attr
->ia_valid
& ATTR_MODE
)
3553 err
= btrfs_acl_chmod(inode
);
3559 void btrfs_evict_inode(struct inode
*inode
)
3561 struct btrfs_trans_handle
*trans
;
3562 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3563 struct btrfs_block_rsv
*rsv
, *global_rsv
;
3564 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
3568 trace_btrfs_inode_evict(inode
);
3570 truncate_inode_pages(&inode
->i_data
, 0);
3571 if (inode
->i_nlink
&& (btrfs_root_refs(&root
->root_item
) != 0 ||
3572 btrfs_is_free_space_inode(root
, inode
)))
3575 if (is_bad_inode(inode
)) {
3576 btrfs_orphan_del(NULL
, inode
);
3579 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
3580 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
3582 if (root
->fs_info
->log_root_recovering
) {
3583 BUG_ON(!list_empty(&BTRFS_I(inode
)->i_orphan
));
3587 if (inode
->i_nlink
> 0) {
3588 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0);
3592 rsv
= btrfs_alloc_block_rsv(root
);
3594 btrfs_orphan_del(NULL
, inode
);
3597 rsv
->size
= min_size
;
3598 global_rsv
= &root
->fs_info
->global_block_rsv
;
3600 btrfs_i_size_write(inode
, 0);
3603 * This is a bit simpler than btrfs_truncate since
3605 * 1) We've already reserved our space for our orphan item in the
3607 * 2) We're going to delete the inode item, so we don't need to update
3610 * So we just need to reserve some slack space in case we add bytes when
3611 * doing the truncate.
3614 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
);
3617 * Try and steal from the global reserve since we will
3618 * likely not use this space anyway, we want to try as
3619 * hard as possible to get this to work.
3622 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
, min_size
);
3625 printk(KERN_WARNING
"Could not get space for a "
3626 "delete, will truncate on mount %d\n", ret
);
3627 btrfs_orphan_del(NULL
, inode
);
3628 btrfs_free_block_rsv(root
, rsv
);
3632 trans
= btrfs_start_transaction(root
, 0);
3633 if (IS_ERR(trans
)) {
3634 btrfs_orphan_del(NULL
, inode
);
3635 btrfs_free_block_rsv(root
, rsv
);
3639 trans
->block_rsv
= rsv
;
3641 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
3645 nr
= trans
->blocks_used
;
3646 btrfs_end_transaction(trans
, root
);
3648 btrfs_btree_balance_dirty(root
, nr
);
3651 btrfs_free_block_rsv(root
, rsv
);
3654 trans
->block_rsv
= root
->orphan_block_rsv
;
3655 ret
= btrfs_orphan_del(trans
, inode
);
3659 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
3660 if (!(root
== root
->fs_info
->tree_root
||
3661 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
3662 btrfs_return_ino(root
, btrfs_ino(inode
));
3664 nr
= trans
->blocks_used
;
3665 btrfs_end_transaction(trans
, root
);
3666 btrfs_btree_balance_dirty(root
, nr
);
3668 end_writeback(inode
);
3673 * this returns the key found in the dir entry in the location pointer.
3674 * If no dir entries were found, location->objectid is 0.
3676 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
3677 struct btrfs_key
*location
)
3679 const char *name
= dentry
->d_name
.name
;
3680 int namelen
= dentry
->d_name
.len
;
3681 struct btrfs_dir_item
*di
;
3682 struct btrfs_path
*path
;
3683 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3686 path
= btrfs_alloc_path();
3690 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(dir
), name
,
3695 if (IS_ERR_OR_NULL(di
))
3698 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
3700 btrfs_free_path(path
);
3703 location
->objectid
= 0;
3708 * when we hit a tree root in a directory, the btrfs part of the inode
3709 * needs to be changed to reflect the root directory of the tree root. This
3710 * is kind of like crossing a mount point.
3712 static int fixup_tree_root_location(struct btrfs_root
*root
,
3714 struct dentry
*dentry
,
3715 struct btrfs_key
*location
,
3716 struct btrfs_root
**sub_root
)
3718 struct btrfs_path
*path
;
3719 struct btrfs_root
*new_root
;
3720 struct btrfs_root_ref
*ref
;
3721 struct extent_buffer
*leaf
;
3725 path
= btrfs_alloc_path();
3732 ret
= btrfs_find_root_ref(root
->fs_info
->tree_root
, path
,
3733 BTRFS_I(dir
)->root
->root_key
.objectid
,
3734 location
->objectid
);
3741 leaf
= path
->nodes
[0];
3742 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
3743 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(dir
) ||
3744 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
3747 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
3748 (unsigned long)(ref
+ 1),
3749 dentry
->d_name
.len
);
3753 btrfs_release_path(path
);
3755 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
3756 if (IS_ERR(new_root
)) {
3757 err
= PTR_ERR(new_root
);
3761 if (btrfs_root_refs(&new_root
->root_item
) == 0) {
3766 *sub_root
= new_root
;
3767 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
3768 location
->type
= BTRFS_INODE_ITEM_KEY
;
3769 location
->offset
= 0;
3772 btrfs_free_path(path
);
3776 static void inode_tree_add(struct inode
*inode
)
3778 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3779 struct btrfs_inode
*entry
;
3781 struct rb_node
*parent
;
3782 u64 ino
= btrfs_ino(inode
);
3784 p
= &root
->inode_tree
.rb_node
;
3787 if (inode_unhashed(inode
))
3790 spin_lock(&root
->inode_lock
);
3793 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
3795 if (ino
< btrfs_ino(&entry
->vfs_inode
))
3796 p
= &parent
->rb_left
;
3797 else if (ino
> btrfs_ino(&entry
->vfs_inode
))
3798 p
= &parent
->rb_right
;
3800 WARN_ON(!(entry
->vfs_inode
.i_state
&
3801 (I_WILL_FREE
| I_FREEING
)));
3802 rb_erase(parent
, &root
->inode_tree
);
3803 RB_CLEAR_NODE(parent
);
3804 spin_unlock(&root
->inode_lock
);
3808 rb_link_node(&BTRFS_I(inode
)->rb_node
, parent
, p
);
3809 rb_insert_color(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
3810 spin_unlock(&root
->inode_lock
);
3813 static void inode_tree_del(struct inode
*inode
)
3815 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3818 spin_lock(&root
->inode_lock
);
3819 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
3820 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
3821 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
3822 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
3824 spin_unlock(&root
->inode_lock
);
3827 * Free space cache has inodes in the tree root, but the tree root has a
3828 * root_refs of 0, so this could end up dropping the tree root as a
3829 * snapshot, so we need the extra !root->fs_info->tree_root check to
3830 * make sure we don't drop it.
3832 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0 &&
3833 root
!= root
->fs_info
->tree_root
) {
3834 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
3835 spin_lock(&root
->inode_lock
);
3836 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
3837 spin_unlock(&root
->inode_lock
);
3839 btrfs_add_dead_root(root
);
3843 int btrfs_invalidate_inodes(struct btrfs_root
*root
)
3845 struct rb_node
*node
;
3846 struct rb_node
*prev
;
3847 struct btrfs_inode
*entry
;
3848 struct inode
*inode
;
3851 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
3853 spin_lock(&root
->inode_lock
);
3855 node
= root
->inode_tree
.rb_node
;
3859 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
3861 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
3862 node
= node
->rb_left
;
3863 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
3864 node
= node
->rb_right
;
3870 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
3871 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
3875 prev
= rb_next(prev
);
3879 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
3880 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
3881 inode
= igrab(&entry
->vfs_inode
);
3883 spin_unlock(&root
->inode_lock
);
3884 if (atomic_read(&inode
->i_count
) > 1)
3885 d_prune_aliases(inode
);
3887 * btrfs_drop_inode will have it removed from
3888 * the inode cache when its usage count
3893 spin_lock(&root
->inode_lock
);
3897 if (cond_resched_lock(&root
->inode_lock
))
3900 node
= rb_next(node
);
3902 spin_unlock(&root
->inode_lock
);
3906 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
3908 struct btrfs_iget_args
*args
= p
;
3909 inode
->i_ino
= args
->ino
;
3910 BTRFS_I(inode
)->root
= args
->root
;
3911 btrfs_set_inode_space_info(args
->root
, inode
);
3915 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
3917 struct btrfs_iget_args
*args
= opaque
;
3918 return args
->ino
== btrfs_ino(inode
) &&
3919 args
->root
== BTRFS_I(inode
)->root
;
3922 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
3924 struct btrfs_root
*root
)
3926 struct inode
*inode
;
3927 struct btrfs_iget_args args
;
3928 args
.ino
= objectid
;
3931 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
3932 btrfs_init_locked_inode
,
3937 /* Get an inode object given its location and corresponding root.
3938 * Returns in *is_new if the inode was read from disk
3940 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
3941 struct btrfs_root
*root
, int *new)
3943 struct inode
*inode
;
3945 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
3947 return ERR_PTR(-ENOMEM
);
3949 if (inode
->i_state
& I_NEW
) {
3950 BTRFS_I(inode
)->root
= root
;
3951 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
3952 btrfs_read_locked_inode(inode
);
3953 if (!is_bad_inode(inode
)) {
3954 inode_tree_add(inode
);
3955 unlock_new_inode(inode
);
3959 unlock_new_inode(inode
);
3961 inode
= ERR_PTR(-ESTALE
);
3968 static struct inode
*new_simple_dir(struct super_block
*s
,
3969 struct btrfs_key
*key
,
3970 struct btrfs_root
*root
)
3972 struct inode
*inode
= new_inode(s
);
3975 return ERR_PTR(-ENOMEM
);
3977 BTRFS_I(inode
)->root
= root
;
3978 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
3979 BTRFS_I(inode
)->dummy_inode
= 1;
3981 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
3982 inode
->i_op
= &simple_dir_inode_operations
;
3983 inode
->i_fop
= &simple_dir_operations
;
3984 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
3985 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
3990 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
3992 struct inode
*inode
;
3993 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3994 struct btrfs_root
*sub_root
= root
;
3995 struct btrfs_key location
;
3999 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
4000 return ERR_PTR(-ENAMETOOLONG
);
4002 if (unlikely(d_need_lookup(dentry
))) {
4003 memcpy(&location
, dentry
->d_fsdata
, sizeof(struct btrfs_key
));
4004 kfree(dentry
->d_fsdata
);
4005 dentry
->d_fsdata
= NULL
;
4006 /* This thing is hashed, drop it for now */
4009 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
4013 return ERR_PTR(ret
);
4015 if (location
.objectid
== 0)
4018 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
4019 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
4023 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
4025 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
4026 ret
= fixup_tree_root_location(root
, dir
, dentry
,
4027 &location
, &sub_root
);
4030 inode
= ERR_PTR(ret
);
4032 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
4034 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
4036 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
4038 if (!IS_ERR(inode
) && root
!= sub_root
) {
4039 down_read(&root
->fs_info
->cleanup_work_sem
);
4040 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
4041 ret
= btrfs_orphan_cleanup(sub_root
);
4042 up_read(&root
->fs_info
->cleanup_work_sem
);
4044 inode
= ERR_PTR(ret
);
4050 static int btrfs_dentry_delete(const struct dentry
*dentry
)
4052 struct btrfs_root
*root
;
4054 if (!dentry
->d_inode
&& !IS_ROOT(dentry
))
4055 dentry
= dentry
->d_parent
;
4057 if (dentry
->d_inode
) {
4058 root
= BTRFS_I(dentry
->d_inode
)->root
;
4059 if (btrfs_root_refs(&root
->root_item
) == 0)
4065 static void btrfs_dentry_release(struct dentry
*dentry
)
4067 if (dentry
->d_fsdata
)
4068 kfree(dentry
->d_fsdata
);
4071 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
4072 struct nameidata
*nd
)
4076 ret
= d_splice_alias(btrfs_lookup_dentry(dir
, dentry
), dentry
);
4077 if (unlikely(d_need_lookup(dentry
))) {
4078 spin_lock(&dentry
->d_lock
);
4079 dentry
->d_flags
&= ~DCACHE_NEED_LOOKUP
;
4080 spin_unlock(&dentry
->d_lock
);
4085 unsigned char btrfs_filetype_table
[] = {
4086 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
4089 static int btrfs_real_readdir(struct file
*filp
, void *dirent
,
4092 struct inode
*inode
= filp
->f_dentry
->d_inode
;
4093 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4094 struct btrfs_item
*item
;
4095 struct btrfs_dir_item
*di
;
4096 struct btrfs_key key
;
4097 struct btrfs_key found_key
;
4098 struct btrfs_path
*path
;
4099 struct list_head ins_list
;
4100 struct list_head del_list
;
4103 struct extent_buffer
*leaf
;
4105 unsigned char d_type
;
4110 int key_type
= BTRFS_DIR_INDEX_KEY
;
4114 int is_curr
= 0; /* filp->f_pos points to the current index? */
4116 /* FIXME, use a real flag for deciding about the key type */
4117 if (root
->fs_info
->tree_root
== root
)
4118 key_type
= BTRFS_DIR_ITEM_KEY
;
4120 /* special case for "." */
4121 if (filp
->f_pos
== 0) {
4122 over
= filldir(dirent
, ".", 1,
4123 filp
->f_pos
, btrfs_ino(inode
), DT_DIR
);
4128 /* special case for .., just use the back ref */
4129 if (filp
->f_pos
== 1) {
4130 u64 pino
= parent_ino(filp
->f_path
.dentry
);
4131 over
= filldir(dirent
, "..", 2,
4132 filp
->f_pos
, pino
, DT_DIR
);
4137 path
= btrfs_alloc_path();
4143 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
4144 INIT_LIST_HEAD(&ins_list
);
4145 INIT_LIST_HEAD(&del_list
);
4146 btrfs_get_delayed_items(inode
, &ins_list
, &del_list
);
4149 btrfs_set_key_type(&key
, key_type
);
4150 key
.offset
= filp
->f_pos
;
4151 key
.objectid
= btrfs_ino(inode
);
4153 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4158 leaf
= path
->nodes
[0];
4159 slot
= path
->slots
[0];
4160 if (slot
>= btrfs_header_nritems(leaf
)) {
4161 ret
= btrfs_next_leaf(root
, path
);
4169 item
= btrfs_item_nr(leaf
, slot
);
4170 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
4172 if (found_key
.objectid
!= key
.objectid
)
4174 if (btrfs_key_type(&found_key
) != key_type
)
4176 if (found_key
.offset
< filp
->f_pos
)
4178 if (key_type
== BTRFS_DIR_INDEX_KEY
&&
4179 btrfs_should_delete_dir_index(&del_list
,
4183 filp
->f_pos
= found_key
.offset
;
4186 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
4188 di_total
= btrfs_item_size(leaf
, item
);
4190 while (di_cur
< di_total
) {
4191 struct btrfs_key location
;
4194 if (verify_dir_item(root
, leaf
, di
))
4197 name_len
= btrfs_dir_name_len(leaf
, di
);
4198 if (name_len
<= sizeof(tmp_name
)) {
4199 name_ptr
= tmp_name
;
4201 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
4207 read_extent_buffer(leaf
, name_ptr
,
4208 (unsigned long)(di
+ 1), name_len
);
4210 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
4211 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
4215 q
.hash
= full_name_hash(q
.name
, q
.len
);
4216 tmp
= d_lookup(filp
->f_dentry
, &q
);
4218 struct btrfs_key
*newkey
;
4220 newkey
= kzalloc(sizeof(struct btrfs_key
),
4224 tmp
= d_alloc(filp
->f_dentry
, &q
);
4230 memcpy(newkey
, &location
,
4231 sizeof(struct btrfs_key
));
4232 tmp
->d_fsdata
= newkey
;
4233 tmp
->d_flags
|= DCACHE_NEED_LOOKUP
;
4240 /* is this a reference to our own snapshot? If so
4243 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
4244 location
.objectid
== root
->root_key
.objectid
) {
4248 over
= filldir(dirent
, name_ptr
, name_len
,
4249 found_key
.offset
, location
.objectid
,
4253 if (name_ptr
!= tmp_name
)
4258 di_len
= btrfs_dir_name_len(leaf
, di
) +
4259 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
4261 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
4267 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
4270 ret
= btrfs_readdir_delayed_dir_index(filp
, dirent
, filldir
,
4276 /* Reached end of directory/root. Bump pos past the last item. */
4277 if (key_type
== BTRFS_DIR_INDEX_KEY
)
4279 * 32-bit glibc will use getdents64, but then strtol -
4280 * so the last number we can serve is this.
4282 filp
->f_pos
= 0x7fffffff;
4288 if (key_type
== BTRFS_DIR_INDEX_KEY
)
4289 btrfs_put_delayed_items(&ins_list
, &del_list
);
4290 btrfs_free_path(path
);
4294 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
4296 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4297 struct btrfs_trans_handle
*trans
;
4299 bool nolock
= false;
4301 if (BTRFS_I(inode
)->dummy_inode
)
4304 if (btrfs_fs_closing(root
->fs_info
) && btrfs_is_free_space_inode(root
, inode
))
4307 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
4309 trans
= btrfs_join_transaction_nolock(root
);
4311 trans
= btrfs_join_transaction(root
);
4313 return PTR_ERR(trans
);
4315 ret
= btrfs_end_transaction_nolock(trans
, root
);
4317 ret
= btrfs_commit_transaction(trans
, root
);
4323 * This is somewhat expensive, updating the tree every time the
4324 * inode changes. But, it is most likely to find the inode in cache.
4325 * FIXME, needs more benchmarking...there are no reasons other than performance
4326 * to keep or drop this code.
4328 void btrfs_dirty_inode(struct inode
*inode
, int flags
)
4330 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4331 struct btrfs_trans_handle
*trans
;
4334 if (BTRFS_I(inode
)->dummy_inode
)
4337 trans
= btrfs_join_transaction(root
);
4338 BUG_ON(IS_ERR(trans
));
4340 ret
= btrfs_update_inode(trans
, root
, inode
);
4341 if (ret
&& ret
== -ENOSPC
) {
4342 /* whoops, lets try again with the full transaction */
4343 btrfs_end_transaction(trans
, root
);
4344 trans
= btrfs_start_transaction(root
, 1);
4345 if (IS_ERR(trans
)) {
4346 printk_ratelimited(KERN_ERR
"btrfs: fail to "
4347 "dirty inode %llu error %ld\n",
4348 (unsigned long long)btrfs_ino(inode
),
4353 ret
= btrfs_update_inode(trans
, root
, inode
);
4355 printk_ratelimited(KERN_ERR
"btrfs: fail to "
4356 "dirty inode %llu error %d\n",
4357 (unsigned long long)btrfs_ino(inode
),
4361 btrfs_end_transaction(trans
, root
);
4362 if (BTRFS_I(inode
)->delayed_node
)
4363 btrfs_balance_delayed_items(root
);
4367 * find the highest existing sequence number in a directory
4368 * and then set the in-memory index_cnt variable to reflect
4369 * free sequence numbers
4371 static int btrfs_set_inode_index_count(struct inode
*inode
)
4373 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4374 struct btrfs_key key
, found_key
;
4375 struct btrfs_path
*path
;
4376 struct extent_buffer
*leaf
;
4379 key
.objectid
= btrfs_ino(inode
);
4380 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
4381 key
.offset
= (u64
)-1;
4383 path
= btrfs_alloc_path();
4387 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4390 /* FIXME: we should be able to handle this */
4396 * MAGIC NUMBER EXPLANATION:
4397 * since we search a directory based on f_pos we have to start at 2
4398 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4399 * else has to start at 2
4401 if (path
->slots
[0] == 0) {
4402 BTRFS_I(inode
)->index_cnt
= 2;
4408 leaf
= path
->nodes
[0];
4409 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4411 if (found_key
.objectid
!= btrfs_ino(inode
) ||
4412 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
4413 BTRFS_I(inode
)->index_cnt
= 2;
4417 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
4419 btrfs_free_path(path
);
4424 * helper to find a free sequence number in a given directory. This current
4425 * code is very simple, later versions will do smarter things in the btree
4427 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
4431 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
4432 ret
= btrfs_inode_delayed_dir_index_count(dir
);
4434 ret
= btrfs_set_inode_index_count(dir
);
4440 *index
= BTRFS_I(dir
)->index_cnt
;
4441 BTRFS_I(dir
)->index_cnt
++;
4446 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
4447 struct btrfs_root
*root
,
4449 const char *name
, int name_len
,
4450 u64 ref_objectid
, u64 objectid
, int mode
,
4453 struct inode
*inode
;
4454 struct btrfs_inode_item
*inode_item
;
4455 struct btrfs_key
*location
;
4456 struct btrfs_path
*path
;
4457 struct btrfs_inode_ref
*ref
;
4458 struct btrfs_key key
[2];
4464 path
= btrfs_alloc_path();
4466 return ERR_PTR(-ENOMEM
);
4468 inode
= new_inode(root
->fs_info
->sb
);
4470 btrfs_free_path(path
);
4471 return ERR_PTR(-ENOMEM
);
4475 * we have to initialize this early, so we can reclaim the inode
4476 * number if we fail afterwards in this function.
4478 inode
->i_ino
= objectid
;
4481 trace_btrfs_inode_request(dir
);
4483 ret
= btrfs_set_inode_index(dir
, index
);
4485 btrfs_free_path(path
);
4487 return ERR_PTR(ret
);
4491 * index_cnt is ignored for everything but a dir,
4492 * btrfs_get_inode_index_count has an explanation for the magic
4495 BTRFS_I(inode
)->index_cnt
= 2;
4496 BTRFS_I(inode
)->root
= root
;
4497 BTRFS_I(inode
)->generation
= trans
->transid
;
4498 inode
->i_generation
= BTRFS_I(inode
)->generation
;
4499 btrfs_set_inode_space_info(root
, inode
);
4506 key
[0].objectid
= objectid
;
4507 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
4510 key
[1].objectid
= objectid
;
4511 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
4512 key
[1].offset
= ref_objectid
;
4514 sizes
[0] = sizeof(struct btrfs_inode_item
);
4515 sizes
[1] = name_len
+ sizeof(*ref
);
4517 path
->leave_spinning
= 1;
4518 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
4522 inode_init_owner(inode
, dir
, mode
);
4523 inode_set_bytes(inode
, 0);
4524 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4525 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4526 struct btrfs_inode_item
);
4527 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
4529 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
4530 struct btrfs_inode_ref
);
4531 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
4532 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
4533 ptr
= (unsigned long)(ref
+ 1);
4534 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
4536 btrfs_mark_buffer_dirty(path
->nodes
[0]);
4537 btrfs_free_path(path
);
4539 location
= &BTRFS_I(inode
)->location
;
4540 location
->objectid
= objectid
;
4541 location
->offset
= 0;
4542 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
4544 btrfs_inherit_iflags(inode
, dir
);
4546 if (S_ISREG(mode
)) {
4547 if (btrfs_test_opt(root
, NODATASUM
))
4548 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
4549 if (btrfs_test_opt(root
, NODATACOW
) ||
4550 (BTRFS_I(dir
)->flags
& BTRFS_INODE_NODATACOW
))
4551 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
;
4554 insert_inode_hash(inode
);
4555 inode_tree_add(inode
);
4557 trace_btrfs_inode_new(inode
);
4558 btrfs_set_inode_last_trans(trans
, inode
);
4563 BTRFS_I(dir
)->index_cnt
--;
4564 btrfs_free_path(path
);
4566 return ERR_PTR(ret
);
4569 static inline u8
btrfs_inode_type(struct inode
*inode
)
4571 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
4575 * utility function to add 'inode' into 'parent_inode' with
4576 * a give name and a given sequence number.
4577 * if 'add_backref' is true, also insert a backref from the
4578 * inode to the parent directory.
4580 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
4581 struct inode
*parent_inode
, struct inode
*inode
,
4582 const char *name
, int name_len
, int add_backref
, u64 index
)
4585 struct btrfs_key key
;
4586 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
4587 u64 ino
= btrfs_ino(inode
);
4588 u64 parent_ino
= btrfs_ino(parent_inode
);
4590 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4591 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
4594 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
4598 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4599 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
4600 key
.objectid
, root
->root_key
.objectid
,
4601 parent_ino
, index
, name
, name_len
);
4602 } else if (add_backref
) {
4603 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
4608 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
4610 btrfs_inode_type(inode
), index
);
4613 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
4615 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
4616 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
4621 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
4622 struct inode
*dir
, struct dentry
*dentry
,
4623 struct inode
*inode
, int backref
, u64 index
)
4625 int err
= btrfs_add_link(trans
, dir
, inode
,
4626 dentry
->d_name
.name
, dentry
->d_name
.len
,
4629 d_instantiate(dentry
, inode
);
4637 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
4638 int mode
, dev_t rdev
)
4640 struct btrfs_trans_handle
*trans
;
4641 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4642 struct inode
*inode
= NULL
;
4646 unsigned long nr
= 0;
4649 if (!new_valid_dev(rdev
))
4653 * 2 for inode item and ref
4655 * 1 for xattr if selinux is on
4657 trans
= btrfs_start_transaction(root
, 5);
4659 return PTR_ERR(trans
);
4661 err
= btrfs_find_free_ino(root
, &objectid
);
4665 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4666 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
4668 if (IS_ERR(inode
)) {
4669 err
= PTR_ERR(inode
);
4673 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
4679 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
4683 inode
->i_op
= &btrfs_special_inode_operations
;
4684 init_special_inode(inode
, inode
->i_mode
, rdev
);
4685 btrfs_update_inode(trans
, root
, inode
);
4688 nr
= trans
->blocks_used
;
4689 btrfs_end_transaction_throttle(trans
, root
);
4690 btrfs_btree_balance_dirty(root
, nr
);
4692 inode_dec_link_count(inode
);
4698 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
4699 int mode
, struct nameidata
*nd
)
4701 struct btrfs_trans_handle
*trans
;
4702 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4703 struct inode
*inode
= NULL
;
4706 unsigned long nr
= 0;
4711 * 2 for inode item and ref
4713 * 1 for xattr if selinux is on
4715 trans
= btrfs_start_transaction(root
, 5);
4717 return PTR_ERR(trans
);
4719 err
= btrfs_find_free_ino(root
, &objectid
);
4723 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4724 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
4726 if (IS_ERR(inode
)) {
4727 err
= PTR_ERR(inode
);
4731 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
4737 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
4741 inode
->i_mapping
->a_ops
= &btrfs_aops
;
4742 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
4743 inode
->i_fop
= &btrfs_file_operations
;
4744 inode
->i_op
= &btrfs_file_inode_operations
;
4745 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
4748 nr
= trans
->blocks_used
;
4749 btrfs_end_transaction_throttle(trans
, root
);
4751 inode_dec_link_count(inode
);
4754 btrfs_btree_balance_dirty(root
, nr
);
4758 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
4759 struct dentry
*dentry
)
4761 struct btrfs_trans_handle
*trans
;
4762 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4763 struct inode
*inode
= old_dentry
->d_inode
;
4765 unsigned long nr
= 0;
4769 /* do not allow sys_link's with other subvols of the same device */
4770 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
4773 if (inode
->i_nlink
== ~0U)
4776 err
= btrfs_set_inode_index(dir
, &index
);
4781 * 2 items for inode and inode ref
4782 * 2 items for dir items
4783 * 1 item for parent inode
4785 trans
= btrfs_start_transaction(root
, 5);
4786 if (IS_ERR(trans
)) {
4787 err
= PTR_ERR(trans
);
4791 btrfs_inc_nlink(inode
);
4792 inode
->i_ctime
= CURRENT_TIME
;
4795 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
4800 struct dentry
*parent
= dentry
->d_parent
;
4801 err
= btrfs_update_inode(trans
, root
, inode
);
4803 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
4806 nr
= trans
->blocks_used
;
4807 btrfs_end_transaction_throttle(trans
, root
);
4810 inode_dec_link_count(inode
);
4813 btrfs_btree_balance_dirty(root
, nr
);
4817 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
4819 struct inode
*inode
= NULL
;
4820 struct btrfs_trans_handle
*trans
;
4821 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4823 int drop_on_err
= 0;
4826 unsigned long nr
= 1;
4829 * 2 items for inode and ref
4830 * 2 items for dir items
4831 * 1 for xattr if selinux is on
4833 trans
= btrfs_start_transaction(root
, 5);
4835 return PTR_ERR(trans
);
4837 err
= btrfs_find_free_ino(root
, &objectid
);
4841 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4842 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
4843 S_IFDIR
| mode
, &index
);
4844 if (IS_ERR(inode
)) {
4845 err
= PTR_ERR(inode
);
4851 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
4855 inode
->i_op
= &btrfs_dir_inode_operations
;
4856 inode
->i_fop
= &btrfs_dir_file_operations
;
4858 btrfs_i_size_write(inode
, 0);
4859 err
= btrfs_update_inode(trans
, root
, inode
);
4863 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
4864 dentry
->d_name
.len
, 0, index
);
4868 d_instantiate(dentry
, inode
);
4872 nr
= trans
->blocks_used
;
4873 btrfs_end_transaction_throttle(trans
, root
);
4876 btrfs_btree_balance_dirty(root
, nr
);
4880 /* helper for btfs_get_extent. Given an existing extent in the tree,
4881 * and an extent that you want to insert, deal with overlap and insert
4882 * the new extent into the tree.
4884 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
4885 struct extent_map
*existing
,
4886 struct extent_map
*em
,
4887 u64 map_start
, u64 map_len
)
4891 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
4892 start_diff
= map_start
- em
->start
;
4893 em
->start
= map_start
;
4895 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
4896 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
4897 em
->block_start
+= start_diff
;
4898 em
->block_len
-= start_diff
;
4900 return add_extent_mapping(em_tree
, em
);
4903 static noinline
int uncompress_inline(struct btrfs_path
*path
,
4904 struct inode
*inode
, struct page
*page
,
4905 size_t pg_offset
, u64 extent_offset
,
4906 struct btrfs_file_extent_item
*item
)
4909 struct extent_buffer
*leaf
= path
->nodes
[0];
4912 unsigned long inline_size
;
4916 WARN_ON(pg_offset
!= 0);
4917 compress_type
= btrfs_file_extent_compression(leaf
, item
);
4918 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
4919 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
4920 btrfs_item_nr(leaf
, path
->slots
[0]));
4921 tmp
= kmalloc(inline_size
, GFP_NOFS
);
4924 ptr
= btrfs_file_extent_inline_start(item
);
4926 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
4928 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
4929 ret
= btrfs_decompress(compress_type
, tmp
, page
,
4930 extent_offset
, inline_size
, max_size
);
4932 char *kaddr
= kmap_atomic(page
, KM_USER0
);
4933 unsigned long copy_size
= min_t(u64
,
4934 PAGE_CACHE_SIZE
- pg_offset
,
4935 max_size
- extent_offset
);
4936 memset(kaddr
+ pg_offset
, 0, copy_size
);
4937 kunmap_atomic(kaddr
, KM_USER0
);
4944 * a bit scary, this does extent mapping from logical file offset to the disk.
4945 * the ugly parts come from merging extents from the disk with the in-ram
4946 * representation. This gets more complex because of the data=ordered code,
4947 * where the in-ram extents might be locked pending data=ordered completion.
4949 * This also copies inline extents directly into the page.
4952 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
4953 size_t pg_offset
, u64 start
, u64 len
,
4959 u64 extent_start
= 0;
4961 u64 objectid
= btrfs_ino(inode
);
4963 struct btrfs_path
*path
= NULL
;
4964 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4965 struct btrfs_file_extent_item
*item
;
4966 struct extent_buffer
*leaf
;
4967 struct btrfs_key found_key
;
4968 struct extent_map
*em
= NULL
;
4969 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4970 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4971 struct btrfs_trans_handle
*trans
= NULL
;
4975 read_lock(&em_tree
->lock
);
4976 em
= lookup_extent_mapping(em_tree
, start
, len
);
4978 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4979 read_unlock(&em_tree
->lock
);
4982 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
4983 free_extent_map(em
);
4984 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
4985 free_extent_map(em
);
4989 em
= alloc_extent_map();
4994 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4995 em
->start
= EXTENT_MAP_HOLE
;
4996 em
->orig_start
= EXTENT_MAP_HOLE
;
4998 em
->block_len
= (u64
)-1;
5001 path
= btrfs_alloc_path();
5007 * Chances are we'll be called again, so go ahead and do
5013 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
5014 objectid
, start
, trans
!= NULL
);
5021 if (path
->slots
[0] == 0)
5026 leaf
= path
->nodes
[0];
5027 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5028 struct btrfs_file_extent_item
);
5029 /* are we inside the extent that was found? */
5030 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5031 found_type
= btrfs_key_type(&found_key
);
5032 if (found_key
.objectid
!= objectid
||
5033 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
5037 found_type
= btrfs_file_extent_type(leaf
, item
);
5038 extent_start
= found_key
.offset
;
5039 compress_type
= btrfs_file_extent_compression(leaf
, item
);
5040 if (found_type
== BTRFS_FILE_EXTENT_REG
||
5041 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5042 extent_end
= extent_start
+
5043 btrfs_file_extent_num_bytes(leaf
, item
);
5044 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
5046 size
= btrfs_file_extent_inline_len(leaf
, item
);
5047 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
5048 ~((u64
)root
->sectorsize
- 1);
5051 if (start
>= extent_end
) {
5053 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
5054 ret
= btrfs_next_leaf(root
, path
);
5061 leaf
= path
->nodes
[0];
5063 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5064 if (found_key
.objectid
!= objectid
||
5065 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
5067 if (start
+ len
<= found_key
.offset
)
5070 em
->len
= found_key
.offset
- start
;
5074 if (found_type
== BTRFS_FILE_EXTENT_REG
||
5075 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5076 em
->start
= extent_start
;
5077 em
->len
= extent_end
- extent_start
;
5078 em
->orig_start
= extent_start
-
5079 btrfs_file_extent_offset(leaf
, item
);
5080 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
5082 em
->block_start
= EXTENT_MAP_HOLE
;
5085 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
5086 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
5087 em
->compress_type
= compress_type
;
5088 em
->block_start
= bytenr
;
5089 em
->block_len
= btrfs_file_extent_disk_num_bytes(leaf
,
5092 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
5093 em
->block_start
= bytenr
;
5094 em
->block_len
= em
->len
;
5095 if (found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
5096 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
5099 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
5103 size_t extent_offset
;
5106 em
->block_start
= EXTENT_MAP_INLINE
;
5107 if (!page
|| create
) {
5108 em
->start
= extent_start
;
5109 em
->len
= extent_end
- extent_start
;
5113 size
= btrfs_file_extent_inline_len(leaf
, item
);
5114 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
5115 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
5116 size
- extent_offset
);
5117 em
->start
= extent_start
+ extent_offset
;
5118 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
5119 ~((u64
)root
->sectorsize
- 1);
5120 em
->orig_start
= EXTENT_MAP_INLINE
;
5121 if (compress_type
) {
5122 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
5123 em
->compress_type
= compress_type
;
5125 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
5126 if (create
== 0 && !PageUptodate(page
)) {
5127 if (btrfs_file_extent_compression(leaf
, item
) !=
5128 BTRFS_COMPRESS_NONE
) {
5129 ret
= uncompress_inline(path
, inode
, page
,
5131 extent_offset
, item
);
5135 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
5137 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
5138 memset(map
+ pg_offset
+ copy_size
, 0,
5139 PAGE_CACHE_SIZE
- pg_offset
-
5144 flush_dcache_page(page
);
5145 } else if (create
&& PageUptodate(page
)) {
5149 free_extent_map(em
);
5152 btrfs_release_path(path
);
5153 trans
= btrfs_join_transaction(root
);
5156 return ERR_CAST(trans
);
5160 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
5163 btrfs_mark_buffer_dirty(leaf
);
5165 set_extent_uptodate(io_tree
, em
->start
,
5166 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
5169 printk(KERN_ERR
"btrfs unknown found_type %d\n", found_type
);
5176 em
->block_start
= EXTENT_MAP_HOLE
;
5177 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
5179 btrfs_release_path(path
);
5180 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
5181 printk(KERN_ERR
"Btrfs: bad extent! em: [%llu %llu] passed "
5182 "[%llu %llu]\n", (unsigned long long)em
->start
,
5183 (unsigned long long)em
->len
,
5184 (unsigned long long)start
,
5185 (unsigned long long)len
);
5191 write_lock(&em_tree
->lock
);
5192 ret
= add_extent_mapping(em_tree
, em
);
5193 /* it is possible that someone inserted the extent into the tree
5194 * while we had the lock dropped. It is also possible that
5195 * an overlapping map exists in the tree
5197 if (ret
== -EEXIST
) {
5198 struct extent_map
*existing
;
5202 existing
= lookup_extent_mapping(em_tree
, start
, len
);
5203 if (existing
&& (existing
->start
> start
||
5204 existing
->start
+ existing
->len
<= start
)) {
5205 free_extent_map(existing
);
5209 existing
= lookup_extent_mapping(em_tree
, em
->start
,
5212 err
= merge_extent_mapping(em_tree
, existing
,
5215 free_extent_map(existing
);
5217 free_extent_map(em
);
5222 free_extent_map(em
);
5226 free_extent_map(em
);
5231 write_unlock(&em_tree
->lock
);
5234 trace_btrfs_get_extent(root
, em
);
5237 btrfs_free_path(path
);
5239 ret
= btrfs_end_transaction(trans
, root
);
5244 free_extent_map(em
);
5245 return ERR_PTR(err
);
5250 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
5251 size_t pg_offset
, u64 start
, u64 len
,
5254 struct extent_map
*em
;
5255 struct extent_map
*hole_em
= NULL
;
5256 u64 range_start
= start
;
5262 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
5267 * if our em maps to a hole, there might
5268 * actually be delalloc bytes behind it
5270 if (em
->block_start
!= EXTENT_MAP_HOLE
)
5276 /* check to see if we've wrapped (len == -1 or similar) */
5285 /* ok, we didn't find anything, lets look for delalloc */
5286 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
5287 end
, len
, EXTENT_DELALLOC
, 1);
5288 found_end
= range_start
+ found
;
5289 if (found_end
< range_start
)
5290 found_end
= (u64
)-1;
5293 * we didn't find anything useful, return
5294 * the original results from get_extent()
5296 if (range_start
> end
|| found_end
<= start
) {
5302 /* adjust the range_start to make sure it doesn't
5303 * go backwards from the start they passed in
5305 range_start
= max(start
,range_start
);
5306 found
= found_end
- range_start
;
5309 u64 hole_start
= start
;
5312 em
= alloc_extent_map();
5318 * when btrfs_get_extent can't find anything it
5319 * returns one huge hole
5321 * make sure what it found really fits our range, and
5322 * adjust to make sure it is based on the start from
5326 u64 calc_end
= extent_map_end(hole_em
);
5328 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
5329 free_extent_map(hole_em
);
5332 hole_start
= max(hole_em
->start
, start
);
5333 hole_len
= calc_end
- hole_start
;
5337 if (hole_em
&& range_start
> hole_start
) {
5338 /* our hole starts before our delalloc, so we
5339 * have to return just the parts of the hole
5340 * that go until the delalloc starts
5342 em
->len
= min(hole_len
,
5343 range_start
- hole_start
);
5344 em
->start
= hole_start
;
5345 em
->orig_start
= hole_start
;
5347 * don't adjust block start at all,
5348 * it is fixed at EXTENT_MAP_HOLE
5350 em
->block_start
= hole_em
->block_start
;
5351 em
->block_len
= hole_len
;
5353 em
->start
= range_start
;
5355 em
->orig_start
= range_start
;
5356 em
->block_start
= EXTENT_MAP_DELALLOC
;
5357 em
->block_len
= found
;
5359 } else if (hole_em
) {
5364 free_extent_map(hole_em
);
5366 free_extent_map(em
);
5367 return ERR_PTR(err
);
5372 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
5373 struct extent_map
*em
,
5376 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5377 struct btrfs_trans_handle
*trans
;
5378 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
5379 struct btrfs_key ins
;
5382 bool insert
= false;
5385 * Ok if the extent map we looked up is a hole and is for the exact
5386 * range we want, there is no reason to allocate a new one, however if
5387 * it is not right then we need to free this one and drop the cache for
5390 if (em
->block_start
!= EXTENT_MAP_HOLE
|| em
->start
!= start
||
5392 free_extent_map(em
);
5395 btrfs_drop_extent_cache(inode
, start
, start
+ len
- 1, 0);
5398 trans
= btrfs_join_transaction(root
);
5400 return ERR_CAST(trans
);
5402 if (start
<= BTRFS_I(inode
)->disk_i_size
&& len
< 64 * 1024)
5403 btrfs_add_inode_defrag(trans
, inode
);
5405 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
5407 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
5408 ret
= btrfs_reserve_extent(trans
, root
, len
, root
->sectorsize
, 0,
5409 alloc_hint
, (u64
)-1, &ins
, 1);
5416 em
= alloc_extent_map();
5418 em
= ERR_PTR(-ENOMEM
);
5424 em
->orig_start
= em
->start
;
5425 em
->len
= ins
.offset
;
5427 em
->block_start
= ins
.objectid
;
5428 em
->block_len
= ins
.offset
;
5429 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5432 * We need to do this because if we're using the original em we searched
5433 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5436 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
5439 write_lock(&em_tree
->lock
);
5440 ret
= add_extent_mapping(em_tree
, em
);
5441 write_unlock(&em_tree
->lock
);
5444 btrfs_drop_extent_cache(inode
, start
, start
+ em
->len
- 1, 0);
5447 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
5448 ins
.offset
, ins
.offset
, 0);
5450 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
5454 btrfs_end_transaction(trans
, root
);
5459 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5460 * block must be cow'd
5462 static noinline
int can_nocow_odirect(struct btrfs_trans_handle
*trans
,
5463 struct inode
*inode
, u64 offset
, u64 len
)
5465 struct btrfs_path
*path
;
5467 struct extent_buffer
*leaf
;
5468 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5469 struct btrfs_file_extent_item
*fi
;
5470 struct btrfs_key key
;
5478 path
= btrfs_alloc_path();
5482 ret
= btrfs_lookup_file_extent(trans
, root
, path
, btrfs_ino(inode
),
5487 slot
= path
->slots
[0];
5490 /* can't find the item, must cow */
5497 leaf
= path
->nodes
[0];
5498 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
5499 if (key
.objectid
!= btrfs_ino(inode
) ||
5500 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
5501 /* not our file or wrong item type, must cow */
5505 if (key
.offset
> offset
) {
5506 /* Wrong offset, must cow */
5510 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
5511 found_type
= btrfs_file_extent_type(leaf
, fi
);
5512 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
5513 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
5514 /* not a regular extent, must cow */
5517 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
5518 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
5520 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
5521 if (extent_end
< offset
+ len
) {
5522 /* extent doesn't include our full range, must cow */
5526 if (btrfs_extent_readonly(root
, disk_bytenr
))
5530 * look for other files referencing this extent, if we
5531 * find any we must cow
5533 if (btrfs_cross_ref_exist(trans
, root
, btrfs_ino(inode
),
5534 key
.offset
- backref_offset
, disk_bytenr
))
5538 * adjust disk_bytenr and num_bytes to cover just the bytes
5539 * in this extent we are about to write. If there
5540 * are any csums in that range we have to cow in order
5541 * to keep the csums correct
5543 disk_bytenr
+= backref_offset
;
5544 disk_bytenr
+= offset
- key
.offset
;
5545 num_bytes
= min(offset
+ len
, extent_end
) - offset
;
5546 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
5549 * all of the above have passed, it is safe to overwrite this extent
5554 btrfs_free_path(path
);
5558 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
5559 struct buffer_head
*bh_result
, int create
)
5561 struct extent_map
*em
;
5562 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5563 u64 start
= iblock
<< inode
->i_blkbits
;
5564 u64 len
= bh_result
->b_size
;
5565 struct btrfs_trans_handle
*trans
;
5567 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
5572 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5573 * io. INLINE is special, and we could probably kludge it in here, but
5574 * it's still buffered so for safety lets just fall back to the generic
5577 * For COMPRESSED we _have_ to read the entire extent in so we can
5578 * decompress it, so there will be buffering required no matter what we
5579 * do, so go ahead and fallback to buffered.
5581 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5582 * to buffered IO. Don't blame me, this is the price we pay for using
5585 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
5586 em
->block_start
== EXTENT_MAP_INLINE
) {
5587 free_extent_map(em
);
5591 /* Just a good old fashioned hole, return */
5592 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
5593 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
5594 free_extent_map(em
);
5595 /* DIO will do one hole at a time, so just unlock a sector */
5596 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
,
5597 start
+ root
->sectorsize
- 1, GFP_NOFS
);
5602 * We don't allocate a new extent in the following cases
5604 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5606 * 2) The extent is marked as PREALLOC. We're good to go here and can
5607 * just use the extent.
5611 len
= em
->len
- (start
- em
->start
);
5615 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
5616 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
5617 em
->block_start
!= EXTENT_MAP_HOLE
)) {
5622 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
5623 type
= BTRFS_ORDERED_PREALLOC
;
5625 type
= BTRFS_ORDERED_NOCOW
;
5626 len
= min(len
, em
->len
- (start
- em
->start
));
5627 block_start
= em
->block_start
+ (start
- em
->start
);
5630 * we're not going to log anything, but we do need
5631 * to make sure the current transaction stays open
5632 * while we look for nocow cross refs
5634 trans
= btrfs_join_transaction(root
);
5638 if (can_nocow_odirect(trans
, inode
, start
, len
) == 1) {
5639 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
5640 block_start
, len
, len
, type
);
5641 btrfs_end_transaction(trans
, root
);
5643 free_extent_map(em
);
5648 btrfs_end_transaction(trans
, root
);
5652 * this will cow the extent, reset the len in case we changed
5655 len
= bh_result
->b_size
;
5656 em
= btrfs_new_extent_direct(inode
, em
, start
, len
);
5659 len
= min(len
, em
->len
- (start
- em
->start
));
5661 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
- 1,
5662 EXTENT_LOCKED
| EXTENT_DELALLOC
| EXTENT_DIRTY
, 1,
5665 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
5667 bh_result
->b_size
= len
;
5668 bh_result
->b_bdev
= em
->bdev
;
5669 set_buffer_mapped(bh_result
);
5670 if (create
&& !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
5671 set_buffer_new(bh_result
);
5673 free_extent_map(em
);
5678 struct btrfs_dio_private
{
5679 struct inode
*inode
;
5686 /* number of bios pending for this dio */
5687 atomic_t pending_bios
;
5692 struct bio
*orig_bio
;
5695 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
5697 struct btrfs_dio_private
*dip
= bio
->bi_private
;
5698 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
5699 struct bio_vec
*bvec
= bio
->bi_io_vec
;
5700 struct inode
*inode
= dip
->inode
;
5701 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5703 u32
*private = dip
->csums
;
5705 start
= dip
->logical_offset
;
5707 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
5708 struct page
*page
= bvec
->bv_page
;
5711 unsigned long flags
;
5713 local_irq_save(flags
);
5714 kaddr
= kmap_atomic(page
, KM_IRQ0
);
5715 csum
= btrfs_csum_data(root
, kaddr
+ bvec
->bv_offset
,
5716 csum
, bvec
->bv_len
);
5717 btrfs_csum_final(csum
, (char *)&csum
);
5718 kunmap_atomic(kaddr
, KM_IRQ0
);
5719 local_irq_restore(flags
);
5721 flush_dcache_page(bvec
->bv_page
);
5722 if (csum
!= *private) {
5723 printk(KERN_ERR
"btrfs csum failed ino %llu off"
5724 " %llu csum %u private %u\n",
5725 (unsigned long long)btrfs_ino(inode
),
5726 (unsigned long long)start
,
5732 start
+= bvec
->bv_len
;
5735 } while (bvec
<= bvec_end
);
5737 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
5738 dip
->logical_offset
+ dip
->bytes
- 1, GFP_NOFS
);
5739 bio
->bi_private
= dip
->private;
5744 /* If we had a csum failure make sure to clear the uptodate flag */
5746 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
5747 dio_end_io(bio
, err
);
5750 static void btrfs_endio_direct_write(struct bio
*bio
, int err
)
5752 struct btrfs_dio_private
*dip
= bio
->bi_private
;
5753 struct inode
*inode
= dip
->inode
;
5754 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5755 struct btrfs_trans_handle
*trans
;
5756 struct btrfs_ordered_extent
*ordered
= NULL
;
5757 struct extent_state
*cached_state
= NULL
;
5758 u64 ordered_offset
= dip
->logical_offset
;
5759 u64 ordered_bytes
= dip
->bytes
;
5765 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
5773 trans
= btrfs_join_transaction(root
);
5774 if (IS_ERR(trans
)) {
5778 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
5780 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
)) {
5781 ret
= btrfs_ordered_update_i_size(inode
, 0, ordered
);
5783 err
= btrfs_update_inode(trans
, root
, inode
);
5787 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, ordered
->file_offset
,
5788 ordered
->file_offset
+ ordered
->len
- 1, 0,
5789 &cached_state
, GFP_NOFS
);
5791 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
)) {
5792 ret
= btrfs_mark_extent_written(trans
, inode
,
5793 ordered
->file_offset
,
5794 ordered
->file_offset
+
5801 ret
= insert_reserved_file_extent(trans
, inode
,
5802 ordered
->file_offset
,
5808 BTRFS_FILE_EXTENT_REG
);
5809 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
5810 ordered
->file_offset
, ordered
->len
);
5818 add_pending_csums(trans
, inode
, ordered
->file_offset
, &ordered
->list
);
5819 ret
= btrfs_ordered_update_i_size(inode
, 0, ordered
);
5820 if (!ret
|| !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
))
5821 btrfs_update_inode(trans
, root
, inode
);
5824 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, ordered
->file_offset
,
5825 ordered
->file_offset
+ ordered
->len
- 1,
5826 &cached_state
, GFP_NOFS
);
5828 btrfs_delalloc_release_metadata(inode
, ordered
->len
);
5829 btrfs_end_transaction(trans
, root
);
5830 ordered_offset
= ordered
->file_offset
+ ordered
->len
;
5831 btrfs_put_ordered_extent(ordered
);
5832 btrfs_put_ordered_extent(ordered
);
5836 * our bio might span multiple ordered extents. If we haven't
5837 * completed the accounting for the whole dio, go back and try again
5839 if (ordered_offset
< dip
->logical_offset
+ dip
->bytes
) {
5840 ordered_bytes
= dip
->logical_offset
+ dip
->bytes
-
5845 bio
->bi_private
= dip
->private;
5850 /* If we had an error make sure to clear the uptodate flag */
5852 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
5853 dio_end_io(bio
, err
);
5856 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
5857 struct bio
*bio
, int mirror_num
,
5858 unsigned long bio_flags
, u64 offset
)
5861 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5862 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
5867 static void btrfs_end_dio_bio(struct bio
*bio
, int err
)
5869 struct btrfs_dio_private
*dip
= bio
->bi_private
;
5872 printk(KERN_ERR
"btrfs direct IO failed ino %llu rw %lu "
5873 "sector %#Lx len %u err no %d\n",
5874 (unsigned long long)btrfs_ino(dip
->inode
), bio
->bi_rw
,
5875 (unsigned long long)bio
->bi_sector
, bio
->bi_size
, err
);
5879 * before atomic variable goto zero, we must make sure
5880 * dip->errors is perceived to be set.
5882 smp_mb__before_atomic_dec();
5885 /* if there are more bios still pending for this dio, just exit */
5886 if (!atomic_dec_and_test(&dip
->pending_bios
))
5890 bio_io_error(dip
->orig_bio
);
5892 set_bit(BIO_UPTODATE
, &dip
->orig_bio
->bi_flags
);
5893 bio_endio(dip
->orig_bio
, 0);
5899 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
5900 u64 first_sector
, gfp_t gfp_flags
)
5902 int nr_vecs
= bio_get_nr_vecs(bdev
);
5903 return btrfs_bio_alloc(bdev
, first_sector
, nr_vecs
, gfp_flags
);
5906 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
5907 int rw
, u64 file_offset
, int skip_sum
,
5908 u32
*csums
, int async_submit
)
5910 int write
= rw
& REQ_WRITE
;
5911 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5915 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
5922 if (write
&& async_submit
) {
5923 ret
= btrfs_wq_submit_bio(root
->fs_info
,
5924 inode
, rw
, bio
, 0, 0,
5926 __btrfs_submit_bio_start_direct_io
,
5927 __btrfs_submit_bio_done
);
5931 * If we aren't doing async submit, calculate the csum of the
5934 ret
= btrfs_csum_one_bio(root
, inode
, bio
, file_offset
, 1);
5937 } else if (!skip_sum
) {
5938 ret
= btrfs_lookup_bio_sums_dio(root
, inode
, bio
,
5939 file_offset
, csums
);
5945 ret
= btrfs_map_bio(root
, rw
, bio
, 0, async_submit
);
5951 static int btrfs_submit_direct_hook(int rw
, struct btrfs_dio_private
*dip
,
5954 struct inode
*inode
= dip
->inode
;
5955 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5956 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
5958 struct bio
*orig_bio
= dip
->orig_bio
;
5959 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
5960 u64 start_sector
= orig_bio
->bi_sector
;
5961 u64 file_offset
= dip
->logical_offset
;
5965 u32
*csums
= dip
->csums
;
5967 int async_submit
= 0;
5968 int write
= rw
& REQ_WRITE
;
5970 map_length
= orig_bio
->bi_size
;
5971 ret
= btrfs_map_block(map_tree
, READ
, start_sector
<< 9,
5972 &map_length
, NULL
, 0);
5978 if (map_length
>= orig_bio
->bi_size
) {
5984 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
5987 bio
->bi_private
= dip
;
5988 bio
->bi_end_io
= btrfs_end_dio_bio
;
5989 atomic_inc(&dip
->pending_bios
);
5991 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
5992 if (unlikely(map_length
< submit_len
+ bvec
->bv_len
||
5993 bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
5994 bvec
->bv_offset
) < bvec
->bv_len
)) {
5996 * inc the count before we submit the bio so
5997 * we know the end IO handler won't happen before
5998 * we inc the count. Otherwise, the dip might get freed
5999 * before we're done setting it up
6001 atomic_inc(&dip
->pending_bios
);
6002 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
,
6003 file_offset
, skip_sum
,
6004 csums
, async_submit
);
6007 atomic_dec(&dip
->pending_bios
);
6011 /* Write's use the ordered csums */
6012 if (!write
&& !skip_sum
)
6013 csums
= csums
+ nr_pages
;
6014 start_sector
+= submit_len
>> 9;
6015 file_offset
+= submit_len
;
6020 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
6021 start_sector
, GFP_NOFS
);
6024 bio
->bi_private
= dip
;
6025 bio
->bi_end_io
= btrfs_end_dio_bio
;
6027 map_length
= orig_bio
->bi_size
;
6028 ret
= btrfs_map_block(map_tree
, READ
, start_sector
<< 9,
6029 &map_length
, NULL
, 0);
6035 submit_len
+= bvec
->bv_len
;
6042 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
, file_offset
, skip_sum
,
6043 csums
, async_submit
);
6051 * before atomic variable goto zero, we must
6052 * make sure dip->errors is perceived to be set.
6054 smp_mb__before_atomic_dec();
6055 if (atomic_dec_and_test(&dip
->pending_bios
))
6056 bio_io_error(dip
->orig_bio
);
6058 /* bio_end_io() will handle error, so we needn't return it */
6062 static void btrfs_submit_direct(int rw
, struct bio
*bio
, struct inode
*inode
,
6065 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6066 struct btrfs_dio_private
*dip
;
6067 struct bio_vec
*bvec
= bio
->bi_io_vec
;
6069 int write
= rw
& REQ_WRITE
;
6072 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
6074 dip
= kmalloc(sizeof(*dip
), GFP_NOFS
);
6081 /* Write's use the ordered csum stuff, so we don't need dip->csums */
6082 if (!write
&& !skip_sum
) {
6083 dip
->csums
= kmalloc(sizeof(u32
) * bio
->bi_vcnt
, GFP_NOFS
);
6091 dip
->private = bio
->bi_private
;
6093 dip
->logical_offset
= file_offset
;
6097 dip
->bytes
+= bvec
->bv_len
;
6099 } while (bvec
<= (bio
->bi_io_vec
+ bio
->bi_vcnt
- 1));
6101 dip
->disk_bytenr
= (u64
)bio
->bi_sector
<< 9;
6102 bio
->bi_private
= dip
;
6104 dip
->orig_bio
= bio
;
6105 atomic_set(&dip
->pending_bios
, 0);
6108 bio
->bi_end_io
= btrfs_endio_direct_write
;
6110 bio
->bi_end_io
= btrfs_endio_direct_read
;
6112 ret
= btrfs_submit_direct_hook(rw
, dip
, skip_sum
);
6117 * If this is a write, we need to clean up the reserved space and kill
6118 * the ordered extent.
6121 struct btrfs_ordered_extent
*ordered
;
6122 ordered
= btrfs_lookup_ordered_extent(inode
, file_offset
);
6123 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
6124 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
6125 btrfs_free_reserved_extent(root
, ordered
->start
,
6127 btrfs_put_ordered_extent(ordered
);
6128 btrfs_put_ordered_extent(ordered
);
6130 bio_endio(bio
, ret
);
6133 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
6134 const struct iovec
*iov
, loff_t offset
,
6135 unsigned long nr_segs
)
6141 unsigned blocksize_mask
= root
->sectorsize
- 1;
6142 ssize_t retval
= -EINVAL
;
6143 loff_t end
= offset
;
6145 if (offset
& blocksize_mask
)
6148 /* Check the memory alignment. Blocks cannot straddle pages */
6149 for (seg
= 0; seg
< nr_segs
; seg
++) {
6150 addr
= (unsigned long)iov
[seg
].iov_base
;
6151 size
= iov
[seg
].iov_len
;
6153 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
6156 /* If this is a write we don't need to check anymore */
6161 * Check to make sure we don't have duplicate iov_base's in this
6162 * iovec, if so return EINVAL, otherwise we'll get csum errors
6163 * when reading back.
6165 for (i
= seg
+ 1; i
< nr_segs
; i
++) {
6166 if (iov
[seg
].iov_base
== iov
[i
].iov_base
)
6174 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
6175 const struct iovec
*iov
, loff_t offset
,
6176 unsigned long nr_segs
)
6178 struct file
*file
= iocb
->ki_filp
;
6179 struct inode
*inode
= file
->f_mapping
->host
;
6180 struct btrfs_ordered_extent
*ordered
;
6181 struct extent_state
*cached_state
= NULL
;
6182 u64 lockstart
, lockend
;
6184 int writing
= rw
& WRITE
;
6186 size_t count
= iov_length(iov
, nr_segs
);
6188 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iov
,
6194 lockend
= offset
+ count
- 1;
6197 ret
= btrfs_delalloc_reserve_space(inode
, count
);
6203 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6204 0, &cached_state
, GFP_NOFS
);
6206 * We're concerned with the entire range that we're going to be
6207 * doing DIO to, so we need to make sure theres no ordered
6208 * extents in this range.
6210 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
6211 lockend
- lockstart
+ 1);
6214 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6215 &cached_state
, GFP_NOFS
);
6216 btrfs_start_ordered_extent(inode
, ordered
, 1);
6217 btrfs_put_ordered_extent(ordered
);
6222 * we don't use btrfs_set_extent_delalloc because we don't want
6223 * the dirty or uptodate bits
6226 write_bits
= EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
;
6227 ret
= set_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6228 EXTENT_DELALLOC
, 0, NULL
, &cached_state
,
6231 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6232 lockend
, EXTENT_LOCKED
| write_bits
,
6233 1, 0, &cached_state
, GFP_NOFS
);
6238 free_extent_state(cached_state
);
6239 cached_state
= NULL
;
6241 ret
= __blockdev_direct_IO(rw
, iocb
, inode
,
6242 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
6243 iov
, offset
, nr_segs
, btrfs_get_blocks_direct
, NULL
,
6244 btrfs_submit_direct
, 0);
6246 if (ret
< 0 && ret
!= -EIOCBQUEUED
) {
6247 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, offset
,
6248 offset
+ iov_length(iov
, nr_segs
) - 1,
6249 EXTENT_LOCKED
| write_bits
, 1, 0,
6250 &cached_state
, GFP_NOFS
);
6251 } else if (ret
>= 0 && ret
< iov_length(iov
, nr_segs
)) {
6253 * We're falling back to buffered, unlock the section we didn't
6256 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, offset
+ ret
,
6257 offset
+ iov_length(iov
, nr_segs
) - 1,
6258 EXTENT_LOCKED
| write_bits
, 1, 0,
6259 &cached_state
, GFP_NOFS
);
6262 free_extent_state(cached_state
);
6266 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
6267 __u64 start
, __u64 len
)
6269 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
6272 int btrfs_readpage(struct file
*file
, struct page
*page
)
6274 struct extent_io_tree
*tree
;
6275 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6276 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
6279 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
6281 struct extent_io_tree
*tree
;
6284 if (current
->flags
& PF_MEMALLOC
) {
6285 redirty_page_for_writepage(wbc
, page
);
6289 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6290 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
6293 int btrfs_writepages(struct address_space
*mapping
,
6294 struct writeback_control
*wbc
)
6296 struct extent_io_tree
*tree
;
6298 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
6299 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
6303 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
6304 struct list_head
*pages
, unsigned nr_pages
)
6306 struct extent_io_tree
*tree
;
6307 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
6308 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
6311 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
6313 struct extent_io_tree
*tree
;
6314 struct extent_map_tree
*map
;
6317 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6318 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
6319 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
6321 ClearPagePrivate(page
);
6322 set_page_private(page
, 0);
6323 page_cache_release(page
);
6328 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
6330 if (PageWriteback(page
) || PageDirty(page
))
6332 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
6335 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
6337 struct extent_io_tree
*tree
;
6338 struct btrfs_ordered_extent
*ordered
;
6339 struct extent_state
*cached_state
= NULL
;
6340 u64 page_start
= page_offset(page
);
6341 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6345 * we have the page locked, so new writeback can't start,
6346 * and the dirty bit won't be cleared while we are here.
6348 * Wait for IO on this page so that we can safely clear
6349 * the PagePrivate2 bit and do ordered accounting
6351 wait_on_page_writeback(page
);
6353 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6355 btrfs_releasepage(page
, GFP_NOFS
);
6358 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
,
6360 ordered
= btrfs_lookup_ordered_extent(page
->mapping
->host
,
6364 * IO on this page will never be started, so we need
6365 * to account for any ordered extents now
6367 clear_extent_bit(tree
, page_start
, page_end
,
6368 EXTENT_DIRTY
| EXTENT_DELALLOC
|
6369 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
, 1, 0,
6370 &cached_state
, GFP_NOFS
);
6372 * whoever cleared the private bit is responsible
6373 * for the finish_ordered_io
6375 if (TestClearPagePrivate2(page
)) {
6376 btrfs_finish_ordered_io(page
->mapping
->host
,
6377 page_start
, page_end
);
6379 btrfs_put_ordered_extent(ordered
);
6380 cached_state
= NULL
;
6381 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
,
6384 clear_extent_bit(tree
, page_start
, page_end
,
6385 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
6386 EXTENT_DO_ACCOUNTING
, 1, 1, &cached_state
, GFP_NOFS
);
6387 __btrfs_releasepage(page
, GFP_NOFS
);
6389 ClearPageChecked(page
);
6390 if (PagePrivate(page
)) {
6391 ClearPagePrivate(page
);
6392 set_page_private(page
, 0);
6393 page_cache_release(page
);
6398 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6399 * called from a page fault handler when a page is first dirtied. Hence we must
6400 * be careful to check for EOF conditions here. We set the page up correctly
6401 * for a written page which means we get ENOSPC checking when writing into
6402 * holes and correct delalloc and unwritten extent mapping on filesystems that
6403 * support these features.
6405 * We are not allowed to take the i_mutex here so we have to play games to
6406 * protect against truncate races as the page could now be beyond EOF. Because
6407 * vmtruncate() writes the inode size before removing pages, once we have the
6408 * page lock we can determine safely if the page is beyond EOF. If it is not
6409 * beyond EOF, then the page is guaranteed safe against truncation until we
6412 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
6414 struct page
*page
= vmf
->page
;
6415 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
6416 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6417 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6418 struct btrfs_ordered_extent
*ordered
;
6419 struct extent_state
*cached_state
= NULL
;
6421 unsigned long zero_start
;
6427 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
6431 else /* -ENOSPC, -EIO, etc */
6432 ret
= VM_FAULT_SIGBUS
;
6436 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
6439 size
= i_size_read(inode
);
6440 page_start
= page_offset(page
);
6441 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6443 if ((page
->mapping
!= inode
->i_mapping
) ||
6444 (page_start
>= size
)) {
6445 /* page got truncated out from underneath us */
6448 wait_on_page_writeback(page
);
6450 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
,
6452 set_page_extent_mapped(page
);
6455 * we can't set the delalloc bits if there are pending ordered
6456 * extents. Drop our locks and wait for them to finish
6458 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
6460 unlock_extent_cached(io_tree
, page_start
, page_end
,
6461 &cached_state
, GFP_NOFS
);
6463 btrfs_start_ordered_extent(inode
, ordered
, 1);
6464 btrfs_put_ordered_extent(ordered
);
6469 * XXX - page_mkwrite gets called every time the page is dirtied, even
6470 * if it was already dirty, so for space accounting reasons we need to
6471 * clear any delalloc bits for the range we are fixing to save. There
6472 * is probably a better way to do this, but for now keep consistent with
6473 * prepare_pages in the normal write path.
6475 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
6476 EXTENT_DIRTY
| EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
,
6477 0, 0, &cached_state
, GFP_NOFS
);
6479 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
6482 unlock_extent_cached(io_tree
, page_start
, page_end
,
6483 &cached_state
, GFP_NOFS
);
6484 ret
= VM_FAULT_SIGBUS
;
6489 /* page is wholly or partially inside EOF */
6490 if (page_start
+ PAGE_CACHE_SIZE
> size
)
6491 zero_start
= size
& ~PAGE_CACHE_MASK
;
6493 zero_start
= PAGE_CACHE_SIZE
;
6495 if (zero_start
!= PAGE_CACHE_SIZE
) {
6497 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
6498 flush_dcache_page(page
);
6501 ClearPageChecked(page
);
6502 set_page_dirty(page
);
6503 SetPageUptodate(page
);
6505 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
6506 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
6508 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
6512 return VM_FAULT_LOCKED
;
6514 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
6519 static int btrfs_truncate(struct inode
*inode
)
6521 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6522 struct btrfs_block_rsv
*rsv
;
6525 struct btrfs_trans_handle
*trans
;
6527 u64 mask
= root
->sectorsize
- 1;
6528 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
6530 ret
= btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
6534 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
6535 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
6538 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6539 * 3 things going on here
6541 * 1) We need to reserve space for our orphan item and the space to
6542 * delete our orphan item. Lord knows we don't want to have a dangling
6543 * orphan item because we didn't reserve space to remove it.
6545 * 2) We need to reserve space to update our inode.
6547 * 3) We need to have something to cache all the space that is going to
6548 * be free'd up by the truncate operation, but also have some slack
6549 * space reserved in case it uses space during the truncate (thank you
6550 * very much snapshotting).
6552 * And we need these to all be seperate. The fact is we can use alot of
6553 * space doing the truncate, and we have no earthly idea how much space
6554 * we will use, so we need the truncate reservation to be seperate so it
6555 * doesn't end up using space reserved for updating the inode or
6556 * removing the orphan item. We also need to be able to stop the
6557 * transaction and start a new one, which means we need to be able to
6558 * update the inode several times, and we have no idea of knowing how
6559 * many times that will be, so we can't just reserve 1 item for the
6560 * entirety of the opration, so that has to be done seperately as well.
6561 * Then there is the orphan item, which does indeed need to be held on
6562 * to for the whole operation, and we need nobody to touch this reserved
6563 * space except the orphan code.
6565 * So that leaves us with
6567 * 1) root->orphan_block_rsv - for the orphan deletion.
6568 * 2) rsv - for the truncate reservation, which we will steal from the
6569 * transaction reservation.
6570 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6571 * updating the inode.
6573 rsv
= btrfs_alloc_block_rsv(root
);
6576 rsv
->size
= min_size
;
6579 * 1 for the truncate slack space
6580 * 1 for the orphan item we're going to add
6581 * 1 for the orphan item deletion
6582 * 1 for updating the inode.
6584 trans
= btrfs_start_transaction(root
, 4);
6585 if (IS_ERR(trans
)) {
6586 err
= PTR_ERR(trans
);
6590 /* Migrate the slack space for the truncate to our reserve */
6591 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
, rsv
,
6595 ret
= btrfs_orphan_add(trans
, inode
);
6597 btrfs_end_transaction(trans
, root
);
6602 * setattr is responsible for setting the ordered_data_close flag,
6603 * but that is only tested during the last file release. That
6604 * could happen well after the next commit, leaving a great big
6605 * window where new writes may get lost if someone chooses to write
6606 * to this file after truncating to zero
6608 * The inode doesn't have any dirty data here, and so if we commit
6609 * this is a noop. If someone immediately starts writing to the inode
6610 * it is very likely we'll catch some of their writes in this
6611 * transaction, and the commit will find this file on the ordered
6612 * data list with good things to send down.
6614 * This is a best effort solution, there is still a window where
6615 * using truncate to replace the contents of the file will
6616 * end up with a zero length file after a crash.
6618 if (inode
->i_size
== 0 && BTRFS_I(inode
)->ordered_data_close
)
6619 btrfs_add_ordered_operation(trans
, root
, inode
);
6622 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
);
6625 * This can only happen with the original transaction we
6626 * started above, every other time we shouldn't have a
6627 * transaction started yet.
6636 /* Just need the 1 for updating the inode */
6637 trans
= btrfs_start_transaction(root
, 1);
6638 if (IS_ERR(trans
)) {
6639 err
= PTR_ERR(trans
);
6644 trans
->block_rsv
= rsv
;
6646 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
6648 BTRFS_EXTENT_DATA_KEY
);
6649 if (ret
!= -EAGAIN
) {
6654 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
6655 ret
= btrfs_update_inode(trans
, root
, inode
);
6661 nr
= trans
->blocks_used
;
6662 btrfs_end_transaction(trans
, root
);
6664 btrfs_btree_balance_dirty(root
, nr
);
6667 if (ret
== 0 && inode
->i_nlink
> 0) {
6668 trans
->block_rsv
= root
->orphan_block_rsv
;
6669 ret
= btrfs_orphan_del(trans
, inode
);
6672 } else if (ret
&& inode
->i_nlink
> 0) {
6674 * Failed to do the truncate, remove us from the in memory
6677 ret
= btrfs_orphan_del(NULL
, inode
);
6680 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
6681 ret
= btrfs_update_inode(trans
, root
, inode
);
6685 nr
= trans
->blocks_used
;
6686 ret
= btrfs_end_transaction_throttle(trans
, root
);
6687 btrfs_btree_balance_dirty(root
, nr
);
6690 btrfs_free_block_rsv(root
, rsv
);
6699 * create a new subvolume directory/inode (helper for the ioctl).
6701 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
6702 struct btrfs_root
*new_root
, u64 new_dirid
)
6704 struct inode
*inode
;
6708 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2, new_dirid
,
6709 new_dirid
, S_IFDIR
| 0700, &index
);
6711 return PTR_ERR(inode
);
6712 inode
->i_op
= &btrfs_dir_inode_operations
;
6713 inode
->i_fop
= &btrfs_dir_file_operations
;
6716 btrfs_i_size_write(inode
, 0);
6718 err
= btrfs_update_inode(trans
, new_root
, inode
);
6725 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
6727 struct btrfs_inode
*ei
;
6728 struct inode
*inode
;
6730 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
6735 ei
->space_info
= NULL
;
6739 ei
->last_sub_trans
= 0;
6740 ei
->logged_trans
= 0;
6741 ei
->delalloc_bytes
= 0;
6742 ei
->disk_i_size
= 0;
6745 ei
->index_cnt
= (u64
)-1;
6746 ei
->last_unlink_trans
= 0;
6748 spin_lock_init(&ei
->lock
);
6749 ei
->outstanding_extents
= 0;
6750 ei
->reserved_extents
= 0;
6752 ei
->ordered_data_close
= 0;
6753 ei
->orphan_meta_reserved
= 0;
6754 ei
->dummy_inode
= 0;
6756 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
6758 ei
->delayed_node
= NULL
;
6760 inode
= &ei
->vfs_inode
;
6761 extent_map_tree_init(&ei
->extent_tree
);
6762 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
);
6763 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
);
6764 mutex_init(&ei
->log_mutex
);
6765 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
6766 INIT_LIST_HEAD(&ei
->i_orphan
);
6767 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
6768 INIT_LIST_HEAD(&ei
->ordered_operations
);
6769 RB_CLEAR_NODE(&ei
->rb_node
);
6774 static void btrfs_i_callback(struct rcu_head
*head
)
6776 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
6777 INIT_LIST_HEAD(&inode
->i_dentry
);
6778 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
6781 void btrfs_destroy_inode(struct inode
*inode
)
6783 struct btrfs_ordered_extent
*ordered
;
6784 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6786 WARN_ON(!list_empty(&inode
->i_dentry
));
6787 WARN_ON(inode
->i_data
.nrpages
);
6788 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
6789 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
6790 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
6791 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
6794 * This can happen where we create an inode, but somebody else also
6795 * created the same inode and we need to destroy the one we already
6802 * Make sure we're properly removed from the ordered operation
6806 if (!list_empty(&BTRFS_I(inode
)->ordered_operations
)) {
6807 spin_lock(&root
->fs_info
->ordered_extent_lock
);
6808 list_del_init(&BTRFS_I(inode
)->ordered_operations
);
6809 spin_unlock(&root
->fs_info
->ordered_extent_lock
);
6812 spin_lock(&root
->orphan_lock
);
6813 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
6814 printk(KERN_INFO
"BTRFS: inode %llu still on the orphan list\n",
6815 (unsigned long long)btrfs_ino(inode
));
6816 list_del_init(&BTRFS_I(inode
)->i_orphan
);
6818 spin_unlock(&root
->orphan_lock
);
6821 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
6825 printk(KERN_ERR
"btrfs found ordered "
6826 "extent %llu %llu on inode cleanup\n",
6827 (unsigned long long)ordered
->file_offset
,
6828 (unsigned long long)ordered
->len
);
6829 btrfs_remove_ordered_extent(inode
, ordered
);
6830 btrfs_put_ordered_extent(ordered
);
6831 btrfs_put_ordered_extent(ordered
);
6834 inode_tree_del(inode
);
6835 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
6837 btrfs_remove_delayed_node(inode
);
6838 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
6841 int btrfs_drop_inode(struct inode
*inode
)
6843 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6845 if (btrfs_root_refs(&root
->root_item
) == 0 &&
6846 !btrfs_is_free_space_inode(root
, inode
))
6849 return generic_drop_inode(inode
);
6852 static void init_once(void *foo
)
6854 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
6856 inode_init_once(&ei
->vfs_inode
);
6859 void btrfs_destroy_cachep(void)
6861 if (btrfs_inode_cachep
)
6862 kmem_cache_destroy(btrfs_inode_cachep
);
6863 if (btrfs_trans_handle_cachep
)
6864 kmem_cache_destroy(btrfs_trans_handle_cachep
);
6865 if (btrfs_transaction_cachep
)
6866 kmem_cache_destroy(btrfs_transaction_cachep
);
6867 if (btrfs_path_cachep
)
6868 kmem_cache_destroy(btrfs_path_cachep
);
6869 if (btrfs_free_space_cachep
)
6870 kmem_cache_destroy(btrfs_free_space_cachep
);
6873 int btrfs_init_cachep(void)
6875 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode_cache",
6876 sizeof(struct btrfs_inode
), 0,
6877 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
6878 if (!btrfs_inode_cachep
)
6881 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle_cache",
6882 sizeof(struct btrfs_trans_handle
), 0,
6883 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6884 if (!btrfs_trans_handle_cachep
)
6887 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction_cache",
6888 sizeof(struct btrfs_transaction
), 0,
6889 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6890 if (!btrfs_transaction_cachep
)
6893 btrfs_path_cachep
= kmem_cache_create("btrfs_path_cache",
6894 sizeof(struct btrfs_path
), 0,
6895 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6896 if (!btrfs_path_cachep
)
6899 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space_cache",
6900 sizeof(struct btrfs_free_space
), 0,
6901 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
6902 if (!btrfs_free_space_cachep
)
6907 btrfs_destroy_cachep();
6911 static int btrfs_getattr(struct vfsmount
*mnt
,
6912 struct dentry
*dentry
, struct kstat
*stat
)
6914 struct inode
*inode
= dentry
->d_inode
;
6915 generic_fillattr(inode
, stat
);
6916 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
6917 stat
->blksize
= PAGE_CACHE_SIZE
;
6918 stat
->blocks
= (inode_get_bytes(inode
) +
6919 BTRFS_I(inode
)->delalloc_bytes
) >> 9;
6924 * If a file is moved, it will inherit the cow and compression flags of the new
6927 static void fixup_inode_flags(struct inode
*dir
, struct inode
*inode
)
6929 struct btrfs_inode
*b_dir
= BTRFS_I(dir
);
6930 struct btrfs_inode
*b_inode
= BTRFS_I(inode
);
6932 if (b_dir
->flags
& BTRFS_INODE_NODATACOW
)
6933 b_inode
->flags
|= BTRFS_INODE_NODATACOW
;
6935 b_inode
->flags
&= ~BTRFS_INODE_NODATACOW
;
6937 if (b_dir
->flags
& BTRFS_INODE_COMPRESS
)
6938 b_inode
->flags
|= BTRFS_INODE_COMPRESS
;
6940 b_inode
->flags
&= ~BTRFS_INODE_COMPRESS
;
6943 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
6944 struct inode
*new_dir
, struct dentry
*new_dentry
)
6946 struct btrfs_trans_handle
*trans
;
6947 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
6948 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
6949 struct inode
*new_inode
= new_dentry
->d_inode
;
6950 struct inode
*old_inode
= old_dentry
->d_inode
;
6951 struct timespec ctime
= CURRENT_TIME
;
6955 u64 old_ino
= btrfs_ino(old_inode
);
6957 if (btrfs_ino(new_dir
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
6960 /* we only allow rename subvolume link between subvolumes */
6961 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
6964 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
6965 (new_inode
&& btrfs_ino(new_inode
) == BTRFS_FIRST_FREE_OBJECTID
))
6968 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
6969 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
6972 * we're using rename to replace one file with another.
6973 * and the replacement file is large. Start IO on it now so
6974 * we don't add too much work to the end of the transaction
6976 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
&&
6977 old_inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
6978 filemap_flush(old_inode
->i_mapping
);
6980 /* close the racy window with snapshot create/destroy ioctl */
6981 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
6982 down_read(&root
->fs_info
->subvol_sem
);
6984 * We want to reserve the absolute worst case amount of items. So if
6985 * both inodes are subvols and we need to unlink them then that would
6986 * require 4 item modifications, but if they are both normal inodes it
6987 * would require 5 item modifications, so we'll assume their normal
6988 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
6989 * should cover the worst case number of items we'll modify.
6991 trans
= btrfs_start_transaction(root
, 20);
6992 if (IS_ERR(trans
)) {
6993 ret
= PTR_ERR(trans
);
6998 btrfs_record_root_in_trans(trans
, dest
);
7000 ret
= btrfs_set_inode_index(new_dir
, &index
);
7004 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
7005 /* force full log commit if subvolume involved. */
7006 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
7008 ret
= btrfs_insert_inode_ref(trans
, dest
,
7009 new_dentry
->d_name
.name
,
7010 new_dentry
->d_name
.len
,
7012 btrfs_ino(new_dir
), index
);
7016 * this is an ugly little race, but the rename is required
7017 * to make sure that if we crash, the inode is either at the
7018 * old name or the new one. pinning the log transaction lets
7019 * us make sure we don't allow a log commit to come in after
7020 * we unlink the name but before we add the new name back in.
7022 btrfs_pin_log_trans(root
);
7025 * make sure the inode gets flushed if it is replacing
7028 if (new_inode
&& new_inode
->i_size
&& S_ISREG(old_inode
->i_mode
))
7029 btrfs_add_ordered_operation(trans
, root
, old_inode
);
7031 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
7032 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
7033 old_inode
->i_ctime
= ctime
;
7035 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
7036 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
7038 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
7039 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
7040 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
7041 old_dentry
->d_name
.name
,
7042 old_dentry
->d_name
.len
);
7044 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
7045 old_dentry
->d_inode
,
7046 old_dentry
->d_name
.name
,
7047 old_dentry
->d_name
.len
);
7049 ret
= btrfs_update_inode(trans
, root
, old_inode
);
7054 new_inode
->i_ctime
= CURRENT_TIME
;
7055 if (unlikely(btrfs_ino(new_inode
) ==
7056 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
7057 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
7058 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
7060 new_dentry
->d_name
.name
,
7061 new_dentry
->d_name
.len
);
7062 BUG_ON(new_inode
->i_nlink
== 0);
7064 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
7065 new_dentry
->d_inode
,
7066 new_dentry
->d_name
.name
,
7067 new_dentry
->d_name
.len
);
7070 if (new_inode
->i_nlink
== 0) {
7071 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
7076 fixup_inode_flags(new_dir
, old_inode
);
7078 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
7079 new_dentry
->d_name
.name
,
7080 new_dentry
->d_name
.len
, 0, index
);
7083 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
7084 struct dentry
*parent
= new_dentry
->d_parent
;
7085 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
7086 btrfs_end_log_trans(root
);
7089 btrfs_end_transaction_throttle(trans
, root
);
7091 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
7092 up_read(&root
->fs_info
->subvol_sem
);
7098 * some fairly slow code that needs optimization. This walks the list
7099 * of all the inodes with pending delalloc and forces them to disk.
7101 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
7103 struct list_head
*head
= &root
->fs_info
->delalloc_inodes
;
7104 struct btrfs_inode
*binode
;
7105 struct inode
*inode
;
7107 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
7110 spin_lock(&root
->fs_info
->delalloc_lock
);
7111 while (!list_empty(head
)) {
7112 binode
= list_entry(head
->next
, struct btrfs_inode
,
7114 inode
= igrab(&binode
->vfs_inode
);
7116 list_del_init(&binode
->delalloc_inodes
);
7117 spin_unlock(&root
->fs_info
->delalloc_lock
);
7119 filemap_flush(inode
->i_mapping
);
7121 btrfs_add_delayed_iput(inode
);
7126 spin_lock(&root
->fs_info
->delalloc_lock
);
7128 spin_unlock(&root
->fs_info
->delalloc_lock
);
7130 /* the filemap_flush will queue IO into the worker threads, but
7131 * we have to make sure the IO is actually started and that
7132 * ordered extents get created before we return
7134 atomic_inc(&root
->fs_info
->async_submit_draining
);
7135 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
7136 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
7137 wait_event(root
->fs_info
->async_submit_wait
,
7138 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
7139 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
7141 atomic_dec(&root
->fs_info
->async_submit_draining
);
7145 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
7146 const char *symname
)
7148 struct btrfs_trans_handle
*trans
;
7149 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
7150 struct btrfs_path
*path
;
7151 struct btrfs_key key
;
7152 struct inode
*inode
= NULL
;
7160 struct btrfs_file_extent_item
*ei
;
7161 struct extent_buffer
*leaf
;
7162 unsigned long nr
= 0;
7164 name_len
= strlen(symname
) + 1;
7165 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
7166 return -ENAMETOOLONG
;
7169 * 2 items for inode item and ref
7170 * 2 items for dir items
7171 * 1 item for xattr if selinux is on
7173 trans
= btrfs_start_transaction(root
, 5);
7175 return PTR_ERR(trans
);
7177 err
= btrfs_find_free_ino(root
, &objectid
);
7181 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
7182 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
7183 S_IFLNK
|S_IRWXUGO
, &index
);
7184 if (IS_ERR(inode
)) {
7185 err
= PTR_ERR(inode
);
7189 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
7195 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
7199 inode
->i_mapping
->a_ops
= &btrfs_aops
;
7200 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
7201 inode
->i_fop
= &btrfs_file_operations
;
7202 inode
->i_op
= &btrfs_file_inode_operations
;
7203 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
7208 path
= btrfs_alloc_path();
7214 key
.objectid
= btrfs_ino(inode
);
7216 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
7217 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
7218 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
7222 btrfs_free_path(path
);
7225 leaf
= path
->nodes
[0];
7226 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
7227 struct btrfs_file_extent_item
);
7228 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
7229 btrfs_set_file_extent_type(leaf
, ei
,
7230 BTRFS_FILE_EXTENT_INLINE
);
7231 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
7232 btrfs_set_file_extent_compression(leaf
, ei
, 0);
7233 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
7234 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
7236 ptr
= btrfs_file_extent_inline_start(ei
);
7237 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
7238 btrfs_mark_buffer_dirty(leaf
);
7239 btrfs_free_path(path
);
7241 inode
->i_op
= &btrfs_symlink_inode_operations
;
7242 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
7243 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
7244 inode_set_bytes(inode
, name_len
);
7245 btrfs_i_size_write(inode
, name_len
- 1);
7246 err
= btrfs_update_inode(trans
, root
, inode
);
7251 nr
= trans
->blocks_used
;
7252 btrfs_end_transaction_throttle(trans
, root
);
7254 inode_dec_link_count(inode
);
7257 btrfs_btree_balance_dirty(root
, nr
);
7261 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
7262 u64 start
, u64 num_bytes
, u64 min_size
,
7263 loff_t actual_len
, u64
*alloc_hint
,
7264 struct btrfs_trans_handle
*trans
)
7266 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7267 struct btrfs_key ins
;
7268 u64 cur_offset
= start
;
7271 bool own_trans
= true;
7275 while (num_bytes
> 0) {
7277 trans
= btrfs_start_transaction(root
, 3);
7278 if (IS_ERR(trans
)) {
7279 ret
= PTR_ERR(trans
);
7284 ret
= btrfs_reserve_extent(trans
, root
, num_bytes
, min_size
,
7285 0, *alloc_hint
, (u64
)-1, &ins
, 1);
7288 btrfs_end_transaction(trans
, root
);
7292 ret
= insert_reserved_file_extent(trans
, inode
,
7293 cur_offset
, ins
.objectid
,
7294 ins
.offset
, ins
.offset
,
7295 ins
.offset
, 0, 0, 0,
7296 BTRFS_FILE_EXTENT_PREALLOC
);
7298 btrfs_drop_extent_cache(inode
, cur_offset
,
7299 cur_offset
+ ins
.offset
-1, 0);
7301 num_bytes
-= ins
.offset
;
7302 cur_offset
+= ins
.offset
;
7303 *alloc_hint
= ins
.objectid
+ ins
.offset
;
7305 inode
->i_ctime
= CURRENT_TIME
;
7306 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
7307 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
7308 (actual_len
> inode
->i_size
) &&
7309 (cur_offset
> inode
->i_size
)) {
7310 if (cur_offset
> actual_len
)
7311 i_size
= actual_len
;
7313 i_size
= cur_offset
;
7314 i_size_write(inode
, i_size
);
7315 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
7318 ret
= btrfs_update_inode(trans
, root
, inode
);
7322 btrfs_end_transaction(trans
, root
);
7327 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
7328 u64 start
, u64 num_bytes
, u64 min_size
,
7329 loff_t actual_len
, u64
*alloc_hint
)
7331 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
7332 min_size
, actual_len
, alloc_hint
,
7336 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
7337 struct btrfs_trans_handle
*trans
, int mode
,
7338 u64 start
, u64 num_bytes
, u64 min_size
,
7339 loff_t actual_len
, u64
*alloc_hint
)
7341 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
7342 min_size
, actual_len
, alloc_hint
, trans
);
7345 static int btrfs_set_page_dirty(struct page
*page
)
7347 return __set_page_dirty_nobuffers(page
);
7350 static int btrfs_permission(struct inode
*inode
, int mask
)
7352 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7353 umode_t mode
= inode
->i_mode
;
7355 if (mask
& MAY_WRITE
&&
7356 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
7357 if (btrfs_root_readonly(root
))
7359 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
7362 return generic_permission(inode
, mask
);
7365 static const struct inode_operations btrfs_dir_inode_operations
= {
7366 .getattr
= btrfs_getattr
,
7367 .lookup
= btrfs_lookup
,
7368 .create
= btrfs_create
,
7369 .unlink
= btrfs_unlink
,
7371 .mkdir
= btrfs_mkdir
,
7372 .rmdir
= btrfs_rmdir
,
7373 .rename
= btrfs_rename
,
7374 .symlink
= btrfs_symlink
,
7375 .setattr
= btrfs_setattr
,
7376 .mknod
= btrfs_mknod
,
7377 .setxattr
= btrfs_setxattr
,
7378 .getxattr
= btrfs_getxattr
,
7379 .listxattr
= btrfs_listxattr
,
7380 .removexattr
= btrfs_removexattr
,
7381 .permission
= btrfs_permission
,
7382 .get_acl
= btrfs_get_acl
,
7384 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
7385 .lookup
= btrfs_lookup
,
7386 .permission
= btrfs_permission
,
7387 .get_acl
= btrfs_get_acl
,
7390 static const struct file_operations btrfs_dir_file_operations
= {
7391 .llseek
= generic_file_llseek
,
7392 .read
= generic_read_dir
,
7393 .readdir
= btrfs_real_readdir
,
7394 .unlocked_ioctl
= btrfs_ioctl
,
7395 #ifdef CONFIG_COMPAT
7396 .compat_ioctl
= btrfs_ioctl
,
7398 .release
= btrfs_release_file
,
7399 .fsync
= btrfs_sync_file
,
7402 static struct extent_io_ops btrfs_extent_io_ops
= {
7403 .fill_delalloc
= run_delalloc_range
,
7404 .submit_bio_hook
= btrfs_submit_bio_hook
,
7405 .merge_bio_hook
= btrfs_merge_bio_hook
,
7406 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
7407 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
7408 .writepage_start_hook
= btrfs_writepage_start_hook
,
7409 .readpage_io_failed_hook
= btrfs_io_failed_hook
,
7410 .set_bit_hook
= btrfs_set_bit_hook
,
7411 .clear_bit_hook
= btrfs_clear_bit_hook
,
7412 .merge_extent_hook
= btrfs_merge_extent_hook
,
7413 .split_extent_hook
= btrfs_split_extent_hook
,
7417 * btrfs doesn't support the bmap operation because swapfiles
7418 * use bmap to make a mapping of extents in the file. They assume
7419 * these extents won't change over the life of the file and they
7420 * use the bmap result to do IO directly to the drive.
7422 * the btrfs bmap call would return logical addresses that aren't
7423 * suitable for IO and they also will change frequently as COW
7424 * operations happen. So, swapfile + btrfs == corruption.
7426 * For now we're avoiding this by dropping bmap.
7428 static const struct address_space_operations btrfs_aops
= {
7429 .readpage
= btrfs_readpage
,
7430 .writepage
= btrfs_writepage
,
7431 .writepages
= btrfs_writepages
,
7432 .readpages
= btrfs_readpages
,
7433 .direct_IO
= btrfs_direct_IO
,
7434 .invalidatepage
= btrfs_invalidatepage
,
7435 .releasepage
= btrfs_releasepage
,
7436 .set_page_dirty
= btrfs_set_page_dirty
,
7437 .error_remove_page
= generic_error_remove_page
,
7440 static const struct address_space_operations btrfs_symlink_aops
= {
7441 .readpage
= btrfs_readpage
,
7442 .writepage
= btrfs_writepage
,
7443 .invalidatepage
= btrfs_invalidatepage
,
7444 .releasepage
= btrfs_releasepage
,
7447 static const struct inode_operations btrfs_file_inode_operations
= {
7448 .getattr
= btrfs_getattr
,
7449 .setattr
= btrfs_setattr
,
7450 .setxattr
= btrfs_setxattr
,
7451 .getxattr
= btrfs_getxattr
,
7452 .listxattr
= btrfs_listxattr
,
7453 .removexattr
= btrfs_removexattr
,
7454 .permission
= btrfs_permission
,
7455 .fiemap
= btrfs_fiemap
,
7456 .get_acl
= btrfs_get_acl
,
7458 static const struct inode_operations btrfs_special_inode_operations
= {
7459 .getattr
= btrfs_getattr
,
7460 .setattr
= btrfs_setattr
,
7461 .permission
= btrfs_permission
,
7462 .setxattr
= btrfs_setxattr
,
7463 .getxattr
= btrfs_getxattr
,
7464 .listxattr
= btrfs_listxattr
,
7465 .removexattr
= btrfs_removexattr
,
7466 .get_acl
= btrfs_get_acl
,
7468 static const struct inode_operations btrfs_symlink_inode_operations
= {
7469 .readlink
= generic_readlink
,
7470 .follow_link
= page_follow_link_light
,
7471 .put_link
= page_put_link
,
7472 .getattr
= btrfs_getattr
,
7473 .permission
= btrfs_permission
,
7474 .setxattr
= btrfs_setxattr
,
7475 .getxattr
= btrfs_getxattr
,
7476 .listxattr
= btrfs_listxattr
,
7477 .removexattr
= btrfs_removexattr
,
7478 .get_acl
= btrfs_get_acl
,
7481 const struct dentry_operations btrfs_dentry_operations
= {
7482 .d_delete
= btrfs_dentry_delete
,
7483 .d_release
= btrfs_dentry_release
,