2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/statfs.h>
34 #include <linux/compat.h>
35 #include <linux/bit_spinlock.h>
36 #include <linux/xattr.h>
37 #include <linux/posix_acl.h>
38 #include <linux/falloc.h>
39 #include <linux/slab.h>
43 #include "transaction.h"
44 #include "btrfs_inode.h"
46 #include "print-tree.h"
48 #include "ordered-data.h"
51 #include "compression.h"
54 struct btrfs_iget_args
{
56 struct btrfs_root
*root
;
59 static const struct inode_operations btrfs_dir_inode_operations
;
60 static const struct inode_operations btrfs_symlink_inode_operations
;
61 static const struct inode_operations btrfs_dir_ro_inode_operations
;
62 static const struct inode_operations btrfs_special_inode_operations
;
63 static const struct inode_operations btrfs_file_inode_operations
;
64 static const struct address_space_operations btrfs_aops
;
65 static const struct address_space_operations btrfs_symlink_aops
;
66 static const struct file_operations btrfs_dir_file_operations
;
67 static struct extent_io_ops btrfs_extent_io_ops
;
69 static struct kmem_cache
*btrfs_inode_cachep
;
70 struct kmem_cache
*btrfs_trans_handle_cachep
;
71 struct kmem_cache
*btrfs_transaction_cachep
;
72 struct kmem_cache
*btrfs_path_cachep
;
75 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
76 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
77 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
78 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
79 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
80 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
81 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
82 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
85 static void btrfs_truncate(struct inode
*inode
);
86 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
);
87 static noinline
int cow_file_range(struct inode
*inode
,
88 struct page
*locked_page
,
89 u64 start
, u64 end
, int *page_started
,
90 unsigned long *nr_written
, int unlock
);
92 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
93 struct inode
*inode
, struct inode
*dir
)
97 err
= btrfs_init_acl(trans
, inode
, dir
);
99 err
= btrfs_xattr_security_init(trans
, inode
, dir
);
104 * this does all the hard work for inserting an inline extent into
105 * the btree. The caller should have done a btrfs_drop_extents so that
106 * no overlapping inline items exist in the btree
108 static noinline
int insert_inline_extent(struct btrfs_trans_handle
*trans
,
109 struct btrfs_root
*root
, struct inode
*inode
,
110 u64 start
, size_t size
, size_t compressed_size
,
111 struct page
**compressed_pages
)
113 struct btrfs_key key
;
114 struct btrfs_path
*path
;
115 struct extent_buffer
*leaf
;
116 struct page
*page
= NULL
;
119 struct btrfs_file_extent_item
*ei
;
122 size_t cur_size
= size
;
124 unsigned long offset
;
125 int use_compress
= 0;
127 if (compressed_size
&& compressed_pages
) {
129 cur_size
= compressed_size
;
132 path
= btrfs_alloc_path();
136 path
->leave_spinning
= 1;
137 btrfs_set_trans_block_group(trans
, inode
);
139 key
.objectid
= inode
->i_ino
;
141 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
142 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
144 inode_add_bytes(inode
, size
);
145 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
152 leaf
= path
->nodes
[0];
153 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
154 struct btrfs_file_extent_item
);
155 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
156 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
157 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
158 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
159 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
160 ptr
= btrfs_file_extent_inline_start(ei
);
165 while (compressed_size
> 0) {
166 cpage
= compressed_pages
[i
];
167 cur_size
= min_t(unsigned long, compressed_size
,
170 kaddr
= kmap_atomic(cpage
, KM_USER0
);
171 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
172 kunmap_atomic(kaddr
, KM_USER0
);
176 compressed_size
-= cur_size
;
178 btrfs_set_file_extent_compression(leaf
, ei
,
179 BTRFS_COMPRESS_ZLIB
);
181 page
= find_get_page(inode
->i_mapping
,
182 start
>> PAGE_CACHE_SHIFT
);
183 btrfs_set_file_extent_compression(leaf
, ei
, 0);
184 kaddr
= kmap_atomic(page
, KM_USER0
);
185 offset
= start
& (PAGE_CACHE_SIZE
- 1);
186 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
187 kunmap_atomic(kaddr
, KM_USER0
);
188 page_cache_release(page
);
190 btrfs_mark_buffer_dirty(leaf
);
191 btrfs_free_path(path
);
194 * we're an inline extent, so nobody can
195 * extend the file past i_size without locking
196 * a page we already have locked.
198 * We must do any isize and inode updates
199 * before we unlock the pages. Otherwise we
200 * could end up racing with unlink.
202 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
203 btrfs_update_inode(trans
, root
, inode
);
207 btrfs_free_path(path
);
213 * conditionally insert an inline extent into the file. This
214 * does the checks required to make sure the data is small enough
215 * to fit as an inline extent.
217 static noinline
int cow_file_range_inline(struct btrfs_trans_handle
*trans
,
218 struct btrfs_root
*root
,
219 struct inode
*inode
, u64 start
, u64 end
,
220 size_t compressed_size
,
221 struct page
**compressed_pages
)
223 u64 isize
= i_size_read(inode
);
224 u64 actual_end
= min(end
+ 1, isize
);
225 u64 inline_len
= actual_end
- start
;
226 u64 aligned_end
= (end
+ root
->sectorsize
- 1) &
227 ~((u64
)root
->sectorsize
- 1);
229 u64 data_len
= inline_len
;
233 data_len
= compressed_size
;
236 actual_end
>= PAGE_CACHE_SIZE
||
237 data_len
>= BTRFS_MAX_INLINE_DATA_SIZE(root
) ||
239 (actual_end
& (root
->sectorsize
- 1)) == 0) ||
241 data_len
> root
->fs_info
->max_inline
) {
245 ret
= btrfs_drop_extents(trans
, inode
, start
, aligned_end
,
249 if (isize
> actual_end
)
250 inline_len
= min_t(u64
, isize
, actual_end
);
251 ret
= insert_inline_extent(trans
, root
, inode
, start
,
252 inline_len
, compressed_size
,
255 btrfs_drop_extent_cache(inode
, start
, aligned_end
- 1, 0);
259 struct async_extent
{
264 unsigned long nr_pages
;
265 struct list_head list
;
270 struct btrfs_root
*root
;
271 struct page
*locked_page
;
274 struct list_head extents
;
275 struct btrfs_work work
;
278 static noinline
int add_async_extent(struct async_cow
*cow
,
279 u64 start
, u64 ram_size
,
282 unsigned long nr_pages
)
284 struct async_extent
*async_extent
;
286 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
287 async_extent
->start
= start
;
288 async_extent
->ram_size
= ram_size
;
289 async_extent
->compressed_size
= compressed_size
;
290 async_extent
->pages
= pages
;
291 async_extent
->nr_pages
= nr_pages
;
292 list_add_tail(&async_extent
->list
, &cow
->extents
);
297 * we create compressed extents in two phases. The first
298 * phase compresses a range of pages that have already been
299 * locked (both pages and state bits are locked).
301 * This is done inside an ordered work queue, and the compression
302 * is spread across many cpus. The actual IO submission is step
303 * two, and the ordered work queue takes care of making sure that
304 * happens in the same order things were put onto the queue by
305 * writepages and friends.
307 * If this code finds it can't get good compression, it puts an
308 * entry onto the work queue to write the uncompressed bytes. This
309 * makes sure that both compressed inodes and uncompressed inodes
310 * are written in the same order that pdflush sent them down.
312 static noinline
int compress_file_range(struct inode
*inode
,
313 struct page
*locked_page
,
315 struct async_cow
*async_cow
,
318 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
319 struct btrfs_trans_handle
*trans
;
323 u64 blocksize
= root
->sectorsize
;
325 u64 isize
= i_size_read(inode
);
327 struct page
**pages
= NULL
;
328 unsigned long nr_pages
;
329 unsigned long nr_pages_ret
= 0;
330 unsigned long total_compressed
= 0;
331 unsigned long total_in
= 0;
332 unsigned long max_compressed
= 128 * 1024;
333 unsigned long max_uncompressed
= 128 * 1024;
339 actual_end
= min_t(u64
, isize
, end
+ 1);
342 nr_pages
= (end
>> PAGE_CACHE_SHIFT
) - (start
>> PAGE_CACHE_SHIFT
) + 1;
343 nr_pages
= min(nr_pages
, (128 * 1024UL) / PAGE_CACHE_SIZE
);
346 * we don't want to send crud past the end of i_size through
347 * compression, that's just a waste of CPU time. So, if the
348 * end of the file is before the start of our current
349 * requested range of bytes, we bail out to the uncompressed
350 * cleanup code that can deal with all of this.
352 * It isn't really the fastest way to fix things, but this is a
353 * very uncommon corner.
355 if (actual_end
<= start
)
356 goto cleanup_and_bail_uncompressed
;
358 total_compressed
= actual_end
- start
;
360 /* we want to make sure that amount of ram required to uncompress
361 * an extent is reasonable, so we limit the total size in ram
362 * of a compressed extent to 128k. This is a crucial number
363 * because it also controls how easily we can spread reads across
364 * cpus for decompression.
366 * We also want to make sure the amount of IO required to do
367 * a random read is reasonably small, so we limit the size of
368 * a compressed extent to 128k.
370 total_compressed
= min(total_compressed
, max_uncompressed
);
371 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
372 num_bytes
= max(blocksize
, num_bytes
);
373 disk_num_bytes
= num_bytes
;
378 * we do compression for mount -o compress and when the
379 * inode has not been flagged as nocompress. This flag can
380 * change at any time if we discover bad compression ratios.
382 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
) &&
383 (btrfs_test_opt(root
, COMPRESS
) ||
384 (BTRFS_I(inode
)->force_compress
))) {
386 pages
= kzalloc(sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
388 ret
= btrfs_zlib_compress_pages(inode
->i_mapping
, start
,
389 total_compressed
, pages
,
390 nr_pages
, &nr_pages_ret
,
396 unsigned long offset
= total_compressed
&
397 (PAGE_CACHE_SIZE
- 1);
398 struct page
*page
= pages
[nr_pages_ret
- 1];
401 /* zero the tail end of the last page, we might be
402 * sending it down to disk
405 kaddr
= kmap_atomic(page
, KM_USER0
);
406 memset(kaddr
+ offset
, 0,
407 PAGE_CACHE_SIZE
- offset
);
408 kunmap_atomic(kaddr
, KM_USER0
);
414 trans
= btrfs_join_transaction(root
, 1);
416 btrfs_set_trans_block_group(trans
, inode
);
418 /* lets try to make an inline extent */
419 if (ret
|| total_in
< (actual_end
- start
)) {
420 /* we didn't compress the entire range, try
421 * to make an uncompressed inline extent.
423 ret
= cow_file_range_inline(trans
, root
, inode
,
424 start
, end
, 0, NULL
);
426 /* try making a compressed inline extent */
427 ret
= cow_file_range_inline(trans
, root
, inode
,
429 total_compressed
, pages
);
433 * inline extent creation worked, we don't need
434 * to create any more async work items. Unlock
435 * and free up our temp pages.
437 extent_clear_unlock_delalloc(inode
,
438 &BTRFS_I(inode
)->io_tree
,
440 EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
441 EXTENT_CLEAR_DELALLOC
|
442 EXTENT_CLEAR_ACCOUNTING
|
443 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
);
445 btrfs_end_transaction(trans
, root
);
448 btrfs_end_transaction(trans
, root
);
453 * we aren't doing an inline extent round the compressed size
454 * up to a block size boundary so the allocator does sane
457 total_compressed
= (total_compressed
+ blocksize
- 1) &
461 * one last check to make sure the compression is really a
462 * win, compare the page count read with the blocks on disk
464 total_in
= (total_in
+ PAGE_CACHE_SIZE
- 1) &
465 ~(PAGE_CACHE_SIZE
- 1);
466 if (total_compressed
>= total_in
) {
469 disk_num_bytes
= total_compressed
;
470 num_bytes
= total_in
;
473 if (!will_compress
&& pages
) {
475 * the compression code ran but failed to make things smaller,
476 * free any pages it allocated and our page pointer array
478 for (i
= 0; i
< nr_pages_ret
; i
++) {
479 WARN_ON(pages
[i
]->mapping
);
480 page_cache_release(pages
[i
]);
484 total_compressed
= 0;
487 /* flag the file so we don't compress in the future */
488 if (!btrfs_test_opt(root
, FORCE_COMPRESS
) &&
489 !(BTRFS_I(inode
)->force_compress
)) {
490 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
496 /* the async work queues will take care of doing actual
497 * allocation on disk for these compressed pages,
498 * and will submit them to the elevator.
500 add_async_extent(async_cow
, start
, num_bytes
,
501 total_compressed
, pages
, nr_pages_ret
);
503 if (start
+ num_bytes
< end
&& start
+ num_bytes
< actual_end
) {
510 cleanup_and_bail_uncompressed
:
512 * No compression, but we still need to write the pages in
513 * the file we've been given so far. redirty the locked
514 * page if it corresponds to our extent and set things up
515 * for the async work queue to run cow_file_range to do
516 * the normal delalloc dance
518 if (page_offset(locked_page
) >= start
&&
519 page_offset(locked_page
) <= end
) {
520 __set_page_dirty_nobuffers(locked_page
);
521 /* unlocked later on in the async handlers */
523 add_async_extent(async_cow
, start
, end
- start
+ 1, 0, NULL
, 0);
531 for (i
= 0; i
< nr_pages_ret
; i
++) {
532 WARN_ON(pages
[i
]->mapping
);
533 page_cache_release(pages
[i
]);
541 * phase two of compressed writeback. This is the ordered portion
542 * of the code, which only gets called in the order the work was
543 * queued. We walk all the async extents created by compress_file_range
544 * and send them down to the disk.
546 static noinline
int submit_compressed_extents(struct inode
*inode
,
547 struct async_cow
*async_cow
)
549 struct async_extent
*async_extent
;
551 struct btrfs_trans_handle
*trans
;
552 struct btrfs_key ins
;
553 struct extent_map
*em
;
554 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
555 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
556 struct extent_io_tree
*io_tree
;
559 if (list_empty(&async_cow
->extents
))
563 while (!list_empty(&async_cow
->extents
)) {
564 async_extent
= list_entry(async_cow
->extents
.next
,
565 struct async_extent
, list
);
566 list_del(&async_extent
->list
);
568 io_tree
= &BTRFS_I(inode
)->io_tree
;
571 /* did the compression code fall back to uncompressed IO? */
572 if (!async_extent
->pages
) {
573 int page_started
= 0;
574 unsigned long nr_written
= 0;
576 lock_extent(io_tree
, async_extent
->start
,
577 async_extent
->start
+
578 async_extent
->ram_size
- 1, GFP_NOFS
);
580 /* allocate blocks */
581 ret
= cow_file_range(inode
, async_cow
->locked_page
,
583 async_extent
->start
+
584 async_extent
->ram_size
- 1,
585 &page_started
, &nr_written
, 0);
588 * if page_started, cow_file_range inserted an
589 * inline extent and took care of all the unlocking
590 * and IO for us. Otherwise, we need to submit
591 * all those pages down to the drive.
593 if (!page_started
&& !ret
)
594 extent_write_locked_range(io_tree
,
595 inode
, async_extent
->start
,
596 async_extent
->start
+
597 async_extent
->ram_size
- 1,
605 lock_extent(io_tree
, async_extent
->start
,
606 async_extent
->start
+ async_extent
->ram_size
- 1,
609 trans
= btrfs_join_transaction(root
, 1);
610 ret
= btrfs_reserve_extent(trans
, root
,
611 async_extent
->compressed_size
,
612 async_extent
->compressed_size
,
615 btrfs_end_transaction(trans
, root
);
619 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
620 WARN_ON(async_extent
->pages
[i
]->mapping
);
621 page_cache_release(async_extent
->pages
[i
]);
623 kfree(async_extent
->pages
);
624 async_extent
->nr_pages
= 0;
625 async_extent
->pages
= NULL
;
626 unlock_extent(io_tree
, async_extent
->start
,
627 async_extent
->start
+
628 async_extent
->ram_size
- 1, GFP_NOFS
);
633 * here we're doing allocation and writeback of the
636 btrfs_drop_extent_cache(inode
, async_extent
->start
,
637 async_extent
->start
+
638 async_extent
->ram_size
- 1, 0);
640 em
= alloc_extent_map(GFP_NOFS
);
641 em
->start
= async_extent
->start
;
642 em
->len
= async_extent
->ram_size
;
643 em
->orig_start
= em
->start
;
645 em
->block_start
= ins
.objectid
;
646 em
->block_len
= ins
.offset
;
647 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
648 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
649 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
652 write_lock(&em_tree
->lock
);
653 ret
= add_extent_mapping(em_tree
, em
);
654 write_unlock(&em_tree
->lock
);
655 if (ret
!= -EEXIST
) {
659 btrfs_drop_extent_cache(inode
, async_extent
->start
,
660 async_extent
->start
+
661 async_extent
->ram_size
- 1, 0);
664 ret
= btrfs_add_ordered_extent(inode
, async_extent
->start
,
666 async_extent
->ram_size
,
668 BTRFS_ORDERED_COMPRESSED
);
672 * clear dirty, set writeback and unlock the pages.
674 extent_clear_unlock_delalloc(inode
,
675 &BTRFS_I(inode
)->io_tree
,
677 async_extent
->start
+
678 async_extent
->ram_size
- 1,
679 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
680 EXTENT_CLEAR_UNLOCK
|
681 EXTENT_CLEAR_DELALLOC
|
682 EXTENT_CLEAR_DIRTY
| EXTENT_SET_WRITEBACK
);
684 ret
= btrfs_submit_compressed_write(inode
,
686 async_extent
->ram_size
,
688 ins
.offset
, async_extent
->pages
,
689 async_extent
->nr_pages
);
692 alloc_hint
= ins
.objectid
+ ins
.offset
;
701 * when extent_io.c finds a delayed allocation range in the file,
702 * the call backs end up in this code. The basic idea is to
703 * allocate extents on disk for the range, and create ordered data structs
704 * in ram to track those extents.
706 * locked_page is the page that writepage had locked already. We use
707 * it to make sure we don't do extra locks or unlocks.
709 * *page_started is set to one if we unlock locked_page and do everything
710 * required to start IO on it. It may be clean and already done with
713 static noinline
int cow_file_range(struct inode
*inode
,
714 struct page
*locked_page
,
715 u64 start
, u64 end
, int *page_started
,
716 unsigned long *nr_written
,
719 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
720 struct btrfs_trans_handle
*trans
;
723 unsigned long ram_size
;
726 u64 blocksize
= root
->sectorsize
;
728 u64 isize
= i_size_read(inode
);
729 struct btrfs_key ins
;
730 struct extent_map
*em
;
731 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
734 trans
= btrfs_join_transaction(root
, 1);
736 btrfs_set_trans_block_group(trans
, inode
);
738 actual_end
= min_t(u64
, isize
, end
+ 1);
740 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
741 num_bytes
= max(blocksize
, num_bytes
);
742 disk_num_bytes
= num_bytes
;
746 /* lets try to make an inline extent */
747 ret
= cow_file_range_inline(trans
, root
, inode
,
748 start
, end
, 0, NULL
);
750 extent_clear_unlock_delalloc(inode
,
751 &BTRFS_I(inode
)->io_tree
,
753 EXTENT_CLEAR_UNLOCK_PAGE
|
754 EXTENT_CLEAR_UNLOCK
|
755 EXTENT_CLEAR_DELALLOC
|
756 EXTENT_CLEAR_ACCOUNTING
|
758 EXTENT_SET_WRITEBACK
|
759 EXTENT_END_WRITEBACK
);
761 *nr_written
= *nr_written
+
762 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
769 BUG_ON(disk_num_bytes
>
770 btrfs_super_total_bytes(&root
->fs_info
->super_copy
));
773 read_lock(&BTRFS_I(inode
)->extent_tree
.lock
);
774 em
= search_extent_mapping(&BTRFS_I(inode
)->extent_tree
,
778 * if block start isn't an actual block number then find the
779 * first block in this inode and use that as a hint. If that
780 * block is also bogus then just don't worry about it.
782 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
784 em
= search_extent_mapping(em_tree
, 0, 0);
785 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
786 alloc_hint
= em
->block_start
;
790 alloc_hint
= em
->block_start
;
794 read_unlock(&BTRFS_I(inode
)->extent_tree
.lock
);
795 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
797 while (disk_num_bytes
> 0) {
800 cur_alloc_size
= disk_num_bytes
;
801 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
802 root
->sectorsize
, 0, alloc_hint
,
806 em
= alloc_extent_map(GFP_NOFS
);
808 em
->orig_start
= em
->start
;
809 ram_size
= ins
.offset
;
810 em
->len
= ins
.offset
;
812 em
->block_start
= ins
.objectid
;
813 em
->block_len
= ins
.offset
;
814 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
815 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
818 write_lock(&em_tree
->lock
);
819 ret
= add_extent_mapping(em_tree
, em
);
820 write_unlock(&em_tree
->lock
);
821 if (ret
!= -EEXIST
) {
825 btrfs_drop_extent_cache(inode
, start
,
826 start
+ ram_size
- 1, 0);
829 cur_alloc_size
= ins
.offset
;
830 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
831 ram_size
, cur_alloc_size
, 0);
834 if (root
->root_key
.objectid
==
835 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
836 ret
= btrfs_reloc_clone_csums(inode
, start
,
841 if (disk_num_bytes
< cur_alloc_size
)
844 /* we're not doing compressed IO, don't unlock the first
845 * page (which the caller expects to stay locked), don't
846 * clear any dirty bits and don't set any writeback bits
848 * Do set the Private2 bit so we know this page was properly
849 * setup for writepage
851 op
= unlock
? EXTENT_CLEAR_UNLOCK_PAGE
: 0;
852 op
|= EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
855 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
856 start
, start
+ ram_size
- 1,
858 disk_num_bytes
-= cur_alloc_size
;
859 num_bytes
-= cur_alloc_size
;
860 alloc_hint
= ins
.objectid
+ ins
.offset
;
861 start
+= cur_alloc_size
;
865 btrfs_end_transaction(trans
, root
);
871 * work queue call back to started compression on a file and pages
873 static noinline
void async_cow_start(struct btrfs_work
*work
)
875 struct async_cow
*async_cow
;
877 async_cow
= container_of(work
, struct async_cow
, work
);
879 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
880 async_cow
->start
, async_cow
->end
, async_cow
,
883 async_cow
->inode
= NULL
;
887 * work queue call back to submit previously compressed pages
889 static noinline
void async_cow_submit(struct btrfs_work
*work
)
891 struct async_cow
*async_cow
;
892 struct btrfs_root
*root
;
893 unsigned long nr_pages
;
895 async_cow
= container_of(work
, struct async_cow
, work
);
897 root
= async_cow
->root
;
898 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
901 atomic_sub(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
903 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
905 waitqueue_active(&root
->fs_info
->async_submit_wait
))
906 wake_up(&root
->fs_info
->async_submit_wait
);
908 if (async_cow
->inode
)
909 submit_compressed_extents(async_cow
->inode
, async_cow
);
912 static noinline
void async_cow_free(struct btrfs_work
*work
)
914 struct async_cow
*async_cow
;
915 async_cow
= container_of(work
, struct async_cow
, work
);
919 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
920 u64 start
, u64 end
, int *page_started
,
921 unsigned long *nr_written
)
923 struct async_cow
*async_cow
;
924 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
925 unsigned long nr_pages
;
927 int limit
= 10 * 1024 * 1042;
929 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
930 1, 0, NULL
, GFP_NOFS
);
931 while (start
< end
) {
932 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
933 async_cow
->inode
= inode
;
934 async_cow
->root
= root
;
935 async_cow
->locked_page
= locked_page
;
936 async_cow
->start
= start
;
938 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
941 cur_end
= min(end
, start
+ 512 * 1024 - 1);
943 async_cow
->end
= cur_end
;
944 INIT_LIST_HEAD(&async_cow
->extents
);
946 async_cow
->work
.func
= async_cow_start
;
947 async_cow
->work
.ordered_func
= async_cow_submit
;
948 async_cow
->work
.ordered_free
= async_cow_free
;
949 async_cow
->work
.flags
= 0;
951 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
953 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
955 btrfs_queue_worker(&root
->fs_info
->delalloc_workers
,
958 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
959 wait_event(root
->fs_info
->async_submit_wait
,
960 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
964 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
965 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
966 wait_event(root
->fs_info
->async_submit_wait
,
967 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
971 *nr_written
+= nr_pages
;
978 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
979 u64 bytenr
, u64 num_bytes
)
982 struct btrfs_ordered_sum
*sums
;
985 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
986 bytenr
+ num_bytes
- 1, &list
);
987 if (ret
== 0 && list_empty(&list
))
990 while (!list_empty(&list
)) {
991 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
992 list_del(&sums
->list
);
999 * when nowcow writeback call back. This checks for snapshots or COW copies
1000 * of the extents that exist in the file, and COWs the file as required.
1002 * If no cow copies or snapshots exist, we write directly to the existing
1005 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1006 struct page
*locked_page
,
1007 u64 start
, u64 end
, int *page_started
, int force
,
1008 unsigned long *nr_written
)
1010 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1011 struct btrfs_trans_handle
*trans
;
1012 struct extent_buffer
*leaf
;
1013 struct btrfs_path
*path
;
1014 struct btrfs_file_extent_item
*fi
;
1015 struct btrfs_key found_key
;
1028 path
= btrfs_alloc_path();
1030 trans
= btrfs_join_transaction(root
, 1);
1033 cow_start
= (u64
)-1;
1036 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
1039 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1040 leaf
= path
->nodes
[0];
1041 btrfs_item_key_to_cpu(leaf
, &found_key
,
1042 path
->slots
[0] - 1);
1043 if (found_key
.objectid
== inode
->i_ino
&&
1044 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1049 leaf
= path
->nodes
[0];
1050 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1051 ret
= btrfs_next_leaf(root
, path
);
1056 leaf
= path
->nodes
[0];
1062 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1064 if (found_key
.objectid
> inode
->i_ino
||
1065 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1066 found_key
.offset
> end
)
1069 if (found_key
.offset
> cur_offset
) {
1070 extent_end
= found_key
.offset
;
1075 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1076 struct btrfs_file_extent_item
);
1077 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1079 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1080 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1081 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1082 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1083 extent_end
= found_key
.offset
+
1084 btrfs_file_extent_num_bytes(leaf
, fi
);
1085 if (extent_end
<= start
) {
1089 if (disk_bytenr
== 0)
1091 if (btrfs_file_extent_compression(leaf
, fi
) ||
1092 btrfs_file_extent_encryption(leaf
, fi
) ||
1093 btrfs_file_extent_other_encoding(leaf
, fi
))
1095 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1097 if (btrfs_extent_readonly(root
, disk_bytenr
))
1099 if (btrfs_cross_ref_exist(trans
, root
, inode
->i_ino
,
1101 extent_offset
, disk_bytenr
))
1103 disk_bytenr
+= extent_offset
;
1104 disk_bytenr
+= cur_offset
- found_key
.offset
;
1105 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1107 * force cow if csum exists in the range.
1108 * this ensure that csum for a given extent are
1109 * either valid or do not exist.
1111 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1114 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1115 extent_end
= found_key
.offset
+
1116 btrfs_file_extent_inline_len(leaf
, fi
);
1117 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1122 if (extent_end
<= start
) {
1127 if (cow_start
== (u64
)-1)
1128 cow_start
= cur_offset
;
1129 cur_offset
= extent_end
;
1130 if (cur_offset
> end
)
1136 btrfs_release_path(root
, path
);
1137 if (cow_start
!= (u64
)-1) {
1138 ret
= cow_file_range(inode
, locked_page
, cow_start
,
1139 found_key
.offset
- 1, page_started
,
1142 cow_start
= (u64
)-1;
1145 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1146 struct extent_map
*em
;
1147 struct extent_map_tree
*em_tree
;
1148 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1149 em
= alloc_extent_map(GFP_NOFS
);
1150 em
->start
= cur_offset
;
1151 em
->orig_start
= em
->start
;
1152 em
->len
= num_bytes
;
1153 em
->block_len
= num_bytes
;
1154 em
->block_start
= disk_bytenr
;
1155 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1156 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1158 write_lock(&em_tree
->lock
);
1159 ret
= add_extent_mapping(em_tree
, em
);
1160 write_unlock(&em_tree
->lock
);
1161 if (ret
!= -EEXIST
) {
1162 free_extent_map(em
);
1165 btrfs_drop_extent_cache(inode
, em
->start
,
1166 em
->start
+ em
->len
- 1, 0);
1168 type
= BTRFS_ORDERED_PREALLOC
;
1170 type
= BTRFS_ORDERED_NOCOW
;
1173 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1174 num_bytes
, num_bytes
, type
);
1177 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
1178 cur_offset
, cur_offset
+ num_bytes
- 1,
1179 locked_page
, EXTENT_CLEAR_UNLOCK_PAGE
|
1180 EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
1181 EXTENT_SET_PRIVATE2
);
1182 cur_offset
= extent_end
;
1183 if (cur_offset
> end
)
1186 btrfs_release_path(root
, path
);
1188 if (cur_offset
<= end
&& cow_start
== (u64
)-1)
1189 cow_start
= cur_offset
;
1190 if (cow_start
!= (u64
)-1) {
1191 ret
= cow_file_range(inode
, locked_page
, cow_start
, end
,
1192 page_started
, nr_written
, 1);
1196 ret
= btrfs_end_transaction(trans
, root
);
1198 btrfs_free_path(path
);
1203 * extent_io.c call back to do delayed allocation processing
1205 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1206 u64 start
, u64 end
, int *page_started
,
1207 unsigned long *nr_written
)
1210 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1212 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
)
1213 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1214 page_started
, 1, nr_written
);
1215 else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
)
1216 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1217 page_started
, 0, nr_written
);
1218 else if (!btrfs_test_opt(root
, COMPRESS
) &&
1219 !(BTRFS_I(inode
)->force_compress
))
1220 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1221 page_started
, nr_written
, 1);
1223 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1224 page_started
, nr_written
);
1228 static int btrfs_split_extent_hook(struct inode
*inode
,
1229 struct extent_state
*orig
, u64 split
)
1231 if (!(orig
->state
& EXTENT_DELALLOC
))
1234 spin_lock(&BTRFS_I(inode
)->accounting_lock
);
1235 BTRFS_I(inode
)->outstanding_extents
++;
1236 spin_unlock(&BTRFS_I(inode
)->accounting_lock
);
1242 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1243 * extents so we can keep track of new extents that are just merged onto old
1244 * extents, such as when we are doing sequential writes, so we can properly
1245 * account for the metadata space we'll need.
1247 static int btrfs_merge_extent_hook(struct inode
*inode
,
1248 struct extent_state
*new,
1249 struct extent_state
*other
)
1251 /* not delalloc, ignore it */
1252 if (!(other
->state
& EXTENT_DELALLOC
))
1255 spin_lock(&BTRFS_I(inode
)->accounting_lock
);
1256 BTRFS_I(inode
)->outstanding_extents
--;
1257 spin_unlock(&BTRFS_I(inode
)->accounting_lock
);
1263 * extent_io.c set_bit_hook, used to track delayed allocation
1264 * bytes in this file, and to maintain the list of inodes that
1265 * have pending delalloc work to be done.
1267 static int btrfs_set_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
1268 unsigned long old
, unsigned long bits
)
1272 * set_bit and clear bit hooks normally require _irqsave/restore
1273 * but in this case, we are only testeing for the DELALLOC
1274 * bit, which is only set or cleared with irqs on
1276 if (!(old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
1277 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1279 spin_lock(&BTRFS_I(inode
)->accounting_lock
);
1280 BTRFS_I(inode
)->outstanding_extents
++;
1281 spin_unlock(&BTRFS_I(inode
)->accounting_lock
);
1282 btrfs_delalloc_reserve_space(root
, inode
, end
- start
+ 1);
1284 spin_lock(&root
->fs_info
->delalloc_lock
);
1285 BTRFS_I(inode
)->delalloc_bytes
+= end
- start
+ 1;
1286 root
->fs_info
->delalloc_bytes
+= end
- start
+ 1;
1287 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1288 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1289 &root
->fs_info
->delalloc_inodes
);
1291 spin_unlock(&root
->fs_info
->delalloc_lock
);
1297 * extent_io.c clear_bit_hook, see set_bit_hook for why
1299 static int btrfs_clear_bit_hook(struct inode
*inode
,
1300 struct extent_state
*state
, unsigned long bits
)
1303 * set_bit and clear bit hooks normally require _irqsave/restore
1304 * but in this case, we are only testeing for the DELALLOC
1305 * bit, which is only set or cleared with irqs on
1307 if ((state
->state
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
1308 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1310 if (bits
& EXTENT_DO_ACCOUNTING
) {
1311 spin_lock(&BTRFS_I(inode
)->accounting_lock
);
1312 WARN_ON(!BTRFS_I(inode
)->outstanding_extents
);
1313 BTRFS_I(inode
)->outstanding_extents
--;
1314 spin_unlock(&BTRFS_I(inode
)->accounting_lock
);
1315 btrfs_unreserve_metadata_for_delalloc(root
, inode
, 1);
1318 spin_lock(&root
->fs_info
->delalloc_lock
);
1319 if (state
->end
- state
->start
+ 1 >
1320 root
->fs_info
->delalloc_bytes
) {
1321 printk(KERN_INFO
"btrfs warning: delalloc account "
1323 (unsigned long long)
1324 state
->end
- state
->start
+ 1,
1325 (unsigned long long)
1326 root
->fs_info
->delalloc_bytes
);
1327 btrfs_delalloc_free_space(root
, inode
, (u64
)-1);
1328 root
->fs_info
->delalloc_bytes
= 0;
1329 BTRFS_I(inode
)->delalloc_bytes
= 0;
1331 btrfs_delalloc_free_space(root
, inode
,
1334 root
->fs_info
->delalloc_bytes
-= state
->end
-
1336 BTRFS_I(inode
)->delalloc_bytes
-= state
->end
-
1339 if (BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1340 !list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1341 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1343 spin_unlock(&root
->fs_info
->delalloc_lock
);
1349 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1350 * we don't create bios that span stripes or chunks
1352 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
1353 size_t size
, struct bio
*bio
,
1354 unsigned long bio_flags
)
1356 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1357 struct btrfs_mapping_tree
*map_tree
;
1358 u64 logical
= (u64
)bio
->bi_sector
<< 9;
1363 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1366 length
= bio
->bi_size
;
1367 map_tree
= &root
->fs_info
->mapping_tree
;
1368 map_length
= length
;
1369 ret
= btrfs_map_block(map_tree
, READ
, logical
,
1370 &map_length
, NULL
, 0);
1372 if (map_length
< length
+ size
)
1378 * in order to insert checksums into the metadata in large chunks,
1379 * we wait until bio submission time. All the pages in the bio are
1380 * checksummed and sums are attached onto the ordered extent record.
1382 * At IO completion time the cums attached on the ordered extent record
1383 * are inserted into the btree
1385 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1386 struct bio
*bio
, int mirror_num
,
1387 unsigned long bio_flags
)
1389 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1392 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1398 * in order to insert checksums into the metadata in large chunks,
1399 * we wait until bio submission time. All the pages in the bio are
1400 * checksummed and sums are attached onto the ordered extent record.
1402 * At IO completion time the cums attached on the ordered extent record
1403 * are inserted into the btree
1405 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1406 int mirror_num
, unsigned long bio_flags
)
1408 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1409 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1413 * extent_io.c submission hook. This does the right thing for csum calculation
1414 * on write, or reading the csums from the tree before a read
1416 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1417 int mirror_num
, unsigned long bio_flags
)
1419 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1423 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1425 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
1428 if (!(rw
& (1 << BIO_RW
))) {
1429 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1430 return btrfs_submit_compressed_read(inode
, bio
,
1431 mirror_num
, bio_flags
);
1432 } else if (!skip_sum
)
1433 btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1435 } else if (!skip_sum
) {
1436 /* csum items have already been cloned */
1437 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1439 /* we're doing a write, do the async checksumming */
1440 return btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1441 inode
, rw
, bio
, mirror_num
,
1442 bio_flags
, __btrfs_submit_bio_start
,
1443 __btrfs_submit_bio_done
);
1447 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1451 * given a list of ordered sums record them in the inode. This happens
1452 * at IO completion time based on sums calculated at bio submission time.
1454 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1455 struct inode
*inode
, u64 file_offset
,
1456 struct list_head
*list
)
1458 struct btrfs_ordered_sum
*sum
;
1460 btrfs_set_trans_block_group(trans
, inode
);
1462 list_for_each_entry(sum
, list
, list
) {
1463 btrfs_csum_file_blocks(trans
,
1464 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1469 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1470 struct extent_state
**cached_state
)
1472 if ((end
& (PAGE_CACHE_SIZE
- 1)) == 0)
1474 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1475 cached_state
, GFP_NOFS
);
1478 /* see btrfs_writepage_start_hook for details on why this is required */
1479 struct btrfs_writepage_fixup
{
1481 struct btrfs_work work
;
1484 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1486 struct btrfs_writepage_fixup
*fixup
;
1487 struct btrfs_ordered_extent
*ordered
;
1488 struct extent_state
*cached_state
= NULL
;
1490 struct inode
*inode
;
1494 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1498 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1499 ClearPageChecked(page
);
1503 inode
= page
->mapping
->host
;
1504 page_start
= page_offset(page
);
1505 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1507 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1508 &cached_state
, GFP_NOFS
);
1510 /* already ordered? We're done */
1511 if (PagePrivate2(page
))
1514 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1516 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1517 page_end
, &cached_state
, GFP_NOFS
);
1519 btrfs_start_ordered_extent(inode
, ordered
, 1);
1523 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1524 ClearPageChecked(page
);
1526 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1527 &cached_state
, GFP_NOFS
);
1530 page_cache_release(page
);
1534 * There are a few paths in the higher layers of the kernel that directly
1535 * set the page dirty bit without asking the filesystem if it is a
1536 * good idea. This causes problems because we want to make sure COW
1537 * properly happens and the data=ordered rules are followed.
1539 * In our case any range that doesn't have the ORDERED bit set
1540 * hasn't been properly setup for IO. We kick off an async process
1541 * to fix it up. The async helper will wait for ordered extents, set
1542 * the delalloc bit and make it safe to write the page.
1544 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1546 struct inode
*inode
= page
->mapping
->host
;
1547 struct btrfs_writepage_fixup
*fixup
;
1548 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1550 /* this page is properly in the ordered list */
1551 if (TestClearPagePrivate2(page
))
1554 if (PageChecked(page
))
1557 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1561 SetPageChecked(page
);
1562 page_cache_get(page
);
1563 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
1565 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
1569 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1570 struct inode
*inode
, u64 file_pos
,
1571 u64 disk_bytenr
, u64 disk_num_bytes
,
1572 u64 num_bytes
, u64 ram_bytes
,
1573 u8 compression
, u8 encryption
,
1574 u16 other_encoding
, int extent_type
)
1576 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1577 struct btrfs_file_extent_item
*fi
;
1578 struct btrfs_path
*path
;
1579 struct extent_buffer
*leaf
;
1580 struct btrfs_key ins
;
1584 path
= btrfs_alloc_path();
1587 path
->leave_spinning
= 1;
1590 * we may be replacing one extent in the tree with another.
1591 * The new extent is pinned in the extent map, and we don't want
1592 * to drop it from the cache until it is completely in the btree.
1594 * So, tell btrfs_drop_extents to leave this extent in the cache.
1595 * the caller is expected to unpin it and allow it to be merged
1598 ret
= btrfs_drop_extents(trans
, inode
, file_pos
, file_pos
+ num_bytes
,
1602 ins
.objectid
= inode
->i_ino
;
1603 ins
.offset
= file_pos
;
1604 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1605 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
, sizeof(*fi
));
1607 leaf
= path
->nodes
[0];
1608 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1609 struct btrfs_file_extent_item
);
1610 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1611 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1612 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1613 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1614 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1615 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1616 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1617 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1618 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1619 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1621 btrfs_unlock_up_safe(path
, 1);
1622 btrfs_set_lock_blocking(leaf
);
1624 btrfs_mark_buffer_dirty(leaf
);
1626 inode_add_bytes(inode
, num_bytes
);
1628 ins
.objectid
= disk_bytenr
;
1629 ins
.offset
= disk_num_bytes
;
1630 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1631 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
1632 root
->root_key
.objectid
,
1633 inode
->i_ino
, file_pos
, &ins
);
1635 btrfs_free_path(path
);
1641 * helper function for btrfs_finish_ordered_io, this
1642 * just reads in some of the csum leaves to prime them into ram
1643 * before we start the transaction. It limits the amount of btree
1644 * reads required while inside the transaction.
1646 /* as ordered data IO finishes, this gets called so we can finish
1647 * an ordered extent if the range of bytes in the file it covers are
1650 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
)
1652 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1653 struct btrfs_trans_handle
*trans
;
1654 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
1655 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1656 struct extent_state
*cached_state
= NULL
;
1660 ret
= btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
1664 BUG_ON(!ordered_extent
);
1666 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
1667 BUG_ON(!list_empty(&ordered_extent
->list
));
1668 ret
= btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
1670 trans
= btrfs_join_transaction(root
, 1);
1671 ret
= btrfs_update_inode(trans
, root
, inode
);
1673 btrfs_end_transaction(trans
, root
);
1678 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
1679 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
1680 0, &cached_state
, GFP_NOFS
);
1682 trans
= btrfs_join_transaction(root
, 1);
1684 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
1686 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
1688 ret
= btrfs_mark_extent_written(trans
, inode
,
1689 ordered_extent
->file_offset
,
1690 ordered_extent
->file_offset
+
1691 ordered_extent
->len
);
1694 ret
= insert_reserved_file_extent(trans
, inode
,
1695 ordered_extent
->file_offset
,
1696 ordered_extent
->start
,
1697 ordered_extent
->disk_len
,
1698 ordered_extent
->len
,
1699 ordered_extent
->len
,
1701 BTRFS_FILE_EXTENT_REG
);
1702 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
1703 ordered_extent
->file_offset
,
1704 ordered_extent
->len
);
1707 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
1708 ordered_extent
->file_offset
+
1709 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
1711 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
1712 &ordered_extent
->list
);
1714 /* this also removes the ordered extent from the tree */
1715 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
1716 ret
= btrfs_update_inode(trans
, root
, inode
);
1718 btrfs_end_transaction(trans
, root
);
1721 btrfs_put_ordered_extent(ordered_extent
);
1722 /* once for the tree */
1723 btrfs_put_ordered_extent(ordered_extent
);
1728 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
1729 struct extent_state
*state
, int uptodate
)
1731 ClearPagePrivate2(page
);
1732 return btrfs_finish_ordered_io(page
->mapping
->host
, start
, end
);
1736 * When IO fails, either with EIO or csum verification fails, we
1737 * try other mirrors that might have a good copy of the data. This
1738 * io_failure_record is used to record state as we go through all the
1739 * mirrors. If another mirror has good data, the page is set up to date
1740 * and things continue. If a good mirror can't be found, the original
1741 * bio end_io callback is called to indicate things have failed.
1743 struct io_failure_record
{
1748 unsigned long bio_flags
;
1752 static int btrfs_io_failed_hook(struct bio
*failed_bio
,
1753 struct page
*page
, u64 start
, u64 end
,
1754 struct extent_state
*state
)
1756 struct io_failure_record
*failrec
= NULL
;
1758 struct extent_map
*em
;
1759 struct inode
*inode
= page
->mapping
->host
;
1760 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
1761 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1768 ret
= get_state_private(failure_tree
, start
, &private);
1770 failrec
= kmalloc(sizeof(*failrec
), GFP_NOFS
);
1773 failrec
->start
= start
;
1774 failrec
->len
= end
- start
+ 1;
1775 failrec
->last_mirror
= 0;
1776 failrec
->bio_flags
= 0;
1778 read_lock(&em_tree
->lock
);
1779 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
1780 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
1781 free_extent_map(em
);
1784 read_unlock(&em_tree
->lock
);
1786 if (!em
|| IS_ERR(em
)) {
1790 logical
= start
- em
->start
;
1791 logical
= em
->block_start
+ logical
;
1792 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
1793 logical
= em
->block_start
;
1794 failrec
->bio_flags
= EXTENT_BIO_COMPRESSED
;
1796 failrec
->logical
= logical
;
1797 free_extent_map(em
);
1798 set_extent_bits(failure_tree
, start
, end
, EXTENT_LOCKED
|
1799 EXTENT_DIRTY
, GFP_NOFS
);
1800 set_state_private(failure_tree
, start
,
1801 (u64
)(unsigned long)failrec
);
1803 failrec
= (struct io_failure_record
*)(unsigned long)private;
1805 num_copies
= btrfs_num_copies(
1806 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
1807 failrec
->logical
, failrec
->len
);
1808 failrec
->last_mirror
++;
1810 spin_lock(&BTRFS_I(inode
)->io_tree
.lock
);
1811 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
1814 if (state
&& state
->start
!= failrec
->start
)
1816 spin_unlock(&BTRFS_I(inode
)->io_tree
.lock
);
1818 if (!state
|| failrec
->last_mirror
> num_copies
) {
1819 set_state_private(failure_tree
, failrec
->start
, 0);
1820 clear_extent_bits(failure_tree
, failrec
->start
,
1821 failrec
->start
+ failrec
->len
- 1,
1822 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
1826 bio
= bio_alloc(GFP_NOFS
, 1);
1827 bio
->bi_private
= state
;
1828 bio
->bi_end_io
= failed_bio
->bi_end_io
;
1829 bio
->bi_sector
= failrec
->logical
>> 9;
1830 bio
->bi_bdev
= failed_bio
->bi_bdev
;
1833 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
1834 if (failed_bio
->bi_rw
& (1 << BIO_RW
))
1839 BTRFS_I(inode
)->io_tree
.ops
->submit_bio_hook(inode
, rw
, bio
,
1840 failrec
->last_mirror
,
1841 failrec
->bio_flags
);
1846 * each time an IO finishes, we do a fast check in the IO failure tree
1847 * to see if we need to process or clean up an io_failure_record
1849 static int btrfs_clean_io_failures(struct inode
*inode
, u64 start
)
1852 u64 private_failure
;
1853 struct io_failure_record
*failure
;
1857 if (count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
1858 (u64
)-1, 1, EXTENT_DIRTY
)) {
1859 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
,
1860 start
, &private_failure
);
1862 failure
= (struct io_failure_record
*)(unsigned long)
1864 set_state_private(&BTRFS_I(inode
)->io_failure_tree
,
1866 clear_extent_bits(&BTRFS_I(inode
)->io_failure_tree
,
1868 failure
->start
+ failure
->len
- 1,
1869 EXTENT_DIRTY
| EXTENT_LOCKED
,
1878 * when reads are done, we need to check csums to verify the data is correct
1879 * if there's a match, we allow the bio to finish. If not, we go through
1880 * the io_failure_record routines to find good copies
1882 static int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
1883 struct extent_state
*state
)
1885 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
1886 struct inode
*inode
= page
->mapping
->host
;
1887 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1889 u64
private = ~(u32
)0;
1891 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1894 if (PageChecked(page
)) {
1895 ClearPageChecked(page
);
1899 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
1902 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
1903 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
1904 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
1909 if (state
&& state
->start
== start
) {
1910 private = state
->private;
1913 ret
= get_state_private(io_tree
, start
, &private);
1915 kaddr
= kmap_atomic(page
, KM_USER0
);
1919 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
1920 btrfs_csum_final(csum
, (char *)&csum
);
1921 if (csum
!= private)
1924 kunmap_atomic(kaddr
, KM_USER0
);
1926 /* if the io failure tree for this inode is non-empty,
1927 * check to see if we've recovered from a failed IO
1929 btrfs_clean_io_failures(inode
, start
);
1933 if (printk_ratelimit()) {
1934 printk(KERN_INFO
"btrfs csum failed ino %lu off %llu csum %u "
1935 "private %llu\n", page
->mapping
->host
->i_ino
,
1936 (unsigned long long)start
, csum
,
1937 (unsigned long long)private);
1939 memset(kaddr
+ offset
, 1, end
- start
+ 1);
1940 flush_dcache_page(page
);
1941 kunmap_atomic(kaddr
, KM_USER0
);
1947 struct delayed_iput
{
1948 struct list_head list
;
1949 struct inode
*inode
;
1952 void btrfs_add_delayed_iput(struct inode
*inode
)
1954 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
1955 struct delayed_iput
*delayed
;
1957 if (atomic_add_unless(&inode
->i_count
, -1, 1))
1960 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
1961 delayed
->inode
= inode
;
1963 spin_lock(&fs_info
->delayed_iput_lock
);
1964 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
1965 spin_unlock(&fs_info
->delayed_iput_lock
);
1968 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
1971 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1972 struct delayed_iput
*delayed
;
1975 spin_lock(&fs_info
->delayed_iput_lock
);
1976 empty
= list_empty(&fs_info
->delayed_iputs
);
1977 spin_unlock(&fs_info
->delayed_iput_lock
);
1981 down_read(&root
->fs_info
->cleanup_work_sem
);
1982 spin_lock(&fs_info
->delayed_iput_lock
);
1983 list_splice_init(&fs_info
->delayed_iputs
, &list
);
1984 spin_unlock(&fs_info
->delayed_iput_lock
);
1986 while (!list_empty(&list
)) {
1987 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
1988 list_del(&delayed
->list
);
1989 iput(delayed
->inode
);
1992 up_read(&root
->fs_info
->cleanup_work_sem
);
1996 * This creates an orphan entry for the given inode in case something goes
1997 * wrong in the middle of an unlink/truncate.
1999 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2001 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2004 spin_lock(&root
->list_lock
);
2006 /* already on the orphan list, we're good */
2007 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
2008 spin_unlock(&root
->list_lock
);
2012 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
2014 spin_unlock(&root
->list_lock
);
2017 * insert an orphan item to track this unlinked/truncated file
2019 ret
= btrfs_insert_orphan_item(trans
, root
, inode
->i_ino
);
2025 * We have done the truncate/delete so we can go ahead and remove the orphan
2026 * item for this particular inode.
2028 int btrfs_orphan_del(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2030 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2033 spin_lock(&root
->list_lock
);
2035 if (list_empty(&BTRFS_I(inode
)->i_orphan
)) {
2036 spin_unlock(&root
->list_lock
);
2040 list_del_init(&BTRFS_I(inode
)->i_orphan
);
2042 spin_unlock(&root
->list_lock
);
2046 spin_unlock(&root
->list_lock
);
2048 ret
= btrfs_del_orphan_item(trans
, root
, inode
->i_ino
);
2054 * this cleans up any orphans that may be left on the list from the last use
2057 void btrfs_orphan_cleanup(struct btrfs_root
*root
)
2059 struct btrfs_path
*path
;
2060 struct extent_buffer
*leaf
;
2061 struct btrfs_item
*item
;
2062 struct btrfs_key key
, found_key
;
2063 struct btrfs_trans_handle
*trans
;
2064 struct inode
*inode
;
2065 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
2067 if (!xchg(&root
->clean_orphans
, 0))
2070 path
= btrfs_alloc_path();
2074 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
2075 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
2076 key
.offset
= (u64
)-1;
2079 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2081 printk(KERN_ERR
"Error searching slot for orphan: %d"
2087 * if ret == 0 means we found what we were searching for, which
2088 * is weird, but possible, so only screw with path if we didnt
2089 * find the key and see if we have stuff that matches
2092 if (path
->slots
[0] == 0)
2097 /* pull out the item */
2098 leaf
= path
->nodes
[0];
2099 item
= btrfs_item_nr(leaf
, path
->slots
[0]);
2100 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2102 /* make sure the item matches what we want */
2103 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
2105 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
2108 /* release the path since we're done with it */
2109 btrfs_release_path(root
, path
);
2112 * this is where we are basically btrfs_lookup, without the
2113 * crossing root thing. we store the inode number in the
2114 * offset of the orphan item.
2116 found_key
.objectid
= found_key
.offset
;
2117 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
2118 found_key
.offset
= 0;
2119 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
2124 * add this inode to the orphan list so btrfs_orphan_del does
2125 * the proper thing when we hit it
2127 spin_lock(&root
->list_lock
);
2128 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
2129 spin_unlock(&root
->list_lock
);
2132 * if this is a bad inode, means we actually succeeded in
2133 * removing the inode, but not the orphan record, which means
2134 * we need to manually delete the orphan since iput will just
2135 * do a destroy_inode
2137 if (is_bad_inode(inode
)) {
2138 trans
= btrfs_start_transaction(root
, 0);
2139 btrfs_orphan_del(trans
, inode
);
2140 btrfs_end_transaction(trans
, root
);
2145 /* if we have links, this was a truncate, lets do that */
2146 if (inode
->i_nlink
) {
2148 btrfs_truncate(inode
);
2153 /* this will do delete_inode and everything for us */
2158 printk(KERN_INFO
"btrfs: unlinked %d orphans\n", nr_unlink
);
2160 printk(KERN_INFO
"btrfs: truncated %d orphans\n", nr_truncate
);
2162 btrfs_free_path(path
);
2166 * very simple check to peek ahead in the leaf looking for xattrs. If we
2167 * don't find any xattrs, we know there can't be any acls.
2169 * slot is the slot the inode is in, objectid is the objectid of the inode
2171 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
2172 int slot
, u64 objectid
)
2174 u32 nritems
= btrfs_header_nritems(leaf
);
2175 struct btrfs_key found_key
;
2179 while (slot
< nritems
) {
2180 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2182 /* we found a different objectid, there must not be acls */
2183 if (found_key
.objectid
!= objectid
)
2186 /* we found an xattr, assume we've got an acl */
2187 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
)
2191 * we found a key greater than an xattr key, there can't
2192 * be any acls later on
2194 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
2201 * it goes inode, inode backrefs, xattrs, extents,
2202 * so if there are a ton of hard links to an inode there can
2203 * be a lot of backrefs. Don't waste time searching too hard,
2204 * this is just an optimization
2209 /* we hit the end of the leaf before we found an xattr or
2210 * something larger than an xattr. We have to assume the inode
2217 * read an inode from the btree into the in-memory inode
2219 static void btrfs_read_locked_inode(struct inode
*inode
)
2221 struct btrfs_path
*path
;
2222 struct extent_buffer
*leaf
;
2223 struct btrfs_inode_item
*inode_item
;
2224 struct btrfs_timespec
*tspec
;
2225 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2226 struct btrfs_key location
;
2228 u64 alloc_group_block
;
2232 path
= btrfs_alloc_path();
2234 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
2236 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
2240 leaf
= path
->nodes
[0];
2241 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2242 struct btrfs_inode_item
);
2244 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
2245 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
2246 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
2247 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
2248 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
2250 tspec
= btrfs_inode_atime(inode_item
);
2251 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2252 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2254 tspec
= btrfs_inode_mtime(inode_item
);
2255 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2256 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2258 tspec
= btrfs_inode_ctime(inode_item
);
2259 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2260 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2262 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
2263 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
2264 BTRFS_I(inode
)->sequence
= btrfs_inode_sequence(leaf
, inode_item
);
2265 inode
->i_generation
= BTRFS_I(inode
)->generation
;
2267 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
2269 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
2270 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
2272 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
2275 * try to precache a NULL acl entry for files that don't have
2276 * any xattrs or acls
2278 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0], inode
->i_ino
);
2280 cache_no_acl(inode
);
2282 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
, 0,
2283 alloc_group_block
, 0);
2284 btrfs_free_path(path
);
2287 switch (inode
->i_mode
& S_IFMT
) {
2289 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2290 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2291 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2292 inode
->i_fop
= &btrfs_file_operations
;
2293 inode
->i_op
= &btrfs_file_inode_operations
;
2296 inode
->i_fop
= &btrfs_dir_file_operations
;
2297 if (root
== root
->fs_info
->tree_root
)
2298 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
2300 inode
->i_op
= &btrfs_dir_inode_operations
;
2303 inode
->i_op
= &btrfs_symlink_inode_operations
;
2304 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2305 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2308 inode
->i_op
= &btrfs_special_inode_operations
;
2309 init_special_inode(inode
, inode
->i_mode
, rdev
);
2313 btrfs_update_iflags(inode
);
2317 btrfs_free_path(path
);
2318 make_bad_inode(inode
);
2322 * given a leaf and an inode, copy the inode fields into the leaf
2324 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
2325 struct extent_buffer
*leaf
,
2326 struct btrfs_inode_item
*item
,
2327 struct inode
*inode
)
2329 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
2330 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
2331 btrfs_set_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
);
2332 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
2333 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
2335 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
2336 inode
->i_atime
.tv_sec
);
2337 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
2338 inode
->i_atime
.tv_nsec
);
2340 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
2341 inode
->i_mtime
.tv_sec
);
2342 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
2343 inode
->i_mtime
.tv_nsec
);
2345 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
2346 inode
->i_ctime
.tv_sec
);
2347 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
2348 inode
->i_ctime
.tv_nsec
);
2350 btrfs_set_inode_nbytes(leaf
, item
, inode_get_bytes(inode
));
2351 btrfs_set_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
);
2352 btrfs_set_inode_sequence(leaf
, item
, BTRFS_I(inode
)->sequence
);
2353 btrfs_set_inode_transid(leaf
, item
, trans
->transid
);
2354 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
2355 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
2356 btrfs_set_inode_block_group(leaf
, item
, BTRFS_I(inode
)->block_group
);
2360 * copy everything in the in-memory inode into the btree.
2362 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
2363 struct btrfs_root
*root
, struct inode
*inode
)
2365 struct btrfs_inode_item
*inode_item
;
2366 struct btrfs_path
*path
;
2367 struct extent_buffer
*leaf
;
2370 path
= btrfs_alloc_path();
2372 path
->leave_spinning
= 1;
2373 ret
= btrfs_lookup_inode(trans
, root
, path
,
2374 &BTRFS_I(inode
)->location
, 1);
2381 btrfs_unlock_up_safe(path
, 1);
2382 leaf
= path
->nodes
[0];
2383 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2384 struct btrfs_inode_item
);
2386 fill_inode_item(trans
, leaf
, inode_item
, inode
);
2387 btrfs_mark_buffer_dirty(leaf
);
2388 btrfs_set_inode_last_trans(trans
, inode
);
2391 btrfs_free_path(path
);
2397 * unlink helper that gets used here in inode.c and in the tree logging
2398 * recovery code. It remove a link in a directory with a given name, and
2399 * also drops the back refs in the inode to the directory
2401 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
2402 struct btrfs_root
*root
,
2403 struct inode
*dir
, struct inode
*inode
,
2404 const char *name
, int name_len
)
2406 struct btrfs_path
*path
;
2408 struct extent_buffer
*leaf
;
2409 struct btrfs_dir_item
*di
;
2410 struct btrfs_key key
;
2413 path
= btrfs_alloc_path();
2419 path
->leave_spinning
= 1;
2420 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
2421 name
, name_len
, -1);
2430 leaf
= path
->nodes
[0];
2431 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2432 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2435 btrfs_release_path(root
, path
);
2437 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
2439 dir
->i_ino
, &index
);
2441 printk(KERN_INFO
"btrfs failed to delete reference to %.*s, "
2442 "inode %lu parent %lu\n", name_len
, name
,
2443 inode
->i_ino
, dir
->i_ino
);
2447 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
2448 index
, name
, name_len
, -1);
2457 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2458 btrfs_release_path(root
, path
);
2460 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
2462 BUG_ON(ret
!= 0 && ret
!= -ENOENT
);
2464 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
2468 btrfs_free_path(path
);
2472 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
2473 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
2474 btrfs_update_inode(trans
, root
, dir
);
2475 btrfs_drop_nlink(inode
);
2476 ret
= btrfs_update_inode(trans
, root
, inode
);
2481 /* helper to check if there is any shared block in the path */
2482 static int check_path_shared(struct btrfs_root
*root
,
2483 struct btrfs_path
*path
)
2485 struct extent_buffer
*eb
;
2490 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
2491 if (!path
->nodes
[level
])
2493 eb
= path
->nodes
[level
];
2494 if (!btrfs_block_can_be_shared(root
, eb
))
2496 ret
= btrfs_lookup_extent_info(NULL
, root
, eb
->start
, eb
->len
,
2505 * helper to start transaction for unlink and rmdir.
2507 * unlink and rmdir are special in btrfs, they do not always free space.
2508 * so in enospc case, we should make sure they will free space before
2509 * allowing them to use the global metadata reservation.
2511 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
,
2512 struct dentry
*dentry
)
2514 struct btrfs_trans_handle
*trans
;
2515 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2516 struct btrfs_path
*path
;
2517 struct btrfs_inode_ref
*ref
;
2518 struct btrfs_dir_item
*di
;
2519 struct inode
*inode
= dentry
->d_inode
;
2525 trans
= btrfs_start_transaction(root
, 10);
2526 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
2529 if (inode
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
2530 return ERR_PTR(-ENOSPC
);
2532 /* check if there is someone else holds reference */
2533 if (S_ISDIR(inode
->i_mode
) && atomic_read(&inode
->i_count
) > 1)
2534 return ERR_PTR(-ENOSPC
);
2536 if (atomic_read(&inode
->i_count
) > 2)
2537 return ERR_PTR(-ENOSPC
);
2539 if (xchg(&root
->fs_info
->enospc_unlink
, 1))
2540 return ERR_PTR(-ENOSPC
);
2542 path
= btrfs_alloc_path();
2544 root
->fs_info
->enospc_unlink
= 0;
2545 return ERR_PTR(-ENOMEM
);
2548 trans
= btrfs_start_transaction(root
, 0);
2549 if (IS_ERR(trans
)) {
2550 btrfs_free_path(path
);
2551 root
->fs_info
->enospc_unlink
= 0;
2555 path
->skip_locking
= 1;
2556 path
->search_commit_root
= 1;
2558 ret
= btrfs_lookup_inode(trans
, root
, path
,
2559 &BTRFS_I(dir
)->location
, 0);
2565 if (check_path_shared(root
, path
))
2570 btrfs_release_path(root
, path
);
2572 ret
= btrfs_lookup_inode(trans
, root
, path
,
2573 &BTRFS_I(inode
)->location
, 0);
2579 if (check_path_shared(root
, path
))
2584 btrfs_release_path(root
, path
);
2586 if (ret
== 0 && S_ISREG(inode
->i_mode
)) {
2587 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
2588 inode
->i_ino
, (u64
)-1, 0);
2594 if (check_path_shared(root
, path
))
2596 btrfs_release_path(root
, path
);
2604 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
2605 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
2611 if (check_path_shared(root
, path
))
2617 btrfs_release_path(root
, path
);
2619 ref
= btrfs_lookup_inode_ref(trans
, root
, path
,
2620 dentry
->d_name
.name
, dentry
->d_name
.len
,
2621 inode
->i_ino
, dir
->i_ino
, 0);
2627 if (check_path_shared(root
, path
))
2629 index
= btrfs_inode_ref_index(path
->nodes
[0], ref
);
2630 btrfs_release_path(root
, path
);
2632 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
, index
,
2633 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
2638 BUG_ON(ret
== -ENOENT
);
2639 if (check_path_shared(root
, path
))
2644 btrfs_free_path(path
);
2646 btrfs_end_transaction(trans
, root
);
2647 root
->fs_info
->enospc_unlink
= 0;
2648 return ERR_PTR(err
);
2651 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
2655 static void __unlink_end_trans(struct btrfs_trans_handle
*trans
,
2656 struct btrfs_root
*root
)
2658 if (trans
->block_rsv
== &root
->fs_info
->global_block_rsv
) {
2659 BUG_ON(!root
->fs_info
->enospc_unlink
);
2660 root
->fs_info
->enospc_unlink
= 0;
2662 btrfs_end_transaction_throttle(trans
, root
);
2665 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
2667 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2668 struct btrfs_trans_handle
*trans
;
2669 struct inode
*inode
= dentry
->d_inode
;
2671 unsigned long nr
= 0;
2673 trans
= __unlink_start_trans(dir
, dentry
);
2675 return PTR_ERR(trans
);
2677 btrfs_set_trans_block_group(trans
, dir
);
2679 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
2681 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
2682 dentry
->d_name
.name
, dentry
->d_name
.len
);
2685 if (inode
->i_nlink
== 0) {
2686 ret
= btrfs_orphan_add(trans
, inode
);
2690 nr
= trans
->blocks_used
;
2691 __unlink_end_trans(trans
, root
);
2692 btrfs_btree_balance_dirty(root
, nr
);
2696 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
2697 struct btrfs_root
*root
,
2698 struct inode
*dir
, u64 objectid
,
2699 const char *name
, int name_len
)
2701 struct btrfs_path
*path
;
2702 struct extent_buffer
*leaf
;
2703 struct btrfs_dir_item
*di
;
2704 struct btrfs_key key
;
2708 path
= btrfs_alloc_path();
2712 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
2713 name
, name_len
, -1);
2714 BUG_ON(!di
|| IS_ERR(di
));
2716 leaf
= path
->nodes
[0];
2717 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2718 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
2719 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2721 btrfs_release_path(root
, path
);
2723 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
2724 objectid
, root
->root_key
.objectid
,
2725 dir
->i_ino
, &index
, name
, name_len
);
2727 BUG_ON(ret
!= -ENOENT
);
2728 di
= btrfs_search_dir_index_item(root
, path
, dir
->i_ino
,
2730 BUG_ON(!di
|| IS_ERR(di
));
2732 leaf
= path
->nodes
[0];
2733 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2734 btrfs_release_path(root
, path
);
2738 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
2739 index
, name
, name_len
, -1);
2740 BUG_ON(!di
|| IS_ERR(di
));
2742 leaf
= path
->nodes
[0];
2743 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2744 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
2745 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2747 btrfs_release_path(root
, path
);
2749 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
2750 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
2751 ret
= btrfs_update_inode(trans
, root
, dir
);
2753 dir
->i_sb
->s_dirt
= 1;
2755 btrfs_free_path(path
);
2759 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2761 struct inode
*inode
= dentry
->d_inode
;
2763 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2764 struct btrfs_trans_handle
*trans
;
2765 unsigned long nr
= 0;
2767 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
||
2768 inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)
2771 trans
= __unlink_start_trans(dir
, dentry
);
2773 return PTR_ERR(trans
);
2775 btrfs_set_trans_block_group(trans
, dir
);
2777 if (unlikely(inode
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
2778 err
= btrfs_unlink_subvol(trans
, root
, dir
,
2779 BTRFS_I(inode
)->location
.objectid
,
2780 dentry
->d_name
.name
,
2781 dentry
->d_name
.len
);
2785 err
= btrfs_orphan_add(trans
, inode
);
2789 /* now the directory is empty */
2790 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
2791 dentry
->d_name
.name
, dentry
->d_name
.len
);
2793 btrfs_i_size_write(inode
, 0);
2795 nr
= trans
->blocks_used
;
2796 __unlink_end_trans(trans
, root
);
2797 btrfs_btree_balance_dirty(root
, nr
);
2804 * when truncating bytes in a file, it is possible to avoid reading
2805 * the leaves that contain only checksum items. This can be the
2806 * majority of the IO required to delete a large file, but it must
2807 * be done carefully.
2809 * The keys in the level just above the leaves are checked to make sure
2810 * the lowest key in a given leaf is a csum key, and starts at an offset
2811 * after the new size.
2813 * Then the key for the next leaf is checked to make sure it also has
2814 * a checksum item for the same file. If it does, we know our target leaf
2815 * contains only checksum items, and it can be safely freed without reading
2818 * This is just an optimization targeted at large files. It may do
2819 * nothing. It will return 0 unless things went badly.
2821 static noinline
int drop_csum_leaves(struct btrfs_trans_handle
*trans
,
2822 struct btrfs_root
*root
,
2823 struct btrfs_path
*path
,
2824 struct inode
*inode
, u64 new_size
)
2826 struct btrfs_key key
;
2829 struct btrfs_key found_key
;
2830 struct btrfs_key other_key
;
2831 struct btrfs_leaf_ref
*ref
;
2835 path
->lowest_level
= 1;
2836 key
.objectid
= inode
->i_ino
;
2837 key
.type
= BTRFS_CSUM_ITEM_KEY
;
2838 key
.offset
= new_size
;
2840 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2844 if (path
->nodes
[1] == NULL
) {
2849 btrfs_node_key_to_cpu(path
->nodes
[1], &found_key
, path
->slots
[1]);
2850 nritems
= btrfs_header_nritems(path
->nodes
[1]);
2855 if (path
->slots
[1] >= nritems
)
2858 /* did we find a key greater than anything we want to delete? */
2859 if (found_key
.objectid
> inode
->i_ino
||
2860 (found_key
.objectid
== inode
->i_ino
&& found_key
.type
> key
.type
))
2863 /* we check the next key in the node to make sure the leave contains
2864 * only checksum items. This comparison doesn't work if our
2865 * leaf is the last one in the node
2867 if (path
->slots
[1] + 1 >= nritems
) {
2869 /* search forward from the last key in the node, this
2870 * will bring us into the next node in the tree
2872 btrfs_node_key_to_cpu(path
->nodes
[1], &found_key
, nritems
- 1);
2874 /* unlikely, but we inc below, so check to be safe */
2875 if (found_key
.offset
== (u64
)-1)
2878 /* search_forward needs a path with locks held, do the
2879 * search again for the original key. It is possible
2880 * this will race with a balance and return a path that
2881 * we could modify, but this drop is just an optimization
2882 * and is allowed to miss some leaves.
2884 btrfs_release_path(root
, path
);
2887 /* setup a max key for search_forward */
2888 other_key
.offset
= (u64
)-1;
2889 other_key
.type
= key
.type
;
2890 other_key
.objectid
= key
.objectid
;
2892 path
->keep_locks
= 1;
2893 ret
= btrfs_search_forward(root
, &found_key
, &other_key
,
2895 path
->keep_locks
= 0;
2896 if (ret
|| found_key
.objectid
!= key
.objectid
||
2897 found_key
.type
!= key
.type
) {
2902 key
.offset
= found_key
.offset
;
2903 btrfs_release_path(root
, path
);
2908 /* we know there's one more slot after us in the tree,
2909 * read that key so we can verify it is also a checksum item
2911 btrfs_node_key_to_cpu(path
->nodes
[1], &other_key
, path
->slots
[1] + 1);
2913 if (found_key
.objectid
< inode
->i_ino
)
2916 if (found_key
.type
!= key
.type
|| found_key
.offset
< new_size
)
2920 * if the key for the next leaf isn't a csum key from this objectid,
2921 * we can't be sure there aren't good items inside this leaf.
2924 if (other_key
.objectid
!= inode
->i_ino
|| other_key
.type
!= key
.type
)
2927 leaf_start
= btrfs_node_blockptr(path
->nodes
[1], path
->slots
[1]);
2928 leaf_gen
= btrfs_node_ptr_generation(path
->nodes
[1], path
->slots
[1]);
2930 * it is safe to delete this leaf, it contains only
2931 * csum items from this inode at an offset >= new_size
2933 ret
= btrfs_del_leaf(trans
, root
, path
, leaf_start
);
2936 if (root
->ref_cows
&& leaf_gen
< trans
->transid
) {
2937 ref
= btrfs_alloc_leaf_ref(root
, 0);
2939 ref
->root_gen
= root
->root_key
.offset
;
2940 ref
->bytenr
= leaf_start
;
2942 ref
->generation
= leaf_gen
;
2945 btrfs_sort_leaf_ref(ref
);
2947 ret
= btrfs_add_leaf_ref(root
, ref
, 0);
2949 btrfs_free_leaf_ref(root
, ref
);
2955 btrfs_release_path(root
, path
);
2957 if (other_key
.objectid
== inode
->i_ino
&&
2958 other_key
.type
== key
.type
&& other_key
.offset
> key
.offset
) {
2959 key
.offset
= other_key
.offset
;
2965 /* fixup any changes we've made to the path */
2966 path
->lowest_level
= 0;
2967 path
->keep_locks
= 0;
2968 btrfs_release_path(root
, path
);
2975 * this can truncate away extent items, csum items and directory items.
2976 * It starts at a high offset and removes keys until it can't find
2977 * any higher than new_size
2979 * csum items that cross the new i_size are truncated to the new size
2982 * min_type is the minimum key type to truncate down to. If set to 0, this
2983 * will kill all the items on this inode, including the INODE_ITEM_KEY.
2985 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
2986 struct btrfs_root
*root
,
2987 struct inode
*inode
,
2988 u64 new_size
, u32 min_type
)
2990 struct btrfs_path
*path
;
2991 struct extent_buffer
*leaf
;
2992 struct btrfs_file_extent_item
*fi
;
2993 struct btrfs_key key
;
2994 struct btrfs_key found_key
;
2995 u64 extent_start
= 0;
2996 u64 extent_num_bytes
= 0;
2997 u64 extent_offset
= 0;
2999 u64 mask
= root
->sectorsize
- 1;
3000 u32 found_type
= (u8
)-1;
3003 int pending_del_nr
= 0;
3004 int pending_del_slot
= 0;
3005 int extent_type
= -1;
3010 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
3013 btrfs_drop_extent_cache(inode
, new_size
& (~mask
), (u64
)-1, 0);
3015 path
= btrfs_alloc_path();
3019 key
.objectid
= inode
->i_ino
;
3020 key
.offset
= (u64
)-1;
3024 path
->leave_spinning
= 1;
3025 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3032 /* there are no items in the tree for us to truncate, we're
3035 if (path
->slots
[0] == 0)
3042 leaf
= path
->nodes
[0];
3043 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3044 found_type
= btrfs_key_type(&found_key
);
3047 if (found_key
.objectid
!= inode
->i_ino
)
3050 if (found_type
< min_type
)
3053 item_end
= found_key
.offset
;
3054 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
3055 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3056 struct btrfs_file_extent_item
);
3057 extent_type
= btrfs_file_extent_type(leaf
, fi
);
3058 encoding
= btrfs_file_extent_compression(leaf
, fi
);
3059 encoding
|= btrfs_file_extent_encryption(leaf
, fi
);
3060 encoding
|= btrfs_file_extent_other_encoding(leaf
, fi
);
3062 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3064 btrfs_file_extent_num_bytes(leaf
, fi
);
3065 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3066 item_end
+= btrfs_file_extent_inline_len(leaf
,
3071 if (found_type
> min_type
) {
3074 if (item_end
< new_size
)
3076 if (found_key
.offset
>= new_size
)
3082 /* FIXME, shrink the extent if the ref count is only 1 */
3083 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
3086 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3088 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
3089 if (!del_item
&& !encoding
) {
3090 u64 orig_num_bytes
=
3091 btrfs_file_extent_num_bytes(leaf
, fi
);
3092 extent_num_bytes
= new_size
-
3093 found_key
.offset
+ root
->sectorsize
- 1;
3094 extent_num_bytes
= extent_num_bytes
&
3095 ~((u64
)root
->sectorsize
- 1);
3096 btrfs_set_file_extent_num_bytes(leaf
, fi
,
3098 num_dec
= (orig_num_bytes
-
3100 if (root
->ref_cows
&& extent_start
!= 0)
3101 inode_sub_bytes(inode
, num_dec
);
3102 btrfs_mark_buffer_dirty(leaf
);
3105 btrfs_file_extent_disk_num_bytes(leaf
,
3107 extent_offset
= found_key
.offset
-
3108 btrfs_file_extent_offset(leaf
, fi
);
3110 /* FIXME blocksize != 4096 */
3111 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
3112 if (extent_start
!= 0) {
3115 inode_sub_bytes(inode
, num_dec
);
3118 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3120 * we can't truncate inline items that have had
3124 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
3125 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
3126 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
3127 u32 size
= new_size
- found_key
.offset
;
3129 if (root
->ref_cows
) {
3130 inode_sub_bytes(inode
, item_end
+ 1 -
3134 btrfs_file_extent_calc_inline_size(size
);
3135 ret
= btrfs_truncate_item(trans
, root
, path
,
3138 } else if (root
->ref_cows
) {
3139 inode_sub_bytes(inode
, item_end
+ 1 -
3145 if (!pending_del_nr
) {
3146 /* no pending yet, add ourselves */
3147 pending_del_slot
= path
->slots
[0];
3149 } else if (pending_del_nr
&&
3150 path
->slots
[0] + 1 == pending_del_slot
) {
3151 /* hop on the pending chunk */
3153 pending_del_slot
= path
->slots
[0];
3160 if (found_extent
&& root
->ref_cows
) {
3161 btrfs_set_path_blocking(path
);
3162 ret
= btrfs_free_extent(trans
, root
, extent_start
,
3163 extent_num_bytes
, 0,
3164 btrfs_header_owner(leaf
),
3165 inode
->i_ino
, extent_offset
);
3169 if (found_type
== BTRFS_INODE_ITEM_KEY
)
3172 if (path
->slots
[0] == 0 ||
3173 path
->slots
[0] != pending_del_slot
) {
3174 if (root
->ref_cows
) {
3178 if (pending_del_nr
) {
3179 ret
= btrfs_del_items(trans
, root
, path
,
3185 btrfs_release_path(root
, path
);
3192 if (pending_del_nr
) {
3193 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
3196 btrfs_free_path(path
);
3201 * taken from block_truncate_page, but does cow as it zeros out
3202 * any bytes left in the last page in the file.
3204 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
3206 struct inode
*inode
= mapping
->host
;
3207 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3208 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3209 struct btrfs_ordered_extent
*ordered
;
3210 struct extent_state
*cached_state
= NULL
;
3212 u32 blocksize
= root
->sectorsize
;
3213 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
3214 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
3220 if ((offset
& (blocksize
- 1)) == 0)
3222 ret
= btrfs_check_data_free_space(root
, inode
, PAGE_CACHE_SIZE
);
3226 ret
= btrfs_reserve_metadata_for_delalloc(root
, inode
, 1);
3232 page
= grab_cache_page(mapping
, index
);
3234 btrfs_free_reserved_data_space(root
, inode
, PAGE_CACHE_SIZE
);
3235 btrfs_unreserve_metadata_for_delalloc(root
, inode
, 1);
3239 page_start
= page_offset(page
);
3240 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3242 if (!PageUptodate(page
)) {
3243 ret
= btrfs_readpage(NULL
, page
);
3245 if (page
->mapping
!= mapping
) {
3247 page_cache_release(page
);
3250 if (!PageUptodate(page
)) {
3255 wait_on_page_writeback(page
);
3257 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
,
3259 set_page_extent_mapped(page
);
3261 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
3263 unlock_extent_cached(io_tree
, page_start
, page_end
,
3264 &cached_state
, GFP_NOFS
);
3266 page_cache_release(page
);
3267 btrfs_start_ordered_extent(inode
, ordered
, 1);
3268 btrfs_put_ordered_extent(ordered
);
3272 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
3273 EXTENT_DIRTY
| EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
,
3274 0, 0, &cached_state
, GFP_NOFS
);
3276 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
3279 unlock_extent_cached(io_tree
, page_start
, page_end
,
3280 &cached_state
, GFP_NOFS
);
3285 if (offset
!= PAGE_CACHE_SIZE
) {
3287 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
3288 flush_dcache_page(page
);
3291 ClearPageChecked(page
);
3292 set_page_dirty(page
);
3293 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
3298 btrfs_free_reserved_data_space(root
, inode
, PAGE_CACHE_SIZE
);
3299 btrfs_unreserve_metadata_for_delalloc(root
, inode
, 1);
3301 page_cache_release(page
);
3306 int btrfs_cont_expand(struct inode
*inode
, loff_t size
)
3308 struct btrfs_trans_handle
*trans
;
3309 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3310 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3311 struct extent_map
*em
= NULL
;
3312 struct extent_state
*cached_state
= NULL
;
3313 u64 mask
= root
->sectorsize
- 1;
3314 u64 hole_start
= (inode
->i_size
+ mask
) & ~mask
;
3315 u64 block_end
= (size
+ mask
) & ~mask
;
3321 if (size
<= hole_start
)
3325 struct btrfs_ordered_extent
*ordered
;
3326 btrfs_wait_ordered_range(inode
, hole_start
,
3327 block_end
- hole_start
);
3328 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
3329 &cached_state
, GFP_NOFS
);
3330 ordered
= btrfs_lookup_ordered_extent(inode
, hole_start
);
3333 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
3334 &cached_state
, GFP_NOFS
);
3335 btrfs_put_ordered_extent(ordered
);
3338 cur_offset
= hole_start
;
3340 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
3341 block_end
- cur_offset
, 0);
3342 BUG_ON(IS_ERR(em
) || !em
);
3343 last_byte
= min(extent_map_end(em
), block_end
);
3344 last_byte
= (last_byte
+ mask
) & ~mask
;
3345 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
3347 hole_size
= last_byte
- cur_offset
;
3349 trans
= btrfs_start_transaction(root
, 2);
3350 if (IS_ERR(trans
)) {
3351 err
= PTR_ERR(trans
);
3354 btrfs_set_trans_block_group(trans
, inode
);
3356 err
= btrfs_drop_extents(trans
, inode
, cur_offset
,
3357 cur_offset
+ hole_size
,
3361 err
= btrfs_insert_file_extent(trans
, root
,
3362 inode
->i_ino
, cur_offset
, 0,
3363 0, hole_size
, 0, hole_size
,
3367 btrfs_drop_extent_cache(inode
, hole_start
,
3370 btrfs_end_transaction(trans
, root
);
3372 free_extent_map(em
);
3374 cur_offset
= last_byte
;
3375 if (cur_offset
>= block_end
)
3379 free_extent_map(em
);
3380 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
3385 static int btrfs_setattr_size(struct inode
*inode
, struct iattr
*attr
)
3387 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3388 struct btrfs_trans_handle
*trans
;
3392 if (attr
->ia_size
== inode
->i_size
)
3395 if (attr
->ia_size
> inode
->i_size
) {
3396 unsigned long limit
;
3397 limit
= current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
3398 if (attr
->ia_size
> inode
->i_sb
->s_maxbytes
)
3400 if (limit
!= RLIM_INFINITY
&& attr
->ia_size
> limit
) {
3401 send_sig(SIGXFSZ
, current
, 0);
3406 trans
= btrfs_start_transaction(root
, 1);
3407 btrfs_set_trans_block_group(trans
, inode
);
3409 ret
= btrfs_orphan_add(trans
, inode
);
3412 nr
= trans
->blocks_used
;
3413 btrfs_end_transaction(trans
, root
);
3414 btrfs_btree_balance_dirty(root
, nr
);
3416 if (attr
->ia_size
> inode
->i_size
) {
3417 ret
= btrfs_cont_expand(inode
, attr
->ia_size
);
3419 btrfs_truncate(inode
);
3423 i_size_write(inode
, attr
->ia_size
);
3424 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
3426 trans
= btrfs_start_transaction(root
, 1);
3427 btrfs_set_trans_block_group(trans
, inode
);
3429 ret
= btrfs_update_inode(trans
, root
, inode
);
3431 if (inode
->i_nlink
> 0) {
3432 ret
= btrfs_orphan_del(trans
, inode
);
3435 nr
= trans
->blocks_used
;
3436 btrfs_end_transaction(trans
, root
);
3437 btrfs_btree_balance_dirty(root
, nr
);
3442 * We're truncating a file that used to have good data down to
3443 * zero. Make sure it gets into the ordered flush list so that
3444 * any new writes get down to disk quickly.
3446 if (attr
->ia_size
== 0)
3447 BTRFS_I(inode
)->ordered_data_close
= 1;
3449 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3450 ret
= vmtruncate(inode
, attr
->ia_size
);
3456 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
3458 struct inode
*inode
= dentry
->d_inode
;
3461 err
= inode_change_ok(inode
, attr
);
3465 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
3466 err
= btrfs_setattr_size(inode
, attr
);
3470 attr
->ia_valid
&= ~ATTR_SIZE
;
3473 err
= inode_setattr(inode
, attr
);
3475 if (!err
&& ((attr
->ia_valid
& ATTR_MODE
)))
3476 err
= btrfs_acl_chmod(inode
);
3480 void btrfs_delete_inode(struct inode
*inode
)
3482 struct btrfs_trans_handle
*trans
;
3483 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3487 truncate_inode_pages(&inode
->i_data
, 0);
3488 if (is_bad_inode(inode
)) {
3489 btrfs_orphan_del(NULL
, inode
);
3492 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
3494 if (root
->fs_info
->log_root_recovering
) {
3495 BUG_ON(!list_empty(&BTRFS_I(inode
)->i_orphan
));
3499 if (inode
->i_nlink
> 0) {
3500 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0);
3504 btrfs_i_size_write(inode
, 0);
3507 trans
= btrfs_start_transaction(root
, 1);
3508 btrfs_set_trans_block_group(trans
, inode
);
3509 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
3514 nr
= trans
->blocks_used
;
3515 btrfs_end_transaction(trans
, root
);
3517 btrfs_btree_balance_dirty(root
, nr
);
3521 ret
= btrfs_orphan_del(trans
, inode
);
3525 nr
= trans
->blocks_used
;
3526 btrfs_end_transaction(trans
, root
);
3527 btrfs_btree_balance_dirty(root
, nr
);
3534 * this returns the key found in the dir entry in the location pointer.
3535 * If no dir entries were found, location->objectid is 0.
3537 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
3538 struct btrfs_key
*location
)
3540 const char *name
= dentry
->d_name
.name
;
3541 int namelen
= dentry
->d_name
.len
;
3542 struct btrfs_dir_item
*di
;
3543 struct btrfs_path
*path
;
3544 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3547 path
= btrfs_alloc_path();
3550 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
3555 if (!di
|| IS_ERR(di
))
3558 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
3560 btrfs_free_path(path
);
3563 location
->objectid
= 0;
3568 * when we hit a tree root in a directory, the btrfs part of the inode
3569 * needs to be changed to reflect the root directory of the tree root. This
3570 * is kind of like crossing a mount point.
3572 static int fixup_tree_root_location(struct btrfs_root
*root
,
3574 struct dentry
*dentry
,
3575 struct btrfs_key
*location
,
3576 struct btrfs_root
**sub_root
)
3578 struct btrfs_path
*path
;
3579 struct btrfs_root
*new_root
;
3580 struct btrfs_root_ref
*ref
;
3581 struct extent_buffer
*leaf
;
3585 path
= btrfs_alloc_path();
3592 ret
= btrfs_find_root_ref(root
->fs_info
->tree_root
, path
,
3593 BTRFS_I(dir
)->root
->root_key
.objectid
,
3594 location
->objectid
);
3601 leaf
= path
->nodes
[0];
3602 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
3603 if (btrfs_root_ref_dirid(leaf
, ref
) != dir
->i_ino
||
3604 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
3607 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
3608 (unsigned long)(ref
+ 1),
3609 dentry
->d_name
.len
);
3613 btrfs_release_path(root
->fs_info
->tree_root
, path
);
3615 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
3616 if (IS_ERR(new_root
)) {
3617 err
= PTR_ERR(new_root
);
3621 if (btrfs_root_refs(&new_root
->root_item
) == 0) {
3626 *sub_root
= new_root
;
3627 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
3628 location
->type
= BTRFS_INODE_ITEM_KEY
;
3629 location
->offset
= 0;
3632 btrfs_free_path(path
);
3636 static void inode_tree_add(struct inode
*inode
)
3638 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3639 struct btrfs_inode
*entry
;
3641 struct rb_node
*parent
;
3643 p
= &root
->inode_tree
.rb_node
;
3646 if (hlist_unhashed(&inode
->i_hash
))
3649 spin_lock(&root
->inode_lock
);
3652 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
3654 if (inode
->i_ino
< entry
->vfs_inode
.i_ino
)
3655 p
= &parent
->rb_left
;
3656 else if (inode
->i_ino
> entry
->vfs_inode
.i_ino
)
3657 p
= &parent
->rb_right
;
3659 WARN_ON(!(entry
->vfs_inode
.i_state
&
3660 (I_WILL_FREE
| I_FREEING
| I_CLEAR
)));
3661 rb_erase(parent
, &root
->inode_tree
);
3662 RB_CLEAR_NODE(parent
);
3663 spin_unlock(&root
->inode_lock
);
3667 rb_link_node(&BTRFS_I(inode
)->rb_node
, parent
, p
);
3668 rb_insert_color(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
3669 spin_unlock(&root
->inode_lock
);
3672 static void inode_tree_del(struct inode
*inode
)
3674 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3677 spin_lock(&root
->inode_lock
);
3678 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
3679 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
3680 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
3681 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
3683 spin_unlock(&root
->inode_lock
);
3685 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0) {
3686 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
3687 spin_lock(&root
->inode_lock
);
3688 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
3689 spin_unlock(&root
->inode_lock
);
3691 btrfs_add_dead_root(root
);
3695 int btrfs_invalidate_inodes(struct btrfs_root
*root
)
3697 struct rb_node
*node
;
3698 struct rb_node
*prev
;
3699 struct btrfs_inode
*entry
;
3700 struct inode
*inode
;
3703 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
3705 spin_lock(&root
->inode_lock
);
3707 node
= root
->inode_tree
.rb_node
;
3711 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
3713 if (objectid
< entry
->vfs_inode
.i_ino
)
3714 node
= node
->rb_left
;
3715 else if (objectid
> entry
->vfs_inode
.i_ino
)
3716 node
= node
->rb_right
;
3722 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
3723 if (objectid
<= entry
->vfs_inode
.i_ino
) {
3727 prev
= rb_next(prev
);
3731 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
3732 objectid
= entry
->vfs_inode
.i_ino
+ 1;
3733 inode
= igrab(&entry
->vfs_inode
);
3735 spin_unlock(&root
->inode_lock
);
3736 if (atomic_read(&inode
->i_count
) > 1)
3737 d_prune_aliases(inode
);
3739 * btrfs_drop_inode will remove it from
3740 * the inode cache when its usage count
3745 spin_lock(&root
->inode_lock
);
3749 if (cond_resched_lock(&root
->inode_lock
))
3752 node
= rb_next(node
);
3754 spin_unlock(&root
->inode_lock
);
3758 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
3760 struct btrfs_iget_args
*args
= p
;
3761 inode
->i_ino
= args
->ino
;
3762 BTRFS_I(inode
)->root
= args
->root
;
3763 btrfs_set_inode_space_info(args
->root
, inode
);
3767 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
3769 struct btrfs_iget_args
*args
= opaque
;
3770 return args
->ino
== inode
->i_ino
&&
3771 args
->root
== BTRFS_I(inode
)->root
;
3774 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
3776 struct btrfs_root
*root
)
3778 struct inode
*inode
;
3779 struct btrfs_iget_args args
;
3780 args
.ino
= objectid
;
3783 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
3784 btrfs_init_locked_inode
,
3789 /* Get an inode object given its location and corresponding root.
3790 * Returns in *is_new if the inode was read from disk
3792 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
3793 struct btrfs_root
*root
, int *new)
3795 struct inode
*inode
;
3797 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
3799 return ERR_PTR(-ENOMEM
);
3801 if (inode
->i_state
& I_NEW
) {
3802 BTRFS_I(inode
)->root
= root
;
3803 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
3804 btrfs_read_locked_inode(inode
);
3806 inode_tree_add(inode
);
3807 unlock_new_inode(inode
);
3815 static struct inode
*new_simple_dir(struct super_block
*s
,
3816 struct btrfs_key
*key
,
3817 struct btrfs_root
*root
)
3819 struct inode
*inode
= new_inode(s
);
3822 return ERR_PTR(-ENOMEM
);
3824 BTRFS_I(inode
)->root
= root
;
3825 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
3826 BTRFS_I(inode
)->dummy_inode
= 1;
3828 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
3829 inode
->i_op
= &simple_dir_inode_operations
;
3830 inode
->i_fop
= &simple_dir_operations
;
3831 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
3832 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
3837 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
3839 struct inode
*inode
;
3840 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3841 struct btrfs_root
*sub_root
= root
;
3842 struct btrfs_key location
;
3846 dentry
->d_op
= &btrfs_dentry_operations
;
3848 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
3849 return ERR_PTR(-ENAMETOOLONG
);
3851 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
3854 return ERR_PTR(ret
);
3856 if (location
.objectid
== 0)
3859 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
3860 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
3864 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
3866 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
3867 ret
= fixup_tree_root_location(root
, dir
, dentry
,
3868 &location
, &sub_root
);
3871 inode
= ERR_PTR(ret
);
3873 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
3875 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
3877 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
3879 if (root
!= sub_root
) {
3880 down_read(&root
->fs_info
->cleanup_work_sem
);
3881 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
3882 btrfs_orphan_cleanup(sub_root
);
3883 up_read(&root
->fs_info
->cleanup_work_sem
);
3889 static int btrfs_dentry_delete(struct dentry
*dentry
)
3891 struct btrfs_root
*root
;
3893 if (!dentry
->d_inode
&& !IS_ROOT(dentry
))
3894 dentry
= dentry
->d_parent
;
3896 if (dentry
->d_inode
) {
3897 root
= BTRFS_I(dentry
->d_inode
)->root
;
3898 if (btrfs_root_refs(&root
->root_item
) == 0)
3904 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
3905 struct nameidata
*nd
)
3907 struct inode
*inode
;
3909 inode
= btrfs_lookup_dentry(dir
, dentry
);
3911 return ERR_CAST(inode
);
3913 return d_splice_alias(inode
, dentry
);
3916 static unsigned char btrfs_filetype_table
[] = {
3917 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
3920 static int btrfs_real_readdir(struct file
*filp
, void *dirent
,
3923 struct inode
*inode
= filp
->f_dentry
->d_inode
;
3924 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3925 struct btrfs_item
*item
;
3926 struct btrfs_dir_item
*di
;
3927 struct btrfs_key key
;
3928 struct btrfs_key found_key
;
3929 struct btrfs_path
*path
;
3932 struct extent_buffer
*leaf
;
3935 unsigned char d_type
;
3940 int key_type
= BTRFS_DIR_INDEX_KEY
;
3945 /* FIXME, use a real flag for deciding about the key type */
3946 if (root
->fs_info
->tree_root
== root
)
3947 key_type
= BTRFS_DIR_ITEM_KEY
;
3949 /* special case for "." */
3950 if (filp
->f_pos
== 0) {
3951 over
= filldir(dirent
, ".", 1,
3958 /* special case for .., just use the back ref */
3959 if (filp
->f_pos
== 1) {
3960 u64 pino
= parent_ino(filp
->f_path
.dentry
);
3961 over
= filldir(dirent
, "..", 2,
3967 path
= btrfs_alloc_path();
3970 btrfs_set_key_type(&key
, key_type
);
3971 key
.offset
= filp
->f_pos
;
3972 key
.objectid
= inode
->i_ino
;
3974 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3980 leaf
= path
->nodes
[0];
3981 nritems
= btrfs_header_nritems(leaf
);
3982 slot
= path
->slots
[0];
3983 if (advance
|| slot
>= nritems
) {
3984 if (slot
>= nritems
- 1) {
3985 ret
= btrfs_next_leaf(root
, path
);
3988 leaf
= path
->nodes
[0];
3989 nritems
= btrfs_header_nritems(leaf
);
3990 slot
= path
->slots
[0];
3998 item
= btrfs_item_nr(leaf
, slot
);
3999 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
4001 if (found_key
.objectid
!= key
.objectid
)
4003 if (btrfs_key_type(&found_key
) != key_type
)
4005 if (found_key
.offset
< filp
->f_pos
)
4008 filp
->f_pos
= found_key
.offset
;
4010 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
4012 di_total
= btrfs_item_size(leaf
, item
);
4014 while (di_cur
< di_total
) {
4015 struct btrfs_key location
;
4017 name_len
= btrfs_dir_name_len(leaf
, di
);
4018 if (name_len
<= sizeof(tmp_name
)) {
4019 name_ptr
= tmp_name
;
4021 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
4027 read_extent_buffer(leaf
, name_ptr
,
4028 (unsigned long)(di
+ 1), name_len
);
4030 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
4031 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
4033 /* is this a reference to our own snapshot? If so
4036 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
4037 location
.objectid
== root
->root_key
.objectid
) {
4041 over
= filldir(dirent
, name_ptr
, name_len
,
4042 found_key
.offset
, location
.objectid
,
4046 if (name_ptr
!= tmp_name
)
4051 di_len
= btrfs_dir_name_len(leaf
, di
) +
4052 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
4054 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
4058 /* Reached end of directory/root. Bump pos past the last item. */
4059 if (key_type
== BTRFS_DIR_INDEX_KEY
)
4061 * 32-bit glibc will use getdents64, but then strtol -
4062 * so the last number we can serve is this.
4064 filp
->f_pos
= 0x7fffffff;
4070 btrfs_free_path(path
);
4074 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
4076 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4077 struct btrfs_trans_handle
*trans
;
4080 if (root
->fs_info
->btree_inode
== inode
)
4083 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
4084 trans
= btrfs_join_transaction(root
, 1);
4085 btrfs_set_trans_block_group(trans
, inode
);
4086 ret
= btrfs_commit_transaction(trans
, root
);
4092 * This is somewhat expensive, updating the tree every time the
4093 * inode changes. But, it is most likely to find the inode in cache.
4094 * FIXME, needs more benchmarking...there are no reasons other than performance
4095 * to keep or drop this code.
4097 void btrfs_dirty_inode(struct inode
*inode
)
4099 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4100 struct btrfs_trans_handle
*trans
;
4102 trans
= btrfs_join_transaction(root
, 1);
4103 btrfs_set_trans_block_group(trans
, inode
);
4104 btrfs_update_inode(trans
, root
, inode
);
4105 btrfs_end_transaction(trans
, root
);
4109 * find the highest existing sequence number in a directory
4110 * and then set the in-memory index_cnt variable to reflect
4111 * free sequence numbers
4113 static int btrfs_set_inode_index_count(struct inode
*inode
)
4115 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4116 struct btrfs_key key
, found_key
;
4117 struct btrfs_path
*path
;
4118 struct extent_buffer
*leaf
;
4121 key
.objectid
= inode
->i_ino
;
4122 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
4123 key
.offset
= (u64
)-1;
4125 path
= btrfs_alloc_path();
4129 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4132 /* FIXME: we should be able to handle this */
4138 * MAGIC NUMBER EXPLANATION:
4139 * since we search a directory based on f_pos we have to start at 2
4140 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4141 * else has to start at 2
4143 if (path
->slots
[0] == 0) {
4144 BTRFS_I(inode
)->index_cnt
= 2;
4150 leaf
= path
->nodes
[0];
4151 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4153 if (found_key
.objectid
!= inode
->i_ino
||
4154 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
4155 BTRFS_I(inode
)->index_cnt
= 2;
4159 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
4161 btrfs_free_path(path
);
4166 * helper to find a free sequence number in a given directory. This current
4167 * code is very simple, later versions will do smarter things in the btree
4169 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
4173 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
4174 ret
= btrfs_set_inode_index_count(dir
);
4179 *index
= BTRFS_I(dir
)->index_cnt
;
4180 BTRFS_I(dir
)->index_cnt
++;
4185 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
4186 struct btrfs_root
*root
,
4188 const char *name
, int name_len
,
4189 u64 ref_objectid
, u64 objectid
,
4190 u64 alloc_hint
, int mode
, u64
*index
)
4192 struct inode
*inode
;
4193 struct btrfs_inode_item
*inode_item
;
4194 struct btrfs_key
*location
;
4195 struct btrfs_path
*path
;
4196 struct btrfs_inode_ref
*ref
;
4197 struct btrfs_key key
[2];
4203 path
= btrfs_alloc_path();
4206 inode
= new_inode(root
->fs_info
->sb
);
4208 return ERR_PTR(-ENOMEM
);
4211 ret
= btrfs_set_inode_index(dir
, index
);
4214 return ERR_PTR(ret
);
4218 * index_cnt is ignored for everything but a dir,
4219 * btrfs_get_inode_index_count has an explanation for the magic
4222 BTRFS_I(inode
)->index_cnt
= 2;
4223 BTRFS_I(inode
)->root
= root
;
4224 BTRFS_I(inode
)->generation
= trans
->transid
;
4225 btrfs_set_inode_space_info(root
, inode
);
4231 BTRFS_I(inode
)->block_group
=
4232 btrfs_find_block_group(root
, 0, alloc_hint
, owner
);
4234 key
[0].objectid
= objectid
;
4235 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
4238 key
[1].objectid
= objectid
;
4239 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
4240 key
[1].offset
= ref_objectid
;
4242 sizes
[0] = sizeof(struct btrfs_inode_item
);
4243 sizes
[1] = name_len
+ sizeof(*ref
);
4245 path
->leave_spinning
= 1;
4246 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
4250 inode
->i_uid
= current_fsuid();
4252 if (dir
&& (dir
->i_mode
& S_ISGID
)) {
4253 inode
->i_gid
= dir
->i_gid
;
4257 inode
->i_gid
= current_fsgid();
4259 inode
->i_mode
= mode
;
4260 inode
->i_ino
= objectid
;
4261 inode_set_bytes(inode
, 0);
4262 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4263 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4264 struct btrfs_inode_item
);
4265 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
4267 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
4268 struct btrfs_inode_ref
);
4269 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
4270 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
4271 ptr
= (unsigned long)(ref
+ 1);
4272 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
4274 btrfs_mark_buffer_dirty(path
->nodes
[0]);
4275 btrfs_free_path(path
);
4277 location
= &BTRFS_I(inode
)->location
;
4278 location
->objectid
= objectid
;
4279 location
->offset
= 0;
4280 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
4282 btrfs_inherit_iflags(inode
, dir
);
4284 if ((mode
& S_IFREG
)) {
4285 if (btrfs_test_opt(root
, NODATASUM
))
4286 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
4287 if (btrfs_test_opt(root
, NODATACOW
))
4288 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
;
4291 insert_inode_hash(inode
);
4292 inode_tree_add(inode
);
4296 BTRFS_I(dir
)->index_cnt
--;
4297 btrfs_free_path(path
);
4299 return ERR_PTR(ret
);
4302 static inline u8
btrfs_inode_type(struct inode
*inode
)
4304 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
4308 * utility function to add 'inode' into 'parent_inode' with
4309 * a give name and a given sequence number.
4310 * if 'add_backref' is true, also insert a backref from the
4311 * inode to the parent directory.
4313 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
4314 struct inode
*parent_inode
, struct inode
*inode
,
4315 const char *name
, int name_len
, int add_backref
, u64 index
)
4318 struct btrfs_key key
;
4319 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
4321 if (unlikely(inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4322 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
4324 key
.objectid
= inode
->i_ino
;
4325 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
4329 if (unlikely(inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4330 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
4331 key
.objectid
, root
->root_key
.objectid
,
4332 parent_inode
->i_ino
,
4333 index
, name
, name_len
);
4334 } else if (add_backref
) {
4335 ret
= btrfs_insert_inode_ref(trans
, root
,
4336 name
, name_len
, inode
->i_ino
,
4337 parent_inode
->i_ino
, index
);
4341 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
4342 parent_inode
->i_ino
, &key
,
4343 btrfs_inode_type(inode
), index
);
4346 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
4348 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
4349 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
4354 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
4355 struct dentry
*dentry
, struct inode
*inode
,
4356 int backref
, u64 index
)
4358 int err
= btrfs_add_link(trans
, dentry
->d_parent
->d_inode
,
4359 inode
, dentry
->d_name
.name
,
4360 dentry
->d_name
.len
, backref
, index
);
4362 d_instantiate(dentry
, inode
);
4370 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
4371 int mode
, dev_t rdev
)
4373 struct btrfs_trans_handle
*trans
;
4374 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4375 struct inode
*inode
= NULL
;
4379 unsigned long nr
= 0;
4382 if (!new_valid_dev(rdev
))
4385 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
4390 * 2 for inode item and ref
4392 * 1 for xattr if selinux is on
4394 trans
= btrfs_start_transaction(root
, 5);
4396 return PTR_ERR(trans
);
4398 btrfs_set_trans_block_group(trans
, dir
);
4400 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4402 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
4403 BTRFS_I(dir
)->block_group
, mode
, &index
);
4404 err
= PTR_ERR(inode
);
4408 err
= btrfs_init_inode_security(trans
, inode
, dir
);
4414 btrfs_set_trans_block_group(trans
, inode
);
4415 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0, index
);
4419 inode
->i_op
= &btrfs_special_inode_operations
;
4420 init_special_inode(inode
, inode
->i_mode
, rdev
);
4421 btrfs_update_inode(trans
, root
, inode
);
4423 btrfs_update_inode_block_group(trans
, inode
);
4424 btrfs_update_inode_block_group(trans
, dir
);
4426 nr
= trans
->blocks_used
;
4427 btrfs_end_transaction_throttle(trans
, root
);
4428 btrfs_btree_balance_dirty(root
, nr
);
4430 inode_dec_link_count(inode
);
4436 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
4437 int mode
, struct nameidata
*nd
)
4439 struct btrfs_trans_handle
*trans
;
4440 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4441 struct inode
*inode
= NULL
;
4444 unsigned long nr
= 0;
4448 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
4452 * 2 for inode item and ref
4454 * 1 for xattr if selinux is on
4456 trans
= btrfs_start_transaction(root
, 5);
4458 return PTR_ERR(trans
);
4460 btrfs_set_trans_block_group(trans
, dir
);
4462 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4464 dentry
->d_parent
->d_inode
->i_ino
,
4465 objectid
, BTRFS_I(dir
)->block_group
, mode
,
4467 err
= PTR_ERR(inode
);
4471 err
= btrfs_init_inode_security(trans
, inode
, dir
);
4477 btrfs_set_trans_block_group(trans
, inode
);
4478 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0, index
);
4482 inode
->i_mapping
->a_ops
= &btrfs_aops
;
4483 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
4484 inode
->i_fop
= &btrfs_file_operations
;
4485 inode
->i_op
= &btrfs_file_inode_operations
;
4486 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
4488 btrfs_update_inode_block_group(trans
, inode
);
4489 btrfs_update_inode_block_group(trans
, dir
);
4491 nr
= trans
->blocks_used
;
4492 btrfs_end_transaction_throttle(trans
, root
);
4494 inode_dec_link_count(inode
);
4497 btrfs_btree_balance_dirty(root
, nr
);
4501 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
4502 struct dentry
*dentry
)
4504 struct btrfs_trans_handle
*trans
;
4505 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4506 struct inode
*inode
= old_dentry
->d_inode
;
4508 unsigned long nr
= 0;
4512 if (inode
->i_nlink
== 0)
4515 /* do not allow sys_link's with other subvols of the same device */
4516 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
4519 btrfs_inc_nlink(inode
);
4521 err
= btrfs_set_inode_index(dir
, &index
);
4526 * 1 item for inode ref
4527 * 2 items for dir items
4529 trans
= btrfs_start_transaction(root
, 3);
4530 if (IS_ERR(trans
)) {
4531 err
= PTR_ERR(trans
);
4535 btrfs_set_trans_block_group(trans
, dir
);
4536 atomic_inc(&inode
->i_count
);
4538 err
= btrfs_add_nondir(trans
, dentry
, inode
, 1, index
);
4543 btrfs_update_inode_block_group(trans
, dir
);
4544 err
= btrfs_update_inode(trans
, root
, inode
);
4546 btrfs_log_new_name(trans
, inode
, NULL
, dentry
->d_parent
);
4549 nr
= trans
->blocks_used
;
4550 btrfs_end_transaction_throttle(trans
, root
);
4553 inode_dec_link_count(inode
);
4556 btrfs_btree_balance_dirty(root
, nr
);
4560 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
4562 struct inode
*inode
= NULL
;
4563 struct btrfs_trans_handle
*trans
;
4564 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4566 int drop_on_err
= 0;
4569 unsigned long nr
= 1;
4571 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
4576 * 2 items for inode and ref
4577 * 2 items for dir items
4578 * 1 for xattr if selinux is on
4580 trans
= btrfs_start_transaction(root
, 5);
4582 return PTR_ERR(trans
);
4583 btrfs_set_trans_block_group(trans
, dir
);
4585 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4587 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
4588 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
,
4590 if (IS_ERR(inode
)) {
4591 err
= PTR_ERR(inode
);
4597 err
= btrfs_init_inode_security(trans
, inode
, dir
);
4601 inode
->i_op
= &btrfs_dir_inode_operations
;
4602 inode
->i_fop
= &btrfs_dir_file_operations
;
4603 btrfs_set_trans_block_group(trans
, inode
);
4605 btrfs_i_size_write(inode
, 0);
4606 err
= btrfs_update_inode(trans
, root
, inode
);
4610 err
= btrfs_add_link(trans
, dentry
->d_parent
->d_inode
,
4611 inode
, dentry
->d_name
.name
,
4612 dentry
->d_name
.len
, 0, index
);
4616 d_instantiate(dentry
, inode
);
4618 btrfs_update_inode_block_group(trans
, inode
);
4619 btrfs_update_inode_block_group(trans
, dir
);
4622 nr
= trans
->blocks_used
;
4623 btrfs_end_transaction_throttle(trans
, root
);
4626 btrfs_btree_balance_dirty(root
, nr
);
4630 /* helper for btfs_get_extent. Given an existing extent in the tree,
4631 * and an extent that you want to insert, deal with overlap and insert
4632 * the new extent into the tree.
4634 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
4635 struct extent_map
*existing
,
4636 struct extent_map
*em
,
4637 u64 map_start
, u64 map_len
)
4641 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
4642 start_diff
= map_start
- em
->start
;
4643 em
->start
= map_start
;
4645 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
4646 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
4647 em
->block_start
+= start_diff
;
4648 em
->block_len
-= start_diff
;
4650 return add_extent_mapping(em_tree
, em
);
4653 static noinline
int uncompress_inline(struct btrfs_path
*path
,
4654 struct inode
*inode
, struct page
*page
,
4655 size_t pg_offset
, u64 extent_offset
,
4656 struct btrfs_file_extent_item
*item
)
4659 struct extent_buffer
*leaf
= path
->nodes
[0];
4662 unsigned long inline_size
;
4665 WARN_ON(pg_offset
!= 0);
4666 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
4667 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
4668 btrfs_item_nr(leaf
, path
->slots
[0]));
4669 tmp
= kmalloc(inline_size
, GFP_NOFS
);
4670 ptr
= btrfs_file_extent_inline_start(item
);
4672 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
4674 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
4675 ret
= btrfs_zlib_decompress(tmp
, page
, extent_offset
,
4676 inline_size
, max_size
);
4678 char *kaddr
= kmap_atomic(page
, KM_USER0
);
4679 unsigned long copy_size
= min_t(u64
,
4680 PAGE_CACHE_SIZE
- pg_offset
,
4681 max_size
- extent_offset
);
4682 memset(kaddr
+ pg_offset
, 0, copy_size
);
4683 kunmap_atomic(kaddr
, KM_USER0
);
4690 * a bit scary, this does extent mapping from logical file offset to the disk.
4691 * the ugly parts come from merging extents from the disk with the in-ram
4692 * representation. This gets more complex because of the data=ordered code,
4693 * where the in-ram extents might be locked pending data=ordered completion.
4695 * This also copies inline extents directly into the page.
4698 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
4699 size_t pg_offset
, u64 start
, u64 len
,
4705 u64 extent_start
= 0;
4707 u64 objectid
= inode
->i_ino
;
4709 struct btrfs_path
*path
= NULL
;
4710 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4711 struct btrfs_file_extent_item
*item
;
4712 struct extent_buffer
*leaf
;
4713 struct btrfs_key found_key
;
4714 struct extent_map
*em
= NULL
;
4715 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4716 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4717 struct btrfs_trans_handle
*trans
= NULL
;
4721 read_lock(&em_tree
->lock
);
4722 em
= lookup_extent_mapping(em_tree
, start
, len
);
4724 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4725 read_unlock(&em_tree
->lock
);
4728 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
4729 free_extent_map(em
);
4730 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
4731 free_extent_map(em
);
4735 em
= alloc_extent_map(GFP_NOFS
);
4740 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4741 em
->start
= EXTENT_MAP_HOLE
;
4742 em
->orig_start
= EXTENT_MAP_HOLE
;
4744 em
->block_len
= (u64
)-1;
4747 path
= btrfs_alloc_path();
4751 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
4752 objectid
, start
, trans
!= NULL
);
4759 if (path
->slots
[0] == 0)
4764 leaf
= path
->nodes
[0];
4765 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
4766 struct btrfs_file_extent_item
);
4767 /* are we inside the extent that was found? */
4768 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4769 found_type
= btrfs_key_type(&found_key
);
4770 if (found_key
.objectid
!= objectid
||
4771 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
4775 found_type
= btrfs_file_extent_type(leaf
, item
);
4776 extent_start
= found_key
.offset
;
4777 compressed
= btrfs_file_extent_compression(leaf
, item
);
4778 if (found_type
== BTRFS_FILE_EXTENT_REG
||
4779 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
4780 extent_end
= extent_start
+
4781 btrfs_file_extent_num_bytes(leaf
, item
);
4782 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
4784 size
= btrfs_file_extent_inline_len(leaf
, item
);
4785 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
4786 ~((u64
)root
->sectorsize
- 1);
4789 if (start
>= extent_end
) {
4791 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
4792 ret
= btrfs_next_leaf(root
, path
);
4799 leaf
= path
->nodes
[0];
4801 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4802 if (found_key
.objectid
!= objectid
||
4803 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
4805 if (start
+ len
<= found_key
.offset
)
4808 em
->len
= found_key
.offset
- start
;
4812 if (found_type
== BTRFS_FILE_EXTENT_REG
||
4813 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
4814 em
->start
= extent_start
;
4815 em
->len
= extent_end
- extent_start
;
4816 em
->orig_start
= extent_start
-
4817 btrfs_file_extent_offset(leaf
, item
);
4818 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
4820 em
->block_start
= EXTENT_MAP_HOLE
;
4824 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
4825 em
->block_start
= bytenr
;
4826 em
->block_len
= btrfs_file_extent_disk_num_bytes(leaf
,
4829 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
4830 em
->block_start
= bytenr
;
4831 em
->block_len
= em
->len
;
4832 if (found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
4833 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
4836 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
4840 size_t extent_offset
;
4843 em
->block_start
= EXTENT_MAP_INLINE
;
4844 if (!page
|| create
) {
4845 em
->start
= extent_start
;
4846 em
->len
= extent_end
- extent_start
;
4850 size
= btrfs_file_extent_inline_len(leaf
, item
);
4851 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
4852 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
4853 size
- extent_offset
);
4854 em
->start
= extent_start
+ extent_offset
;
4855 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
4856 ~((u64
)root
->sectorsize
- 1);
4857 em
->orig_start
= EXTENT_MAP_INLINE
;
4859 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
4860 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
4861 if (create
== 0 && !PageUptodate(page
)) {
4862 if (btrfs_file_extent_compression(leaf
, item
) ==
4863 BTRFS_COMPRESS_ZLIB
) {
4864 ret
= uncompress_inline(path
, inode
, page
,
4866 extent_offset
, item
);
4870 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
4872 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
4873 memset(map
+ pg_offset
+ copy_size
, 0,
4874 PAGE_CACHE_SIZE
- pg_offset
-
4879 flush_dcache_page(page
);
4880 } else if (create
&& PageUptodate(page
)) {
4883 free_extent_map(em
);
4885 btrfs_release_path(root
, path
);
4886 trans
= btrfs_join_transaction(root
, 1);
4890 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
4893 btrfs_mark_buffer_dirty(leaf
);
4895 set_extent_uptodate(io_tree
, em
->start
,
4896 extent_map_end(em
) - 1, GFP_NOFS
);
4899 printk(KERN_ERR
"btrfs unknown found_type %d\n", found_type
);
4906 em
->block_start
= EXTENT_MAP_HOLE
;
4907 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
4909 btrfs_release_path(root
, path
);
4910 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
4911 printk(KERN_ERR
"Btrfs: bad extent! em: [%llu %llu] passed "
4912 "[%llu %llu]\n", (unsigned long long)em
->start
,
4913 (unsigned long long)em
->len
,
4914 (unsigned long long)start
,
4915 (unsigned long long)len
);
4921 write_lock(&em_tree
->lock
);
4922 ret
= add_extent_mapping(em_tree
, em
);
4923 /* it is possible that someone inserted the extent into the tree
4924 * while we had the lock dropped. It is also possible that
4925 * an overlapping map exists in the tree
4927 if (ret
== -EEXIST
) {
4928 struct extent_map
*existing
;
4932 existing
= lookup_extent_mapping(em_tree
, start
, len
);
4933 if (existing
&& (existing
->start
> start
||
4934 existing
->start
+ existing
->len
<= start
)) {
4935 free_extent_map(existing
);
4939 existing
= lookup_extent_mapping(em_tree
, em
->start
,
4942 err
= merge_extent_mapping(em_tree
, existing
,
4945 free_extent_map(existing
);
4947 free_extent_map(em
);
4952 free_extent_map(em
);
4956 free_extent_map(em
);
4961 write_unlock(&em_tree
->lock
);
4964 btrfs_free_path(path
);
4966 ret
= btrfs_end_transaction(trans
, root
);
4971 free_extent_map(em
);
4972 return ERR_PTR(err
);
4977 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
4978 const struct iovec
*iov
, loff_t offset
,
4979 unsigned long nr_segs
)
4984 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
4985 __u64 start
, __u64 len
)
4987 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent
);
4990 int btrfs_readpage(struct file
*file
, struct page
*page
)
4992 struct extent_io_tree
*tree
;
4993 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
4994 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
4997 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
4999 struct extent_io_tree
*tree
;
5002 if (current
->flags
& PF_MEMALLOC
) {
5003 redirty_page_for_writepage(wbc
, page
);
5007 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
5008 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
5011 int btrfs_writepages(struct address_space
*mapping
,
5012 struct writeback_control
*wbc
)
5014 struct extent_io_tree
*tree
;
5016 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
5017 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
5021 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
5022 struct list_head
*pages
, unsigned nr_pages
)
5024 struct extent_io_tree
*tree
;
5025 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
5026 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
5029 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
5031 struct extent_io_tree
*tree
;
5032 struct extent_map_tree
*map
;
5035 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
5036 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
5037 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
5039 ClearPagePrivate(page
);
5040 set_page_private(page
, 0);
5041 page_cache_release(page
);
5046 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
5048 if (PageWriteback(page
) || PageDirty(page
))
5050 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
5053 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
5055 struct extent_io_tree
*tree
;
5056 struct btrfs_ordered_extent
*ordered
;
5057 struct extent_state
*cached_state
= NULL
;
5058 u64 page_start
= page_offset(page
);
5059 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
5063 * we have the page locked, so new writeback can't start,
5064 * and the dirty bit won't be cleared while we are here.
5066 * Wait for IO on this page so that we can safely clear
5067 * the PagePrivate2 bit and do ordered accounting
5069 wait_on_page_writeback(page
);
5071 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
5073 btrfs_releasepage(page
, GFP_NOFS
);
5076 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
,
5078 ordered
= btrfs_lookup_ordered_extent(page
->mapping
->host
,
5082 * IO on this page will never be started, so we need
5083 * to account for any ordered extents now
5085 clear_extent_bit(tree
, page_start
, page_end
,
5086 EXTENT_DIRTY
| EXTENT_DELALLOC
|
5087 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
, 1, 0,
5088 &cached_state
, GFP_NOFS
);
5090 * whoever cleared the private bit is responsible
5091 * for the finish_ordered_io
5093 if (TestClearPagePrivate2(page
)) {
5094 btrfs_finish_ordered_io(page
->mapping
->host
,
5095 page_start
, page_end
);
5097 btrfs_put_ordered_extent(ordered
);
5098 cached_state
= NULL
;
5099 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
,
5102 clear_extent_bit(tree
, page_start
, page_end
,
5103 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
5104 EXTENT_DO_ACCOUNTING
, 1, 1, &cached_state
, GFP_NOFS
);
5105 __btrfs_releasepage(page
, GFP_NOFS
);
5107 ClearPageChecked(page
);
5108 if (PagePrivate(page
)) {
5109 ClearPagePrivate(page
);
5110 set_page_private(page
, 0);
5111 page_cache_release(page
);
5116 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
5117 * called from a page fault handler when a page is first dirtied. Hence we must
5118 * be careful to check for EOF conditions here. We set the page up correctly
5119 * for a written page which means we get ENOSPC checking when writing into
5120 * holes and correct delalloc and unwritten extent mapping on filesystems that
5121 * support these features.
5123 * We are not allowed to take the i_mutex here so we have to play games to
5124 * protect against truncate races as the page could now be beyond EOF. Because
5125 * vmtruncate() writes the inode size before removing pages, once we have the
5126 * page lock we can determine safely if the page is beyond EOF. If it is not
5127 * beyond EOF, then the page is guaranteed safe against truncation until we
5130 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
5132 struct page
*page
= vmf
->page
;
5133 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
5134 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5135 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
5136 struct btrfs_ordered_extent
*ordered
;
5137 struct extent_state
*cached_state
= NULL
;
5139 unsigned long zero_start
;
5145 ret
= btrfs_check_data_free_space(root
, inode
, PAGE_CACHE_SIZE
);
5149 else /* -ENOSPC, -EIO, etc */
5150 ret
= VM_FAULT_SIGBUS
;
5154 ret
= btrfs_reserve_metadata_for_delalloc(root
, inode
, 1);
5156 btrfs_free_reserved_data_space(root
, inode
, PAGE_CACHE_SIZE
);
5157 ret
= VM_FAULT_SIGBUS
;
5161 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
5164 size
= i_size_read(inode
);
5165 page_start
= page_offset(page
);
5166 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
5168 if ((page
->mapping
!= inode
->i_mapping
) ||
5169 (page_start
>= size
)) {
5170 btrfs_free_reserved_data_space(root
, inode
, PAGE_CACHE_SIZE
);
5171 /* page got truncated out from underneath us */
5174 wait_on_page_writeback(page
);
5176 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
,
5178 set_page_extent_mapped(page
);
5181 * we can't set the delalloc bits if there are pending ordered
5182 * extents. Drop our locks and wait for them to finish
5184 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
5186 unlock_extent_cached(io_tree
, page_start
, page_end
,
5187 &cached_state
, GFP_NOFS
);
5189 btrfs_start_ordered_extent(inode
, ordered
, 1);
5190 btrfs_put_ordered_extent(ordered
);
5195 * XXX - page_mkwrite gets called every time the page is dirtied, even
5196 * if it was already dirty, so for space accounting reasons we need to
5197 * clear any delalloc bits for the range we are fixing to save. There
5198 * is probably a better way to do this, but for now keep consistent with
5199 * prepare_pages in the normal write path.
5201 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
5202 EXTENT_DIRTY
| EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
,
5203 0, 0, &cached_state
, GFP_NOFS
);
5205 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
5208 unlock_extent_cached(io_tree
, page_start
, page_end
,
5209 &cached_state
, GFP_NOFS
);
5210 ret
= VM_FAULT_SIGBUS
;
5211 btrfs_free_reserved_data_space(root
, inode
, PAGE_CACHE_SIZE
);
5216 /* page is wholly or partially inside EOF */
5217 if (page_start
+ PAGE_CACHE_SIZE
> size
)
5218 zero_start
= size
& ~PAGE_CACHE_MASK
;
5220 zero_start
= PAGE_CACHE_SIZE
;
5222 if (zero_start
!= PAGE_CACHE_SIZE
) {
5224 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
5225 flush_dcache_page(page
);
5228 ClearPageChecked(page
);
5229 set_page_dirty(page
);
5230 SetPageUptodate(page
);
5232 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
5233 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
5235 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
5238 btrfs_unreserve_metadata_for_delalloc(root
, inode
, 1);
5240 return VM_FAULT_LOCKED
;
5246 static void btrfs_truncate(struct inode
*inode
)
5248 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5250 struct btrfs_trans_handle
*trans
;
5252 u64 mask
= root
->sectorsize
- 1;
5254 if (!S_ISREG(inode
->i_mode
)) {
5259 ret
= btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
5263 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
5264 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
5266 trans
= btrfs_start_transaction(root
, 1);
5267 btrfs_set_trans_block_group(trans
, inode
);
5270 * setattr is responsible for setting the ordered_data_close flag,
5271 * but that is only tested during the last file release. That
5272 * could happen well after the next commit, leaving a great big
5273 * window where new writes may get lost if someone chooses to write
5274 * to this file after truncating to zero
5276 * The inode doesn't have any dirty data here, and so if we commit
5277 * this is a noop. If someone immediately starts writing to the inode
5278 * it is very likely we'll catch some of their writes in this
5279 * transaction, and the commit will find this file on the ordered
5280 * data list with good things to send down.
5282 * This is a best effort solution, there is still a window where
5283 * using truncate to replace the contents of the file will
5284 * end up with a zero length file after a crash.
5286 if (inode
->i_size
== 0 && BTRFS_I(inode
)->ordered_data_close
)
5287 btrfs_add_ordered_operation(trans
, root
, inode
);
5290 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
5292 BTRFS_EXTENT_DATA_KEY
);
5296 ret
= btrfs_update_inode(trans
, root
, inode
);
5299 nr
= trans
->blocks_used
;
5300 btrfs_end_transaction(trans
, root
);
5301 btrfs_btree_balance_dirty(root
, nr
);
5303 trans
= btrfs_start_transaction(root
, 1);
5304 btrfs_set_trans_block_group(trans
, inode
);
5307 if (ret
== 0 && inode
->i_nlink
> 0) {
5308 ret
= btrfs_orphan_del(trans
, inode
);
5312 ret
= btrfs_update_inode(trans
, root
, inode
);
5315 nr
= trans
->blocks_used
;
5316 ret
= btrfs_end_transaction_throttle(trans
, root
);
5318 btrfs_btree_balance_dirty(root
, nr
);
5322 * create a new subvolume directory/inode (helper for the ioctl).
5324 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
5325 struct btrfs_root
*new_root
,
5326 u64 new_dirid
, u64 alloc_hint
)
5328 struct inode
*inode
;
5332 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2, new_dirid
,
5333 new_dirid
, alloc_hint
, S_IFDIR
| 0700, &index
);
5335 return PTR_ERR(inode
);
5336 inode
->i_op
= &btrfs_dir_inode_operations
;
5337 inode
->i_fop
= &btrfs_dir_file_operations
;
5340 btrfs_i_size_write(inode
, 0);
5342 err
= btrfs_update_inode(trans
, new_root
, inode
);
5349 /* helper function for file defrag and space balancing. This
5350 * forces readahead on a given range of bytes in an inode
5352 unsigned long btrfs_force_ra(struct address_space
*mapping
,
5353 struct file_ra_state
*ra
, struct file
*file
,
5354 pgoff_t offset
, pgoff_t last_index
)
5356 pgoff_t req_size
= last_index
- offset
+ 1;
5358 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
5359 return offset
+ req_size
;
5362 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
5364 struct btrfs_inode
*ei
;
5365 struct inode
*inode
;
5367 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
5372 ei
->space_info
= NULL
;
5376 ei
->last_sub_trans
= 0;
5377 ei
->logged_trans
= 0;
5378 ei
->delalloc_bytes
= 0;
5379 ei
->reserved_bytes
= 0;
5380 ei
->disk_i_size
= 0;
5382 ei
->index_cnt
= (u64
)-1;
5383 ei
->last_unlink_trans
= 0;
5385 spin_lock_init(&ei
->accounting_lock
);
5386 ei
->outstanding_extents
= 0;
5387 ei
->reserved_extents
= 0;
5389 ei
->ordered_data_close
= 0;
5390 ei
->dummy_inode
= 0;
5391 ei
->force_compress
= 0;
5393 inode
= &ei
->vfs_inode
;
5394 extent_map_tree_init(&ei
->extent_tree
, GFP_NOFS
);
5395 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
, GFP_NOFS
);
5396 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
, GFP_NOFS
);
5397 mutex_init(&ei
->log_mutex
);
5398 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
5399 INIT_LIST_HEAD(&ei
->i_orphan
);
5400 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
5401 INIT_LIST_HEAD(&ei
->ordered_operations
);
5402 RB_CLEAR_NODE(&ei
->rb_node
);
5407 void btrfs_destroy_inode(struct inode
*inode
)
5409 struct btrfs_ordered_extent
*ordered
;
5410 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5412 WARN_ON(!list_empty(&inode
->i_dentry
));
5413 WARN_ON(inode
->i_data
.nrpages
);
5416 * This can happen where we create an inode, but somebody else also
5417 * created the same inode and we need to destroy the one we already
5424 * Make sure we're properly removed from the ordered operation
5428 if (!list_empty(&BTRFS_I(inode
)->ordered_operations
)) {
5429 spin_lock(&root
->fs_info
->ordered_extent_lock
);
5430 list_del_init(&BTRFS_I(inode
)->ordered_operations
);
5431 spin_unlock(&root
->fs_info
->ordered_extent_lock
);
5434 spin_lock(&root
->list_lock
);
5435 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
5436 printk(KERN_INFO
"BTRFS: inode %lu still on the orphan list\n",
5438 list_del_init(&BTRFS_I(inode
)->i_orphan
);
5440 spin_unlock(&root
->list_lock
);
5443 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
5447 printk(KERN_ERR
"btrfs found ordered "
5448 "extent %llu %llu on inode cleanup\n",
5449 (unsigned long long)ordered
->file_offset
,
5450 (unsigned long long)ordered
->len
);
5451 btrfs_remove_ordered_extent(inode
, ordered
);
5452 btrfs_put_ordered_extent(ordered
);
5453 btrfs_put_ordered_extent(ordered
);
5456 inode_tree_del(inode
);
5457 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
5459 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
5462 void btrfs_drop_inode(struct inode
*inode
)
5464 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5465 if (inode
->i_nlink
> 0 && btrfs_root_refs(&root
->root_item
) == 0)
5466 generic_delete_inode(inode
);
5468 generic_drop_inode(inode
);
5471 static void init_once(void *foo
)
5473 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
5475 inode_init_once(&ei
->vfs_inode
);
5478 void btrfs_destroy_cachep(void)
5480 if (btrfs_inode_cachep
)
5481 kmem_cache_destroy(btrfs_inode_cachep
);
5482 if (btrfs_trans_handle_cachep
)
5483 kmem_cache_destroy(btrfs_trans_handle_cachep
);
5484 if (btrfs_transaction_cachep
)
5485 kmem_cache_destroy(btrfs_transaction_cachep
);
5486 if (btrfs_path_cachep
)
5487 kmem_cache_destroy(btrfs_path_cachep
);
5490 int btrfs_init_cachep(void)
5492 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode_cache",
5493 sizeof(struct btrfs_inode
), 0,
5494 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
5495 if (!btrfs_inode_cachep
)
5498 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle_cache",
5499 sizeof(struct btrfs_trans_handle
), 0,
5500 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
5501 if (!btrfs_trans_handle_cachep
)
5504 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction_cache",
5505 sizeof(struct btrfs_transaction
), 0,
5506 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
5507 if (!btrfs_transaction_cachep
)
5510 btrfs_path_cachep
= kmem_cache_create("btrfs_path_cache",
5511 sizeof(struct btrfs_path
), 0,
5512 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
5513 if (!btrfs_path_cachep
)
5518 btrfs_destroy_cachep();
5522 static int btrfs_getattr(struct vfsmount
*mnt
,
5523 struct dentry
*dentry
, struct kstat
*stat
)
5525 struct inode
*inode
= dentry
->d_inode
;
5526 generic_fillattr(inode
, stat
);
5527 stat
->dev
= BTRFS_I(inode
)->root
->anon_super
.s_dev
;
5528 stat
->blksize
= PAGE_CACHE_SIZE
;
5529 stat
->blocks
= (inode_get_bytes(inode
) +
5530 BTRFS_I(inode
)->delalloc_bytes
) >> 9;
5534 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
5535 struct inode
*new_dir
, struct dentry
*new_dentry
)
5537 struct btrfs_trans_handle
*trans
;
5538 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
5539 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
5540 struct inode
*new_inode
= new_dentry
->d_inode
;
5541 struct inode
*old_inode
= old_dentry
->d_inode
;
5542 struct timespec ctime
= CURRENT_TIME
;
5547 if (new_dir
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
5550 /* we only allow rename subvolume link between subvolumes */
5551 if (old_inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
5554 if (old_inode
->i_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
5555 (new_inode
&& new_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
))
5558 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
5559 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
5562 * we're using rename to replace one file with another.
5563 * and the replacement file is large. Start IO on it now so
5564 * we don't add too much work to the end of the transaction
5566 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
&&
5567 old_inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
5568 filemap_flush(old_inode
->i_mapping
);
5570 /* close the racy window with snapshot create/destroy ioctl */
5571 if (old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)
5572 down_read(&root
->fs_info
->subvol_sem
);
5574 * We want to reserve the absolute worst case amount of items. So if
5575 * both inodes are subvols and we need to unlink them then that would
5576 * require 4 item modifications, but if they are both normal inodes it
5577 * would require 5 item modifications, so we'll assume their normal
5578 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
5579 * should cover the worst case number of items we'll modify.
5581 trans
= btrfs_start_transaction(root
, 20);
5583 return PTR_ERR(trans
);
5585 btrfs_set_trans_block_group(trans
, new_dir
);
5588 btrfs_record_root_in_trans(trans
, dest
);
5590 ret
= btrfs_set_inode_index(new_dir
, &index
);
5594 if (unlikely(old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5595 /* force full log commit if subvolume involved. */
5596 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
5598 ret
= btrfs_insert_inode_ref(trans
, dest
,
5599 new_dentry
->d_name
.name
,
5600 new_dentry
->d_name
.len
,
5602 new_dir
->i_ino
, index
);
5606 * this is an ugly little race, but the rename is required
5607 * to make sure that if we crash, the inode is either at the
5608 * old name or the new one. pinning the log transaction lets
5609 * us make sure we don't allow a log commit to come in after
5610 * we unlink the name but before we add the new name back in.
5612 btrfs_pin_log_trans(root
);
5615 * make sure the inode gets flushed if it is replacing
5618 if (new_inode
&& new_inode
->i_size
&&
5619 old_inode
&& S_ISREG(old_inode
->i_mode
)) {
5620 btrfs_add_ordered_operation(trans
, root
, old_inode
);
5623 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
5624 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
5625 old_inode
->i_ctime
= ctime
;
5627 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
5628 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
5630 if (unlikely(old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5631 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
5632 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
5633 old_dentry
->d_name
.name
,
5634 old_dentry
->d_name
.len
);
5636 btrfs_inc_nlink(old_dentry
->d_inode
);
5637 ret
= btrfs_unlink_inode(trans
, root
, old_dir
,
5638 old_dentry
->d_inode
,
5639 old_dentry
->d_name
.name
,
5640 old_dentry
->d_name
.len
);
5645 new_inode
->i_ctime
= CURRENT_TIME
;
5646 if (unlikely(new_inode
->i_ino
==
5647 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
5648 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
5649 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
5651 new_dentry
->d_name
.name
,
5652 new_dentry
->d_name
.len
);
5653 BUG_ON(new_inode
->i_nlink
== 0);
5655 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
5656 new_dentry
->d_inode
,
5657 new_dentry
->d_name
.name
,
5658 new_dentry
->d_name
.len
);
5661 if (new_inode
->i_nlink
== 0) {
5662 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
5667 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
5668 new_dentry
->d_name
.name
,
5669 new_dentry
->d_name
.len
, 0, index
);
5672 if (old_inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
5673 btrfs_log_new_name(trans
, old_inode
, old_dir
,
5674 new_dentry
->d_parent
);
5675 btrfs_end_log_trans(root
);
5678 btrfs_end_transaction_throttle(trans
, root
);
5680 if (old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)
5681 up_read(&root
->fs_info
->subvol_sem
);
5687 * some fairly slow code that needs optimization. This walks the list
5688 * of all the inodes with pending delalloc and forces them to disk.
5690 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
5692 struct list_head
*head
= &root
->fs_info
->delalloc_inodes
;
5693 struct btrfs_inode
*binode
;
5694 struct inode
*inode
;
5696 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
5699 spin_lock(&root
->fs_info
->delalloc_lock
);
5700 while (!list_empty(head
)) {
5701 binode
= list_entry(head
->next
, struct btrfs_inode
,
5703 inode
= igrab(&binode
->vfs_inode
);
5705 list_del_init(&binode
->delalloc_inodes
);
5706 spin_unlock(&root
->fs_info
->delalloc_lock
);
5708 filemap_flush(inode
->i_mapping
);
5710 btrfs_add_delayed_iput(inode
);
5715 spin_lock(&root
->fs_info
->delalloc_lock
);
5717 spin_unlock(&root
->fs_info
->delalloc_lock
);
5719 /* the filemap_flush will queue IO into the worker threads, but
5720 * we have to make sure the IO is actually started and that
5721 * ordered extents get created before we return
5723 atomic_inc(&root
->fs_info
->async_submit_draining
);
5724 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
5725 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
5726 wait_event(root
->fs_info
->async_submit_wait
,
5727 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
5728 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
5730 atomic_dec(&root
->fs_info
->async_submit_draining
);
5734 int btrfs_start_one_delalloc_inode(struct btrfs_root
*root
, int delay_iput
)
5736 struct btrfs_inode
*binode
;
5737 struct inode
*inode
= NULL
;
5739 spin_lock(&root
->fs_info
->delalloc_lock
);
5740 while (!list_empty(&root
->fs_info
->delalloc_inodes
)) {
5741 binode
= list_entry(root
->fs_info
->delalloc_inodes
.next
,
5742 struct btrfs_inode
, delalloc_inodes
);
5743 inode
= igrab(&binode
->vfs_inode
);
5745 list_move_tail(&binode
->delalloc_inodes
,
5746 &root
->fs_info
->delalloc_inodes
);
5750 list_del_init(&binode
->delalloc_inodes
);
5751 cond_resched_lock(&root
->fs_info
->delalloc_lock
);
5753 spin_unlock(&root
->fs_info
->delalloc_lock
);
5756 write_inode_now(inode
, 0);
5758 btrfs_add_delayed_iput(inode
);
5766 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
5767 const char *symname
)
5769 struct btrfs_trans_handle
*trans
;
5770 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5771 struct btrfs_path
*path
;
5772 struct btrfs_key key
;
5773 struct inode
*inode
= NULL
;
5781 struct btrfs_file_extent_item
*ei
;
5782 struct extent_buffer
*leaf
;
5783 unsigned long nr
= 0;
5785 name_len
= strlen(symname
) + 1;
5786 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
5787 return -ENAMETOOLONG
;
5789 err
= btrfs_find_free_objectid(NULL
, root
, dir
->i_ino
, &objectid
);
5793 * 2 items for inode item and ref
5794 * 2 items for dir items
5795 * 1 item for xattr if selinux is on
5797 trans
= btrfs_start_transaction(root
, 5);
5799 return PTR_ERR(trans
);
5801 btrfs_set_trans_block_group(trans
, dir
);
5803 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5805 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
5806 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
,
5808 err
= PTR_ERR(inode
);
5812 err
= btrfs_init_inode_security(trans
, inode
, dir
);
5818 btrfs_set_trans_block_group(trans
, inode
);
5819 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0, index
);
5823 inode
->i_mapping
->a_ops
= &btrfs_aops
;
5824 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
5825 inode
->i_fop
= &btrfs_file_operations
;
5826 inode
->i_op
= &btrfs_file_inode_operations
;
5827 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
5829 btrfs_update_inode_block_group(trans
, inode
);
5830 btrfs_update_inode_block_group(trans
, dir
);
5834 path
= btrfs_alloc_path();
5836 key
.objectid
= inode
->i_ino
;
5838 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
5839 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
5840 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
5846 leaf
= path
->nodes
[0];
5847 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
5848 struct btrfs_file_extent_item
);
5849 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
5850 btrfs_set_file_extent_type(leaf
, ei
,
5851 BTRFS_FILE_EXTENT_INLINE
);
5852 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
5853 btrfs_set_file_extent_compression(leaf
, ei
, 0);
5854 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
5855 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
5857 ptr
= btrfs_file_extent_inline_start(ei
);
5858 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
5859 btrfs_mark_buffer_dirty(leaf
);
5860 btrfs_free_path(path
);
5862 inode
->i_op
= &btrfs_symlink_inode_operations
;
5863 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
5864 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
5865 inode_set_bytes(inode
, name_len
);
5866 btrfs_i_size_write(inode
, name_len
- 1);
5867 err
= btrfs_update_inode(trans
, root
, inode
);
5872 nr
= trans
->blocks_used
;
5873 btrfs_end_transaction_throttle(trans
, root
);
5875 inode_dec_link_count(inode
);
5878 btrfs_btree_balance_dirty(root
, nr
);
5882 static int prealloc_file_range(struct inode
*inode
, u64 start
, u64 end
,
5883 u64 alloc_hint
, int mode
, loff_t actual_len
)
5885 struct btrfs_trans_handle
*trans
;
5886 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5887 struct btrfs_key ins
;
5888 u64 cur_offset
= start
;
5889 u64 num_bytes
= end
- start
;
5893 while (num_bytes
> 0) {
5894 trans
= btrfs_start_transaction(root
, 3);
5895 if (IS_ERR(trans
)) {
5896 ret
= PTR_ERR(trans
);
5900 ret
= btrfs_reserve_extent(trans
, root
, num_bytes
,
5901 root
->sectorsize
, 0, alloc_hint
,
5904 btrfs_end_transaction(trans
, root
);
5908 ret
= insert_reserved_file_extent(trans
, inode
,
5909 cur_offset
, ins
.objectid
,
5910 ins
.offset
, ins
.offset
,
5911 ins
.offset
, 0, 0, 0,
5912 BTRFS_FILE_EXTENT_PREALLOC
);
5914 btrfs_drop_extent_cache(inode
, cur_offset
,
5915 cur_offset
+ ins
.offset
-1, 0);
5917 num_bytes
-= ins
.offset
;
5918 cur_offset
+= ins
.offset
;
5919 alloc_hint
= ins
.objectid
+ ins
.offset
;
5921 inode
->i_ctime
= CURRENT_TIME
;
5922 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
5923 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
5924 (actual_len
> inode
->i_size
) &&
5925 (cur_offset
> inode
->i_size
)) {
5927 if (cur_offset
> actual_len
)
5928 i_size
= actual_len
;
5930 i_size
= cur_offset
;
5931 i_size_write(inode
, i_size
);
5932 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
5935 ret
= btrfs_update_inode(trans
, root
, inode
);
5938 btrfs_end_transaction(trans
, root
);
5943 static long btrfs_fallocate(struct inode
*inode
, int mode
,
5944 loff_t offset
, loff_t len
)
5946 struct extent_state
*cached_state
= NULL
;
5953 u64 mask
= BTRFS_I(inode
)->root
->sectorsize
- 1;
5954 struct extent_map
*em
;
5957 alloc_start
= offset
& ~mask
;
5958 alloc_end
= (offset
+ len
+ mask
) & ~mask
;
5961 * wait for ordered IO before we have any locks. We'll loop again
5962 * below with the locks held.
5964 btrfs_wait_ordered_range(inode
, alloc_start
, alloc_end
- alloc_start
);
5966 mutex_lock(&inode
->i_mutex
);
5967 if (alloc_start
> inode
->i_size
) {
5968 ret
= btrfs_cont_expand(inode
, alloc_start
);
5973 ret
= btrfs_check_data_free_space(BTRFS_I(inode
)->root
, inode
,
5974 alloc_end
- alloc_start
);
5978 locked_end
= alloc_end
- 1;
5980 struct btrfs_ordered_extent
*ordered
;
5982 /* the extent lock is ordered inside the running
5985 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, alloc_start
,
5986 locked_end
, 0, &cached_state
, GFP_NOFS
);
5987 ordered
= btrfs_lookup_first_ordered_extent(inode
,
5990 ordered
->file_offset
+ ordered
->len
> alloc_start
&&
5991 ordered
->file_offset
< alloc_end
) {
5992 btrfs_put_ordered_extent(ordered
);
5993 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
5994 alloc_start
, locked_end
,
5995 &cached_state
, GFP_NOFS
);
5997 * we can't wait on the range with the transaction
5998 * running or with the extent lock held
6000 btrfs_wait_ordered_range(inode
, alloc_start
,
6001 alloc_end
- alloc_start
);
6004 btrfs_put_ordered_extent(ordered
);
6009 cur_offset
= alloc_start
;
6011 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
6012 alloc_end
- cur_offset
, 0);
6013 BUG_ON(IS_ERR(em
) || !em
);
6014 last_byte
= min(extent_map_end(em
), alloc_end
);
6015 last_byte
= (last_byte
+ mask
) & ~mask
;
6016 if (em
->block_start
== EXTENT_MAP_HOLE
||
6017 (cur_offset
>= inode
->i_size
&&
6018 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
6019 ret
= prealloc_file_range(inode
,
6020 cur_offset
, last_byte
,
6021 alloc_hint
, mode
, offset
+len
);
6023 free_extent_map(em
);
6027 if (em
->block_start
<= EXTENT_MAP_LAST_BYTE
)
6028 alloc_hint
= em
->block_start
;
6029 free_extent_map(em
);
6031 cur_offset
= last_byte
;
6032 if (cur_offset
>= alloc_end
) {
6037 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, alloc_start
, locked_end
,
6038 &cached_state
, GFP_NOFS
);
6040 btrfs_free_reserved_data_space(BTRFS_I(inode
)->root
, inode
,
6041 alloc_end
- alloc_start
);
6043 mutex_unlock(&inode
->i_mutex
);
6047 static int btrfs_set_page_dirty(struct page
*page
)
6049 return __set_page_dirty_nobuffers(page
);
6052 static int btrfs_permission(struct inode
*inode
, int mask
)
6054 if ((BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
) && (mask
& MAY_WRITE
))
6056 return generic_permission(inode
, mask
, btrfs_check_acl
);
6059 static const struct inode_operations btrfs_dir_inode_operations
= {
6060 .getattr
= btrfs_getattr
,
6061 .lookup
= btrfs_lookup
,
6062 .create
= btrfs_create
,
6063 .unlink
= btrfs_unlink
,
6065 .mkdir
= btrfs_mkdir
,
6066 .rmdir
= btrfs_rmdir
,
6067 .rename
= btrfs_rename
,
6068 .symlink
= btrfs_symlink
,
6069 .setattr
= btrfs_setattr
,
6070 .mknod
= btrfs_mknod
,
6071 .setxattr
= btrfs_setxattr
,
6072 .getxattr
= btrfs_getxattr
,
6073 .listxattr
= btrfs_listxattr
,
6074 .removexattr
= btrfs_removexattr
,
6075 .permission
= btrfs_permission
,
6077 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
6078 .lookup
= btrfs_lookup
,
6079 .permission
= btrfs_permission
,
6082 static const struct file_operations btrfs_dir_file_operations
= {
6083 .llseek
= generic_file_llseek
,
6084 .read
= generic_read_dir
,
6085 .readdir
= btrfs_real_readdir
,
6086 .unlocked_ioctl
= btrfs_ioctl
,
6087 #ifdef CONFIG_COMPAT
6088 .compat_ioctl
= btrfs_ioctl
,
6090 .release
= btrfs_release_file
,
6091 .fsync
= btrfs_sync_file
,
6094 static struct extent_io_ops btrfs_extent_io_ops
= {
6095 .fill_delalloc
= run_delalloc_range
,
6096 .submit_bio_hook
= btrfs_submit_bio_hook
,
6097 .merge_bio_hook
= btrfs_merge_bio_hook
,
6098 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
6099 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
6100 .writepage_start_hook
= btrfs_writepage_start_hook
,
6101 .readpage_io_failed_hook
= btrfs_io_failed_hook
,
6102 .set_bit_hook
= btrfs_set_bit_hook
,
6103 .clear_bit_hook
= btrfs_clear_bit_hook
,
6104 .merge_extent_hook
= btrfs_merge_extent_hook
,
6105 .split_extent_hook
= btrfs_split_extent_hook
,
6109 * btrfs doesn't support the bmap operation because swapfiles
6110 * use bmap to make a mapping of extents in the file. They assume
6111 * these extents won't change over the life of the file and they
6112 * use the bmap result to do IO directly to the drive.
6114 * the btrfs bmap call would return logical addresses that aren't
6115 * suitable for IO and they also will change frequently as COW
6116 * operations happen. So, swapfile + btrfs == corruption.
6118 * For now we're avoiding this by dropping bmap.
6120 static const struct address_space_operations btrfs_aops
= {
6121 .readpage
= btrfs_readpage
,
6122 .writepage
= btrfs_writepage
,
6123 .writepages
= btrfs_writepages
,
6124 .readpages
= btrfs_readpages
,
6125 .sync_page
= block_sync_page
,
6126 .direct_IO
= btrfs_direct_IO
,
6127 .invalidatepage
= btrfs_invalidatepage
,
6128 .releasepage
= btrfs_releasepage
,
6129 .set_page_dirty
= btrfs_set_page_dirty
,
6130 .error_remove_page
= generic_error_remove_page
,
6133 static const struct address_space_operations btrfs_symlink_aops
= {
6134 .readpage
= btrfs_readpage
,
6135 .writepage
= btrfs_writepage
,
6136 .invalidatepage
= btrfs_invalidatepage
,
6137 .releasepage
= btrfs_releasepage
,
6140 static const struct inode_operations btrfs_file_inode_operations
= {
6141 .truncate
= btrfs_truncate
,
6142 .getattr
= btrfs_getattr
,
6143 .setattr
= btrfs_setattr
,
6144 .setxattr
= btrfs_setxattr
,
6145 .getxattr
= btrfs_getxattr
,
6146 .listxattr
= btrfs_listxattr
,
6147 .removexattr
= btrfs_removexattr
,
6148 .permission
= btrfs_permission
,
6149 .fallocate
= btrfs_fallocate
,
6150 .fiemap
= btrfs_fiemap
,
6152 static const struct inode_operations btrfs_special_inode_operations
= {
6153 .getattr
= btrfs_getattr
,
6154 .setattr
= btrfs_setattr
,
6155 .permission
= btrfs_permission
,
6156 .setxattr
= btrfs_setxattr
,
6157 .getxattr
= btrfs_getxattr
,
6158 .listxattr
= btrfs_listxattr
,
6159 .removexattr
= btrfs_removexattr
,
6161 static const struct inode_operations btrfs_symlink_inode_operations
= {
6162 .readlink
= generic_readlink
,
6163 .follow_link
= page_follow_link_light
,
6164 .put_link
= page_put_link
,
6165 .permission
= btrfs_permission
,
6166 .setxattr
= btrfs_setxattr
,
6167 .getxattr
= btrfs_getxattr
,
6168 .listxattr
= btrfs_listxattr
,
6169 .removexattr
= btrfs_removexattr
,
6172 const struct dentry_operations btrfs_dentry_operations
= {
6173 .d_delete
= btrfs_dentry_delete
,