2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/statfs.h>
34 #include <linux/compat.h>
35 #include <linux/bit_spinlock.h>
36 #include <linux/xattr.h>
37 #include <linux/posix_acl.h>
38 #include <linux/falloc.h>
39 #include <linux/slab.h>
40 #include <linux/ratelimit.h>
41 #include <linux/mount.h>
45 #include "transaction.h"
46 #include "btrfs_inode.h"
48 #include "print-tree.h"
49 #include "ordered-data.h"
53 #include "compression.h"
55 #include "free-space-cache.h"
56 #include "inode-map.h"
58 struct btrfs_iget_args
{
60 struct btrfs_root
*root
;
63 static const struct inode_operations btrfs_dir_inode_operations
;
64 static const struct inode_operations btrfs_symlink_inode_operations
;
65 static const struct inode_operations btrfs_dir_ro_inode_operations
;
66 static const struct inode_operations btrfs_special_inode_operations
;
67 static const struct inode_operations btrfs_file_inode_operations
;
68 static const struct address_space_operations btrfs_aops
;
69 static const struct address_space_operations btrfs_symlink_aops
;
70 static const struct file_operations btrfs_dir_file_operations
;
71 static struct extent_io_ops btrfs_extent_io_ops
;
73 static struct kmem_cache
*btrfs_inode_cachep
;
74 static struct kmem_cache
*btrfs_delalloc_work_cachep
;
75 struct kmem_cache
*btrfs_trans_handle_cachep
;
76 struct kmem_cache
*btrfs_transaction_cachep
;
77 struct kmem_cache
*btrfs_path_cachep
;
78 struct kmem_cache
*btrfs_free_space_cachep
;
81 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
82 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
83 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
84 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
85 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
86 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
87 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
88 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
91 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
);
92 static int btrfs_truncate(struct inode
*inode
);
93 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
);
94 static noinline
int cow_file_range(struct inode
*inode
,
95 struct page
*locked_page
,
96 u64 start
, u64 end
, int *page_started
,
97 unsigned long *nr_written
, int unlock
);
98 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
99 u64 len
, u64 orig_start
,
100 u64 block_start
, u64 block_len
,
101 u64 orig_block_len
, int type
);
103 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
104 struct inode
*inode
, struct inode
*dir
,
105 const struct qstr
*qstr
)
109 err
= btrfs_init_acl(trans
, inode
, dir
);
111 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
116 * this does all the hard work for inserting an inline extent into
117 * the btree. The caller should have done a btrfs_drop_extents so that
118 * no overlapping inline items exist in the btree
120 static noinline
int insert_inline_extent(struct btrfs_trans_handle
*trans
,
121 struct btrfs_root
*root
, struct inode
*inode
,
122 u64 start
, size_t size
, size_t compressed_size
,
124 struct page
**compressed_pages
)
126 struct btrfs_key key
;
127 struct btrfs_path
*path
;
128 struct extent_buffer
*leaf
;
129 struct page
*page
= NULL
;
132 struct btrfs_file_extent_item
*ei
;
135 size_t cur_size
= size
;
137 unsigned long offset
;
139 if (compressed_size
&& compressed_pages
)
140 cur_size
= compressed_size
;
142 path
= btrfs_alloc_path();
146 path
->leave_spinning
= 1;
148 key
.objectid
= btrfs_ino(inode
);
150 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
151 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
153 inode_add_bytes(inode
, size
);
154 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
160 leaf
= path
->nodes
[0];
161 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
162 struct btrfs_file_extent_item
);
163 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
164 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
165 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
166 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
167 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
168 ptr
= btrfs_file_extent_inline_start(ei
);
170 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
173 while (compressed_size
> 0) {
174 cpage
= compressed_pages
[i
];
175 cur_size
= min_t(unsigned long, compressed_size
,
178 kaddr
= kmap_atomic(cpage
);
179 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
180 kunmap_atomic(kaddr
);
184 compressed_size
-= cur_size
;
186 btrfs_set_file_extent_compression(leaf
, ei
,
189 page
= find_get_page(inode
->i_mapping
,
190 start
>> PAGE_CACHE_SHIFT
);
191 btrfs_set_file_extent_compression(leaf
, ei
, 0);
192 kaddr
= kmap_atomic(page
);
193 offset
= start
& (PAGE_CACHE_SIZE
- 1);
194 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
195 kunmap_atomic(kaddr
);
196 page_cache_release(page
);
198 btrfs_mark_buffer_dirty(leaf
);
199 btrfs_free_path(path
);
202 * we're an inline extent, so nobody can
203 * extend the file past i_size without locking
204 * a page we already have locked.
206 * We must do any isize and inode updates
207 * before we unlock the pages. Otherwise we
208 * could end up racing with unlink.
210 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
211 ret
= btrfs_update_inode(trans
, root
, inode
);
215 btrfs_free_path(path
);
221 * conditionally insert an inline extent into the file. This
222 * does the checks required to make sure the data is small enough
223 * to fit as an inline extent.
225 static noinline
int cow_file_range_inline(struct btrfs_trans_handle
*trans
,
226 struct btrfs_root
*root
,
227 struct inode
*inode
, u64 start
, u64 end
,
228 size_t compressed_size
, int compress_type
,
229 struct page
**compressed_pages
)
231 u64 isize
= i_size_read(inode
);
232 u64 actual_end
= min(end
+ 1, isize
);
233 u64 inline_len
= actual_end
- start
;
234 u64 aligned_end
= (end
+ root
->sectorsize
- 1) &
235 ~((u64
)root
->sectorsize
- 1);
236 u64 data_len
= inline_len
;
240 data_len
= compressed_size
;
243 actual_end
>= PAGE_CACHE_SIZE
||
244 data_len
>= BTRFS_MAX_INLINE_DATA_SIZE(root
) ||
246 (actual_end
& (root
->sectorsize
- 1)) == 0) ||
248 data_len
> root
->fs_info
->max_inline
) {
252 ret
= btrfs_drop_extents(trans
, root
, inode
, start
, aligned_end
, 1);
256 if (isize
> actual_end
)
257 inline_len
= min_t(u64
, isize
, actual_end
);
258 ret
= insert_inline_extent(trans
, root
, inode
, start
,
259 inline_len
, compressed_size
,
260 compress_type
, compressed_pages
);
261 if (ret
&& ret
!= -ENOSPC
) {
262 btrfs_abort_transaction(trans
, root
, ret
);
264 } else if (ret
== -ENOSPC
) {
268 btrfs_delalloc_release_metadata(inode
, end
+ 1 - start
);
269 btrfs_drop_extent_cache(inode
, start
, aligned_end
- 1, 0);
273 struct async_extent
{
278 unsigned long nr_pages
;
280 struct list_head list
;
285 struct btrfs_root
*root
;
286 struct page
*locked_page
;
289 struct list_head extents
;
290 struct btrfs_work work
;
293 static noinline
int add_async_extent(struct async_cow
*cow
,
294 u64 start
, u64 ram_size
,
297 unsigned long nr_pages
,
300 struct async_extent
*async_extent
;
302 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
303 BUG_ON(!async_extent
); /* -ENOMEM */
304 async_extent
->start
= start
;
305 async_extent
->ram_size
= ram_size
;
306 async_extent
->compressed_size
= compressed_size
;
307 async_extent
->pages
= pages
;
308 async_extent
->nr_pages
= nr_pages
;
309 async_extent
->compress_type
= compress_type
;
310 list_add_tail(&async_extent
->list
, &cow
->extents
);
315 * we create compressed extents in two phases. The first
316 * phase compresses a range of pages that have already been
317 * locked (both pages and state bits are locked).
319 * This is done inside an ordered work queue, and the compression
320 * is spread across many cpus. The actual IO submission is step
321 * two, and the ordered work queue takes care of making sure that
322 * happens in the same order things were put onto the queue by
323 * writepages and friends.
325 * If this code finds it can't get good compression, it puts an
326 * entry onto the work queue to write the uncompressed bytes. This
327 * makes sure that both compressed inodes and uncompressed inodes
328 * are written in the same order that the flusher thread sent them
331 static noinline
int compress_file_range(struct inode
*inode
,
332 struct page
*locked_page
,
334 struct async_cow
*async_cow
,
337 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
338 struct btrfs_trans_handle
*trans
;
340 u64 blocksize
= root
->sectorsize
;
342 u64 isize
= i_size_read(inode
);
344 struct page
**pages
= NULL
;
345 unsigned long nr_pages
;
346 unsigned long nr_pages_ret
= 0;
347 unsigned long total_compressed
= 0;
348 unsigned long total_in
= 0;
349 unsigned long max_compressed
= 128 * 1024;
350 unsigned long max_uncompressed
= 128 * 1024;
353 int compress_type
= root
->fs_info
->compress_type
;
355 /* if this is a small write inside eof, kick off a defrag */
356 if ((end
- start
+ 1) < 16 * 1024 &&
357 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
358 btrfs_add_inode_defrag(NULL
, inode
);
360 actual_end
= min_t(u64
, isize
, end
+ 1);
363 nr_pages
= (end
>> PAGE_CACHE_SHIFT
) - (start
>> PAGE_CACHE_SHIFT
) + 1;
364 nr_pages
= min(nr_pages
, (128 * 1024UL) / PAGE_CACHE_SIZE
);
367 * we don't want to send crud past the end of i_size through
368 * compression, that's just a waste of CPU time. So, if the
369 * end of the file is before the start of our current
370 * requested range of bytes, we bail out to the uncompressed
371 * cleanup code that can deal with all of this.
373 * It isn't really the fastest way to fix things, but this is a
374 * very uncommon corner.
376 if (actual_end
<= start
)
377 goto cleanup_and_bail_uncompressed
;
379 total_compressed
= actual_end
- start
;
381 /* we want to make sure that amount of ram required to uncompress
382 * an extent is reasonable, so we limit the total size in ram
383 * of a compressed extent to 128k. This is a crucial number
384 * because it also controls how easily we can spread reads across
385 * cpus for decompression.
387 * We also want to make sure the amount of IO required to do
388 * a random read is reasonably small, so we limit the size of
389 * a compressed extent to 128k.
391 total_compressed
= min(total_compressed
, max_uncompressed
);
392 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
393 num_bytes
= max(blocksize
, num_bytes
);
398 * we do compression for mount -o compress and when the
399 * inode has not been flagged as nocompress. This flag can
400 * change at any time if we discover bad compression ratios.
402 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
) &&
403 (btrfs_test_opt(root
, COMPRESS
) ||
404 (BTRFS_I(inode
)->force_compress
) ||
405 (BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
))) {
407 pages
= kzalloc(sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
409 /* just bail out to the uncompressed code */
413 if (BTRFS_I(inode
)->force_compress
)
414 compress_type
= BTRFS_I(inode
)->force_compress
;
416 ret
= btrfs_compress_pages(compress_type
,
417 inode
->i_mapping
, start
,
418 total_compressed
, pages
,
419 nr_pages
, &nr_pages_ret
,
425 unsigned long offset
= total_compressed
&
426 (PAGE_CACHE_SIZE
- 1);
427 struct page
*page
= pages
[nr_pages_ret
- 1];
430 /* zero the tail end of the last page, we might be
431 * sending it down to disk
434 kaddr
= kmap_atomic(page
);
435 memset(kaddr
+ offset
, 0,
436 PAGE_CACHE_SIZE
- offset
);
437 kunmap_atomic(kaddr
);
444 trans
= btrfs_join_transaction(root
);
446 ret
= PTR_ERR(trans
);
448 goto cleanup_and_out
;
450 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
452 /* lets try to make an inline extent */
453 if (ret
|| total_in
< (actual_end
- start
)) {
454 /* we didn't compress the entire range, try
455 * to make an uncompressed inline extent.
457 ret
= cow_file_range_inline(trans
, root
, inode
,
458 start
, end
, 0, 0, NULL
);
460 /* try making a compressed inline extent */
461 ret
= cow_file_range_inline(trans
, root
, inode
,
464 compress_type
, pages
);
468 * inline extent creation worked or returned error,
469 * we don't need to create any more async work items.
470 * Unlock and free up our temp pages.
472 extent_clear_unlock_delalloc(inode
,
473 &BTRFS_I(inode
)->io_tree
,
475 EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
476 EXTENT_CLEAR_DELALLOC
|
477 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
);
479 btrfs_end_transaction(trans
, root
);
482 btrfs_end_transaction(trans
, root
);
487 * we aren't doing an inline extent round the compressed size
488 * up to a block size boundary so the allocator does sane
491 total_compressed
= (total_compressed
+ blocksize
- 1) &
495 * one last check to make sure the compression is really a
496 * win, compare the page count read with the blocks on disk
498 total_in
= (total_in
+ PAGE_CACHE_SIZE
- 1) &
499 ~(PAGE_CACHE_SIZE
- 1);
500 if (total_compressed
>= total_in
) {
503 num_bytes
= total_in
;
506 if (!will_compress
&& pages
) {
508 * the compression code ran but failed to make things smaller,
509 * free any pages it allocated and our page pointer array
511 for (i
= 0; i
< nr_pages_ret
; i
++) {
512 WARN_ON(pages
[i
]->mapping
);
513 page_cache_release(pages
[i
]);
517 total_compressed
= 0;
520 /* flag the file so we don't compress in the future */
521 if (!btrfs_test_opt(root
, FORCE_COMPRESS
) &&
522 !(BTRFS_I(inode
)->force_compress
)) {
523 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
529 /* the async work queues will take care of doing actual
530 * allocation on disk for these compressed pages,
531 * and will submit them to the elevator.
533 add_async_extent(async_cow
, start
, num_bytes
,
534 total_compressed
, pages
, nr_pages_ret
,
537 if (start
+ num_bytes
< end
) {
544 cleanup_and_bail_uncompressed
:
546 * No compression, but we still need to write the pages in
547 * the file we've been given so far. redirty the locked
548 * page if it corresponds to our extent and set things up
549 * for the async work queue to run cow_file_range to do
550 * the normal delalloc dance
552 if (page_offset(locked_page
) >= start
&&
553 page_offset(locked_page
) <= end
) {
554 __set_page_dirty_nobuffers(locked_page
);
555 /* unlocked later on in the async handlers */
557 add_async_extent(async_cow
, start
, end
- start
+ 1,
558 0, NULL
, 0, BTRFS_COMPRESS_NONE
);
566 for (i
= 0; i
< nr_pages_ret
; i
++) {
567 WARN_ON(pages
[i
]->mapping
);
568 page_cache_release(pages
[i
]);
575 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
577 EXTENT_CLEAR_UNLOCK_PAGE
|
579 EXTENT_CLEAR_DELALLOC
|
580 EXTENT_SET_WRITEBACK
|
581 EXTENT_END_WRITEBACK
);
582 if (!trans
|| IS_ERR(trans
))
583 btrfs_error(root
->fs_info
, ret
, "Failed to join transaction");
585 btrfs_abort_transaction(trans
, root
, ret
);
590 * phase two of compressed writeback. This is the ordered portion
591 * of the code, which only gets called in the order the work was
592 * queued. We walk all the async extents created by compress_file_range
593 * and send them down to the disk.
595 static noinline
int submit_compressed_extents(struct inode
*inode
,
596 struct async_cow
*async_cow
)
598 struct async_extent
*async_extent
;
600 struct btrfs_trans_handle
*trans
;
601 struct btrfs_key ins
;
602 struct extent_map
*em
;
603 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
604 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
605 struct extent_io_tree
*io_tree
;
608 if (list_empty(&async_cow
->extents
))
612 while (!list_empty(&async_cow
->extents
)) {
613 async_extent
= list_entry(async_cow
->extents
.next
,
614 struct async_extent
, list
);
615 list_del(&async_extent
->list
);
617 io_tree
= &BTRFS_I(inode
)->io_tree
;
620 /* did the compression code fall back to uncompressed IO? */
621 if (!async_extent
->pages
) {
622 int page_started
= 0;
623 unsigned long nr_written
= 0;
625 lock_extent(io_tree
, async_extent
->start
,
626 async_extent
->start
+
627 async_extent
->ram_size
- 1);
629 /* allocate blocks */
630 ret
= cow_file_range(inode
, async_cow
->locked_page
,
632 async_extent
->start
+
633 async_extent
->ram_size
- 1,
634 &page_started
, &nr_written
, 0);
639 * if page_started, cow_file_range inserted an
640 * inline extent and took care of all the unlocking
641 * and IO for us. Otherwise, we need to submit
642 * all those pages down to the drive.
644 if (!page_started
&& !ret
)
645 extent_write_locked_range(io_tree
,
646 inode
, async_extent
->start
,
647 async_extent
->start
+
648 async_extent
->ram_size
- 1,
656 lock_extent(io_tree
, async_extent
->start
,
657 async_extent
->start
+ async_extent
->ram_size
- 1);
659 trans
= btrfs_join_transaction(root
);
661 ret
= PTR_ERR(trans
);
663 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
664 ret
= btrfs_reserve_extent(trans
, root
,
665 async_extent
->compressed_size
,
666 async_extent
->compressed_size
,
667 0, alloc_hint
, &ins
, 1);
668 if (ret
&& ret
!= -ENOSPC
)
669 btrfs_abort_transaction(trans
, root
, ret
);
670 btrfs_end_transaction(trans
, root
);
675 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
676 WARN_ON(async_extent
->pages
[i
]->mapping
);
677 page_cache_release(async_extent
->pages
[i
]);
679 kfree(async_extent
->pages
);
680 async_extent
->nr_pages
= 0;
681 async_extent
->pages
= NULL
;
682 unlock_extent(io_tree
, async_extent
->start
,
683 async_extent
->start
+
684 async_extent
->ram_size
- 1);
687 goto out_free
; /* JDM: Requeue? */
691 * here we're doing allocation and writeback of the
694 btrfs_drop_extent_cache(inode
, async_extent
->start
,
695 async_extent
->start
+
696 async_extent
->ram_size
- 1, 0);
698 em
= alloc_extent_map();
699 BUG_ON(!em
); /* -ENOMEM */
700 em
->start
= async_extent
->start
;
701 em
->len
= async_extent
->ram_size
;
702 em
->orig_start
= em
->start
;
704 em
->block_start
= ins
.objectid
;
705 em
->block_len
= ins
.offset
;
706 em
->orig_block_len
= ins
.offset
;
707 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
708 em
->compress_type
= async_extent
->compress_type
;
709 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
710 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
714 write_lock(&em_tree
->lock
);
715 ret
= add_extent_mapping(em_tree
, em
);
718 &em_tree
->modified_extents
);
719 write_unlock(&em_tree
->lock
);
720 if (ret
!= -EEXIST
) {
724 btrfs_drop_extent_cache(inode
, async_extent
->start
,
725 async_extent
->start
+
726 async_extent
->ram_size
- 1, 0);
729 ret
= btrfs_add_ordered_extent_compress(inode
,
732 async_extent
->ram_size
,
734 BTRFS_ORDERED_COMPRESSED
,
735 async_extent
->compress_type
);
736 BUG_ON(ret
); /* -ENOMEM */
739 * clear dirty, set writeback and unlock the pages.
741 extent_clear_unlock_delalloc(inode
,
742 &BTRFS_I(inode
)->io_tree
,
744 async_extent
->start
+
745 async_extent
->ram_size
- 1,
746 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
747 EXTENT_CLEAR_UNLOCK
|
748 EXTENT_CLEAR_DELALLOC
|
749 EXTENT_CLEAR_DIRTY
| EXTENT_SET_WRITEBACK
);
751 ret
= btrfs_submit_compressed_write(inode
,
753 async_extent
->ram_size
,
755 ins
.offset
, async_extent
->pages
,
756 async_extent
->nr_pages
);
758 BUG_ON(ret
); /* -ENOMEM */
759 alloc_hint
= ins
.objectid
+ ins
.offset
;
771 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
774 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
775 struct extent_map
*em
;
778 read_lock(&em_tree
->lock
);
779 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
782 * if block start isn't an actual block number then find the
783 * first block in this inode and use that as a hint. If that
784 * block is also bogus then just don't worry about it.
786 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
788 em
= search_extent_mapping(em_tree
, 0, 0);
789 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
790 alloc_hint
= em
->block_start
;
794 alloc_hint
= em
->block_start
;
798 read_unlock(&em_tree
->lock
);
804 * when extent_io.c finds a delayed allocation range in the file,
805 * the call backs end up in this code. The basic idea is to
806 * allocate extents on disk for the range, and create ordered data structs
807 * in ram to track those extents.
809 * locked_page is the page that writepage had locked already. We use
810 * it to make sure we don't do extra locks or unlocks.
812 * *page_started is set to one if we unlock locked_page and do everything
813 * required to start IO on it. It may be clean and already done with
816 static noinline
int __cow_file_range(struct btrfs_trans_handle
*trans
,
818 struct btrfs_root
*root
,
819 struct page
*locked_page
,
820 u64 start
, u64 end
, int *page_started
,
821 unsigned long *nr_written
,
826 unsigned long ram_size
;
829 u64 blocksize
= root
->sectorsize
;
830 struct btrfs_key ins
;
831 struct extent_map
*em
;
832 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
835 BUG_ON(btrfs_is_free_space_inode(inode
));
837 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
838 num_bytes
= max(blocksize
, num_bytes
);
839 disk_num_bytes
= num_bytes
;
841 /* if this is a small write inside eof, kick off defrag */
842 if (num_bytes
< 64 * 1024 &&
843 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
844 btrfs_add_inode_defrag(trans
, inode
);
847 /* lets try to make an inline extent */
848 ret
= cow_file_range_inline(trans
, root
, inode
,
849 start
, end
, 0, 0, NULL
);
851 extent_clear_unlock_delalloc(inode
,
852 &BTRFS_I(inode
)->io_tree
,
854 EXTENT_CLEAR_UNLOCK_PAGE
|
855 EXTENT_CLEAR_UNLOCK
|
856 EXTENT_CLEAR_DELALLOC
|
858 EXTENT_SET_WRITEBACK
|
859 EXTENT_END_WRITEBACK
);
861 *nr_written
= *nr_written
+
862 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
865 } else if (ret
< 0) {
866 btrfs_abort_transaction(trans
, root
, ret
);
871 BUG_ON(disk_num_bytes
>
872 btrfs_super_total_bytes(root
->fs_info
->super_copy
));
874 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
875 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
877 while (disk_num_bytes
> 0) {
880 cur_alloc_size
= disk_num_bytes
;
881 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
882 root
->sectorsize
, 0, alloc_hint
,
885 btrfs_abort_transaction(trans
, root
, ret
);
889 em
= alloc_extent_map();
890 BUG_ON(!em
); /* -ENOMEM */
892 em
->orig_start
= em
->start
;
893 ram_size
= ins
.offset
;
894 em
->len
= ins
.offset
;
896 em
->block_start
= ins
.objectid
;
897 em
->block_len
= ins
.offset
;
898 em
->orig_block_len
= ins
.offset
;
899 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
900 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
904 write_lock(&em_tree
->lock
);
905 ret
= add_extent_mapping(em_tree
, em
);
908 &em_tree
->modified_extents
);
909 write_unlock(&em_tree
->lock
);
910 if (ret
!= -EEXIST
) {
914 btrfs_drop_extent_cache(inode
, start
,
915 start
+ ram_size
- 1, 0);
918 cur_alloc_size
= ins
.offset
;
919 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
920 ram_size
, cur_alloc_size
, 0);
921 BUG_ON(ret
); /* -ENOMEM */
923 if (root
->root_key
.objectid
==
924 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
925 ret
= btrfs_reloc_clone_csums(inode
, start
,
928 btrfs_abort_transaction(trans
, root
, ret
);
933 if (disk_num_bytes
< cur_alloc_size
)
936 /* we're not doing compressed IO, don't unlock the first
937 * page (which the caller expects to stay locked), don't
938 * clear any dirty bits and don't set any writeback bits
940 * Do set the Private2 bit so we know this page was properly
941 * setup for writepage
943 op
= unlock
? EXTENT_CLEAR_UNLOCK_PAGE
: 0;
944 op
|= EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
947 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
948 start
, start
+ ram_size
- 1,
950 disk_num_bytes
-= cur_alloc_size
;
951 num_bytes
-= cur_alloc_size
;
952 alloc_hint
= ins
.objectid
+ ins
.offset
;
953 start
+= cur_alloc_size
;
959 extent_clear_unlock_delalloc(inode
,
960 &BTRFS_I(inode
)->io_tree
,
961 start
, end
, locked_page
,
962 EXTENT_CLEAR_UNLOCK_PAGE
|
963 EXTENT_CLEAR_UNLOCK
|
964 EXTENT_CLEAR_DELALLOC
|
966 EXTENT_SET_WRITEBACK
|
967 EXTENT_END_WRITEBACK
);
972 static noinline
int cow_file_range(struct inode
*inode
,
973 struct page
*locked_page
,
974 u64 start
, u64 end
, int *page_started
,
975 unsigned long *nr_written
,
978 struct btrfs_trans_handle
*trans
;
979 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
982 trans
= btrfs_join_transaction(root
);
984 extent_clear_unlock_delalloc(inode
,
985 &BTRFS_I(inode
)->io_tree
,
986 start
, end
, locked_page
,
987 EXTENT_CLEAR_UNLOCK_PAGE
|
988 EXTENT_CLEAR_UNLOCK
|
989 EXTENT_CLEAR_DELALLOC
|
991 EXTENT_SET_WRITEBACK
|
992 EXTENT_END_WRITEBACK
);
993 return PTR_ERR(trans
);
995 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
997 ret
= __cow_file_range(trans
, inode
, root
, locked_page
, start
, end
,
998 page_started
, nr_written
, unlock
);
1000 btrfs_end_transaction(trans
, root
);
1006 * work queue call back to started compression on a file and pages
1008 static noinline
void async_cow_start(struct btrfs_work
*work
)
1010 struct async_cow
*async_cow
;
1012 async_cow
= container_of(work
, struct async_cow
, work
);
1014 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
1015 async_cow
->start
, async_cow
->end
, async_cow
,
1017 if (num_added
== 0) {
1018 btrfs_add_delayed_iput(async_cow
->inode
);
1019 async_cow
->inode
= NULL
;
1024 * work queue call back to submit previously compressed pages
1026 static noinline
void async_cow_submit(struct btrfs_work
*work
)
1028 struct async_cow
*async_cow
;
1029 struct btrfs_root
*root
;
1030 unsigned long nr_pages
;
1032 async_cow
= container_of(work
, struct async_cow
, work
);
1034 root
= async_cow
->root
;
1035 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
1038 if (atomic_sub_return(nr_pages
, &root
->fs_info
->async_delalloc_pages
) <
1040 waitqueue_active(&root
->fs_info
->async_submit_wait
))
1041 wake_up(&root
->fs_info
->async_submit_wait
);
1043 if (async_cow
->inode
)
1044 submit_compressed_extents(async_cow
->inode
, async_cow
);
1047 static noinline
void async_cow_free(struct btrfs_work
*work
)
1049 struct async_cow
*async_cow
;
1050 async_cow
= container_of(work
, struct async_cow
, work
);
1051 if (async_cow
->inode
)
1052 btrfs_add_delayed_iput(async_cow
->inode
);
1056 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
1057 u64 start
, u64 end
, int *page_started
,
1058 unsigned long *nr_written
)
1060 struct async_cow
*async_cow
;
1061 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1062 unsigned long nr_pages
;
1064 int limit
= 10 * 1024 * 1024;
1066 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
1067 1, 0, NULL
, GFP_NOFS
);
1068 while (start
< end
) {
1069 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
1070 BUG_ON(!async_cow
); /* -ENOMEM */
1071 async_cow
->inode
= igrab(inode
);
1072 async_cow
->root
= root
;
1073 async_cow
->locked_page
= locked_page
;
1074 async_cow
->start
= start
;
1076 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
1079 cur_end
= min(end
, start
+ 512 * 1024 - 1);
1081 async_cow
->end
= cur_end
;
1082 INIT_LIST_HEAD(&async_cow
->extents
);
1084 async_cow
->work
.func
= async_cow_start
;
1085 async_cow
->work
.ordered_func
= async_cow_submit
;
1086 async_cow
->work
.ordered_free
= async_cow_free
;
1087 async_cow
->work
.flags
= 0;
1089 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
1091 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
1093 btrfs_queue_worker(&root
->fs_info
->delalloc_workers
,
1096 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
1097 wait_event(root
->fs_info
->async_submit_wait
,
1098 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
1102 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
1103 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1104 wait_event(root
->fs_info
->async_submit_wait
,
1105 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
1109 *nr_written
+= nr_pages
;
1110 start
= cur_end
+ 1;
1116 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
1117 u64 bytenr
, u64 num_bytes
)
1120 struct btrfs_ordered_sum
*sums
;
1123 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
1124 bytenr
+ num_bytes
- 1, &list
, 0);
1125 if (ret
== 0 && list_empty(&list
))
1128 while (!list_empty(&list
)) {
1129 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1130 list_del(&sums
->list
);
1137 * when nowcow writeback call back. This checks for snapshots or COW copies
1138 * of the extents that exist in the file, and COWs the file as required.
1140 * If no cow copies or snapshots exist, we write directly to the existing
1143 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1144 struct page
*locked_page
,
1145 u64 start
, u64 end
, int *page_started
, int force
,
1146 unsigned long *nr_written
)
1148 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1149 struct btrfs_trans_handle
*trans
;
1150 struct extent_buffer
*leaf
;
1151 struct btrfs_path
*path
;
1152 struct btrfs_file_extent_item
*fi
;
1153 struct btrfs_key found_key
;
1167 u64 ino
= btrfs_ino(inode
);
1169 path
= btrfs_alloc_path();
1171 extent_clear_unlock_delalloc(inode
,
1172 &BTRFS_I(inode
)->io_tree
,
1173 start
, end
, locked_page
,
1174 EXTENT_CLEAR_UNLOCK_PAGE
|
1175 EXTENT_CLEAR_UNLOCK
|
1176 EXTENT_CLEAR_DELALLOC
|
1177 EXTENT_CLEAR_DIRTY
|
1178 EXTENT_SET_WRITEBACK
|
1179 EXTENT_END_WRITEBACK
);
1183 nolock
= btrfs_is_free_space_inode(inode
);
1186 trans
= btrfs_join_transaction_nolock(root
);
1188 trans
= btrfs_join_transaction(root
);
1190 if (IS_ERR(trans
)) {
1191 extent_clear_unlock_delalloc(inode
,
1192 &BTRFS_I(inode
)->io_tree
,
1193 start
, end
, locked_page
,
1194 EXTENT_CLEAR_UNLOCK_PAGE
|
1195 EXTENT_CLEAR_UNLOCK
|
1196 EXTENT_CLEAR_DELALLOC
|
1197 EXTENT_CLEAR_DIRTY
|
1198 EXTENT_SET_WRITEBACK
|
1199 EXTENT_END_WRITEBACK
);
1200 btrfs_free_path(path
);
1201 return PTR_ERR(trans
);
1204 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1206 cow_start
= (u64
)-1;
1209 ret
= btrfs_lookup_file_extent(trans
, root
, path
, ino
,
1212 btrfs_abort_transaction(trans
, root
, ret
);
1215 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1216 leaf
= path
->nodes
[0];
1217 btrfs_item_key_to_cpu(leaf
, &found_key
,
1218 path
->slots
[0] - 1);
1219 if (found_key
.objectid
== ino
&&
1220 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1225 leaf
= path
->nodes
[0];
1226 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1227 ret
= btrfs_next_leaf(root
, path
);
1229 btrfs_abort_transaction(trans
, root
, ret
);
1234 leaf
= path
->nodes
[0];
1240 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1242 if (found_key
.objectid
> ino
||
1243 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1244 found_key
.offset
> end
)
1247 if (found_key
.offset
> cur_offset
) {
1248 extent_end
= found_key
.offset
;
1253 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1254 struct btrfs_file_extent_item
);
1255 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1257 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1258 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1259 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1260 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1261 extent_end
= found_key
.offset
+
1262 btrfs_file_extent_num_bytes(leaf
, fi
);
1264 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1265 if (extent_end
<= start
) {
1269 if (disk_bytenr
== 0)
1271 if (btrfs_file_extent_compression(leaf
, fi
) ||
1272 btrfs_file_extent_encryption(leaf
, fi
) ||
1273 btrfs_file_extent_other_encoding(leaf
, fi
))
1275 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1277 if (btrfs_extent_readonly(root
, disk_bytenr
))
1279 if (btrfs_cross_ref_exist(trans
, root
, ino
,
1281 extent_offset
, disk_bytenr
))
1283 disk_bytenr
+= extent_offset
;
1284 disk_bytenr
+= cur_offset
- found_key
.offset
;
1285 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1287 * force cow if csum exists in the range.
1288 * this ensure that csum for a given extent are
1289 * either valid or do not exist.
1291 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1294 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1295 extent_end
= found_key
.offset
+
1296 btrfs_file_extent_inline_len(leaf
, fi
);
1297 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1302 if (extent_end
<= start
) {
1307 if (cow_start
== (u64
)-1)
1308 cow_start
= cur_offset
;
1309 cur_offset
= extent_end
;
1310 if (cur_offset
> end
)
1316 btrfs_release_path(path
);
1317 if (cow_start
!= (u64
)-1) {
1318 ret
= __cow_file_range(trans
, inode
, root
, locked_page
,
1319 cow_start
, found_key
.offset
- 1,
1320 page_started
, nr_written
, 1);
1322 btrfs_abort_transaction(trans
, root
, ret
);
1325 cow_start
= (u64
)-1;
1328 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1329 struct extent_map
*em
;
1330 struct extent_map_tree
*em_tree
;
1331 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1332 em
= alloc_extent_map();
1333 BUG_ON(!em
); /* -ENOMEM */
1334 em
->start
= cur_offset
;
1335 em
->orig_start
= found_key
.offset
- extent_offset
;
1336 em
->len
= num_bytes
;
1337 em
->block_len
= num_bytes
;
1338 em
->block_start
= disk_bytenr
;
1339 em
->orig_block_len
= disk_num_bytes
;
1340 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1341 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1342 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
1343 em
->generation
= -1;
1345 write_lock(&em_tree
->lock
);
1346 ret
= add_extent_mapping(em_tree
, em
);
1348 list_move(&em
->list
,
1349 &em_tree
->modified_extents
);
1350 write_unlock(&em_tree
->lock
);
1351 if (ret
!= -EEXIST
) {
1352 free_extent_map(em
);
1355 btrfs_drop_extent_cache(inode
, em
->start
,
1356 em
->start
+ em
->len
- 1, 0);
1358 type
= BTRFS_ORDERED_PREALLOC
;
1360 type
= BTRFS_ORDERED_NOCOW
;
1363 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1364 num_bytes
, num_bytes
, type
);
1365 BUG_ON(ret
); /* -ENOMEM */
1367 if (root
->root_key
.objectid
==
1368 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1369 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1372 btrfs_abort_transaction(trans
, root
, ret
);
1377 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
1378 cur_offset
, cur_offset
+ num_bytes
- 1,
1379 locked_page
, EXTENT_CLEAR_UNLOCK_PAGE
|
1380 EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
1381 EXTENT_SET_PRIVATE2
);
1382 cur_offset
= extent_end
;
1383 if (cur_offset
> end
)
1386 btrfs_release_path(path
);
1388 if (cur_offset
<= end
&& cow_start
== (u64
)-1) {
1389 cow_start
= cur_offset
;
1393 if (cow_start
!= (u64
)-1) {
1394 ret
= __cow_file_range(trans
, inode
, root
, locked_page
,
1396 page_started
, nr_written
, 1);
1398 btrfs_abort_transaction(trans
, root
, ret
);
1404 err
= btrfs_end_transaction(trans
, root
);
1408 if (ret
&& cur_offset
< end
)
1409 extent_clear_unlock_delalloc(inode
,
1410 &BTRFS_I(inode
)->io_tree
,
1411 cur_offset
, end
, locked_page
,
1412 EXTENT_CLEAR_UNLOCK_PAGE
|
1413 EXTENT_CLEAR_UNLOCK
|
1414 EXTENT_CLEAR_DELALLOC
|
1415 EXTENT_CLEAR_DIRTY
|
1416 EXTENT_SET_WRITEBACK
|
1417 EXTENT_END_WRITEBACK
);
1419 btrfs_free_path(path
);
1424 * extent_io.c call back to do delayed allocation processing
1426 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1427 u64 start
, u64 end
, int *page_started
,
1428 unsigned long *nr_written
)
1431 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1433 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) {
1434 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1435 page_started
, 1, nr_written
);
1436 } else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
) {
1437 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1438 page_started
, 0, nr_written
);
1439 } else if (!btrfs_test_opt(root
, COMPRESS
) &&
1440 !(BTRFS_I(inode
)->force_compress
) &&
1441 !(BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
)) {
1442 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1443 page_started
, nr_written
, 1);
1445 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1446 &BTRFS_I(inode
)->runtime_flags
);
1447 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1448 page_started
, nr_written
);
1453 static void btrfs_split_extent_hook(struct inode
*inode
,
1454 struct extent_state
*orig
, u64 split
)
1456 /* not delalloc, ignore it */
1457 if (!(orig
->state
& EXTENT_DELALLOC
))
1460 spin_lock(&BTRFS_I(inode
)->lock
);
1461 BTRFS_I(inode
)->outstanding_extents
++;
1462 spin_unlock(&BTRFS_I(inode
)->lock
);
1466 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1467 * extents so we can keep track of new extents that are just merged onto old
1468 * extents, such as when we are doing sequential writes, so we can properly
1469 * account for the metadata space we'll need.
1471 static void btrfs_merge_extent_hook(struct inode
*inode
,
1472 struct extent_state
*new,
1473 struct extent_state
*other
)
1475 /* not delalloc, ignore it */
1476 if (!(other
->state
& EXTENT_DELALLOC
))
1479 spin_lock(&BTRFS_I(inode
)->lock
);
1480 BTRFS_I(inode
)->outstanding_extents
--;
1481 spin_unlock(&BTRFS_I(inode
)->lock
);
1485 * extent_io.c set_bit_hook, used to track delayed allocation
1486 * bytes in this file, and to maintain the list of inodes that
1487 * have pending delalloc work to be done.
1489 static void btrfs_set_bit_hook(struct inode
*inode
,
1490 struct extent_state
*state
, int *bits
)
1494 * set_bit and clear bit hooks normally require _irqsave/restore
1495 * but in this case, we are only testing for the DELALLOC
1496 * bit, which is only set or cleared with irqs on
1498 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1499 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1500 u64 len
= state
->end
+ 1 - state
->start
;
1501 bool do_list
= !btrfs_is_free_space_inode(inode
);
1503 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1504 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1506 spin_lock(&BTRFS_I(inode
)->lock
);
1507 BTRFS_I(inode
)->outstanding_extents
++;
1508 spin_unlock(&BTRFS_I(inode
)->lock
);
1511 spin_lock(&root
->fs_info
->delalloc_lock
);
1512 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1513 root
->fs_info
->delalloc_bytes
+= len
;
1514 if (do_list
&& list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1515 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1516 &root
->fs_info
->delalloc_inodes
);
1518 spin_unlock(&root
->fs_info
->delalloc_lock
);
1523 * extent_io.c clear_bit_hook, see set_bit_hook for why
1525 static void btrfs_clear_bit_hook(struct inode
*inode
,
1526 struct extent_state
*state
, int *bits
)
1529 * set_bit and clear bit hooks normally require _irqsave/restore
1530 * but in this case, we are only testing for the DELALLOC
1531 * bit, which is only set or cleared with irqs on
1533 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1534 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1535 u64 len
= state
->end
+ 1 - state
->start
;
1536 bool do_list
= !btrfs_is_free_space_inode(inode
);
1538 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1539 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1540 } else if (!(*bits
& EXTENT_DO_ACCOUNTING
)) {
1541 spin_lock(&BTRFS_I(inode
)->lock
);
1542 BTRFS_I(inode
)->outstanding_extents
--;
1543 spin_unlock(&BTRFS_I(inode
)->lock
);
1546 if (*bits
& EXTENT_DO_ACCOUNTING
)
1547 btrfs_delalloc_release_metadata(inode
, len
);
1549 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1551 btrfs_free_reserved_data_space(inode
, len
);
1553 spin_lock(&root
->fs_info
->delalloc_lock
);
1554 root
->fs_info
->delalloc_bytes
-= len
;
1555 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1557 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1558 !list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1559 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1561 spin_unlock(&root
->fs_info
->delalloc_lock
);
1566 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1567 * we don't create bios that span stripes or chunks
1569 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
1570 size_t size
, struct bio
*bio
,
1571 unsigned long bio_flags
)
1573 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1574 u64 logical
= (u64
)bio
->bi_sector
<< 9;
1579 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1582 length
= bio
->bi_size
;
1583 map_length
= length
;
1584 ret
= btrfs_map_block(root
->fs_info
, READ
, logical
,
1585 &map_length
, NULL
, 0);
1586 /* Will always return 0 with map_multi == NULL */
1588 if (map_length
< length
+ size
)
1594 * in order to insert checksums into the metadata in large chunks,
1595 * we wait until bio submission time. All the pages in the bio are
1596 * checksummed and sums are attached onto the ordered extent record.
1598 * At IO completion time the cums attached on the ordered extent record
1599 * are inserted into the btree
1601 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1602 struct bio
*bio
, int mirror_num
,
1603 unsigned long bio_flags
,
1606 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1609 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1610 BUG_ON(ret
); /* -ENOMEM */
1615 * in order to insert checksums into the metadata in large chunks,
1616 * we wait until bio submission time. All the pages in the bio are
1617 * checksummed and sums are attached onto the ordered extent record.
1619 * At IO completion time the cums attached on the ordered extent record
1620 * are inserted into the btree
1622 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1623 int mirror_num
, unsigned long bio_flags
,
1626 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1629 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1631 bio_endio(bio
, ret
);
1636 * extent_io.c submission hook. This does the right thing for csum calculation
1637 * on write, or reading the csums from the tree before a read
1639 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1640 int mirror_num
, unsigned long bio_flags
,
1643 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1647 int async
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
1649 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1651 if (btrfs_is_free_space_inode(inode
))
1654 if (!(rw
& REQ_WRITE
)) {
1655 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, metadata
);
1659 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1660 ret
= btrfs_submit_compressed_read(inode
, bio
,
1664 } else if (!skip_sum
) {
1665 ret
= btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1670 } else if (async
&& !skip_sum
) {
1671 /* csum items have already been cloned */
1672 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1674 /* we're doing a write, do the async checksumming */
1675 ret
= btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1676 inode
, rw
, bio
, mirror_num
,
1677 bio_flags
, bio_offset
,
1678 __btrfs_submit_bio_start
,
1679 __btrfs_submit_bio_done
);
1681 } else if (!skip_sum
) {
1682 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1688 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1692 bio_endio(bio
, ret
);
1697 * given a list of ordered sums record them in the inode. This happens
1698 * at IO completion time based on sums calculated at bio submission time.
1700 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1701 struct inode
*inode
, u64 file_offset
,
1702 struct list_head
*list
)
1704 struct btrfs_ordered_sum
*sum
;
1706 list_for_each_entry(sum
, list
, list
) {
1707 btrfs_csum_file_blocks(trans
,
1708 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1713 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1714 struct extent_state
**cached_state
)
1716 WARN_ON((end
& (PAGE_CACHE_SIZE
- 1)) == 0);
1717 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1718 cached_state
, GFP_NOFS
);
1721 /* see btrfs_writepage_start_hook for details on why this is required */
1722 struct btrfs_writepage_fixup
{
1724 struct btrfs_work work
;
1727 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1729 struct btrfs_writepage_fixup
*fixup
;
1730 struct btrfs_ordered_extent
*ordered
;
1731 struct extent_state
*cached_state
= NULL
;
1733 struct inode
*inode
;
1738 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1742 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1743 ClearPageChecked(page
);
1747 inode
= page
->mapping
->host
;
1748 page_start
= page_offset(page
);
1749 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1751 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1754 /* already ordered? We're done */
1755 if (PagePrivate2(page
))
1758 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1760 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1761 page_end
, &cached_state
, GFP_NOFS
);
1763 btrfs_start_ordered_extent(inode
, ordered
, 1);
1764 btrfs_put_ordered_extent(ordered
);
1768 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
1770 mapping_set_error(page
->mapping
, ret
);
1771 end_extent_writepage(page
, ret
, page_start
, page_end
);
1772 ClearPageChecked(page
);
1776 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1777 ClearPageChecked(page
);
1778 set_page_dirty(page
);
1780 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1781 &cached_state
, GFP_NOFS
);
1784 page_cache_release(page
);
1789 * There are a few paths in the higher layers of the kernel that directly
1790 * set the page dirty bit without asking the filesystem if it is a
1791 * good idea. This causes problems because we want to make sure COW
1792 * properly happens and the data=ordered rules are followed.
1794 * In our case any range that doesn't have the ORDERED bit set
1795 * hasn't been properly setup for IO. We kick off an async process
1796 * to fix it up. The async helper will wait for ordered extents, set
1797 * the delalloc bit and make it safe to write the page.
1799 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1801 struct inode
*inode
= page
->mapping
->host
;
1802 struct btrfs_writepage_fixup
*fixup
;
1803 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1805 /* this page is properly in the ordered list */
1806 if (TestClearPagePrivate2(page
))
1809 if (PageChecked(page
))
1812 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1816 SetPageChecked(page
);
1817 page_cache_get(page
);
1818 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
1820 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
1824 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1825 struct inode
*inode
, u64 file_pos
,
1826 u64 disk_bytenr
, u64 disk_num_bytes
,
1827 u64 num_bytes
, u64 ram_bytes
,
1828 u8 compression
, u8 encryption
,
1829 u16 other_encoding
, int extent_type
)
1831 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1832 struct btrfs_file_extent_item
*fi
;
1833 struct btrfs_path
*path
;
1834 struct extent_buffer
*leaf
;
1835 struct btrfs_key ins
;
1838 path
= btrfs_alloc_path();
1842 path
->leave_spinning
= 1;
1845 * we may be replacing one extent in the tree with another.
1846 * The new extent is pinned in the extent map, and we don't want
1847 * to drop it from the cache until it is completely in the btree.
1849 * So, tell btrfs_drop_extents to leave this extent in the cache.
1850 * the caller is expected to unpin it and allow it to be merged
1853 ret
= btrfs_drop_extents(trans
, root
, inode
, file_pos
,
1854 file_pos
+ num_bytes
, 0);
1858 ins
.objectid
= btrfs_ino(inode
);
1859 ins
.offset
= file_pos
;
1860 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1861 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
, sizeof(*fi
));
1864 leaf
= path
->nodes
[0];
1865 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1866 struct btrfs_file_extent_item
);
1867 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1868 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1869 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1870 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1871 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1872 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1873 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1874 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1875 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1876 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1878 btrfs_mark_buffer_dirty(leaf
);
1879 btrfs_release_path(path
);
1881 inode_add_bytes(inode
, num_bytes
);
1883 ins
.objectid
= disk_bytenr
;
1884 ins
.offset
= disk_num_bytes
;
1885 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1886 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
1887 root
->root_key
.objectid
,
1888 btrfs_ino(inode
), file_pos
, &ins
);
1890 btrfs_free_path(path
);
1896 * helper function for btrfs_finish_ordered_io, this
1897 * just reads in some of the csum leaves to prime them into ram
1898 * before we start the transaction. It limits the amount of btree
1899 * reads required while inside the transaction.
1901 /* as ordered data IO finishes, this gets called so we can finish
1902 * an ordered extent if the range of bytes in the file it covers are
1905 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
)
1907 struct inode
*inode
= ordered_extent
->inode
;
1908 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1909 struct btrfs_trans_handle
*trans
= NULL
;
1910 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1911 struct extent_state
*cached_state
= NULL
;
1912 int compress_type
= 0;
1916 nolock
= btrfs_is_free_space_inode(inode
);
1918 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered_extent
->flags
)) {
1923 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
1924 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
1925 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
1927 trans
= btrfs_join_transaction_nolock(root
);
1929 trans
= btrfs_join_transaction(root
);
1930 if (IS_ERR(trans
)) {
1931 ret
= PTR_ERR(trans
);
1935 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1936 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
1937 if (ret
) /* -ENOMEM or corruption */
1938 btrfs_abort_transaction(trans
, root
, ret
);
1942 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
1943 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
1947 trans
= btrfs_join_transaction_nolock(root
);
1949 trans
= btrfs_join_transaction(root
);
1950 if (IS_ERR(trans
)) {
1951 ret
= PTR_ERR(trans
);
1955 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1957 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
1958 compress_type
= ordered_extent
->compress_type
;
1959 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
1960 BUG_ON(compress_type
);
1961 ret
= btrfs_mark_extent_written(trans
, inode
,
1962 ordered_extent
->file_offset
,
1963 ordered_extent
->file_offset
+
1964 ordered_extent
->len
);
1966 BUG_ON(root
== root
->fs_info
->tree_root
);
1967 ret
= insert_reserved_file_extent(trans
, inode
,
1968 ordered_extent
->file_offset
,
1969 ordered_extent
->start
,
1970 ordered_extent
->disk_len
,
1971 ordered_extent
->len
,
1972 ordered_extent
->len
,
1973 compress_type
, 0, 0,
1974 BTRFS_FILE_EXTENT_REG
);
1976 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
1977 ordered_extent
->file_offset
, ordered_extent
->len
,
1980 btrfs_abort_transaction(trans
, root
, ret
);
1984 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
1985 &ordered_extent
->list
);
1987 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
1988 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
1989 if (ret
) { /* -ENOMEM or corruption */
1990 btrfs_abort_transaction(trans
, root
, ret
);
1995 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
1996 ordered_extent
->file_offset
+
1997 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
1999 if (root
!= root
->fs_info
->tree_root
)
2000 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
2002 btrfs_end_transaction(trans
, root
);
2005 clear_extent_uptodate(io_tree
, ordered_extent
->file_offset
,
2006 ordered_extent
->file_offset
+
2007 ordered_extent
->len
- 1, NULL
, GFP_NOFS
);
2010 * This needs to be done to make sure anybody waiting knows we are done
2011 * updating everything for this ordered extent.
2013 btrfs_remove_ordered_extent(inode
, ordered_extent
);
2016 btrfs_put_ordered_extent(ordered_extent
);
2017 /* once for the tree */
2018 btrfs_put_ordered_extent(ordered_extent
);
2023 static void finish_ordered_fn(struct btrfs_work
*work
)
2025 struct btrfs_ordered_extent
*ordered_extent
;
2026 ordered_extent
= container_of(work
, struct btrfs_ordered_extent
, work
);
2027 btrfs_finish_ordered_io(ordered_extent
);
2030 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2031 struct extent_state
*state
, int uptodate
)
2033 struct inode
*inode
= page
->mapping
->host
;
2034 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2035 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
2036 struct btrfs_workers
*workers
;
2038 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
2040 ClearPagePrivate2(page
);
2041 if (!btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
2042 end
- start
+ 1, uptodate
))
2045 ordered_extent
->work
.func
= finish_ordered_fn
;
2046 ordered_extent
->work
.flags
= 0;
2048 if (btrfs_is_free_space_inode(inode
))
2049 workers
= &root
->fs_info
->endio_freespace_worker
;
2051 workers
= &root
->fs_info
->endio_write_workers
;
2052 btrfs_queue_worker(workers
, &ordered_extent
->work
);
2058 * when reads are done, we need to check csums to verify the data is correct
2059 * if there's a match, we allow the bio to finish. If not, the code in
2060 * extent_io.c will try to find good copies for us.
2062 static int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2063 struct extent_state
*state
, int mirror
)
2065 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
2066 struct inode
*inode
= page
->mapping
->host
;
2067 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2069 u64
private = ~(u32
)0;
2071 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2074 if (PageChecked(page
)) {
2075 ClearPageChecked(page
);
2079 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
2082 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
2083 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
2084 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
2089 if (state
&& state
->start
== start
) {
2090 private = state
->private;
2093 ret
= get_state_private(io_tree
, start
, &private);
2095 kaddr
= kmap_atomic(page
);
2099 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
2100 btrfs_csum_final(csum
, (char *)&csum
);
2101 if (csum
!= private)
2104 kunmap_atomic(kaddr
);
2109 printk_ratelimited(KERN_INFO
"btrfs csum failed ino %llu off %llu csum %u "
2111 (unsigned long long)btrfs_ino(page
->mapping
->host
),
2112 (unsigned long long)start
, csum
,
2113 (unsigned long long)private);
2114 memset(kaddr
+ offset
, 1, end
- start
+ 1);
2115 flush_dcache_page(page
);
2116 kunmap_atomic(kaddr
);
2122 struct delayed_iput
{
2123 struct list_head list
;
2124 struct inode
*inode
;
2127 /* JDM: If this is fs-wide, why can't we add a pointer to
2128 * btrfs_inode instead and avoid the allocation? */
2129 void btrfs_add_delayed_iput(struct inode
*inode
)
2131 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2132 struct delayed_iput
*delayed
;
2134 if (atomic_add_unless(&inode
->i_count
, -1, 1))
2137 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
2138 delayed
->inode
= inode
;
2140 spin_lock(&fs_info
->delayed_iput_lock
);
2141 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
2142 spin_unlock(&fs_info
->delayed_iput_lock
);
2145 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
2148 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2149 struct delayed_iput
*delayed
;
2152 spin_lock(&fs_info
->delayed_iput_lock
);
2153 empty
= list_empty(&fs_info
->delayed_iputs
);
2154 spin_unlock(&fs_info
->delayed_iput_lock
);
2158 spin_lock(&fs_info
->delayed_iput_lock
);
2159 list_splice_init(&fs_info
->delayed_iputs
, &list
);
2160 spin_unlock(&fs_info
->delayed_iput_lock
);
2162 while (!list_empty(&list
)) {
2163 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
2164 list_del(&delayed
->list
);
2165 iput(delayed
->inode
);
2170 enum btrfs_orphan_cleanup_state
{
2171 ORPHAN_CLEANUP_STARTED
= 1,
2172 ORPHAN_CLEANUP_DONE
= 2,
2176 * This is called in transaction commit time. If there are no orphan
2177 * files in the subvolume, it removes orphan item and frees block_rsv
2180 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
2181 struct btrfs_root
*root
)
2183 struct btrfs_block_rsv
*block_rsv
;
2186 if (atomic_read(&root
->orphan_inodes
) ||
2187 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
2190 spin_lock(&root
->orphan_lock
);
2191 if (atomic_read(&root
->orphan_inodes
)) {
2192 spin_unlock(&root
->orphan_lock
);
2196 if (root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
) {
2197 spin_unlock(&root
->orphan_lock
);
2201 block_rsv
= root
->orphan_block_rsv
;
2202 root
->orphan_block_rsv
= NULL
;
2203 spin_unlock(&root
->orphan_lock
);
2205 if (root
->orphan_item_inserted
&&
2206 btrfs_root_refs(&root
->root_item
) > 0) {
2207 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
2208 root
->root_key
.objectid
);
2210 root
->orphan_item_inserted
= 0;
2214 WARN_ON(block_rsv
->size
> 0);
2215 btrfs_free_block_rsv(root
, block_rsv
);
2220 * This creates an orphan entry for the given inode in case something goes
2221 * wrong in the middle of an unlink/truncate.
2223 * NOTE: caller of this function should reserve 5 units of metadata for
2226 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2228 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2229 struct btrfs_block_rsv
*block_rsv
= NULL
;
2234 if (!root
->orphan_block_rsv
) {
2235 block_rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
2240 spin_lock(&root
->orphan_lock
);
2241 if (!root
->orphan_block_rsv
) {
2242 root
->orphan_block_rsv
= block_rsv
;
2243 } else if (block_rsv
) {
2244 btrfs_free_block_rsv(root
, block_rsv
);
2248 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2249 &BTRFS_I(inode
)->runtime_flags
)) {
2252 * For proper ENOSPC handling, we should do orphan
2253 * cleanup when mounting. But this introduces backward
2254 * compatibility issue.
2256 if (!xchg(&root
->orphan_item_inserted
, 1))
2262 atomic_inc(&root
->orphan_inodes
);
2265 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
2266 &BTRFS_I(inode
)->runtime_flags
))
2268 spin_unlock(&root
->orphan_lock
);
2270 /* grab metadata reservation from transaction handle */
2272 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
2273 BUG_ON(ret
); /* -ENOSPC in reservation; Logic error? JDM */
2276 /* insert an orphan item to track this unlinked/truncated file */
2278 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
2279 if (ret
&& ret
!= -EEXIST
) {
2280 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2281 &BTRFS_I(inode
)->runtime_flags
);
2282 btrfs_abort_transaction(trans
, root
, ret
);
2288 /* insert an orphan item to track subvolume contains orphan files */
2290 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
2291 root
->root_key
.objectid
);
2292 if (ret
&& ret
!= -EEXIST
) {
2293 btrfs_abort_transaction(trans
, root
, ret
);
2301 * We have done the truncate/delete so we can go ahead and remove the orphan
2302 * item for this particular inode.
2304 int btrfs_orphan_del(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2306 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2307 int delete_item
= 0;
2308 int release_rsv
= 0;
2311 spin_lock(&root
->orphan_lock
);
2312 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2313 &BTRFS_I(inode
)->runtime_flags
))
2316 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
2317 &BTRFS_I(inode
)->runtime_flags
))
2319 spin_unlock(&root
->orphan_lock
);
2321 if (trans
&& delete_item
) {
2322 ret
= btrfs_del_orphan_item(trans
, root
, btrfs_ino(inode
));
2323 BUG_ON(ret
); /* -ENOMEM or corruption (JDM: Recheck) */
2327 btrfs_orphan_release_metadata(inode
);
2328 atomic_dec(&root
->orphan_inodes
);
2335 * this cleans up any orphans that may be left on the list from the last use
2338 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
2340 struct btrfs_path
*path
;
2341 struct extent_buffer
*leaf
;
2342 struct btrfs_key key
, found_key
;
2343 struct btrfs_trans_handle
*trans
;
2344 struct inode
*inode
;
2345 u64 last_objectid
= 0;
2346 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
2348 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
2351 path
= btrfs_alloc_path();
2358 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
2359 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
2360 key
.offset
= (u64
)-1;
2363 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2368 * if ret == 0 means we found what we were searching for, which
2369 * is weird, but possible, so only screw with path if we didn't
2370 * find the key and see if we have stuff that matches
2374 if (path
->slots
[0] == 0)
2379 /* pull out the item */
2380 leaf
= path
->nodes
[0];
2381 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2383 /* make sure the item matches what we want */
2384 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
2386 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
2389 /* release the path since we're done with it */
2390 btrfs_release_path(path
);
2393 * this is where we are basically btrfs_lookup, without the
2394 * crossing root thing. we store the inode number in the
2395 * offset of the orphan item.
2398 if (found_key
.offset
== last_objectid
) {
2399 printk(KERN_ERR
"btrfs: Error removing orphan entry, "
2400 "stopping orphan cleanup\n");
2405 last_objectid
= found_key
.offset
;
2407 found_key
.objectid
= found_key
.offset
;
2408 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
2409 found_key
.offset
= 0;
2410 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
2411 ret
= PTR_RET(inode
);
2412 if (ret
&& ret
!= -ESTALE
)
2415 if (ret
== -ESTALE
&& root
== root
->fs_info
->tree_root
) {
2416 struct btrfs_root
*dead_root
;
2417 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2418 int is_dead_root
= 0;
2421 * this is an orphan in the tree root. Currently these
2422 * could come from 2 sources:
2423 * a) a snapshot deletion in progress
2424 * b) a free space cache inode
2425 * We need to distinguish those two, as the snapshot
2426 * orphan must not get deleted.
2427 * find_dead_roots already ran before us, so if this
2428 * is a snapshot deletion, we should find the root
2429 * in the dead_roots list
2431 spin_lock(&fs_info
->trans_lock
);
2432 list_for_each_entry(dead_root
, &fs_info
->dead_roots
,
2434 if (dead_root
->root_key
.objectid
==
2435 found_key
.objectid
) {
2440 spin_unlock(&fs_info
->trans_lock
);
2442 /* prevent this orphan from being found again */
2443 key
.offset
= found_key
.objectid
- 1;
2448 * Inode is already gone but the orphan item is still there,
2449 * kill the orphan item.
2451 if (ret
== -ESTALE
) {
2452 trans
= btrfs_start_transaction(root
, 1);
2453 if (IS_ERR(trans
)) {
2454 ret
= PTR_ERR(trans
);
2457 printk(KERN_ERR
"auto deleting %Lu\n",
2458 found_key
.objectid
);
2459 ret
= btrfs_del_orphan_item(trans
, root
,
2460 found_key
.objectid
);
2461 BUG_ON(ret
); /* -ENOMEM or corruption (JDM: Recheck) */
2462 btrfs_end_transaction(trans
, root
);
2467 * add this inode to the orphan list so btrfs_orphan_del does
2468 * the proper thing when we hit it
2470 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2471 &BTRFS_I(inode
)->runtime_flags
);
2473 /* if we have links, this was a truncate, lets do that */
2474 if (inode
->i_nlink
) {
2475 if (!S_ISREG(inode
->i_mode
)) {
2482 /* 1 for the orphan item deletion. */
2483 trans
= btrfs_start_transaction(root
, 1);
2484 if (IS_ERR(trans
)) {
2485 ret
= PTR_ERR(trans
);
2488 ret
= btrfs_orphan_add(trans
, inode
);
2489 btrfs_end_transaction(trans
, root
);
2493 ret
= btrfs_truncate(inode
);
2498 /* this will do delete_inode and everything for us */
2503 /* release the path since we're done with it */
2504 btrfs_release_path(path
);
2506 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
2508 if (root
->orphan_block_rsv
)
2509 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
2512 if (root
->orphan_block_rsv
|| root
->orphan_item_inserted
) {
2513 trans
= btrfs_join_transaction(root
);
2515 btrfs_end_transaction(trans
, root
);
2519 printk(KERN_INFO
"btrfs: unlinked %d orphans\n", nr_unlink
);
2521 printk(KERN_INFO
"btrfs: truncated %d orphans\n", nr_truncate
);
2525 printk(KERN_CRIT
"btrfs: could not do orphan cleanup %d\n", ret
);
2526 btrfs_free_path(path
);
2531 * very simple check to peek ahead in the leaf looking for xattrs. If we
2532 * don't find any xattrs, we know there can't be any acls.
2534 * slot is the slot the inode is in, objectid is the objectid of the inode
2536 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
2537 int slot
, u64 objectid
)
2539 u32 nritems
= btrfs_header_nritems(leaf
);
2540 struct btrfs_key found_key
;
2544 while (slot
< nritems
) {
2545 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2547 /* we found a different objectid, there must not be acls */
2548 if (found_key
.objectid
!= objectid
)
2551 /* we found an xattr, assume we've got an acl */
2552 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
)
2556 * we found a key greater than an xattr key, there can't
2557 * be any acls later on
2559 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
2566 * it goes inode, inode backrefs, xattrs, extents,
2567 * so if there are a ton of hard links to an inode there can
2568 * be a lot of backrefs. Don't waste time searching too hard,
2569 * this is just an optimization
2574 /* we hit the end of the leaf before we found an xattr or
2575 * something larger than an xattr. We have to assume the inode
2582 * read an inode from the btree into the in-memory inode
2584 static void btrfs_read_locked_inode(struct inode
*inode
)
2586 struct btrfs_path
*path
;
2587 struct extent_buffer
*leaf
;
2588 struct btrfs_inode_item
*inode_item
;
2589 struct btrfs_timespec
*tspec
;
2590 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2591 struct btrfs_key location
;
2595 bool filled
= false;
2597 ret
= btrfs_fill_inode(inode
, &rdev
);
2601 path
= btrfs_alloc_path();
2605 path
->leave_spinning
= 1;
2606 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
2608 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
2612 leaf
= path
->nodes
[0];
2617 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2618 struct btrfs_inode_item
);
2619 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
2620 set_nlink(inode
, btrfs_inode_nlink(leaf
, inode_item
));
2621 i_uid_write(inode
, btrfs_inode_uid(leaf
, inode_item
));
2622 i_gid_write(inode
, btrfs_inode_gid(leaf
, inode_item
));
2623 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
2625 tspec
= btrfs_inode_atime(inode_item
);
2626 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2627 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2629 tspec
= btrfs_inode_mtime(inode_item
);
2630 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2631 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2633 tspec
= btrfs_inode_ctime(inode_item
);
2634 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2635 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2637 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
2638 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
2639 BTRFS_I(inode
)->last_trans
= btrfs_inode_transid(leaf
, inode_item
);
2642 * If we were modified in the current generation and evicted from memory
2643 * and then re-read we need to do a full sync since we don't have any
2644 * idea about which extents were modified before we were evicted from
2647 if (BTRFS_I(inode
)->last_trans
== root
->fs_info
->generation
)
2648 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
2649 &BTRFS_I(inode
)->runtime_flags
);
2651 inode
->i_version
= btrfs_inode_sequence(leaf
, inode_item
);
2652 inode
->i_generation
= BTRFS_I(inode
)->generation
;
2654 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
2656 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
2657 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
2660 * try to precache a NULL acl entry for files that don't have
2661 * any xattrs or acls
2663 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
2666 cache_no_acl(inode
);
2668 btrfs_free_path(path
);
2670 switch (inode
->i_mode
& S_IFMT
) {
2672 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2673 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2674 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2675 inode
->i_fop
= &btrfs_file_operations
;
2676 inode
->i_op
= &btrfs_file_inode_operations
;
2679 inode
->i_fop
= &btrfs_dir_file_operations
;
2680 if (root
== root
->fs_info
->tree_root
)
2681 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
2683 inode
->i_op
= &btrfs_dir_inode_operations
;
2686 inode
->i_op
= &btrfs_symlink_inode_operations
;
2687 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2688 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2691 inode
->i_op
= &btrfs_special_inode_operations
;
2692 init_special_inode(inode
, inode
->i_mode
, rdev
);
2696 btrfs_update_iflags(inode
);
2700 btrfs_free_path(path
);
2701 make_bad_inode(inode
);
2705 * given a leaf and an inode, copy the inode fields into the leaf
2707 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
2708 struct extent_buffer
*leaf
,
2709 struct btrfs_inode_item
*item
,
2710 struct inode
*inode
)
2712 btrfs_set_inode_uid(leaf
, item
, i_uid_read(inode
));
2713 btrfs_set_inode_gid(leaf
, item
, i_gid_read(inode
));
2714 btrfs_set_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
);
2715 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
2716 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
2718 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
2719 inode
->i_atime
.tv_sec
);
2720 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
2721 inode
->i_atime
.tv_nsec
);
2723 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
2724 inode
->i_mtime
.tv_sec
);
2725 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
2726 inode
->i_mtime
.tv_nsec
);
2728 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
2729 inode
->i_ctime
.tv_sec
);
2730 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
2731 inode
->i_ctime
.tv_nsec
);
2733 btrfs_set_inode_nbytes(leaf
, item
, inode_get_bytes(inode
));
2734 btrfs_set_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
);
2735 btrfs_set_inode_sequence(leaf
, item
, inode
->i_version
);
2736 btrfs_set_inode_transid(leaf
, item
, trans
->transid
);
2737 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
2738 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
2739 btrfs_set_inode_block_group(leaf
, item
, 0);
2743 * copy everything in the in-memory inode into the btree.
2745 static noinline
int btrfs_update_inode_item(struct btrfs_trans_handle
*trans
,
2746 struct btrfs_root
*root
, struct inode
*inode
)
2748 struct btrfs_inode_item
*inode_item
;
2749 struct btrfs_path
*path
;
2750 struct extent_buffer
*leaf
;
2753 path
= btrfs_alloc_path();
2757 path
->leave_spinning
= 1;
2758 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
2766 btrfs_unlock_up_safe(path
, 1);
2767 leaf
= path
->nodes
[0];
2768 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2769 struct btrfs_inode_item
);
2771 fill_inode_item(trans
, leaf
, inode_item
, inode
);
2772 btrfs_mark_buffer_dirty(leaf
);
2773 btrfs_set_inode_last_trans(trans
, inode
);
2776 btrfs_free_path(path
);
2781 * copy everything in the in-memory inode into the btree.
2783 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
2784 struct btrfs_root
*root
, struct inode
*inode
)
2789 * If the inode is a free space inode, we can deadlock during commit
2790 * if we put it into the delayed code.
2792 * The data relocation inode should also be directly updated
2795 if (!btrfs_is_free_space_inode(inode
)
2796 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
) {
2797 btrfs_update_root_times(trans
, root
);
2799 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
2801 btrfs_set_inode_last_trans(trans
, inode
);
2805 return btrfs_update_inode_item(trans
, root
, inode
);
2808 noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
2809 struct btrfs_root
*root
,
2810 struct inode
*inode
)
2814 ret
= btrfs_update_inode(trans
, root
, inode
);
2816 return btrfs_update_inode_item(trans
, root
, inode
);
2821 * unlink helper that gets used here in inode.c and in the tree logging
2822 * recovery code. It remove a link in a directory with a given name, and
2823 * also drops the back refs in the inode to the directory
2825 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
2826 struct btrfs_root
*root
,
2827 struct inode
*dir
, struct inode
*inode
,
2828 const char *name
, int name_len
)
2830 struct btrfs_path
*path
;
2832 struct extent_buffer
*leaf
;
2833 struct btrfs_dir_item
*di
;
2834 struct btrfs_key key
;
2836 u64 ino
= btrfs_ino(inode
);
2837 u64 dir_ino
= btrfs_ino(dir
);
2839 path
= btrfs_alloc_path();
2845 path
->leave_spinning
= 1;
2846 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
2847 name
, name_len
, -1);
2856 leaf
= path
->nodes
[0];
2857 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2858 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2861 btrfs_release_path(path
);
2863 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
2866 printk(KERN_INFO
"btrfs failed to delete reference to %.*s, "
2867 "inode %llu parent %llu\n", name_len
, name
,
2868 (unsigned long long)ino
, (unsigned long long)dir_ino
);
2869 btrfs_abort_transaction(trans
, root
, ret
);
2873 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
2875 btrfs_abort_transaction(trans
, root
, ret
);
2879 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
2881 if (ret
!= 0 && ret
!= -ENOENT
) {
2882 btrfs_abort_transaction(trans
, root
, ret
);
2886 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
2891 btrfs_free_path(path
);
2895 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
2896 inode_inc_iversion(inode
);
2897 inode_inc_iversion(dir
);
2898 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
2899 ret
= btrfs_update_inode(trans
, root
, dir
);
2904 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
2905 struct btrfs_root
*root
,
2906 struct inode
*dir
, struct inode
*inode
,
2907 const char *name
, int name_len
)
2910 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
2912 btrfs_drop_nlink(inode
);
2913 ret
= btrfs_update_inode(trans
, root
, inode
);
2919 /* helper to check if there is any shared block in the path */
2920 static int check_path_shared(struct btrfs_root
*root
,
2921 struct btrfs_path
*path
)
2923 struct extent_buffer
*eb
;
2927 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
2930 if (!path
->nodes
[level
])
2932 eb
= path
->nodes
[level
];
2933 if (!btrfs_block_can_be_shared(root
, eb
))
2935 ret
= btrfs_lookup_extent_info(NULL
, root
, eb
->start
, eb
->len
,
2944 * helper to start transaction for unlink and rmdir.
2946 * unlink and rmdir are special in btrfs, they do not always free space.
2947 * so in enospc case, we should make sure they will free space before
2948 * allowing them to use the global metadata reservation.
2950 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
,
2951 struct dentry
*dentry
)
2953 struct btrfs_trans_handle
*trans
;
2954 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2955 struct btrfs_path
*path
;
2956 struct btrfs_dir_item
*di
;
2957 struct inode
*inode
= dentry
->d_inode
;
2962 u64 ino
= btrfs_ino(inode
);
2963 u64 dir_ino
= btrfs_ino(dir
);
2966 * 1 for the possible orphan item
2967 * 1 for the dir item
2968 * 1 for the dir index
2969 * 1 for the inode ref
2970 * 1 for the inode ref in the tree log
2971 * 2 for the dir entries in the log
2974 trans
= btrfs_start_transaction(root
, 8);
2975 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
2978 if (ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
2979 return ERR_PTR(-ENOSPC
);
2981 /* check if there is someone else holds reference */
2982 if (S_ISDIR(inode
->i_mode
) && atomic_read(&inode
->i_count
) > 1)
2983 return ERR_PTR(-ENOSPC
);
2985 if (atomic_read(&inode
->i_count
) > 2)
2986 return ERR_PTR(-ENOSPC
);
2988 if (xchg(&root
->fs_info
->enospc_unlink
, 1))
2989 return ERR_PTR(-ENOSPC
);
2991 path
= btrfs_alloc_path();
2993 root
->fs_info
->enospc_unlink
= 0;
2994 return ERR_PTR(-ENOMEM
);
2997 /* 1 for the orphan item */
2998 trans
= btrfs_start_transaction(root
, 1);
2999 if (IS_ERR(trans
)) {
3000 btrfs_free_path(path
);
3001 root
->fs_info
->enospc_unlink
= 0;
3005 path
->skip_locking
= 1;
3006 path
->search_commit_root
= 1;
3008 ret
= btrfs_lookup_inode(trans
, root
, path
,
3009 &BTRFS_I(dir
)->location
, 0);
3015 if (check_path_shared(root
, path
))
3020 btrfs_release_path(path
);
3022 ret
= btrfs_lookup_inode(trans
, root
, path
,
3023 &BTRFS_I(inode
)->location
, 0);
3029 if (check_path_shared(root
, path
))
3034 btrfs_release_path(path
);
3036 if (ret
== 0 && S_ISREG(inode
->i_mode
)) {
3037 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
3043 BUG_ON(ret
== 0); /* Corruption */
3044 if (check_path_shared(root
, path
))
3046 btrfs_release_path(path
);
3054 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3055 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
3061 if (check_path_shared(root
, path
))
3067 btrfs_release_path(path
);
3069 ret
= btrfs_get_inode_ref_index(trans
, root
, path
, dentry
->d_name
.name
,
3070 dentry
->d_name
.len
, ino
, dir_ino
, 0,
3077 if (check_path_shared(root
, path
))
3080 btrfs_release_path(path
);
3083 * This is a commit root search, if we can lookup inode item and other
3084 * relative items in the commit root, it means the transaction of
3085 * dir/file creation has been committed, and the dir index item that we
3086 * delay to insert has also been inserted into the commit root. So
3087 * we needn't worry about the delayed insertion of the dir index item
3090 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir_ino
, index
,
3091 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
3096 BUG_ON(ret
== -ENOENT
);
3097 if (check_path_shared(root
, path
))
3102 btrfs_free_path(path
);
3103 /* Migrate the orphan reservation over */
3105 err
= btrfs_block_rsv_migrate(trans
->block_rsv
,
3106 &root
->fs_info
->global_block_rsv
,
3107 trans
->bytes_reserved
);
3110 btrfs_end_transaction(trans
, root
);
3111 root
->fs_info
->enospc_unlink
= 0;
3112 return ERR_PTR(err
);
3115 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
3119 static void __unlink_end_trans(struct btrfs_trans_handle
*trans
,
3120 struct btrfs_root
*root
)
3122 if (trans
->block_rsv
->type
== BTRFS_BLOCK_RSV_GLOBAL
) {
3123 btrfs_block_rsv_release(root
, trans
->block_rsv
,
3124 trans
->bytes_reserved
);
3125 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
3126 BUG_ON(!root
->fs_info
->enospc_unlink
);
3127 root
->fs_info
->enospc_unlink
= 0;
3129 btrfs_end_transaction(trans
, root
);
3132 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
3134 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3135 struct btrfs_trans_handle
*trans
;
3136 struct inode
*inode
= dentry
->d_inode
;
3139 trans
= __unlink_start_trans(dir
, dentry
);
3141 return PTR_ERR(trans
);
3143 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
3145 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3146 dentry
->d_name
.name
, dentry
->d_name
.len
);
3150 if (inode
->i_nlink
== 0) {
3151 ret
= btrfs_orphan_add(trans
, inode
);
3157 __unlink_end_trans(trans
, root
);
3158 btrfs_btree_balance_dirty(root
);
3162 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
3163 struct btrfs_root
*root
,
3164 struct inode
*dir
, u64 objectid
,
3165 const char *name
, int name_len
)
3167 struct btrfs_path
*path
;
3168 struct extent_buffer
*leaf
;
3169 struct btrfs_dir_item
*di
;
3170 struct btrfs_key key
;
3173 u64 dir_ino
= btrfs_ino(dir
);
3175 path
= btrfs_alloc_path();
3179 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3180 name
, name_len
, -1);
3181 if (IS_ERR_OR_NULL(di
)) {
3189 leaf
= path
->nodes
[0];
3190 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3191 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
3192 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3194 btrfs_abort_transaction(trans
, root
, ret
);
3197 btrfs_release_path(path
);
3199 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
3200 objectid
, root
->root_key
.objectid
,
3201 dir_ino
, &index
, name
, name_len
);
3203 if (ret
!= -ENOENT
) {
3204 btrfs_abort_transaction(trans
, root
, ret
);
3207 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
3209 if (IS_ERR_OR_NULL(di
)) {
3214 btrfs_abort_transaction(trans
, root
, ret
);
3218 leaf
= path
->nodes
[0];
3219 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3220 btrfs_release_path(path
);
3223 btrfs_release_path(path
);
3225 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3227 btrfs_abort_transaction(trans
, root
, ret
);
3231 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3232 inode_inc_iversion(dir
);
3233 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3234 ret
= btrfs_update_inode_fallback(trans
, root
, dir
);
3236 btrfs_abort_transaction(trans
, root
, ret
);
3238 btrfs_free_path(path
);
3242 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3244 struct inode
*inode
= dentry
->d_inode
;
3246 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3247 struct btrfs_trans_handle
*trans
;
3249 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
3251 if (btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
)
3254 trans
= __unlink_start_trans(dir
, dentry
);
3256 return PTR_ERR(trans
);
3258 if (unlikely(btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
3259 err
= btrfs_unlink_subvol(trans
, root
, dir
,
3260 BTRFS_I(inode
)->location
.objectid
,
3261 dentry
->d_name
.name
,
3262 dentry
->d_name
.len
);
3266 err
= btrfs_orphan_add(trans
, inode
);
3270 /* now the directory is empty */
3271 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3272 dentry
->d_name
.name
, dentry
->d_name
.len
);
3274 btrfs_i_size_write(inode
, 0);
3276 __unlink_end_trans(trans
, root
);
3277 btrfs_btree_balance_dirty(root
);
3283 * this can truncate away extent items, csum items and directory items.
3284 * It starts at a high offset and removes keys until it can't find
3285 * any higher than new_size
3287 * csum items that cross the new i_size are truncated to the new size
3290 * min_type is the minimum key type to truncate down to. If set to 0, this
3291 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3293 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
3294 struct btrfs_root
*root
,
3295 struct inode
*inode
,
3296 u64 new_size
, u32 min_type
)
3298 struct btrfs_path
*path
;
3299 struct extent_buffer
*leaf
;
3300 struct btrfs_file_extent_item
*fi
;
3301 struct btrfs_key key
;
3302 struct btrfs_key found_key
;
3303 u64 extent_start
= 0;
3304 u64 extent_num_bytes
= 0;
3305 u64 extent_offset
= 0;
3307 u64 mask
= root
->sectorsize
- 1;
3308 u32 found_type
= (u8
)-1;
3311 int pending_del_nr
= 0;
3312 int pending_del_slot
= 0;
3313 int extent_type
= -1;
3316 u64 ino
= btrfs_ino(inode
);
3318 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
3320 path
= btrfs_alloc_path();
3326 * We want to drop from the next block forward in case this new size is
3327 * not block aligned since we will be keeping the last block of the
3328 * extent just the way it is.
3330 if (root
->ref_cows
|| root
== root
->fs_info
->tree_root
)
3331 btrfs_drop_extent_cache(inode
, (new_size
+ mask
) & (~mask
), (u64
)-1, 0);
3334 * This function is also used to drop the items in the log tree before
3335 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3336 * it is used to drop the loged items. So we shouldn't kill the delayed
3339 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
3340 btrfs_kill_delayed_inode_items(inode
);
3343 key
.offset
= (u64
)-1;
3347 path
->leave_spinning
= 1;
3348 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3355 /* there are no items in the tree for us to truncate, we're
3358 if (path
->slots
[0] == 0)
3365 leaf
= path
->nodes
[0];
3366 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3367 found_type
= btrfs_key_type(&found_key
);
3369 if (found_key
.objectid
!= ino
)
3372 if (found_type
< min_type
)
3375 item_end
= found_key
.offset
;
3376 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
3377 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3378 struct btrfs_file_extent_item
);
3379 extent_type
= btrfs_file_extent_type(leaf
, fi
);
3380 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3382 btrfs_file_extent_num_bytes(leaf
, fi
);
3383 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3384 item_end
+= btrfs_file_extent_inline_len(leaf
,
3389 if (found_type
> min_type
) {
3392 if (item_end
< new_size
)
3394 if (found_key
.offset
>= new_size
)
3400 /* FIXME, shrink the extent if the ref count is only 1 */
3401 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
3404 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3406 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
3408 u64 orig_num_bytes
=
3409 btrfs_file_extent_num_bytes(leaf
, fi
);
3410 extent_num_bytes
= new_size
-
3411 found_key
.offset
+ root
->sectorsize
- 1;
3412 extent_num_bytes
= extent_num_bytes
&
3413 ~((u64
)root
->sectorsize
- 1);
3414 btrfs_set_file_extent_num_bytes(leaf
, fi
,
3416 num_dec
= (orig_num_bytes
-
3418 if (root
->ref_cows
&& extent_start
!= 0)
3419 inode_sub_bytes(inode
, num_dec
);
3420 btrfs_mark_buffer_dirty(leaf
);
3423 btrfs_file_extent_disk_num_bytes(leaf
,
3425 extent_offset
= found_key
.offset
-
3426 btrfs_file_extent_offset(leaf
, fi
);
3428 /* FIXME blocksize != 4096 */
3429 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
3430 if (extent_start
!= 0) {
3433 inode_sub_bytes(inode
, num_dec
);
3436 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3438 * we can't truncate inline items that have had
3442 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
3443 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
3444 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
3445 u32 size
= new_size
- found_key
.offset
;
3447 if (root
->ref_cows
) {
3448 inode_sub_bytes(inode
, item_end
+ 1 -
3452 btrfs_file_extent_calc_inline_size(size
);
3453 btrfs_truncate_item(trans
, root
, path
,
3455 } else if (root
->ref_cows
) {
3456 inode_sub_bytes(inode
, item_end
+ 1 -
3462 if (!pending_del_nr
) {
3463 /* no pending yet, add ourselves */
3464 pending_del_slot
= path
->slots
[0];
3466 } else if (pending_del_nr
&&
3467 path
->slots
[0] + 1 == pending_del_slot
) {
3468 /* hop on the pending chunk */
3470 pending_del_slot
= path
->slots
[0];
3477 if (found_extent
&& (root
->ref_cows
||
3478 root
== root
->fs_info
->tree_root
)) {
3479 btrfs_set_path_blocking(path
);
3480 ret
= btrfs_free_extent(trans
, root
, extent_start
,
3481 extent_num_bytes
, 0,
3482 btrfs_header_owner(leaf
),
3483 ino
, extent_offset
, 0);
3487 if (found_type
== BTRFS_INODE_ITEM_KEY
)
3490 if (path
->slots
[0] == 0 ||
3491 path
->slots
[0] != pending_del_slot
) {
3492 if (pending_del_nr
) {
3493 ret
= btrfs_del_items(trans
, root
, path
,
3497 btrfs_abort_transaction(trans
,
3503 btrfs_release_path(path
);
3510 if (pending_del_nr
) {
3511 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
3514 btrfs_abort_transaction(trans
, root
, ret
);
3517 btrfs_free_path(path
);
3522 * btrfs_truncate_page - read, zero a chunk and write a page
3523 * @inode - inode that we're zeroing
3524 * @from - the offset to start zeroing
3525 * @len - the length to zero, 0 to zero the entire range respective to the
3527 * @front - zero up to the offset instead of from the offset on
3529 * This will find the page for the "from" offset and cow the page and zero the
3530 * part we want to zero. This is used with truncate and hole punching.
3532 int btrfs_truncate_page(struct inode
*inode
, loff_t from
, loff_t len
,
3535 struct address_space
*mapping
= inode
->i_mapping
;
3536 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3537 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3538 struct btrfs_ordered_extent
*ordered
;
3539 struct extent_state
*cached_state
= NULL
;
3541 u32 blocksize
= root
->sectorsize
;
3542 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
3543 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
3545 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
3550 if ((offset
& (blocksize
- 1)) == 0 &&
3551 (!len
|| ((len
& (blocksize
- 1)) == 0)))
3553 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
3558 page
= find_or_create_page(mapping
, index
, mask
);
3560 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
3565 page_start
= page_offset(page
);
3566 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3568 if (!PageUptodate(page
)) {
3569 ret
= btrfs_readpage(NULL
, page
);
3571 if (page
->mapping
!= mapping
) {
3573 page_cache_release(page
);
3576 if (!PageUptodate(page
)) {
3581 wait_on_page_writeback(page
);
3583 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
3584 set_page_extent_mapped(page
);
3586 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
3588 unlock_extent_cached(io_tree
, page_start
, page_end
,
3589 &cached_state
, GFP_NOFS
);
3591 page_cache_release(page
);
3592 btrfs_start_ordered_extent(inode
, ordered
, 1);
3593 btrfs_put_ordered_extent(ordered
);
3597 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
3598 EXTENT_DIRTY
| EXTENT_DELALLOC
|
3599 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
3600 0, 0, &cached_state
, GFP_NOFS
);
3602 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
3605 unlock_extent_cached(io_tree
, page_start
, page_end
,
3606 &cached_state
, GFP_NOFS
);
3610 if (offset
!= PAGE_CACHE_SIZE
) {
3612 len
= PAGE_CACHE_SIZE
- offset
;
3615 memset(kaddr
, 0, offset
);
3617 memset(kaddr
+ offset
, 0, len
);
3618 flush_dcache_page(page
);
3621 ClearPageChecked(page
);
3622 set_page_dirty(page
);
3623 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
3628 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
3630 page_cache_release(page
);
3636 * This function puts in dummy file extents for the area we're creating a hole
3637 * for. So if we are truncating this file to a larger size we need to insert
3638 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3639 * the range between oldsize and size
3641 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
3643 struct btrfs_trans_handle
*trans
;
3644 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3645 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3646 struct extent_map
*em
= NULL
;
3647 struct extent_state
*cached_state
= NULL
;
3648 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
3649 u64 mask
= root
->sectorsize
- 1;
3650 u64 hole_start
= (oldsize
+ mask
) & ~mask
;
3651 u64 block_end
= (size
+ mask
) & ~mask
;
3657 if (size
<= hole_start
)
3661 struct btrfs_ordered_extent
*ordered
;
3662 btrfs_wait_ordered_range(inode
, hole_start
,
3663 block_end
- hole_start
);
3664 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
3666 ordered
= btrfs_lookup_ordered_extent(inode
, hole_start
);
3669 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
3670 &cached_state
, GFP_NOFS
);
3671 btrfs_put_ordered_extent(ordered
);
3674 cur_offset
= hole_start
;
3676 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
3677 block_end
- cur_offset
, 0);
3683 last_byte
= min(extent_map_end(em
), block_end
);
3684 last_byte
= (last_byte
+ mask
) & ~mask
;
3685 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
3686 struct extent_map
*hole_em
;
3687 hole_size
= last_byte
- cur_offset
;
3689 trans
= btrfs_start_transaction(root
, 3);
3690 if (IS_ERR(trans
)) {
3691 err
= PTR_ERR(trans
);
3695 err
= btrfs_drop_extents(trans
, root
, inode
,
3697 cur_offset
+ hole_size
, 1);
3699 btrfs_abort_transaction(trans
, root
, err
);
3700 btrfs_end_transaction(trans
, root
);
3704 err
= btrfs_insert_file_extent(trans
, root
,
3705 btrfs_ino(inode
), cur_offset
, 0,
3706 0, hole_size
, 0, hole_size
,
3709 btrfs_abort_transaction(trans
, root
, err
);
3710 btrfs_end_transaction(trans
, root
);
3714 btrfs_drop_extent_cache(inode
, cur_offset
,
3715 cur_offset
+ hole_size
- 1, 0);
3716 hole_em
= alloc_extent_map();
3718 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3719 &BTRFS_I(inode
)->runtime_flags
);
3722 hole_em
->start
= cur_offset
;
3723 hole_em
->len
= hole_size
;
3724 hole_em
->orig_start
= cur_offset
;
3726 hole_em
->block_start
= EXTENT_MAP_HOLE
;
3727 hole_em
->block_len
= 0;
3728 hole_em
->orig_block_len
= 0;
3729 hole_em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
3730 hole_em
->compress_type
= BTRFS_COMPRESS_NONE
;
3731 hole_em
->generation
= trans
->transid
;
3734 write_lock(&em_tree
->lock
);
3735 err
= add_extent_mapping(em_tree
, hole_em
);
3737 list_move(&hole_em
->list
,
3738 &em_tree
->modified_extents
);
3739 write_unlock(&em_tree
->lock
);
3742 btrfs_drop_extent_cache(inode
, cur_offset
,
3746 free_extent_map(hole_em
);
3748 btrfs_update_inode(trans
, root
, inode
);
3749 btrfs_end_transaction(trans
, root
);
3751 free_extent_map(em
);
3753 cur_offset
= last_byte
;
3754 if (cur_offset
>= block_end
)
3758 free_extent_map(em
);
3759 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
3764 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
)
3766 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3767 struct btrfs_trans_handle
*trans
;
3768 loff_t oldsize
= i_size_read(inode
);
3769 loff_t newsize
= attr
->ia_size
;
3770 int mask
= attr
->ia_valid
;
3773 if (newsize
== oldsize
)
3777 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
3778 * special case where we need to update the times despite not having
3779 * these flags set. For all other operations the VFS set these flags
3780 * explicitly if it wants a timestamp update.
3782 if (newsize
!= oldsize
&& (!(mask
& (ATTR_CTIME
| ATTR_MTIME
))))
3783 inode
->i_ctime
= inode
->i_mtime
= current_fs_time(inode
->i_sb
);
3785 if (newsize
> oldsize
) {
3786 truncate_pagecache(inode
, oldsize
, newsize
);
3787 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
3791 trans
= btrfs_start_transaction(root
, 1);
3793 return PTR_ERR(trans
);
3795 i_size_write(inode
, newsize
);
3796 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
3797 ret
= btrfs_update_inode(trans
, root
, inode
);
3798 btrfs_end_transaction(trans
, root
);
3802 * We're truncating a file that used to have good data down to
3803 * zero. Make sure it gets into the ordered flush list so that
3804 * any new writes get down to disk quickly.
3807 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
3808 &BTRFS_I(inode
)->runtime_flags
);
3811 * 1 for the orphan item we're going to add
3812 * 1 for the orphan item deletion.
3814 trans
= btrfs_start_transaction(root
, 2);
3816 return PTR_ERR(trans
);
3819 * We need to do this in case we fail at _any_ point during the
3820 * actual truncate. Once we do the truncate_setsize we could
3821 * invalidate pages which forces any outstanding ordered io to
3822 * be instantly completed which will give us extents that need
3823 * to be truncated. If we fail to get an orphan inode down we
3824 * could have left over extents that were never meant to live,
3825 * so we need to garuntee from this point on that everything
3826 * will be consistent.
3828 ret
= btrfs_orphan_add(trans
, inode
);
3829 btrfs_end_transaction(trans
, root
);
3833 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3834 truncate_setsize(inode
, newsize
);
3835 ret
= btrfs_truncate(inode
);
3836 if (ret
&& inode
->i_nlink
)
3837 btrfs_orphan_del(NULL
, inode
);
3843 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
3845 struct inode
*inode
= dentry
->d_inode
;
3846 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3849 if (btrfs_root_readonly(root
))
3852 err
= inode_change_ok(inode
, attr
);
3856 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
3857 err
= btrfs_setsize(inode
, attr
);
3862 if (attr
->ia_valid
) {
3863 setattr_copy(inode
, attr
);
3864 inode_inc_iversion(inode
);
3865 err
= btrfs_dirty_inode(inode
);
3867 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
3868 err
= btrfs_acl_chmod(inode
);
3874 void btrfs_evict_inode(struct inode
*inode
)
3876 struct btrfs_trans_handle
*trans
;
3877 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3878 struct btrfs_block_rsv
*rsv
, *global_rsv
;
3879 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
3882 trace_btrfs_inode_evict(inode
);
3884 truncate_inode_pages(&inode
->i_data
, 0);
3885 if (inode
->i_nlink
&& (btrfs_root_refs(&root
->root_item
) != 0 ||
3886 btrfs_is_free_space_inode(inode
)))
3889 if (is_bad_inode(inode
)) {
3890 btrfs_orphan_del(NULL
, inode
);
3893 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
3894 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
3896 if (root
->fs_info
->log_root_recovering
) {
3897 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3898 &BTRFS_I(inode
)->runtime_flags
));
3902 if (inode
->i_nlink
> 0) {
3903 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0);
3907 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
3909 btrfs_orphan_del(NULL
, inode
);
3912 rsv
->size
= min_size
;
3914 global_rsv
= &root
->fs_info
->global_block_rsv
;
3916 btrfs_i_size_write(inode
, 0);
3919 * This is a bit simpler than btrfs_truncate since we've already
3920 * reserved our space for our orphan item in the unlink, so we just
3921 * need to reserve some slack space in case we add bytes and update
3922 * inode item when doing the truncate.
3925 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
,
3926 BTRFS_RESERVE_FLUSH_LIMIT
);
3929 * Try and steal from the global reserve since we will
3930 * likely not use this space anyway, we want to try as
3931 * hard as possible to get this to work.
3934 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
, min_size
);
3937 printk(KERN_WARNING
"Could not get space for a "
3938 "delete, will truncate on mount %d\n", ret
);
3939 btrfs_orphan_del(NULL
, inode
);
3940 btrfs_free_block_rsv(root
, rsv
);
3944 trans
= btrfs_start_transaction_lflush(root
, 1);
3945 if (IS_ERR(trans
)) {
3946 btrfs_orphan_del(NULL
, inode
);
3947 btrfs_free_block_rsv(root
, rsv
);
3951 trans
->block_rsv
= rsv
;
3953 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
3957 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
3958 ret
= btrfs_update_inode(trans
, root
, inode
);
3961 btrfs_end_transaction(trans
, root
);
3963 btrfs_btree_balance_dirty(root
);
3966 btrfs_free_block_rsv(root
, rsv
);
3969 trans
->block_rsv
= root
->orphan_block_rsv
;
3970 ret
= btrfs_orphan_del(trans
, inode
);
3974 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
3975 if (!(root
== root
->fs_info
->tree_root
||
3976 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
3977 btrfs_return_ino(root
, btrfs_ino(inode
));
3979 btrfs_end_transaction(trans
, root
);
3980 btrfs_btree_balance_dirty(root
);
3987 * this returns the key found in the dir entry in the location pointer.
3988 * If no dir entries were found, location->objectid is 0.
3990 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
3991 struct btrfs_key
*location
)
3993 const char *name
= dentry
->d_name
.name
;
3994 int namelen
= dentry
->d_name
.len
;
3995 struct btrfs_dir_item
*di
;
3996 struct btrfs_path
*path
;
3997 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4000 path
= btrfs_alloc_path();
4004 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(dir
), name
,
4009 if (IS_ERR_OR_NULL(di
))
4012 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
4014 btrfs_free_path(path
);
4017 location
->objectid
= 0;
4022 * when we hit a tree root in a directory, the btrfs part of the inode
4023 * needs to be changed to reflect the root directory of the tree root. This
4024 * is kind of like crossing a mount point.
4026 static int fixup_tree_root_location(struct btrfs_root
*root
,
4028 struct dentry
*dentry
,
4029 struct btrfs_key
*location
,
4030 struct btrfs_root
**sub_root
)
4032 struct btrfs_path
*path
;
4033 struct btrfs_root
*new_root
;
4034 struct btrfs_root_ref
*ref
;
4035 struct extent_buffer
*leaf
;
4039 path
= btrfs_alloc_path();
4046 ret
= btrfs_find_root_ref(root
->fs_info
->tree_root
, path
,
4047 BTRFS_I(dir
)->root
->root_key
.objectid
,
4048 location
->objectid
);
4055 leaf
= path
->nodes
[0];
4056 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
4057 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(dir
) ||
4058 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
4061 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
4062 (unsigned long)(ref
+ 1),
4063 dentry
->d_name
.len
);
4067 btrfs_release_path(path
);
4069 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
4070 if (IS_ERR(new_root
)) {
4071 err
= PTR_ERR(new_root
);
4075 if (btrfs_root_refs(&new_root
->root_item
) == 0) {
4080 *sub_root
= new_root
;
4081 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
4082 location
->type
= BTRFS_INODE_ITEM_KEY
;
4083 location
->offset
= 0;
4086 btrfs_free_path(path
);
4090 static void inode_tree_add(struct inode
*inode
)
4092 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4093 struct btrfs_inode
*entry
;
4095 struct rb_node
*parent
;
4096 u64 ino
= btrfs_ino(inode
);
4098 p
= &root
->inode_tree
.rb_node
;
4101 if (inode_unhashed(inode
))
4104 spin_lock(&root
->inode_lock
);
4107 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
4109 if (ino
< btrfs_ino(&entry
->vfs_inode
))
4110 p
= &parent
->rb_left
;
4111 else if (ino
> btrfs_ino(&entry
->vfs_inode
))
4112 p
= &parent
->rb_right
;
4114 WARN_ON(!(entry
->vfs_inode
.i_state
&
4115 (I_WILL_FREE
| I_FREEING
)));
4116 rb_erase(parent
, &root
->inode_tree
);
4117 RB_CLEAR_NODE(parent
);
4118 spin_unlock(&root
->inode_lock
);
4122 rb_link_node(&BTRFS_I(inode
)->rb_node
, parent
, p
);
4123 rb_insert_color(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4124 spin_unlock(&root
->inode_lock
);
4127 static void inode_tree_del(struct inode
*inode
)
4129 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4132 spin_lock(&root
->inode_lock
);
4133 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
4134 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4135 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
4136 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4138 spin_unlock(&root
->inode_lock
);
4141 * Free space cache has inodes in the tree root, but the tree root has a
4142 * root_refs of 0, so this could end up dropping the tree root as a
4143 * snapshot, so we need the extra !root->fs_info->tree_root check to
4144 * make sure we don't drop it.
4146 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0 &&
4147 root
!= root
->fs_info
->tree_root
) {
4148 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
4149 spin_lock(&root
->inode_lock
);
4150 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4151 spin_unlock(&root
->inode_lock
);
4153 btrfs_add_dead_root(root
);
4157 void btrfs_invalidate_inodes(struct btrfs_root
*root
)
4159 struct rb_node
*node
;
4160 struct rb_node
*prev
;
4161 struct btrfs_inode
*entry
;
4162 struct inode
*inode
;
4165 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
4167 spin_lock(&root
->inode_lock
);
4169 node
= root
->inode_tree
.rb_node
;
4173 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4175 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
4176 node
= node
->rb_left
;
4177 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
4178 node
= node
->rb_right
;
4184 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
4185 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
4189 prev
= rb_next(prev
);
4193 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4194 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
4195 inode
= igrab(&entry
->vfs_inode
);
4197 spin_unlock(&root
->inode_lock
);
4198 if (atomic_read(&inode
->i_count
) > 1)
4199 d_prune_aliases(inode
);
4201 * btrfs_drop_inode will have it removed from
4202 * the inode cache when its usage count
4207 spin_lock(&root
->inode_lock
);
4211 if (cond_resched_lock(&root
->inode_lock
))
4214 node
= rb_next(node
);
4216 spin_unlock(&root
->inode_lock
);
4219 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
4221 struct btrfs_iget_args
*args
= p
;
4222 inode
->i_ino
= args
->ino
;
4223 BTRFS_I(inode
)->root
= args
->root
;
4227 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
4229 struct btrfs_iget_args
*args
= opaque
;
4230 return args
->ino
== btrfs_ino(inode
) &&
4231 args
->root
== BTRFS_I(inode
)->root
;
4234 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
4236 struct btrfs_root
*root
)
4238 struct inode
*inode
;
4239 struct btrfs_iget_args args
;
4240 args
.ino
= objectid
;
4243 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
4244 btrfs_init_locked_inode
,
4249 /* Get an inode object given its location and corresponding root.
4250 * Returns in *is_new if the inode was read from disk
4252 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
4253 struct btrfs_root
*root
, int *new)
4255 struct inode
*inode
;
4257 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
4259 return ERR_PTR(-ENOMEM
);
4261 if (inode
->i_state
& I_NEW
) {
4262 BTRFS_I(inode
)->root
= root
;
4263 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
4264 btrfs_read_locked_inode(inode
);
4265 if (!is_bad_inode(inode
)) {
4266 inode_tree_add(inode
);
4267 unlock_new_inode(inode
);
4271 unlock_new_inode(inode
);
4273 inode
= ERR_PTR(-ESTALE
);
4280 static struct inode
*new_simple_dir(struct super_block
*s
,
4281 struct btrfs_key
*key
,
4282 struct btrfs_root
*root
)
4284 struct inode
*inode
= new_inode(s
);
4287 return ERR_PTR(-ENOMEM
);
4289 BTRFS_I(inode
)->root
= root
;
4290 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
4291 set_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
);
4293 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
4294 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
4295 inode
->i_fop
= &simple_dir_operations
;
4296 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
4297 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4302 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
4304 struct inode
*inode
;
4305 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4306 struct btrfs_root
*sub_root
= root
;
4307 struct btrfs_key location
;
4311 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
4312 return ERR_PTR(-ENAMETOOLONG
);
4314 if (unlikely(d_need_lookup(dentry
))) {
4315 memcpy(&location
, dentry
->d_fsdata
, sizeof(struct btrfs_key
));
4316 kfree(dentry
->d_fsdata
);
4317 dentry
->d_fsdata
= NULL
;
4318 /* This thing is hashed, drop it for now */
4321 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
4325 return ERR_PTR(ret
);
4327 if (location
.objectid
== 0)
4330 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
4331 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
4335 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
4337 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
4338 ret
= fixup_tree_root_location(root
, dir
, dentry
,
4339 &location
, &sub_root
);
4342 inode
= ERR_PTR(ret
);
4344 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
4346 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
4348 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
4350 if (!IS_ERR(inode
) && root
!= sub_root
) {
4351 down_read(&root
->fs_info
->cleanup_work_sem
);
4352 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
4353 ret
= btrfs_orphan_cleanup(sub_root
);
4354 up_read(&root
->fs_info
->cleanup_work_sem
);
4356 inode
= ERR_PTR(ret
);
4362 static int btrfs_dentry_delete(const struct dentry
*dentry
)
4364 struct btrfs_root
*root
;
4365 struct inode
*inode
= dentry
->d_inode
;
4367 if (!inode
&& !IS_ROOT(dentry
))
4368 inode
= dentry
->d_parent
->d_inode
;
4371 root
= BTRFS_I(inode
)->root
;
4372 if (btrfs_root_refs(&root
->root_item
) == 0)
4375 if (btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
4381 static void btrfs_dentry_release(struct dentry
*dentry
)
4383 if (dentry
->d_fsdata
)
4384 kfree(dentry
->d_fsdata
);
4387 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
4392 ret
= d_splice_alias(btrfs_lookup_dentry(dir
, dentry
), dentry
);
4393 if (unlikely(d_need_lookup(dentry
))) {
4394 spin_lock(&dentry
->d_lock
);
4395 dentry
->d_flags
&= ~DCACHE_NEED_LOOKUP
;
4396 spin_unlock(&dentry
->d_lock
);
4401 unsigned char btrfs_filetype_table
[] = {
4402 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
4405 static int btrfs_real_readdir(struct file
*filp
, void *dirent
,
4408 struct inode
*inode
= filp
->f_dentry
->d_inode
;
4409 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4410 struct btrfs_item
*item
;
4411 struct btrfs_dir_item
*di
;
4412 struct btrfs_key key
;
4413 struct btrfs_key found_key
;
4414 struct btrfs_path
*path
;
4415 struct list_head ins_list
;
4416 struct list_head del_list
;
4418 struct extent_buffer
*leaf
;
4420 unsigned char d_type
;
4425 int key_type
= BTRFS_DIR_INDEX_KEY
;
4429 int is_curr
= 0; /* filp->f_pos points to the current index? */
4431 /* FIXME, use a real flag for deciding about the key type */
4432 if (root
->fs_info
->tree_root
== root
)
4433 key_type
= BTRFS_DIR_ITEM_KEY
;
4435 /* special case for "." */
4436 if (filp
->f_pos
== 0) {
4437 over
= filldir(dirent
, ".", 1,
4438 filp
->f_pos
, btrfs_ino(inode
), DT_DIR
);
4443 /* special case for .., just use the back ref */
4444 if (filp
->f_pos
== 1) {
4445 u64 pino
= parent_ino(filp
->f_path
.dentry
);
4446 over
= filldir(dirent
, "..", 2,
4447 filp
->f_pos
, pino
, DT_DIR
);
4452 path
= btrfs_alloc_path();
4458 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
4459 INIT_LIST_HEAD(&ins_list
);
4460 INIT_LIST_HEAD(&del_list
);
4461 btrfs_get_delayed_items(inode
, &ins_list
, &del_list
);
4464 btrfs_set_key_type(&key
, key_type
);
4465 key
.offset
= filp
->f_pos
;
4466 key
.objectid
= btrfs_ino(inode
);
4468 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4473 leaf
= path
->nodes
[0];
4474 slot
= path
->slots
[0];
4475 if (slot
>= btrfs_header_nritems(leaf
)) {
4476 ret
= btrfs_next_leaf(root
, path
);
4484 item
= btrfs_item_nr(leaf
, slot
);
4485 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
4487 if (found_key
.objectid
!= key
.objectid
)
4489 if (btrfs_key_type(&found_key
) != key_type
)
4491 if (found_key
.offset
< filp
->f_pos
)
4493 if (key_type
== BTRFS_DIR_INDEX_KEY
&&
4494 btrfs_should_delete_dir_index(&del_list
,
4498 filp
->f_pos
= found_key
.offset
;
4501 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
4503 di_total
= btrfs_item_size(leaf
, item
);
4505 while (di_cur
< di_total
) {
4506 struct btrfs_key location
;
4508 if (verify_dir_item(root
, leaf
, di
))
4511 name_len
= btrfs_dir_name_len(leaf
, di
);
4512 if (name_len
<= sizeof(tmp_name
)) {
4513 name_ptr
= tmp_name
;
4515 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
4521 read_extent_buffer(leaf
, name_ptr
,
4522 (unsigned long)(di
+ 1), name_len
);
4524 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
4525 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
4528 /* is this a reference to our own snapshot? If so
4531 * In contrast to old kernels, we insert the snapshot's
4532 * dir item and dir index after it has been created, so
4533 * we won't find a reference to our own snapshot. We
4534 * still keep the following code for backward
4537 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
4538 location
.objectid
== root
->root_key
.objectid
) {
4542 over
= filldir(dirent
, name_ptr
, name_len
,
4543 found_key
.offset
, location
.objectid
,
4547 if (name_ptr
!= tmp_name
)
4552 di_len
= btrfs_dir_name_len(leaf
, di
) +
4553 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
4555 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
4561 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
4564 ret
= btrfs_readdir_delayed_dir_index(filp
, dirent
, filldir
,
4570 /* Reached end of directory/root. Bump pos past the last item. */
4571 if (key_type
== BTRFS_DIR_INDEX_KEY
)
4573 * 32-bit glibc will use getdents64, but then strtol -
4574 * so the last number we can serve is this.
4576 filp
->f_pos
= 0x7fffffff;
4582 if (key_type
== BTRFS_DIR_INDEX_KEY
)
4583 btrfs_put_delayed_items(&ins_list
, &del_list
);
4584 btrfs_free_path(path
);
4588 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
4590 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4591 struct btrfs_trans_handle
*trans
;
4593 bool nolock
= false;
4595 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
4598 if (btrfs_fs_closing(root
->fs_info
) && btrfs_is_free_space_inode(inode
))
4601 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
4603 trans
= btrfs_join_transaction_nolock(root
);
4605 trans
= btrfs_join_transaction(root
);
4607 return PTR_ERR(trans
);
4608 ret
= btrfs_commit_transaction(trans
, root
);
4614 * This is somewhat expensive, updating the tree every time the
4615 * inode changes. But, it is most likely to find the inode in cache.
4616 * FIXME, needs more benchmarking...there are no reasons other than performance
4617 * to keep or drop this code.
4619 int btrfs_dirty_inode(struct inode
*inode
)
4621 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4622 struct btrfs_trans_handle
*trans
;
4625 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
4628 trans
= btrfs_join_transaction(root
);
4630 return PTR_ERR(trans
);
4632 ret
= btrfs_update_inode(trans
, root
, inode
);
4633 if (ret
&& ret
== -ENOSPC
) {
4634 /* whoops, lets try again with the full transaction */
4635 btrfs_end_transaction(trans
, root
);
4636 trans
= btrfs_start_transaction(root
, 1);
4638 return PTR_ERR(trans
);
4640 ret
= btrfs_update_inode(trans
, root
, inode
);
4642 btrfs_end_transaction(trans
, root
);
4643 if (BTRFS_I(inode
)->delayed_node
)
4644 btrfs_balance_delayed_items(root
);
4650 * This is a copy of file_update_time. We need this so we can return error on
4651 * ENOSPC for updating the inode in the case of file write and mmap writes.
4653 static int btrfs_update_time(struct inode
*inode
, struct timespec
*now
,
4656 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4658 if (btrfs_root_readonly(root
))
4661 if (flags
& S_VERSION
)
4662 inode_inc_iversion(inode
);
4663 if (flags
& S_CTIME
)
4664 inode
->i_ctime
= *now
;
4665 if (flags
& S_MTIME
)
4666 inode
->i_mtime
= *now
;
4667 if (flags
& S_ATIME
)
4668 inode
->i_atime
= *now
;
4669 return btrfs_dirty_inode(inode
);
4673 * find the highest existing sequence number in a directory
4674 * and then set the in-memory index_cnt variable to reflect
4675 * free sequence numbers
4677 static int btrfs_set_inode_index_count(struct inode
*inode
)
4679 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4680 struct btrfs_key key
, found_key
;
4681 struct btrfs_path
*path
;
4682 struct extent_buffer
*leaf
;
4685 key
.objectid
= btrfs_ino(inode
);
4686 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
4687 key
.offset
= (u64
)-1;
4689 path
= btrfs_alloc_path();
4693 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4696 /* FIXME: we should be able to handle this */
4702 * MAGIC NUMBER EXPLANATION:
4703 * since we search a directory based on f_pos we have to start at 2
4704 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4705 * else has to start at 2
4707 if (path
->slots
[0] == 0) {
4708 BTRFS_I(inode
)->index_cnt
= 2;
4714 leaf
= path
->nodes
[0];
4715 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4717 if (found_key
.objectid
!= btrfs_ino(inode
) ||
4718 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
4719 BTRFS_I(inode
)->index_cnt
= 2;
4723 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
4725 btrfs_free_path(path
);
4730 * helper to find a free sequence number in a given directory. This current
4731 * code is very simple, later versions will do smarter things in the btree
4733 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
4737 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
4738 ret
= btrfs_inode_delayed_dir_index_count(dir
);
4740 ret
= btrfs_set_inode_index_count(dir
);
4746 *index
= BTRFS_I(dir
)->index_cnt
;
4747 BTRFS_I(dir
)->index_cnt
++;
4752 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
4753 struct btrfs_root
*root
,
4755 const char *name
, int name_len
,
4756 u64 ref_objectid
, u64 objectid
,
4757 umode_t mode
, u64
*index
)
4759 struct inode
*inode
;
4760 struct btrfs_inode_item
*inode_item
;
4761 struct btrfs_key
*location
;
4762 struct btrfs_path
*path
;
4763 struct btrfs_inode_ref
*ref
;
4764 struct btrfs_key key
[2];
4770 path
= btrfs_alloc_path();
4772 return ERR_PTR(-ENOMEM
);
4774 inode
= new_inode(root
->fs_info
->sb
);
4776 btrfs_free_path(path
);
4777 return ERR_PTR(-ENOMEM
);
4781 * we have to initialize this early, so we can reclaim the inode
4782 * number if we fail afterwards in this function.
4784 inode
->i_ino
= objectid
;
4787 trace_btrfs_inode_request(dir
);
4789 ret
= btrfs_set_inode_index(dir
, index
);
4791 btrfs_free_path(path
);
4793 return ERR_PTR(ret
);
4797 * index_cnt is ignored for everything but a dir,
4798 * btrfs_get_inode_index_count has an explanation for the magic
4801 BTRFS_I(inode
)->index_cnt
= 2;
4802 BTRFS_I(inode
)->root
= root
;
4803 BTRFS_I(inode
)->generation
= trans
->transid
;
4804 inode
->i_generation
= BTRFS_I(inode
)->generation
;
4807 * We could have gotten an inode number from somebody who was fsynced
4808 * and then removed in this same transaction, so let's just set full
4809 * sync since it will be a full sync anyway and this will blow away the
4810 * old info in the log.
4812 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
4819 key
[0].objectid
= objectid
;
4820 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
4824 * Start new inodes with an inode_ref. This is slightly more
4825 * efficient for small numbers of hard links since they will
4826 * be packed into one item. Extended refs will kick in if we
4827 * add more hard links than can fit in the ref item.
4829 key
[1].objectid
= objectid
;
4830 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
4831 key
[1].offset
= ref_objectid
;
4833 sizes
[0] = sizeof(struct btrfs_inode_item
);
4834 sizes
[1] = name_len
+ sizeof(*ref
);
4836 path
->leave_spinning
= 1;
4837 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
4841 inode_init_owner(inode
, dir
, mode
);
4842 inode_set_bytes(inode
, 0);
4843 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4844 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4845 struct btrfs_inode_item
);
4846 memset_extent_buffer(path
->nodes
[0], 0, (unsigned long)inode_item
,
4847 sizeof(*inode_item
));
4848 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
4850 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
4851 struct btrfs_inode_ref
);
4852 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
4853 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
4854 ptr
= (unsigned long)(ref
+ 1);
4855 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
4857 btrfs_mark_buffer_dirty(path
->nodes
[0]);
4858 btrfs_free_path(path
);
4860 location
= &BTRFS_I(inode
)->location
;
4861 location
->objectid
= objectid
;
4862 location
->offset
= 0;
4863 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
4865 btrfs_inherit_iflags(inode
, dir
);
4867 if (S_ISREG(mode
)) {
4868 if (btrfs_test_opt(root
, NODATASUM
))
4869 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
4870 if (btrfs_test_opt(root
, NODATACOW
))
4871 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
;
4874 insert_inode_hash(inode
);
4875 inode_tree_add(inode
);
4877 trace_btrfs_inode_new(inode
);
4878 btrfs_set_inode_last_trans(trans
, inode
);
4880 btrfs_update_root_times(trans
, root
);
4885 BTRFS_I(dir
)->index_cnt
--;
4886 btrfs_free_path(path
);
4888 return ERR_PTR(ret
);
4891 static inline u8
btrfs_inode_type(struct inode
*inode
)
4893 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
4897 * utility function to add 'inode' into 'parent_inode' with
4898 * a give name and a given sequence number.
4899 * if 'add_backref' is true, also insert a backref from the
4900 * inode to the parent directory.
4902 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
4903 struct inode
*parent_inode
, struct inode
*inode
,
4904 const char *name
, int name_len
, int add_backref
, u64 index
)
4907 struct btrfs_key key
;
4908 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
4909 u64 ino
= btrfs_ino(inode
);
4910 u64 parent_ino
= btrfs_ino(parent_inode
);
4912 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4913 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
4916 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
4920 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4921 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
4922 key
.objectid
, root
->root_key
.objectid
,
4923 parent_ino
, index
, name
, name_len
);
4924 } else if (add_backref
) {
4925 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
4929 /* Nothing to clean up yet */
4933 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
4935 btrfs_inode_type(inode
), index
);
4936 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
)
4939 btrfs_abort_transaction(trans
, root
, ret
);
4943 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
4945 inode_inc_iversion(parent_inode
);
4946 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
4947 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
4949 btrfs_abort_transaction(trans
, root
, ret
);
4953 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4956 err
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
4957 key
.objectid
, root
->root_key
.objectid
,
4958 parent_ino
, &local_index
, name
, name_len
);
4960 } else if (add_backref
) {
4964 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
4965 ino
, parent_ino
, &local_index
);
4970 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
4971 struct inode
*dir
, struct dentry
*dentry
,
4972 struct inode
*inode
, int backref
, u64 index
)
4974 int err
= btrfs_add_link(trans
, dir
, inode
,
4975 dentry
->d_name
.name
, dentry
->d_name
.len
,
4982 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
4983 umode_t mode
, dev_t rdev
)
4985 struct btrfs_trans_handle
*trans
;
4986 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4987 struct inode
*inode
= NULL
;
4993 if (!new_valid_dev(rdev
))
4997 * 2 for inode item and ref
4999 * 1 for xattr if selinux is on
5001 trans
= btrfs_start_transaction(root
, 5);
5003 return PTR_ERR(trans
);
5005 err
= btrfs_find_free_ino(root
, &objectid
);
5009 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5010 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5012 if (IS_ERR(inode
)) {
5013 err
= PTR_ERR(inode
);
5017 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5023 err
= btrfs_update_inode(trans
, root
, inode
);
5030 * If the active LSM wants to access the inode during
5031 * d_instantiate it needs these. Smack checks to see
5032 * if the filesystem supports xattrs by looking at the
5036 inode
->i_op
= &btrfs_special_inode_operations
;
5037 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5041 init_special_inode(inode
, inode
->i_mode
, rdev
);
5042 btrfs_update_inode(trans
, root
, inode
);
5043 d_instantiate(dentry
, inode
);
5046 btrfs_end_transaction(trans
, root
);
5047 btrfs_btree_balance_dirty(root
);
5049 inode_dec_link_count(inode
);
5055 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
5056 umode_t mode
, bool excl
)
5058 struct btrfs_trans_handle
*trans
;
5059 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5060 struct inode
*inode
= NULL
;
5061 int drop_inode_on_err
= 0;
5067 * 2 for inode item and ref
5069 * 1 for xattr if selinux is on
5071 trans
= btrfs_start_transaction(root
, 5);
5073 return PTR_ERR(trans
);
5075 err
= btrfs_find_free_ino(root
, &objectid
);
5079 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5080 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5082 if (IS_ERR(inode
)) {
5083 err
= PTR_ERR(inode
);
5086 drop_inode_on_err
= 1;
5088 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5092 err
= btrfs_update_inode(trans
, root
, inode
);
5097 * If the active LSM wants to access the inode during
5098 * d_instantiate it needs these. Smack checks to see
5099 * if the filesystem supports xattrs by looking at the
5102 inode
->i_fop
= &btrfs_file_operations
;
5103 inode
->i_op
= &btrfs_file_inode_operations
;
5105 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5109 inode
->i_mapping
->a_ops
= &btrfs_aops
;
5110 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
5111 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
5112 d_instantiate(dentry
, inode
);
5115 btrfs_end_transaction(trans
, root
);
5116 if (err
&& drop_inode_on_err
) {
5117 inode_dec_link_count(inode
);
5120 btrfs_btree_balance_dirty(root
);
5124 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
5125 struct dentry
*dentry
)
5127 struct btrfs_trans_handle
*trans
;
5128 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5129 struct inode
*inode
= old_dentry
->d_inode
;
5134 /* do not allow sys_link's with other subvols of the same device */
5135 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
5138 if (inode
->i_nlink
>= BTRFS_LINK_MAX
)
5141 err
= btrfs_set_inode_index(dir
, &index
);
5146 * 2 items for inode and inode ref
5147 * 2 items for dir items
5148 * 1 item for parent inode
5150 trans
= btrfs_start_transaction(root
, 5);
5151 if (IS_ERR(trans
)) {
5152 err
= PTR_ERR(trans
);
5156 btrfs_inc_nlink(inode
);
5157 inode_inc_iversion(inode
);
5158 inode
->i_ctime
= CURRENT_TIME
;
5160 set_bit(BTRFS_INODE_COPY_EVERYTHING
, &BTRFS_I(inode
)->runtime_flags
);
5162 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
5167 struct dentry
*parent
= dentry
->d_parent
;
5168 err
= btrfs_update_inode(trans
, root
, inode
);
5171 d_instantiate(dentry
, inode
);
5172 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
5175 btrfs_end_transaction(trans
, root
);
5178 inode_dec_link_count(inode
);
5181 btrfs_btree_balance_dirty(root
);
5185 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
5187 struct inode
*inode
= NULL
;
5188 struct btrfs_trans_handle
*trans
;
5189 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5191 int drop_on_err
= 0;
5196 * 2 items for inode and ref
5197 * 2 items for dir items
5198 * 1 for xattr if selinux is on
5200 trans
= btrfs_start_transaction(root
, 5);
5202 return PTR_ERR(trans
);
5204 err
= btrfs_find_free_ino(root
, &objectid
);
5208 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5209 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5210 S_IFDIR
| mode
, &index
);
5211 if (IS_ERR(inode
)) {
5212 err
= PTR_ERR(inode
);
5218 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5222 inode
->i_op
= &btrfs_dir_inode_operations
;
5223 inode
->i_fop
= &btrfs_dir_file_operations
;
5225 btrfs_i_size_write(inode
, 0);
5226 err
= btrfs_update_inode(trans
, root
, inode
);
5230 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
5231 dentry
->d_name
.len
, 0, index
);
5235 d_instantiate(dentry
, inode
);
5239 btrfs_end_transaction(trans
, root
);
5242 btrfs_btree_balance_dirty(root
);
5246 /* helper for btfs_get_extent. Given an existing extent in the tree,
5247 * and an extent that you want to insert, deal with overlap and insert
5248 * the new extent into the tree.
5250 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
5251 struct extent_map
*existing
,
5252 struct extent_map
*em
,
5253 u64 map_start
, u64 map_len
)
5257 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
5258 start_diff
= map_start
- em
->start
;
5259 em
->start
= map_start
;
5261 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
5262 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
5263 em
->block_start
+= start_diff
;
5264 em
->block_len
-= start_diff
;
5266 return add_extent_mapping(em_tree
, em
);
5269 static noinline
int uncompress_inline(struct btrfs_path
*path
,
5270 struct inode
*inode
, struct page
*page
,
5271 size_t pg_offset
, u64 extent_offset
,
5272 struct btrfs_file_extent_item
*item
)
5275 struct extent_buffer
*leaf
= path
->nodes
[0];
5278 unsigned long inline_size
;
5282 WARN_ON(pg_offset
!= 0);
5283 compress_type
= btrfs_file_extent_compression(leaf
, item
);
5284 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
5285 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
5286 btrfs_item_nr(leaf
, path
->slots
[0]));
5287 tmp
= kmalloc(inline_size
, GFP_NOFS
);
5290 ptr
= btrfs_file_extent_inline_start(item
);
5292 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
5294 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
5295 ret
= btrfs_decompress(compress_type
, tmp
, page
,
5296 extent_offset
, inline_size
, max_size
);
5298 char *kaddr
= kmap_atomic(page
);
5299 unsigned long copy_size
= min_t(u64
,
5300 PAGE_CACHE_SIZE
- pg_offset
,
5301 max_size
- extent_offset
);
5302 memset(kaddr
+ pg_offset
, 0, copy_size
);
5303 kunmap_atomic(kaddr
);
5310 * a bit scary, this does extent mapping from logical file offset to the disk.
5311 * the ugly parts come from merging extents from the disk with the in-ram
5312 * representation. This gets more complex because of the data=ordered code,
5313 * where the in-ram extents might be locked pending data=ordered completion.
5315 * This also copies inline extents directly into the page.
5318 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
5319 size_t pg_offset
, u64 start
, u64 len
,
5325 u64 extent_start
= 0;
5327 u64 objectid
= btrfs_ino(inode
);
5329 struct btrfs_path
*path
= NULL
;
5330 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5331 struct btrfs_file_extent_item
*item
;
5332 struct extent_buffer
*leaf
;
5333 struct btrfs_key found_key
;
5334 struct extent_map
*em
= NULL
;
5335 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
5336 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
5337 struct btrfs_trans_handle
*trans
= NULL
;
5341 read_lock(&em_tree
->lock
);
5342 em
= lookup_extent_mapping(em_tree
, start
, len
);
5344 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5345 read_unlock(&em_tree
->lock
);
5348 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
5349 free_extent_map(em
);
5350 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
5351 free_extent_map(em
);
5355 em
= alloc_extent_map();
5360 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5361 em
->start
= EXTENT_MAP_HOLE
;
5362 em
->orig_start
= EXTENT_MAP_HOLE
;
5364 em
->block_len
= (u64
)-1;
5367 path
= btrfs_alloc_path();
5373 * Chances are we'll be called again, so go ahead and do
5379 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
5380 objectid
, start
, trans
!= NULL
);
5387 if (path
->slots
[0] == 0)
5392 leaf
= path
->nodes
[0];
5393 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5394 struct btrfs_file_extent_item
);
5395 /* are we inside the extent that was found? */
5396 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5397 found_type
= btrfs_key_type(&found_key
);
5398 if (found_key
.objectid
!= objectid
||
5399 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
5403 found_type
= btrfs_file_extent_type(leaf
, item
);
5404 extent_start
= found_key
.offset
;
5405 compress_type
= btrfs_file_extent_compression(leaf
, item
);
5406 if (found_type
== BTRFS_FILE_EXTENT_REG
||
5407 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5408 extent_end
= extent_start
+
5409 btrfs_file_extent_num_bytes(leaf
, item
);
5410 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
5412 size
= btrfs_file_extent_inline_len(leaf
, item
);
5413 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
5414 ~((u64
)root
->sectorsize
- 1);
5417 if (start
>= extent_end
) {
5419 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
5420 ret
= btrfs_next_leaf(root
, path
);
5427 leaf
= path
->nodes
[0];
5429 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5430 if (found_key
.objectid
!= objectid
||
5431 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
5433 if (start
+ len
<= found_key
.offset
)
5436 em
->orig_start
= start
;
5437 em
->len
= found_key
.offset
- start
;
5441 if (found_type
== BTRFS_FILE_EXTENT_REG
||
5442 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5443 em
->start
= extent_start
;
5444 em
->len
= extent_end
- extent_start
;
5445 em
->orig_start
= extent_start
-
5446 btrfs_file_extent_offset(leaf
, item
);
5447 em
->orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
,
5449 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
5451 em
->block_start
= EXTENT_MAP_HOLE
;
5454 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
5455 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
5456 em
->compress_type
= compress_type
;
5457 em
->block_start
= bytenr
;
5458 em
->block_len
= em
->orig_block_len
;
5460 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
5461 em
->block_start
= bytenr
;
5462 em
->block_len
= em
->len
;
5463 if (found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
5464 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
5467 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
5471 size_t extent_offset
;
5474 em
->block_start
= EXTENT_MAP_INLINE
;
5475 if (!page
|| create
) {
5476 em
->start
= extent_start
;
5477 em
->len
= extent_end
- extent_start
;
5481 size
= btrfs_file_extent_inline_len(leaf
, item
);
5482 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
5483 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
5484 size
- extent_offset
);
5485 em
->start
= extent_start
+ extent_offset
;
5486 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
5487 ~((u64
)root
->sectorsize
- 1);
5488 em
->orig_block_len
= em
->len
;
5489 em
->orig_start
= em
->start
;
5490 if (compress_type
) {
5491 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
5492 em
->compress_type
= compress_type
;
5494 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
5495 if (create
== 0 && !PageUptodate(page
)) {
5496 if (btrfs_file_extent_compression(leaf
, item
) !=
5497 BTRFS_COMPRESS_NONE
) {
5498 ret
= uncompress_inline(path
, inode
, page
,
5500 extent_offset
, item
);
5501 BUG_ON(ret
); /* -ENOMEM */
5504 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
5506 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
5507 memset(map
+ pg_offset
+ copy_size
, 0,
5508 PAGE_CACHE_SIZE
- pg_offset
-
5513 flush_dcache_page(page
);
5514 } else if (create
&& PageUptodate(page
)) {
5518 free_extent_map(em
);
5521 btrfs_release_path(path
);
5522 trans
= btrfs_join_transaction(root
);
5525 return ERR_CAST(trans
);
5529 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
5532 btrfs_mark_buffer_dirty(leaf
);
5534 set_extent_uptodate(io_tree
, em
->start
,
5535 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
5538 WARN(1, KERN_ERR
"btrfs unknown found_type %d\n", found_type
);
5542 em
->orig_start
= start
;
5545 em
->block_start
= EXTENT_MAP_HOLE
;
5546 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
5548 btrfs_release_path(path
);
5549 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
5550 printk(KERN_ERR
"Btrfs: bad extent! em: [%llu %llu] passed "
5551 "[%llu %llu]\n", (unsigned long long)em
->start
,
5552 (unsigned long long)em
->len
,
5553 (unsigned long long)start
,
5554 (unsigned long long)len
);
5560 write_lock(&em_tree
->lock
);
5561 ret
= add_extent_mapping(em_tree
, em
);
5562 /* it is possible that someone inserted the extent into the tree
5563 * while we had the lock dropped. It is also possible that
5564 * an overlapping map exists in the tree
5566 if (ret
== -EEXIST
) {
5567 struct extent_map
*existing
;
5571 existing
= lookup_extent_mapping(em_tree
, start
, len
);
5572 if (existing
&& (existing
->start
> start
||
5573 existing
->start
+ existing
->len
<= start
)) {
5574 free_extent_map(existing
);
5578 existing
= lookup_extent_mapping(em_tree
, em
->start
,
5581 err
= merge_extent_mapping(em_tree
, existing
,
5584 free_extent_map(existing
);
5586 free_extent_map(em
);
5591 free_extent_map(em
);
5595 free_extent_map(em
);
5600 write_unlock(&em_tree
->lock
);
5604 trace_btrfs_get_extent(root
, em
);
5607 btrfs_free_path(path
);
5609 ret
= btrfs_end_transaction(trans
, root
);
5614 free_extent_map(em
);
5615 return ERR_PTR(err
);
5617 BUG_ON(!em
); /* Error is always set */
5621 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
5622 size_t pg_offset
, u64 start
, u64 len
,
5625 struct extent_map
*em
;
5626 struct extent_map
*hole_em
= NULL
;
5627 u64 range_start
= start
;
5633 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
5640 * - a pre-alloc extent,
5641 * there might actually be delalloc bytes behind it.
5643 if (em
->block_start
!= EXTENT_MAP_HOLE
&&
5644 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
5650 /* check to see if we've wrapped (len == -1 or similar) */
5659 /* ok, we didn't find anything, lets look for delalloc */
5660 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
5661 end
, len
, EXTENT_DELALLOC
, 1);
5662 found_end
= range_start
+ found
;
5663 if (found_end
< range_start
)
5664 found_end
= (u64
)-1;
5667 * we didn't find anything useful, return
5668 * the original results from get_extent()
5670 if (range_start
> end
|| found_end
<= start
) {
5676 /* adjust the range_start to make sure it doesn't
5677 * go backwards from the start they passed in
5679 range_start
= max(start
,range_start
);
5680 found
= found_end
- range_start
;
5683 u64 hole_start
= start
;
5686 em
= alloc_extent_map();
5692 * when btrfs_get_extent can't find anything it
5693 * returns one huge hole
5695 * make sure what it found really fits our range, and
5696 * adjust to make sure it is based on the start from
5700 u64 calc_end
= extent_map_end(hole_em
);
5702 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
5703 free_extent_map(hole_em
);
5706 hole_start
= max(hole_em
->start
, start
);
5707 hole_len
= calc_end
- hole_start
;
5711 if (hole_em
&& range_start
> hole_start
) {
5712 /* our hole starts before our delalloc, so we
5713 * have to return just the parts of the hole
5714 * that go until the delalloc starts
5716 em
->len
= min(hole_len
,
5717 range_start
- hole_start
);
5718 em
->start
= hole_start
;
5719 em
->orig_start
= hole_start
;
5721 * don't adjust block start at all,
5722 * it is fixed at EXTENT_MAP_HOLE
5724 em
->block_start
= hole_em
->block_start
;
5725 em
->block_len
= hole_len
;
5726 if (test_bit(EXTENT_FLAG_PREALLOC
, &hole_em
->flags
))
5727 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
5729 em
->start
= range_start
;
5731 em
->orig_start
= range_start
;
5732 em
->block_start
= EXTENT_MAP_DELALLOC
;
5733 em
->block_len
= found
;
5735 } else if (hole_em
) {
5740 free_extent_map(hole_em
);
5742 free_extent_map(em
);
5743 return ERR_PTR(err
);
5748 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
5751 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5752 struct btrfs_trans_handle
*trans
;
5753 struct extent_map
*em
;
5754 struct btrfs_key ins
;
5758 trans
= btrfs_join_transaction(root
);
5760 return ERR_CAST(trans
);
5762 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
5764 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
5765 ret
= btrfs_reserve_extent(trans
, root
, len
, root
->sectorsize
, 0,
5766 alloc_hint
, &ins
, 1);
5772 em
= create_pinned_em(inode
, start
, ins
.offset
, start
, ins
.objectid
,
5773 ins
.offset
, ins
.offset
, 0);
5777 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
5778 ins
.offset
, ins
.offset
, 0);
5780 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
5784 btrfs_end_transaction(trans
, root
);
5789 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5790 * block must be cow'd
5792 static noinline
int can_nocow_odirect(struct btrfs_trans_handle
*trans
,
5793 struct inode
*inode
, u64 offset
, u64 len
)
5795 struct btrfs_path
*path
;
5797 struct extent_buffer
*leaf
;
5798 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5799 struct btrfs_file_extent_item
*fi
;
5800 struct btrfs_key key
;
5808 path
= btrfs_alloc_path();
5812 ret
= btrfs_lookup_file_extent(trans
, root
, path
, btrfs_ino(inode
),
5817 slot
= path
->slots
[0];
5820 /* can't find the item, must cow */
5827 leaf
= path
->nodes
[0];
5828 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
5829 if (key
.objectid
!= btrfs_ino(inode
) ||
5830 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
5831 /* not our file or wrong item type, must cow */
5835 if (key
.offset
> offset
) {
5836 /* Wrong offset, must cow */
5840 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
5841 found_type
= btrfs_file_extent_type(leaf
, fi
);
5842 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
5843 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
5844 /* not a regular extent, must cow */
5847 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
5848 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
5850 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
5851 if (extent_end
< offset
+ len
) {
5852 /* extent doesn't include our full range, must cow */
5856 if (btrfs_extent_readonly(root
, disk_bytenr
))
5860 * look for other files referencing this extent, if we
5861 * find any we must cow
5863 if (btrfs_cross_ref_exist(trans
, root
, btrfs_ino(inode
),
5864 key
.offset
- backref_offset
, disk_bytenr
))
5868 * adjust disk_bytenr and num_bytes to cover just the bytes
5869 * in this extent we are about to write. If there
5870 * are any csums in that range we have to cow in order
5871 * to keep the csums correct
5873 disk_bytenr
+= backref_offset
;
5874 disk_bytenr
+= offset
- key
.offset
;
5875 num_bytes
= min(offset
+ len
, extent_end
) - offset
;
5876 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
5879 * all of the above have passed, it is safe to overwrite this extent
5884 btrfs_free_path(path
);
5888 static int lock_extent_direct(struct inode
*inode
, u64 lockstart
, u64 lockend
,
5889 struct extent_state
**cached_state
, int writing
)
5891 struct btrfs_ordered_extent
*ordered
;
5895 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
5898 * We're concerned with the entire range that we're going to be
5899 * doing DIO to, so we need to make sure theres no ordered
5900 * extents in this range.
5902 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
5903 lockend
- lockstart
+ 1);
5906 * We need to make sure there are no buffered pages in this
5907 * range either, we could have raced between the invalidate in
5908 * generic_file_direct_write and locking the extent. The
5909 * invalidate needs to happen so that reads after a write do not
5912 if (!ordered
&& (!writing
||
5913 !test_range_bit(&BTRFS_I(inode
)->io_tree
,
5914 lockstart
, lockend
, EXTENT_UPTODATE
, 0,
5918 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
5919 cached_state
, GFP_NOFS
);
5922 btrfs_start_ordered_extent(inode
, ordered
, 1);
5923 btrfs_put_ordered_extent(ordered
);
5925 /* Screw you mmap */
5926 ret
= filemap_write_and_wait_range(inode
->i_mapping
,
5933 * If we found a page that couldn't be invalidated just
5934 * fall back to buffered.
5936 ret
= invalidate_inode_pages2_range(inode
->i_mapping
,
5937 lockstart
>> PAGE_CACHE_SHIFT
,
5938 lockend
>> PAGE_CACHE_SHIFT
);
5949 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
5950 u64 len
, u64 orig_start
,
5951 u64 block_start
, u64 block_len
,
5952 u64 orig_block_len
, int type
)
5954 struct extent_map_tree
*em_tree
;
5955 struct extent_map
*em
;
5956 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5959 em_tree
= &BTRFS_I(inode
)->extent_tree
;
5960 em
= alloc_extent_map();
5962 return ERR_PTR(-ENOMEM
);
5965 em
->orig_start
= orig_start
;
5967 em
->block_len
= block_len
;
5968 em
->block_start
= block_start
;
5969 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5970 em
->orig_block_len
= orig_block_len
;
5971 em
->generation
= -1;
5972 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
5973 if (type
== BTRFS_ORDERED_PREALLOC
)
5974 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
5977 btrfs_drop_extent_cache(inode
, em
->start
,
5978 em
->start
+ em
->len
- 1, 0);
5979 write_lock(&em_tree
->lock
);
5980 ret
= add_extent_mapping(em_tree
, em
);
5982 list_move(&em
->list
,
5983 &em_tree
->modified_extents
);
5984 write_unlock(&em_tree
->lock
);
5985 } while (ret
== -EEXIST
);
5988 free_extent_map(em
);
5989 return ERR_PTR(ret
);
5996 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
5997 struct buffer_head
*bh_result
, int create
)
5999 struct extent_map
*em
;
6000 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6001 struct extent_state
*cached_state
= NULL
;
6002 u64 start
= iblock
<< inode
->i_blkbits
;
6003 u64 lockstart
, lockend
;
6004 u64 len
= bh_result
->b_size
;
6005 struct btrfs_trans_handle
*trans
;
6006 int unlock_bits
= EXTENT_LOCKED
;
6010 ret
= btrfs_delalloc_reserve_space(inode
, len
);
6013 unlock_bits
|= EXTENT_DELALLOC
| EXTENT_DIRTY
;
6015 len
= min_t(u64
, len
, root
->sectorsize
);
6019 lockend
= start
+ len
- 1;
6022 * If this errors out it's because we couldn't invalidate pagecache for
6023 * this range and we need to fallback to buffered.
6025 if (lock_extent_direct(inode
, lockstart
, lockend
, &cached_state
, create
))
6029 ret
= set_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6030 lockend
, EXTENT_DELALLOC
, NULL
,
6031 &cached_state
, GFP_NOFS
);
6036 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
6043 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6044 * io. INLINE is special, and we could probably kludge it in here, but
6045 * it's still buffered so for safety lets just fall back to the generic
6048 * For COMPRESSED we _have_ to read the entire extent in so we can
6049 * decompress it, so there will be buffering required no matter what we
6050 * do, so go ahead and fallback to buffered.
6052 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6053 * to buffered IO. Don't blame me, this is the price we pay for using
6056 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
6057 em
->block_start
== EXTENT_MAP_INLINE
) {
6058 free_extent_map(em
);
6063 /* Just a good old fashioned hole, return */
6064 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
6065 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
6066 free_extent_map(em
);
6072 * We don't allocate a new extent in the following cases
6074 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6076 * 2) The extent is marked as PREALLOC. We're good to go here and can
6077 * just use the extent.
6081 len
= min(len
, em
->len
- (start
- em
->start
));
6082 lockstart
= start
+ len
;
6086 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
6087 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
6088 em
->block_start
!= EXTENT_MAP_HOLE
)) {
6093 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6094 type
= BTRFS_ORDERED_PREALLOC
;
6096 type
= BTRFS_ORDERED_NOCOW
;
6097 len
= min(len
, em
->len
- (start
- em
->start
));
6098 block_start
= em
->block_start
+ (start
- em
->start
);
6101 * we're not going to log anything, but we do need
6102 * to make sure the current transaction stays open
6103 * while we look for nocow cross refs
6105 trans
= btrfs_join_transaction(root
);
6109 if (can_nocow_odirect(trans
, inode
, start
, len
) == 1) {
6110 u64 orig_start
= em
->orig_start
;
6111 u64 orig_block_len
= em
->orig_block_len
;
6113 if (type
== BTRFS_ORDERED_PREALLOC
) {
6114 free_extent_map(em
);
6115 em
= create_pinned_em(inode
, start
, len
,
6118 orig_block_len
, type
);
6120 btrfs_end_transaction(trans
, root
);
6125 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
6126 block_start
, len
, len
, type
);
6127 btrfs_end_transaction(trans
, root
);
6129 free_extent_map(em
);
6134 btrfs_end_transaction(trans
, root
);
6138 * this will cow the extent, reset the len in case we changed
6141 len
= bh_result
->b_size
;
6142 free_extent_map(em
);
6143 em
= btrfs_new_extent_direct(inode
, start
, len
);
6148 len
= min(len
, em
->len
- (start
- em
->start
));
6150 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
6152 bh_result
->b_size
= len
;
6153 bh_result
->b_bdev
= em
->bdev
;
6154 set_buffer_mapped(bh_result
);
6156 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6157 set_buffer_new(bh_result
);
6160 * Need to update the i_size under the extent lock so buffered
6161 * readers will get the updated i_size when we unlock.
6163 if (start
+ len
> i_size_read(inode
))
6164 i_size_write(inode
, start
+ len
);
6168 * In the case of write we need to clear and unlock the entire range,
6169 * in the case of read we need to unlock only the end area that we
6170 * aren't using if there is any left over space.
6172 if (lockstart
< lockend
) {
6173 if (create
&& len
< lockend
- lockstart
) {
6174 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6175 lockstart
+ len
- 1,
6176 unlock_bits
| EXTENT_DEFRAG
, 1, 0,
6177 &cached_state
, GFP_NOFS
);
6179 * Beside unlock, we also need to cleanup reserved space
6180 * for the left range by attaching EXTENT_DO_ACCOUNTING.
6182 clear_extent_bit(&BTRFS_I(inode
)->io_tree
,
6183 lockstart
+ len
, lockend
,
6184 unlock_bits
| EXTENT_DO_ACCOUNTING
|
6185 EXTENT_DEFRAG
, 1, 0, NULL
, GFP_NOFS
);
6187 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6188 lockend
, unlock_bits
, 1, 0,
6189 &cached_state
, GFP_NOFS
);
6192 free_extent_state(cached_state
);
6195 free_extent_map(em
);
6201 unlock_bits
|= EXTENT_DO_ACCOUNTING
;
6203 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6204 unlock_bits
, 1, 0, &cached_state
, GFP_NOFS
);
6208 struct btrfs_dio_private
{
6209 struct inode
*inode
;
6215 /* number of bios pending for this dio */
6216 atomic_t pending_bios
;
6221 struct bio
*orig_bio
;
6224 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
6226 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6227 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
6228 struct bio_vec
*bvec
= bio
->bi_io_vec
;
6229 struct inode
*inode
= dip
->inode
;
6230 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6233 start
= dip
->logical_offset
;
6235 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
6236 struct page
*page
= bvec
->bv_page
;
6239 u64
private = ~(u32
)0;
6240 unsigned long flags
;
6242 if (get_state_private(&BTRFS_I(inode
)->io_tree
,
6245 local_irq_save(flags
);
6246 kaddr
= kmap_atomic(page
);
6247 csum
= btrfs_csum_data(root
, kaddr
+ bvec
->bv_offset
,
6248 csum
, bvec
->bv_len
);
6249 btrfs_csum_final(csum
, (char *)&csum
);
6250 kunmap_atomic(kaddr
);
6251 local_irq_restore(flags
);
6253 flush_dcache_page(bvec
->bv_page
);
6254 if (csum
!= private) {
6256 printk(KERN_ERR
"btrfs csum failed ino %llu off"
6257 " %llu csum %u private %u\n",
6258 (unsigned long long)btrfs_ino(inode
),
6259 (unsigned long long)start
,
6260 csum
, (unsigned)private);
6265 start
+= bvec
->bv_len
;
6267 } while (bvec
<= bvec_end
);
6269 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
6270 dip
->logical_offset
+ dip
->bytes
- 1);
6271 bio
->bi_private
= dip
->private;
6275 /* If we had a csum failure make sure to clear the uptodate flag */
6277 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
6278 dio_end_io(bio
, err
);
6281 static void btrfs_endio_direct_write(struct bio
*bio
, int err
)
6283 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6284 struct inode
*inode
= dip
->inode
;
6285 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6286 struct btrfs_ordered_extent
*ordered
= NULL
;
6287 u64 ordered_offset
= dip
->logical_offset
;
6288 u64 ordered_bytes
= dip
->bytes
;
6294 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
6296 ordered_bytes
, !err
);
6300 ordered
->work
.func
= finish_ordered_fn
;
6301 ordered
->work
.flags
= 0;
6302 btrfs_queue_worker(&root
->fs_info
->endio_write_workers
,
6306 * our bio might span multiple ordered extents. If we haven't
6307 * completed the accounting for the whole dio, go back and try again
6309 if (ordered_offset
< dip
->logical_offset
+ dip
->bytes
) {
6310 ordered_bytes
= dip
->logical_offset
+ dip
->bytes
-
6316 bio
->bi_private
= dip
->private;
6320 /* If we had an error make sure to clear the uptodate flag */
6322 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
6323 dio_end_io(bio
, err
);
6326 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
6327 struct bio
*bio
, int mirror_num
,
6328 unsigned long bio_flags
, u64 offset
)
6331 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6332 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
6333 BUG_ON(ret
); /* -ENOMEM */
6337 static void btrfs_end_dio_bio(struct bio
*bio
, int err
)
6339 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6342 printk(KERN_ERR
"btrfs direct IO failed ino %llu rw %lu "
6343 "sector %#Lx len %u err no %d\n",
6344 (unsigned long long)btrfs_ino(dip
->inode
), bio
->bi_rw
,
6345 (unsigned long long)bio
->bi_sector
, bio
->bi_size
, err
);
6349 * before atomic variable goto zero, we must make sure
6350 * dip->errors is perceived to be set.
6352 smp_mb__before_atomic_dec();
6355 /* if there are more bios still pending for this dio, just exit */
6356 if (!atomic_dec_and_test(&dip
->pending_bios
))
6360 bio_io_error(dip
->orig_bio
);
6362 set_bit(BIO_UPTODATE
, &dip
->orig_bio
->bi_flags
);
6363 bio_endio(dip
->orig_bio
, 0);
6369 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
6370 u64 first_sector
, gfp_t gfp_flags
)
6372 int nr_vecs
= bio_get_nr_vecs(bdev
);
6373 return btrfs_bio_alloc(bdev
, first_sector
, nr_vecs
, gfp_flags
);
6376 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
6377 int rw
, u64 file_offset
, int skip_sum
,
6380 int write
= rw
& REQ_WRITE
;
6381 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6385 async_submit
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
6390 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
6398 if (write
&& async_submit
) {
6399 ret
= btrfs_wq_submit_bio(root
->fs_info
,
6400 inode
, rw
, bio
, 0, 0,
6402 __btrfs_submit_bio_start_direct_io
,
6403 __btrfs_submit_bio_done
);
6407 * If we aren't doing async submit, calculate the csum of the
6410 ret
= btrfs_csum_one_bio(root
, inode
, bio
, file_offset
, 1);
6413 } else if (!skip_sum
) {
6414 ret
= btrfs_lookup_bio_sums_dio(root
, inode
, bio
, file_offset
);
6420 ret
= btrfs_map_bio(root
, rw
, bio
, 0, async_submit
);
6426 static int btrfs_submit_direct_hook(int rw
, struct btrfs_dio_private
*dip
,
6429 struct inode
*inode
= dip
->inode
;
6430 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6432 struct bio
*orig_bio
= dip
->orig_bio
;
6433 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
6434 u64 start_sector
= orig_bio
->bi_sector
;
6435 u64 file_offset
= dip
->logical_offset
;
6440 int async_submit
= 0;
6442 map_length
= orig_bio
->bi_size
;
6443 ret
= btrfs_map_block(root
->fs_info
, READ
, start_sector
<< 9,
6444 &map_length
, NULL
, 0);
6450 if (map_length
>= orig_bio
->bi_size
) {
6456 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
6459 bio
->bi_private
= dip
;
6460 bio
->bi_end_io
= btrfs_end_dio_bio
;
6461 atomic_inc(&dip
->pending_bios
);
6463 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
6464 if (unlikely(map_length
< submit_len
+ bvec
->bv_len
||
6465 bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
6466 bvec
->bv_offset
) < bvec
->bv_len
)) {
6468 * inc the count before we submit the bio so
6469 * we know the end IO handler won't happen before
6470 * we inc the count. Otherwise, the dip might get freed
6471 * before we're done setting it up
6473 atomic_inc(&dip
->pending_bios
);
6474 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
,
6475 file_offset
, skip_sum
,
6479 atomic_dec(&dip
->pending_bios
);
6483 start_sector
+= submit_len
>> 9;
6484 file_offset
+= submit_len
;
6489 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
6490 start_sector
, GFP_NOFS
);
6493 bio
->bi_private
= dip
;
6494 bio
->bi_end_io
= btrfs_end_dio_bio
;
6496 map_length
= orig_bio
->bi_size
;
6497 ret
= btrfs_map_block(root
->fs_info
, READ
,
6499 &map_length
, NULL
, 0);
6505 submit_len
+= bvec
->bv_len
;
6512 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
, file_offset
, skip_sum
,
6521 * before atomic variable goto zero, we must
6522 * make sure dip->errors is perceived to be set.
6524 smp_mb__before_atomic_dec();
6525 if (atomic_dec_and_test(&dip
->pending_bios
))
6526 bio_io_error(dip
->orig_bio
);
6528 /* bio_end_io() will handle error, so we needn't return it */
6532 static void btrfs_submit_direct(int rw
, struct bio
*bio
, struct inode
*inode
,
6535 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6536 struct btrfs_dio_private
*dip
;
6537 struct bio_vec
*bvec
= bio
->bi_io_vec
;
6539 int write
= rw
& REQ_WRITE
;
6542 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
6544 dip
= kmalloc(sizeof(*dip
), GFP_NOFS
);
6550 dip
->private = bio
->bi_private
;
6552 dip
->logical_offset
= file_offset
;
6556 dip
->bytes
+= bvec
->bv_len
;
6558 } while (bvec
<= (bio
->bi_io_vec
+ bio
->bi_vcnt
- 1));
6560 dip
->disk_bytenr
= (u64
)bio
->bi_sector
<< 9;
6561 bio
->bi_private
= dip
;
6563 dip
->orig_bio
= bio
;
6564 atomic_set(&dip
->pending_bios
, 0);
6567 bio
->bi_end_io
= btrfs_endio_direct_write
;
6569 bio
->bi_end_io
= btrfs_endio_direct_read
;
6571 ret
= btrfs_submit_direct_hook(rw
, dip
, skip_sum
);
6576 * If this is a write, we need to clean up the reserved space and kill
6577 * the ordered extent.
6580 struct btrfs_ordered_extent
*ordered
;
6581 ordered
= btrfs_lookup_ordered_extent(inode
, file_offset
);
6582 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
6583 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
6584 btrfs_free_reserved_extent(root
, ordered
->start
,
6586 btrfs_put_ordered_extent(ordered
);
6587 btrfs_put_ordered_extent(ordered
);
6589 bio_endio(bio
, ret
);
6592 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
6593 const struct iovec
*iov
, loff_t offset
,
6594 unsigned long nr_segs
)
6600 unsigned blocksize_mask
= root
->sectorsize
- 1;
6601 ssize_t retval
= -EINVAL
;
6602 loff_t end
= offset
;
6604 if (offset
& blocksize_mask
)
6607 /* Check the memory alignment. Blocks cannot straddle pages */
6608 for (seg
= 0; seg
< nr_segs
; seg
++) {
6609 addr
= (unsigned long)iov
[seg
].iov_base
;
6610 size
= iov
[seg
].iov_len
;
6612 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
6615 /* If this is a write we don't need to check anymore */
6620 * Check to make sure we don't have duplicate iov_base's in this
6621 * iovec, if so return EINVAL, otherwise we'll get csum errors
6622 * when reading back.
6624 for (i
= seg
+ 1; i
< nr_segs
; i
++) {
6625 if (iov
[seg
].iov_base
== iov
[i
].iov_base
)
6634 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
6635 const struct iovec
*iov
, loff_t offset
,
6636 unsigned long nr_segs
)
6638 struct file
*file
= iocb
->ki_filp
;
6639 struct inode
*inode
= file
->f_mapping
->host
;
6641 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iov
,
6645 return __blockdev_direct_IO(rw
, iocb
, inode
,
6646 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
6647 iov
, offset
, nr_segs
, btrfs_get_blocks_direct
, NULL
,
6648 btrfs_submit_direct
, 0);
6651 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
6653 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
6654 __u64 start
, __u64 len
)
6658 ret
= fiemap_check_flags(fieinfo
, BTRFS_FIEMAP_FLAGS
);
6662 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
6665 int btrfs_readpage(struct file
*file
, struct page
*page
)
6667 struct extent_io_tree
*tree
;
6668 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6669 return extent_read_full_page(tree
, page
, btrfs_get_extent
, 0);
6672 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
6674 struct extent_io_tree
*tree
;
6677 if (current
->flags
& PF_MEMALLOC
) {
6678 redirty_page_for_writepage(wbc
, page
);
6682 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6683 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
6686 int btrfs_writepages(struct address_space
*mapping
,
6687 struct writeback_control
*wbc
)
6689 struct extent_io_tree
*tree
;
6691 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
6692 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
6696 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
6697 struct list_head
*pages
, unsigned nr_pages
)
6699 struct extent_io_tree
*tree
;
6700 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
6701 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
6704 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
6706 struct extent_io_tree
*tree
;
6707 struct extent_map_tree
*map
;
6710 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6711 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
6712 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
6714 ClearPagePrivate(page
);
6715 set_page_private(page
, 0);
6716 page_cache_release(page
);
6721 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
6723 if (PageWriteback(page
) || PageDirty(page
))
6725 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
6728 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
6730 struct inode
*inode
= page
->mapping
->host
;
6731 struct extent_io_tree
*tree
;
6732 struct btrfs_ordered_extent
*ordered
;
6733 struct extent_state
*cached_state
= NULL
;
6734 u64 page_start
= page_offset(page
);
6735 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6738 * we have the page locked, so new writeback can't start,
6739 * and the dirty bit won't be cleared while we are here.
6741 * Wait for IO on this page so that we can safely clear
6742 * the PagePrivate2 bit and do ordered accounting
6744 wait_on_page_writeback(page
);
6746 tree
= &BTRFS_I(inode
)->io_tree
;
6748 btrfs_releasepage(page
, GFP_NOFS
);
6751 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
6752 ordered
= btrfs_lookup_ordered_extent(inode
,
6756 * IO on this page will never be started, so we need
6757 * to account for any ordered extents now
6759 clear_extent_bit(tree
, page_start
, page_end
,
6760 EXTENT_DIRTY
| EXTENT_DELALLOC
|
6761 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
6762 EXTENT_DEFRAG
, 1, 0, &cached_state
, GFP_NOFS
);
6764 * whoever cleared the private bit is responsible
6765 * for the finish_ordered_io
6767 if (TestClearPagePrivate2(page
) &&
6768 btrfs_dec_test_ordered_pending(inode
, &ordered
, page_start
,
6769 PAGE_CACHE_SIZE
, 1)) {
6770 btrfs_finish_ordered_io(ordered
);
6772 btrfs_put_ordered_extent(ordered
);
6773 cached_state
= NULL
;
6774 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
6776 clear_extent_bit(tree
, page_start
, page_end
,
6777 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
6778 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 1, 1,
6779 &cached_state
, GFP_NOFS
);
6780 __btrfs_releasepage(page
, GFP_NOFS
);
6782 ClearPageChecked(page
);
6783 if (PagePrivate(page
)) {
6784 ClearPagePrivate(page
);
6785 set_page_private(page
, 0);
6786 page_cache_release(page
);
6791 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6792 * called from a page fault handler when a page is first dirtied. Hence we must
6793 * be careful to check for EOF conditions here. We set the page up correctly
6794 * for a written page which means we get ENOSPC checking when writing into
6795 * holes and correct delalloc and unwritten extent mapping on filesystems that
6796 * support these features.
6798 * We are not allowed to take the i_mutex here so we have to play games to
6799 * protect against truncate races as the page could now be beyond EOF. Because
6800 * vmtruncate() writes the inode size before removing pages, once we have the
6801 * page lock we can determine safely if the page is beyond EOF. If it is not
6802 * beyond EOF, then the page is guaranteed safe against truncation until we
6805 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
6807 struct page
*page
= vmf
->page
;
6808 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
6809 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6810 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6811 struct btrfs_ordered_extent
*ordered
;
6812 struct extent_state
*cached_state
= NULL
;
6814 unsigned long zero_start
;
6821 sb_start_pagefault(inode
->i_sb
);
6822 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
6824 ret
= file_update_time(vma
->vm_file
);
6830 else /* -ENOSPC, -EIO, etc */
6831 ret
= VM_FAULT_SIGBUS
;
6837 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
6840 size
= i_size_read(inode
);
6841 page_start
= page_offset(page
);
6842 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6844 if ((page
->mapping
!= inode
->i_mapping
) ||
6845 (page_start
>= size
)) {
6846 /* page got truncated out from underneath us */
6849 wait_on_page_writeback(page
);
6851 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
6852 set_page_extent_mapped(page
);
6855 * we can't set the delalloc bits if there are pending ordered
6856 * extents. Drop our locks and wait for them to finish
6858 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
6860 unlock_extent_cached(io_tree
, page_start
, page_end
,
6861 &cached_state
, GFP_NOFS
);
6863 btrfs_start_ordered_extent(inode
, ordered
, 1);
6864 btrfs_put_ordered_extent(ordered
);
6869 * XXX - page_mkwrite gets called every time the page is dirtied, even
6870 * if it was already dirty, so for space accounting reasons we need to
6871 * clear any delalloc bits for the range we are fixing to save. There
6872 * is probably a better way to do this, but for now keep consistent with
6873 * prepare_pages in the normal write path.
6875 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
6876 EXTENT_DIRTY
| EXTENT_DELALLOC
|
6877 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
6878 0, 0, &cached_state
, GFP_NOFS
);
6880 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
6883 unlock_extent_cached(io_tree
, page_start
, page_end
,
6884 &cached_state
, GFP_NOFS
);
6885 ret
= VM_FAULT_SIGBUS
;
6890 /* page is wholly or partially inside EOF */
6891 if (page_start
+ PAGE_CACHE_SIZE
> size
)
6892 zero_start
= size
& ~PAGE_CACHE_MASK
;
6894 zero_start
= PAGE_CACHE_SIZE
;
6896 if (zero_start
!= PAGE_CACHE_SIZE
) {
6898 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
6899 flush_dcache_page(page
);
6902 ClearPageChecked(page
);
6903 set_page_dirty(page
);
6904 SetPageUptodate(page
);
6906 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
6907 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
6908 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->root
->last_log_commit
;
6910 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
6914 sb_end_pagefault(inode
->i_sb
);
6915 return VM_FAULT_LOCKED
;
6919 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
6921 sb_end_pagefault(inode
->i_sb
);
6925 static int btrfs_truncate(struct inode
*inode
)
6927 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6928 struct btrfs_block_rsv
*rsv
;
6931 struct btrfs_trans_handle
*trans
;
6932 u64 mask
= root
->sectorsize
- 1;
6933 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
6935 ret
= btrfs_truncate_page(inode
, inode
->i_size
, 0, 0);
6939 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
6940 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
6943 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6944 * 3 things going on here
6946 * 1) We need to reserve space for our orphan item and the space to
6947 * delete our orphan item. Lord knows we don't want to have a dangling
6948 * orphan item because we didn't reserve space to remove it.
6950 * 2) We need to reserve space to update our inode.
6952 * 3) We need to have something to cache all the space that is going to
6953 * be free'd up by the truncate operation, but also have some slack
6954 * space reserved in case it uses space during the truncate (thank you
6955 * very much snapshotting).
6957 * And we need these to all be seperate. The fact is we can use alot of
6958 * space doing the truncate, and we have no earthly idea how much space
6959 * we will use, so we need the truncate reservation to be seperate so it
6960 * doesn't end up using space reserved for updating the inode or
6961 * removing the orphan item. We also need to be able to stop the
6962 * transaction and start a new one, which means we need to be able to
6963 * update the inode several times, and we have no idea of knowing how
6964 * many times that will be, so we can't just reserve 1 item for the
6965 * entirety of the opration, so that has to be done seperately as well.
6966 * Then there is the orphan item, which does indeed need to be held on
6967 * to for the whole operation, and we need nobody to touch this reserved
6968 * space except the orphan code.
6970 * So that leaves us with
6972 * 1) root->orphan_block_rsv - for the orphan deletion.
6973 * 2) rsv - for the truncate reservation, which we will steal from the
6974 * transaction reservation.
6975 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6976 * updating the inode.
6978 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
6981 rsv
->size
= min_size
;
6985 * 1 for the truncate slack space
6986 * 1 for updating the inode.
6988 trans
= btrfs_start_transaction(root
, 2);
6989 if (IS_ERR(trans
)) {
6990 err
= PTR_ERR(trans
);
6994 /* Migrate the slack space for the truncate to our reserve */
6995 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
, rsv
,
7000 * setattr is responsible for setting the ordered_data_close flag,
7001 * but that is only tested during the last file release. That
7002 * could happen well after the next commit, leaving a great big
7003 * window where new writes may get lost if someone chooses to write
7004 * to this file after truncating to zero
7006 * The inode doesn't have any dirty data here, and so if we commit
7007 * this is a noop. If someone immediately starts writing to the inode
7008 * it is very likely we'll catch some of their writes in this
7009 * transaction, and the commit will find this file on the ordered
7010 * data list with good things to send down.
7012 * This is a best effort solution, there is still a window where
7013 * using truncate to replace the contents of the file will
7014 * end up with a zero length file after a crash.
7016 if (inode
->i_size
== 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
7017 &BTRFS_I(inode
)->runtime_flags
))
7018 btrfs_add_ordered_operation(trans
, root
, inode
);
7021 * So if we truncate and then write and fsync we normally would just
7022 * write the extents that changed, which is a problem if we need to
7023 * first truncate that entire inode. So set this flag so we write out
7024 * all of the extents in the inode to the sync log so we're completely
7027 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
7028 trans
->block_rsv
= rsv
;
7031 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
7033 BTRFS_EXTENT_DATA_KEY
);
7034 if (ret
!= -ENOSPC
) {
7039 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7040 ret
= btrfs_update_inode(trans
, root
, inode
);
7046 btrfs_end_transaction(trans
, root
);
7047 btrfs_btree_balance_dirty(root
);
7049 trans
= btrfs_start_transaction(root
, 2);
7050 if (IS_ERR(trans
)) {
7051 ret
= err
= PTR_ERR(trans
);
7056 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
,
7058 BUG_ON(ret
); /* shouldn't happen */
7059 trans
->block_rsv
= rsv
;
7062 if (ret
== 0 && inode
->i_nlink
> 0) {
7063 trans
->block_rsv
= root
->orphan_block_rsv
;
7064 ret
= btrfs_orphan_del(trans
, inode
);
7070 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7071 ret
= btrfs_update_inode(trans
, root
, inode
);
7075 ret
= btrfs_end_transaction(trans
, root
);
7076 btrfs_btree_balance_dirty(root
);
7080 btrfs_free_block_rsv(root
, rsv
);
7089 * create a new subvolume directory/inode (helper for the ioctl).
7091 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
7092 struct btrfs_root
*new_root
, u64 new_dirid
)
7094 struct inode
*inode
;
7098 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
7099 new_dirid
, new_dirid
,
7100 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
7103 return PTR_ERR(inode
);
7104 inode
->i_op
= &btrfs_dir_inode_operations
;
7105 inode
->i_fop
= &btrfs_dir_file_operations
;
7107 set_nlink(inode
, 1);
7108 btrfs_i_size_write(inode
, 0);
7110 err
= btrfs_update_inode(trans
, new_root
, inode
);
7116 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
7118 struct btrfs_inode
*ei
;
7119 struct inode
*inode
;
7121 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
7128 ei
->last_sub_trans
= 0;
7129 ei
->logged_trans
= 0;
7130 ei
->delalloc_bytes
= 0;
7131 ei
->disk_i_size
= 0;
7134 ei
->index_cnt
= (u64
)-1;
7135 ei
->last_unlink_trans
= 0;
7136 ei
->last_log_commit
= 0;
7138 spin_lock_init(&ei
->lock
);
7139 ei
->outstanding_extents
= 0;
7140 ei
->reserved_extents
= 0;
7142 ei
->runtime_flags
= 0;
7143 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
7145 ei
->delayed_node
= NULL
;
7147 inode
= &ei
->vfs_inode
;
7148 extent_map_tree_init(&ei
->extent_tree
);
7149 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
);
7150 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
);
7151 ei
->io_tree
.track_uptodate
= 1;
7152 ei
->io_failure_tree
.track_uptodate
= 1;
7153 atomic_set(&ei
->sync_writers
, 0);
7154 mutex_init(&ei
->log_mutex
);
7155 mutex_init(&ei
->delalloc_mutex
);
7156 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
7157 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
7158 INIT_LIST_HEAD(&ei
->ordered_operations
);
7159 RB_CLEAR_NODE(&ei
->rb_node
);
7164 static void btrfs_i_callback(struct rcu_head
*head
)
7166 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
7167 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
7170 void btrfs_destroy_inode(struct inode
*inode
)
7172 struct btrfs_ordered_extent
*ordered
;
7173 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7175 WARN_ON(!hlist_empty(&inode
->i_dentry
));
7176 WARN_ON(inode
->i_data
.nrpages
);
7177 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
7178 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
7179 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
7180 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
7183 * This can happen where we create an inode, but somebody else also
7184 * created the same inode and we need to destroy the one we already
7191 * Make sure we're properly removed from the ordered operation
7195 if (!list_empty(&BTRFS_I(inode
)->ordered_operations
)) {
7196 spin_lock(&root
->fs_info
->ordered_extent_lock
);
7197 list_del_init(&BTRFS_I(inode
)->ordered_operations
);
7198 spin_unlock(&root
->fs_info
->ordered_extent_lock
);
7201 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
7202 &BTRFS_I(inode
)->runtime_flags
)) {
7203 printk(KERN_INFO
"BTRFS: inode %llu still on the orphan list\n",
7204 (unsigned long long)btrfs_ino(inode
));
7205 atomic_dec(&root
->orphan_inodes
);
7209 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
7213 printk(KERN_ERR
"btrfs found ordered "
7214 "extent %llu %llu on inode cleanup\n",
7215 (unsigned long long)ordered
->file_offset
,
7216 (unsigned long long)ordered
->len
);
7217 btrfs_remove_ordered_extent(inode
, ordered
);
7218 btrfs_put_ordered_extent(ordered
);
7219 btrfs_put_ordered_extent(ordered
);
7222 inode_tree_del(inode
);
7223 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
7225 btrfs_remove_delayed_node(inode
);
7226 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
7229 int btrfs_drop_inode(struct inode
*inode
)
7231 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7233 if (btrfs_root_refs(&root
->root_item
) == 0 &&
7234 !btrfs_is_free_space_inode(inode
))
7237 return generic_drop_inode(inode
);
7240 static void init_once(void *foo
)
7242 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
7244 inode_init_once(&ei
->vfs_inode
);
7247 void btrfs_destroy_cachep(void)
7250 * Make sure all delayed rcu free inodes are flushed before we
7254 if (btrfs_inode_cachep
)
7255 kmem_cache_destroy(btrfs_inode_cachep
);
7256 if (btrfs_trans_handle_cachep
)
7257 kmem_cache_destroy(btrfs_trans_handle_cachep
);
7258 if (btrfs_transaction_cachep
)
7259 kmem_cache_destroy(btrfs_transaction_cachep
);
7260 if (btrfs_path_cachep
)
7261 kmem_cache_destroy(btrfs_path_cachep
);
7262 if (btrfs_free_space_cachep
)
7263 kmem_cache_destroy(btrfs_free_space_cachep
);
7264 if (btrfs_delalloc_work_cachep
)
7265 kmem_cache_destroy(btrfs_delalloc_work_cachep
);
7268 int btrfs_init_cachep(void)
7270 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode",
7271 sizeof(struct btrfs_inode
), 0,
7272 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
7273 if (!btrfs_inode_cachep
)
7276 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle",
7277 sizeof(struct btrfs_trans_handle
), 0,
7278 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7279 if (!btrfs_trans_handle_cachep
)
7282 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction",
7283 sizeof(struct btrfs_transaction
), 0,
7284 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7285 if (!btrfs_transaction_cachep
)
7288 btrfs_path_cachep
= kmem_cache_create("btrfs_path",
7289 sizeof(struct btrfs_path
), 0,
7290 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7291 if (!btrfs_path_cachep
)
7294 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space",
7295 sizeof(struct btrfs_free_space
), 0,
7296 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7297 if (!btrfs_free_space_cachep
)
7300 btrfs_delalloc_work_cachep
= kmem_cache_create("btrfs_delalloc_work",
7301 sizeof(struct btrfs_delalloc_work
), 0,
7302 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
,
7304 if (!btrfs_delalloc_work_cachep
)
7309 btrfs_destroy_cachep();
7313 static int btrfs_getattr(struct vfsmount
*mnt
,
7314 struct dentry
*dentry
, struct kstat
*stat
)
7316 struct inode
*inode
= dentry
->d_inode
;
7317 u32 blocksize
= inode
->i_sb
->s_blocksize
;
7319 generic_fillattr(inode
, stat
);
7320 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
7321 stat
->blksize
= PAGE_CACHE_SIZE
;
7322 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
7323 ALIGN(BTRFS_I(inode
)->delalloc_bytes
, blocksize
)) >> 9;
7328 * If a file is moved, it will inherit the cow and compression flags of the new
7331 static void fixup_inode_flags(struct inode
*dir
, struct inode
*inode
)
7333 struct btrfs_inode
*b_dir
= BTRFS_I(dir
);
7334 struct btrfs_inode
*b_inode
= BTRFS_I(inode
);
7336 if (b_dir
->flags
& BTRFS_INODE_NODATACOW
)
7337 b_inode
->flags
|= BTRFS_INODE_NODATACOW
;
7339 b_inode
->flags
&= ~BTRFS_INODE_NODATACOW
;
7341 if (b_dir
->flags
& BTRFS_INODE_COMPRESS
) {
7342 b_inode
->flags
|= BTRFS_INODE_COMPRESS
;
7343 b_inode
->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
7345 b_inode
->flags
&= ~(BTRFS_INODE_COMPRESS
|
7346 BTRFS_INODE_NOCOMPRESS
);
7350 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
7351 struct inode
*new_dir
, struct dentry
*new_dentry
)
7353 struct btrfs_trans_handle
*trans
;
7354 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
7355 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
7356 struct inode
*new_inode
= new_dentry
->d_inode
;
7357 struct inode
*old_inode
= old_dentry
->d_inode
;
7358 struct timespec ctime
= CURRENT_TIME
;
7362 u64 old_ino
= btrfs_ino(old_inode
);
7364 if (btrfs_ino(new_dir
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
7367 /* we only allow rename subvolume link between subvolumes */
7368 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
7371 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
7372 (new_inode
&& btrfs_ino(new_inode
) == BTRFS_FIRST_FREE_OBJECTID
))
7375 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
7376 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
7380 /* check for collisions, even if the name isn't there */
7381 ret
= btrfs_check_dir_item_collision(root
, new_dir
->i_ino
,
7382 new_dentry
->d_name
.name
,
7383 new_dentry
->d_name
.len
);
7386 if (ret
== -EEXIST
) {
7388 * eexist without a new_inode */
7394 /* maybe -EOVERFLOW */
7401 * we're using rename to replace one file with another.
7402 * and the replacement file is large. Start IO on it now so
7403 * we don't add too much work to the end of the transaction
7405 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
&&
7406 old_inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
7407 filemap_flush(old_inode
->i_mapping
);
7409 /* close the racy window with snapshot create/destroy ioctl */
7410 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
7411 down_read(&root
->fs_info
->subvol_sem
);
7413 * We want to reserve the absolute worst case amount of items. So if
7414 * both inodes are subvols and we need to unlink them then that would
7415 * require 4 item modifications, but if they are both normal inodes it
7416 * would require 5 item modifications, so we'll assume their normal
7417 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7418 * should cover the worst case number of items we'll modify.
7420 trans
= btrfs_start_transaction(root
, 20);
7421 if (IS_ERR(trans
)) {
7422 ret
= PTR_ERR(trans
);
7427 btrfs_record_root_in_trans(trans
, dest
);
7429 ret
= btrfs_set_inode_index(new_dir
, &index
);
7433 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
7434 /* force full log commit if subvolume involved. */
7435 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
7437 ret
= btrfs_insert_inode_ref(trans
, dest
,
7438 new_dentry
->d_name
.name
,
7439 new_dentry
->d_name
.len
,
7441 btrfs_ino(new_dir
), index
);
7445 * this is an ugly little race, but the rename is required
7446 * to make sure that if we crash, the inode is either at the
7447 * old name or the new one. pinning the log transaction lets
7448 * us make sure we don't allow a log commit to come in after
7449 * we unlink the name but before we add the new name back in.
7451 btrfs_pin_log_trans(root
);
7454 * make sure the inode gets flushed if it is replacing
7457 if (new_inode
&& new_inode
->i_size
&& S_ISREG(old_inode
->i_mode
))
7458 btrfs_add_ordered_operation(trans
, root
, old_inode
);
7460 inode_inc_iversion(old_dir
);
7461 inode_inc_iversion(new_dir
);
7462 inode_inc_iversion(old_inode
);
7463 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
7464 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
7465 old_inode
->i_ctime
= ctime
;
7467 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
7468 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
7470 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
7471 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
7472 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
7473 old_dentry
->d_name
.name
,
7474 old_dentry
->d_name
.len
);
7476 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
7477 old_dentry
->d_inode
,
7478 old_dentry
->d_name
.name
,
7479 old_dentry
->d_name
.len
);
7481 ret
= btrfs_update_inode(trans
, root
, old_inode
);
7484 btrfs_abort_transaction(trans
, root
, ret
);
7489 inode_inc_iversion(new_inode
);
7490 new_inode
->i_ctime
= CURRENT_TIME
;
7491 if (unlikely(btrfs_ino(new_inode
) ==
7492 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
7493 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
7494 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
7496 new_dentry
->d_name
.name
,
7497 new_dentry
->d_name
.len
);
7498 BUG_ON(new_inode
->i_nlink
== 0);
7500 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
7501 new_dentry
->d_inode
,
7502 new_dentry
->d_name
.name
,
7503 new_dentry
->d_name
.len
);
7505 if (!ret
&& new_inode
->i_nlink
== 0) {
7506 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
7510 btrfs_abort_transaction(trans
, root
, ret
);
7515 fixup_inode_flags(new_dir
, old_inode
);
7517 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
7518 new_dentry
->d_name
.name
,
7519 new_dentry
->d_name
.len
, 0, index
);
7521 btrfs_abort_transaction(trans
, root
, ret
);
7525 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
7526 struct dentry
*parent
= new_dentry
->d_parent
;
7527 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
7528 btrfs_end_log_trans(root
);
7531 btrfs_end_transaction(trans
, root
);
7533 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
7534 up_read(&root
->fs_info
->subvol_sem
);
7539 static void btrfs_run_delalloc_work(struct btrfs_work
*work
)
7541 struct btrfs_delalloc_work
*delalloc_work
;
7543 delalloc_work
= container_of(work
, struct btrfs_delalloc_work
,
7545 if (delalloc_work
->wait
)
7546 btrfs_wait_ordered_range(delalloc_work
->inode
, 0, (u64
)-1);
7548 filemap_flush(delalloc_work
->inode
->i_mapping
);
7550 if (delalloc_work
->delay_iput
)
7551 btrfs_add_delayed_iput(delalloc_work
->inode
);
7553 iput(delalloc_work
->inode
);
7554 complete(&delalloc_work
->completion
);
7557 struct btrfs_delalloc_work
*btrfs_alloc_delalloc_work(struct inode
*inode
,
7558 int wait
, int delay_iput
)
7560 struct btrfs_delalloc_work
*work
;
7562 work
= kmem_cache_zalloc(btrfs_delalloc_work_cachep
, GFP_NOFS
);
7566 init_completion(&work
->completion
);
7567 INIT_LIST_HEAD(&work
->list
);
7568 work
->inode
= inode
;
7570 work
->delay_iput
= delay_iput
;
7571 work
->work
.func
= btrfs_run_delalloc_work
;
7576 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work
*work
)
7578 wait_for_completion(&work
->completion
);
7579 kmem_cache_free(btrfs_delalloc_work_cachep
, work
);
7583 * some fairly slow code that needs optimization. This walks the list
7584 * of all the inodes with pending delalloc and forces them to disk.
7586 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
7588 struct btrfs_inode
*binode
;
7589 struct inode
*inode
;
7590 struct btrfs_delalloc_work
*work
, *next
;
7591 struct list_head works
;
7592 struct list_head splice
;
7595 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
7598 INIT_LIST_HEAD(&works
);
7599 INIT_LIST_HEAD(&splice
);
7601 spin_lock(&root
->fs_info
->delalloc_lock
);
7602 list_splice_init(&root
->fs_info
->delalloc_inodes
, &splice
);
7603 while (!list_empty(&splice
)) {
7604 binode
= list_entry(splice
.next
, struct btrfs_inode
,
7607 list_del_init(&binode
->delalloc_inodes
);
7609 inode
= igrab(&binode
->vfs_inode
);
7613 list_add_tail(&binode
->delalloc_inodes
,
7614 &root
->fs_info
->delalloc_inodes
);
7615 spin_unlock(&root
->fs_info
->delalloc_lock
);
7617 work
= btrfs_alloc_delalloc_work(inode
, 0, delay_iput
);
7618 if (unlikely(!work
)) {
7622 list_add_tail(&work
->list
, &works
);
7623 btrfs_queue_worker(&root
->fs_info
->flush_workers
,
7627 spin_lock(&root
->fs_info
->delalloc_lock
);
7629 spin_unlock(&root
->fs_info
->delalloc_lock
);
7631 list_for_each_entry_safe(work
, next
, &works
, list
) {
7632 list_del_init(&work
->list
);
7633 btrfs_wait_and_free_delalloc_work(work
);
7636 spin_lock(&root
->fs_info
->delalloc_lock
);
7637 if (!list_empty(&root
->fs_info
->delalloc_inodes
)) {
7638 spin_unlock(&root
->fs_info
->delalloc_lock
);
7641 spin_unlock(&root
->fs_info
->delalloc_lock
);
7643 /* the filemap_flush will queue IO into the worker threads, but
7644 * we have to make sure the IO is actually started and that
7645 * ordered extents get created before we return
7647 atomic_inc(&root
->fs_info
->async_submit_draining
);
7648 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
7649 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
7650 wait_event(root
->fs_info
->async_submit_wait
,
7651 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
7652 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
7654 atomic_dec(&root
->fs_info
->async_submit_draining
);
7657 list_for_each_entry_safe(work
, next
, &works
, list
) {
7658 list_del_init(&work
->list
);
7659 btrfs_wait_and_free_delalloc_work(work
);
7662 if (!list_empty_careful(&splice
)) {
7663 spin_lock(&root
->fs_info
->delalloc_lock
);
7664 list_splice_tail(&splice
, &root
->fs_info
->delalloc_inodes
);
7665 spin_unlock(&root
->fs_info
->delalloc_lock
);
7670 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
7671 const char *symname
)
7673 struct btrfs_trans_handle
*trans
;
7674 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
7675 struct btrfs_path
*path
;
7676 struct btrfs_key key
;
7677 struct inode
*inode
= NULL
;
7685 struct btrfs_file_extent_item
*ei
;
7686 struct extent_buffer
*leaf
;
7688 name_len
= strlen(symname
) + 1;
7689 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
7690 return -ENAMETOOLONG
;
7693 * 2 items for inode item and ref
7694 * 2 items for dir items
7695 * 1 item for xattr if selinux is on
7697 trans
= btrfs_start_transaction(root
, 5);
7699 return PTR_ERR(trans
);
7701 err
= btrfs_find_free_ino(root
, &objectid
);
7705 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
7706 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
7707 S_IFLNK
|S_IRWXUGO
, &index
);
7708 if (IS_ERR(inode
)) {
7709 err
= PTR_ERR(inode
);
7713 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
7720 * If the active LSM wants to access the inode during
7721 * d_instantiate it needs these. Smack checks to see
7722 * if the filesystem supports xattrs by looking at the
7725 inode
->i_fop
= &btrfs_file_operations
;
7726 inode
->i_op
= &btrfs_file_inode_operations
;
7728 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
7732 inode
->i_mapping
->a_ops
= &btrfs_aops
;
7733 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
7734 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
7739 path
= btrfs_alloc_path();
7745 key
.objectid
= btrfs_ino(inode
);
7747 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
7748 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
7749 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
7753 btrfs_free_path(path
);
7756 leaf
= path
->nodes
[0];
7757 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
7758 struct btrfs_file_extent_item
);
7759 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
7760 btrfs_set_file_extent_type(leaf
, ei
,
7761 BTRFS_FILE_EXTENT_INLINE
);
7762 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
7763 btrfs_set_file_extent_compression(leaf
, ei
, 0);
7764 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
7765 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
7767 ptr
= btrfs_file_extent_inline_start(ei
);
7768 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
7769 btrfs_mark_buffer_dirty(leaf
);
7770 btrfs_free_path(path
);
7772 inode
->i_op
= &btrfs_symlink_inode_operations
;
7773 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
7774 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
7775 inode_set_bytes(inode
, name_len
);
7776 btrfs_i_size_write(inode
, name_len
- 1);
7777 err
= btrfs_update_inode(trans
, root
, inode
);
7783 d_instantiate(dentry
, inode
);
7784 btrfs_end_transaction(trans
, root
);
7786 inode_dec_link_count(inode
);
7789 btrfs_btree_balance_dirty(root
);
7793 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
7794 u64 start
, u64 num_bytes
, u64 min_size
,
7795 loff_t actual_len
, u64
*alloc_hint
,
7796 struct btrfs_trans_handle
*trans
)
7798 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
7799 struct extent_map
*em
;
7800 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7801 struct btrfs_key ins
;
7802 u64 cur_offset
= start
;
7805 bool own_trans
= true;
7809 while (num_bytes
> 0) {
7811 trans
= btrfs_start_transaction(root
, 3);
7812 if (IS_ERR(trans
)) {
7813 ret
= PTR_ERR(trans
);
7818 ret
= btrfs_reserve_extent(trans
, root
, num_bytes
, min_size
,
7819 0, *alloc_hint
, &ins
, 1);
7822 btrfs_end_transaction(trans
, root
);
7826 ret
= insert_reserved_file_extent(trans
, inode
,
7827 cur_offset
, ins
.objectid
,
7828 ins
.offset
, ins
.offset
,
7829 ins
.offset
, 0, 0, 0,
7830 BTRFS_FILE_EXTENT_PREALLOC
);
7832 btrfs_abort_transaction(trans
, root
, ret
);
7834 btrfs_end_transaction(trans
, root
);
7837 btrfs_drop_extent_cache(inode
, cur_offset
,
7838 cur_offset
+ ins
.offset
-1, 0);
7840 em
= alloc_extent_map();
7842 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
7843 &BTRFS_I(inode
)->runtime_flags
);
7847 em
->start
= cur_offset
;
7848 em
->orig_start
= cur_offset
;
7849 em
->len
= ins
.offset
;
7850 em
->block_start
= ins
.objectid
;
7851 em
->block_len
= ins
.offset
;
7852 em
->orig_block_len
= ins
.offset
;
7853 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
7854 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
7855 em
->generation
= trans
->transid
;
7858 write_lock(&em_tree
->lock
);
7859 ret
= add_extent_mapping(em_tree
, em
);
7861 list_move(&em
->list
,
7862 &em_tree
->modified_extents
);
7863 write_unlock(&em_tree
->lock
);
7866 btrfs_drop_extent_cache(inode
, cur_offset
,
7867 cur_offset
+ ins
.offset
- 1,
7870 free_extent_map(em
);
7872 num_bytes
-= ins
.offset
;
7873 cur_offset
+= ins
.offset
;
7874 *alloc_hint
= ins
.objectid
+ ins
.offset
;
7876 inode_inc_iversion(inode
);
7877 inode
->i_ctime
= CURRENT_TIME
;
7878 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
7879 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
7880 (actual_len
> inode
->i_size
) &&
7881 (cur_offset
> inode
->i_size
)) {
7882 if (cur_offset
> actual_len
)
7883 i_size
= actual_len
;
7885 i_size
= cur_offset
;
7886 i_size_write(inode
, i_size
);
7887 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
7890 ret
= btrfs_update_inode(trans
, root
, inode
);
7893 btrfs_abort_transaction(trans
, root
, ret
);
7895 btrfs_end_transaction(trans
, root
);
7900 btrfs_end_transaction(trans
, root
);
7905 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
7906 u64 start
, u64 num_bytes
, u64 min_size
,
7907 loff_t actual_len
, u64
*alloc_hint
)
7909 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
7910 min_size
, actual_len
, alloc_hint
,
7914 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
7915 struct btrfs_trans_handle
*trans
, int mode
,
7916 u64 start
, u64 num_bytes
, u64 min_size
,
7917 loff_t actual_len
, u64
*alloc_hint
)
7919 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
7920 min_size
, actual_len
, alloc_hint
, trans
);
7923 static int btrfs_set_page_dirty(struct page
*page
)
7925 return __set_page_dirty_nobuffers(page
);
7928 static int btrfs_permission(struct inode
*inode
, int mask
)
7930 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7931 umode_t mode
= inode
->i_mode
;
7933 if (mask
& MAY_WRITE
&&
7934 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
7935 if (btrfs_root_readonly(root
))
7937 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
7940 return generic_permission(inode
, mask
);
7943 static const struct inode_operations btrfs_dir_inode_operations
= {
7944 .getattr
= btrfs_getattr
,
7945 .lookup
= btrfs_lookup
,
7946 .create
= btrfs_create
,
7947 .unlink
= btrfs_unlink
,
7949 .mkdir
= btrfs_mkdir
,
7950 .rmdir
= btrfs_rmdir
,
7951 .rename
= btrfs_rename
,
7952 .symlink
= btrfs_symlink
,
7953 .setattr
= btrfs_setattr
,
7954 .mknod
= btrfs_mknod
,
7955 .setxattr
= btrfs_setxattr
,
7956 .getxattr
= btrfs_getxattr
,
7957 .listxattr
= btrfs_listxattr
,
7958 .removexattr
= btrfs_removexattr
,
7959 .permission
= btrfs_permission
,
7960 .get_acl
= btrfs_get_acl
,
7962 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
7963 .lookup
= btrfs_lookup
,
7964 .permission
= btrfs_permission
,
7965 .get_acl
= btrfs_get_acl
,
7968 static const struct file_operations btrfs_dir_file_operations
= {
7969 .llseek
= generic_file_llseek
,
7970 .read
= generic_read_dir
,
7971 .readdir
= btrfs_real_readdir
,
7972 .unlocked_ioctl
= btrfs_ioctl
,
7973 #ifdef CONFIG_COMPAT
7974 .compat_ioctl
= btrfs_ioctl
,
7976 .release
= btrfs_release_file
,
7977 .fsync
= btrfs_sync_file
,
7980 static struct extent_io_ops btrfs_extent_io_ops
= {
7981 .fill_delalloc
= run_delalloc_range
,
7982 .submit_bio_hook
= btrfs_submit_bio_hook
,
7983 .merge_bio_hook
= btrfs_merge_bio_hook
,
7984 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
7985 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
7986 .writepage_start_hook
= btrfs_writepage_start_hook
,
7987 .set_bit_hook
= btrfs_set_bit_hook
,
7988 .clear_bit_hook
= btrfs_clear_bit_hook
,
7989 .merge_extent_hook
= btrfs_merge_extent_hook
,
7990 .split_extent_hook
= btrfs_split_extent_hook
,
7994 * btrfs doesn't support the bmap operation because swapfiles
7995 * use bmap to make a mapping of extents in the file. They assume
7996 * these extents won't change over the life of the file and they
7997 * use the bmap result to do IO directly to the drive.
7999 * the btrfs bmap call would return logical addresses that aren't
8000 * suitable for IO and they also will change frequently as COW
8001 * operations happen. So, swapfile + btrfs == corruption.
8003 * For now we're avoiding this by dropping bmap.
8005 static const struct address_space_operations btrfs_aops
= {
8006 .readpage
= btrfs_readpage
,
8007 .writepage
= btrfs_writepage
,
8008 .writepages
= btrfs_writepages
,
8009 .readpages
= btrfs_readpages
,
8010 .direct_IO
= btrfs_direct_IO
,
8011 .invalidatepage
= btrfs_invalidatepage
,
8012 .releasepage
= btrfs_releasepage
,
8013 .set_page_dirty
= btrfs_set_page_dirty
,
8014 .error_remove_page
= generic_error_remove_page
,
8017 static const struct address_space_operations btrfs_symlink_aops
= {
8018 .readpage
= btrfs_readpage
,
8019 .writepage
= btrfs_writepage
,
8020 .invalidatepage
= btrfs_invalidatepage
,
8021 .releasepage
= btrfs_releasepage
,
8024 static const struct inode_operations btrfs_file_inode_operations
= {
8025 .getattr
= btrfs_getattr
,
8026 .setattr
= btrfs_setattr
,
8027 .setxattr
= btrfs_setxattr
,
8028 .getxattr
= btrfs_getxattr
,
8029 .listxattr
= btrfs_listxattr
,
8030 .removexattr
= btrfs_removexattr
,
8031 .permission
= btrfs_permission
,
8032 .fiemap
= btrfs_fiemap
,
8033 .get_acl
= btrfs_get_acl
,
8034 .update_time
= btrfs_update_time
,
8036 static const struct inode_operations btrfs_special_inode_operations
= {
8037 .getattr
= btrfs_getattr
,
8038 .setattr
= btrfs_setattr
,
8039 .permission
= btrfs_permission
,
8040 .setxattr
= btrfs_setxattr
,
8041 .getxattr
= btrfs_getxattr
,
8042 .listxattr
= btrfs_listxattr
,
8043 .removexattr
= btrfs_removexattr
,
8044 .get_acl
= btrfs_get_acl
,
8045 .update_time
= btrfs_update_time
,
8047 static const struct inode_operations btrfs_symlink_inode_operations
= {
8048 .readlink
= generic_readlink
,
8049 .follow_link
= page_follow_link_light
,
8050 .put_link
= page_put_link
,
8051 .getattr
= btrfs_getattr
,
8052 .setattr
= btrfs_setattr
,
8053 .permission
= btrfs_permission
,
8054 .setxattr
= btrfs_setxattr
,
8055 .getxattr
= btrfs_getxattr
,
8056 .listxattr
= btrfs_listxattr
,
8057 .removexattr
= btrfs_removexattr
,
8058 .get_acl
= btrfs_get_acl
,
8059 .update_time
= btrfs_update_time
,
8062 const struct dentry_operations btrfs_dentry_operations
= {
8063 .d_delete
= btrfs_dentry_delete
,
8064 .d_release
= btrfs_dentry_release
,