2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/statfs.h>
34 #include <linux/compat.h>
35 #include <linux/bit_spinlock.h>
36 #include <linux/xattr.h>
37 #include <linux/posix_acl.h>
38 #include <linux/falloc.h>
39 #include <linux/slab.h>
40 #include <linux/ratelimit.h>
41 #include <linux/mount.h>
45 #include "transaction.h"
46 #include "btrfs_inode.h"
48 #include "print-tree.h"
49 #include "ordered-data.h"
53 #include "compression.h"
55 #include "free-space-cache.h"
56 #include "inode-map.h"
58 struct btrfs_iget_args
{
60 struct btrfs_root
*root
;
63 static const struct inode_operations btrfs_dir_inode_operations
;
64 static const struct inode_operations btrfs_symlink_inode_operations
;
65 static const struct inode_operations btrfs_dir_ro_inode_operations
;
66 static const struct inode_operations btrfs_special_inode_operations
;
67 static const struct inode_operations btrfs_file_inode_operations
;
68 static const struct address_space_operations btrfs_aops
;
69 static const struct address_space_operations btrfs_symlink_aops
;
70 static const struct file_operations btrfs_dir_file_operations
;
71 static struct extent_io_ops btrfs_extent_io_ops
;
73 static struct kmem_cache
*btrfs_inode_cachep
;
74 struct kmem_cache
*btrfs_trans_handle_cachep
;
75 struct kmem_cache
*btrfs_transaction_cachep
;
76 struct kmem_cache
*btrfs_path_cachep
;
77 struct kmem_cache
*btrfs_free_space_cachep
;
80 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
81 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
82 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
83 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
84 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
85 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
86 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
87 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
90 static int btrfs_setsize(struct inode
*inode
, loff_t newsize
);
91 static int btrfs_truncate(struct inode
*inode
);
92 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
);
93 static noinline
int cow_file_range(struct inode
*inode
,
94 struct page
*locked_page
,
95 u64 start
, u64 end
, int *page_started
,
96 unsigned long *nr_written
, int unlock
);
97 static noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
98 struct btrfs_root
*root
, struct inode
*inode
);
100 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
101 struct inode
*inode
, struct inode
*dir
,
102 const struct qstr
*qstr
)
106 err
= btrfs_init_acl(trans
, inode
, dir
);
108 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
113 * this does all the hard work for inserting an inline extent into
114 * the btree. The caller should have done a btrfs_drop_extents so that
115 * no overlapping inline items exist in the btree
117 static noinline
int insert_inline_extent(struct btrfs_trans_handle
*trans
,
118 struct btrfs_root
*root
, struct inode
*inode
,
119 u64 start
, size_t size
, size_t compressed_size
,
121 struct page
**compressed_pages
)
123 struct btrfs_key key
;
124 struct btrfs_path
*path
;
125 struct extent_buffer
*leaf
;
126 struct page
*page
= NULL
;
129 struct btrfs_file_extent_item
*ei
;
132 size_t cur_size
= size
;
134 unsigned long offset
;
136 if (compressed_size
&& compressed_pages
)
137 cur_size
= compressed_size
;
139 path
= btrfs_alloc_path();
143 path
->leave_spinning
= 1;
145 key
.objectid
= btrfs_ino(inode
);
147 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
148 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
150 inode_add_bytes(inode
, size
);
151 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
157 leaf
= path
->nodes
[0];
158 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
159 struct btrfs_file_extent_item
);
160 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
161 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
162 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
163 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
164 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
165 ptr
= btrfs_file_extent_inline_start(ei
);
167 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
170 while (compressed_size
> 0) {
171 cpage
= compressed_pages
[i
];
172 cur_size
= min_t(unsigned long, compressed_size
,
175 kaddr
= kmap_atomic(cpage
);
176 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
177 kunmap_atomic(kaddr
);
181 compressed_size
-= cur_size
;
183 btrfs_set_file_extent_compression(leaf
, ei
,
186 page
= find_get_page(inode
->i_mapping
,
187 start
>> PAGE_CACHE_SHIFT
);
188 btrfs_set_file_extent_compression(leaf
, ei
, 0);
189 kaddr
= kmap_atomic(page
);
190 offset
= start
& (PAGE_CACHE_SIZE
- 1);
191 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
192 kunmap_atomic(kaddr
);
193 page_cache_release(page
);
195 btrfs_mark_buffer_dirty(leaf
);
196 btrfs_free_path(path
);
199 * we're an inline extent, so nobody can
200 * extend the file past i_size without locking
201 * a page we already have locked.
203 * We must do any isize and inode updates
204 * before we unlock the pages. Otherwise we
205 * could end up racing with unlink.
207 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
208 ret
= btrfs_update_inode(trans
, root
, inode
);
212 btrfs_free_path(path
);
218 * conditionally insert an inline extent into the file. This
219 * does the checks required to make sure the data is small enough
220 * to fit as an inline extent.
222 static noinline
int cow_file_range_inline(struct btrfs_trans_handle
*trans
,
223 struct btrfs_root
*root
,
224 struct inode
*inode
, u64 start
, u64 end
,
225 size_t compressed_size
, int compress_type
,
226 struct page
**compressed_pages
)
228 u64 isize
= i_size_read(inode
);
229 u64 actual_end
= min(end
+ 1, isize
);
230 u64 inline_len
= actual_end
- start
;
231 u64 aligned_end
= (end
+ root
->sectorsize
- 1) &
232 ~((u64
)root
->sectorsize
- 1);
234 u64 data_len
= inline_len
;
238 data_len
= compressed_size
;
241 actual_end
>= PAGE_CACHE_SIZE
||
242 data_len
>= BTRFS_MAX_INLINE_DATA_SIZE(root
) ||
244 (actual_end
& (root
->sectorsize
- 1)) == 0) ||
246 data_len
> root
->fs_info
->max_inline
) {
250 ret
= btrfs_drop_extents(trans
, inode
, start
, aligned_end
,
255 if (isize
> actual_end
)
256 inline_len
= min_t(u64
, isize
, actual_end
);
257 ret
= insert_inline_extent(trans
, root
, inode
, start
,
258 inline_len
, compressed_size
,
259 compress_type
, compressed_pages
);
261 btrfs_abort_transaction(trans
, root
, ret
);
264 btrfs_delalloc_release_metadata(inode
, end
+ 1 - start
);
265 btrfs_drop_extent_cache(inode
, start
, aligned_end
- 1, 0);
269 struct async_extent
{
274 unsigned long nr_pages
;
276 struct list_head list
;
281 struct btrfs_root
*root
;
282 struct page
*locked_page
;
285 struct list_head extents
;
286 struct btrfs_work work
;
289 static noinline
int add_async_extent(struct async_cow
*cow
,
290 u64 start
, u64 ram_size
,
293 unsigned long nr_pages
,
296 struct async_extent
*async_extent
;
298 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
299 BUG_ON(!async_extent
); /* -ENOMEM */
300 async_extent
->start
= start
;
301 async_extent
->ram_size
= ram_size
;
302 async_extent
->compressed_size
= compressed_size
;
303 async_extent
->pages
= pages
;
304 async_extent
->nr_pages
= nr_pages
;
305 async_extent
->compress_type
= compress_type
;
306 list_add_tail(&async_extent
->list
, &cow
->extents
);
311 * we create compressed extents in two phases. The first
312 * phase compresses a range of pages that have already been
313 * locked (both pages and state bits are locked).
315 * This is done inside an ordered work queue, and the compression
316 * is spread across many cpus. The actual IO submission is step
317 * two, and the ordered work queue takes care of making sure that
318 * happens in the same order things were put onto the queue by
319 * writepages and friends.
321 * If this code finds it can't get good compression, it puts an
322 * entry onto the work queue to write the uncompressed bytes. This
323 * makes sure that both compressed inodes and uncompressed inodes
324 * are written in the same order that pdflush sent them down.
326 static noinline
int compress_file_range(struct inode
*inode
,
327 struct page
*locked_page
,
329 struct async_cow
*async_cow
,
332 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
333 struct btrfs_trans_handle
*trans
;
335 u64 blocksize
= root
->sectorsize
;
337 u64 isize
= i_size_read(inode
);
339 struct page
**pages
= NULL
;
340 unsigned long nr_pages
;
341 unsigned long nr_pages_ret
= 0;
342 unsigned long total_compressed
= 0;
343 unsigned long total_in
= 0;
344 unsigned long max_compressed
= 128 * 1024;
345 unsigned long max_uncompressed
= 128 * 1024;
348 int compress_type
= root
->fs_info
->compress_type
;
350 /* if this is a small write inside eof, kick off a defrag */
351 if ((end
- start
+ 1) < 16 * 1024 &&
352 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
353 btrfs_add_inode_defrag(NULL
, inode
);
355 actual_end
= min_t(u64
, isize
, end
+ 1);
358 nr_pages
= (end
>> PAGE_CACHE_SHIFT
) - (start
>> PAGE_CACHE_SHIFT
) + 1;
359 nr_pages
= min(nr_pages
, (128 * 1024UL) / PAGE_CACHE_SIZE
);
362 * we don't want to send crud past the end of i_size through
363 * compression, that's just a waste of CPU time. So, if the
364 * end of the file is before the start of our current
365 * requested range of bytes, we bail out to the uncompressed
366 * cleanup code that can deal with all of this.
368 * It isn't really the fastest way to fix things, but this is a
369 * very uncommon corner.
371 if (actual_end
<= start
)
372 goto cleanup_and_bail_uncompressed
;
374 total_compressed
= actual_end
- start
;
376 /* we want to make sure that amount of ram required to uncompress
377 * an extent is reasonable, so we limit the total size in ram
378 * of a compressed extent to 128k. This is a crucial number
379 * because it also controls how easily we can spread reads across
380 * cpus for decompression.
382 * We also want to make sure the amount of IO required to do
383 * a random read is reasonably small, so we limit the size of
384 * a compressed extent to 128k.
386 total_compressed
= min(total_compressed
, max_uncompressed
);
387 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
388 num_bytes
= max(blocksize
, num_bytes
);
393 * we do compression for mount -o compress and when the
394 * inode has not been flagged as nocompress. This flag can
395 * change at any time if we discover bad compression ratios.
397 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
) &&
398 (btrfs_test_opt(root
, COMPRESS
) ||
399 (BTRFS_I(inode
)->force_compress
) ||
400 (BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
))) {
402 pages
= kzalloc(sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
404 /* just bail out to the uncompressed code */
408 if (BTRFS_I(inode
)->force_compress
)
409 compress_type
= BTRFS_I(inode
)->force_compress
;
411 ret
= btrfs_compress_pages(compress_type
,
412 inode
->i_mapping
, start
,
413 total_compressed
, pages
,
414 nr_pages
, &nr_pages_ret
,
420 unsigned long offset
= total_compressed
&
421 (PAGE_CACHE_SIZE
- 1);
422 struct page
*page
= pages
[nr_pages_ret
- 1];
425 /* zero the tail end of the last page, we might be
426 * sending it down to disk
429 kaddr
= kmap_atomic(page
);
430 memset(kaddr
+ offset
, 0,
431 PAGE_CACHE_SIZE
- offset
);
432 kunmap_atomic(kaddr
);
439 trans
= btrfs_join_transaction(root
);
441 ret
= PTR_ERR(trans
);
443 goto cleanup_and_out
;
445 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
447 /* lets try to make an inline extent */
448 if (ret
|| total_in
< (actual_end
- start
)) {
449 /* we didn't compress the entire range, try
450 * to make an uncompressed inline extent.
452 ret
= cow_file_range_inline(trans
, root
, inode
,
453 start
, end
, 0, 0, NULL
);
455 /* try making a compressed inline extent */
456 ret
= cow_file_range_inline(trans
, root
, inode
,
459 compress_type
, pages
);
463 * inline extent creation worked or returned error,
464 * we don't need to create any more async work items.
465 * Unlock and free up our temp pages.
467 extent_clear_unlock_delalloc(inode
,
468 &BTRFS_I(inode
)->io_tree
,
470 EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
471 EXTENT_CLEAR_DELALLOC
|
472 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
);
474 btrfs_end_transaction(trans
, root
);
477 btrfs_end_transaction(trans
, root
);
482 * we aren't doing an inline extent round the compressed size
483 * up to a block size boundary so the allocator does sane
486 total_compressed
= (total_compressed
+ blocksize
- 1) &
490 * one last check to make sure the compression is really a
491 * win, compare the page count read with the blocks on disk
493 total_in
= (total_in
+ PAGE_CACHE_SIZE
- 1) &
494 ~(PAGE_CACHE_SIZE
- 1);
495 if (total_compressed
>= total_in
) {
498 num_bytes
= total_in
;
501 if (!will_compress
&& pages
) {
503 * the compression code ran but failed to make things smaller,
504 * free any pages it allocated and our page pointer array
506 for (i
= 0; i
< nr_pages_ret
; i
++) {
507 WARN_ON(pages
[i
]->mapping
);
508 page_cache_release(pages
[i
]);
512 total_compressed
= 0;
515 /* flag the file so we don't compress in the future */
516 if (!btrfs_test_opt(root
, FORCE_COMPRESS
) &&
517 !(BTRFS_I(inode
)->force_compress
)) {
518 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
524 /* the async work queues will take care of doing actual
525 * allocation on disk for these compressed pages,
526 * and will submit them to the elevator.
528 add_async_extent(async_cow
, start
, num_bytes
,
529 total_compressed
, pages
, nr_pages_ret
,
532 if (start
+ num_bytes
< end
) {
539 cleanup_and_bail_uncompressed
:
541 * No compression, but we still need to write the pages in
542 * the file we've been given so far. redirty the locked
543 * page if it corresponds to our extent and set things up
544 * for the async work queue to run cow_file_range to do
545 * the normal delalloc dance
547 if (page_offset(locked_page
) >= start
&&
548 page_offset(locked_page
) <= end
) {
549 __set_page_dirty_nobuffers(locked_page
);
550 /* unlocked later on in the async handlers */
552 add_async_extent(async_cow
, start
, end
- start
+ 1,
553 0, NULL
, 0, BTRFS_COMPRESS_NONE
);
561 for (i
= 0; i
< nr_pages_ret
; i
++) {
562 WARN_ON(pages
[i
]->mapping
);
563 page_cache_release(pages
[i
]);
570 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
572 EXTENT_CLEAR_UNLOCK_PAGE
|
574 EXTENT_CLEAR_DELALLOC
|
575 EXTENT_SET_WRITEBACK
|
576 EXTENT_END_WRITEBACK
);
577 if (!trans
|| IS_ERR(trans
))
578 btrfs_error(root
->fs_info
, ret
, "Failed to join transaction");
580 btrfs_abort_transaction(trans
, root
, ret
);
585 * phase two of compressed writeback. This is the ordered portion
586 * of the code, which only gets called in the order the work was
587 * queued. We walk all the async extents created by compress_file_range
588 * and send them down to the disk.
590 static noinline
int submit_compressed_extents(struct inode
*inode
,
591 struct async_cow
*async_cow
)
593 struct async_extent
*async_extent
;
595 struct btrfs_trans_handle
*trans
;
596 struct btrfs_key ins
;
597 struct extent_map
*em
;
598 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
599 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
600 struct extent_io_tree
*io_tree
;
603 if (list_empty(&async_cow
->extents
))
607 while (!list_empty(&async_cow
->extents
)) {
608 async_extent
= list_entry(async_cow
->extents
.next
,
609 struct async_extent
, list
);
610 list_del(&async_extent
->list
);
612 io_tree
= &BTRFS_I(inode
)->io_tree
;
615 /* did the compression code fall back to uncompressed IO? */
616 if (!async_extent
->pages
) {
617 int page_started
= 0;
618 unsigned long nr_written
= 0;
620 lock_extent(io_tree
, async_extent
->start
,
621 async_extent
->start
+
622 async_extent
->ram_size
- 1);
624 /* allocate blocks */
625 ret
= cow_file_range(inode
, async_cow
->locked_page
,
627 async_extent
->start
+
628 async_extent
->ram_size
- 1,
629 &page_started
, &nr_written
, 0);
634 * if page_started, cow_file_range inserted an
635 * inline extent and took care of all the unlocking
636 * and IO for us. Otherwise, we need to submit
637 * all those pages down to the drive.
639 if (!page_started
&& !ret
)
640 extent_write_locked_range(io_tree
,
641 inode
, async_extent
->start
,
642 async_extent
->start
+
643 async_extent
->ram_size
- 1,
651 lock_extent(io_tree
, async_extent
->start
,
652 async_extent
->start
+ async_extent
->ram_size
- 1);
654 trans
= btrfs_join_transaction(root
);
656 ret
= PTR_ERR(trans
);
658 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
659 ret
= btrfs_reserve_extent(trans
, root
,
660 async_extent
->compressed_size
,
661 async_extent
->compressed_size
,
662 0, alloc_hint
, &ins
, 1);
664 btrfs_abort_transaction(trans
, root
, ret
);
665 btrfs_end_transaction(trans
, root
);
670 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
671 WARN_ON(async_extent
->pages
[i
]->mapping
);
672 page_cache_release(async_extent
->pages
[i
]);
674 kfree(async_extent
->pages
);
675 async_extent
->nr_pages
= 0;
676 async_extent
->pages
= NULL
;
677 unlock_extent(io_tree
, async_extent
->start
,
678 async_extent
->start
+
679 async_extent
->ram_size
- 1);
682 goto out_free
; /* JDM: Requeue? */
686 * here we're doing allocation and writeback of the
689 btrfs_drop_extent_cache(inode
, async_extent
->start
,
690 async_extent
->start
+
691 async_extent
->ram_size
- 1, 0);
693 em
= alloc_extent_map();
694 BUG_ON(!em
); /* -ENOMEM */
695 em
->start
= async_extent
->start
;
696 em
->len
= async_extent
->ram_size
;
697 em
->orig_start
= em
->start
;
699 em
->block_start
= ins
.objectid
;
700 em
->block_len
= ins
.offset
;
701 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
702 em
->compress_type
= async_extent
->compress_type
;
703 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
704 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
707 write_lock(&em_tree
->lock
);
708 ret
= add_extent_mapping(em_tree
, em
);
709 write_unlock(&em_tree
->lock
);
710 if (ret
!= -EEXIST
) {
714 btrfs_drop_extent_cache(inode
, async_extent
->start
,
715 async_extent
->start
+
716 async_extent
->ram_size
- 1, 0);
719 ret
= btrfs_add_ordered_extent_compress(inode
,
722 async_extent
->ram_size
,
724 BTRFS_ORDERED_COMPRESSED
,
725 async_extent
->compress_type
);
726 BUG_ON(ret
); /* -ENOMEM */
729 * clear dirty, set writeback and unlock the pages.
731 extent_clear_unlock_delalloc(inode
,
732 &BTRFS_I(inode
)->io_tree
,
734 async_extent
->start
+
735 async_extent
->ram_size
- 1,
736 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
737 EXTENT_CLEAR_UNLOCK
|
738 EXTENT_CLEAR_DELALLOC
|
739 EXTENT_CLEAR_DIRTY
| EXTENT_SET_WRITEBACK
);
741 ret
= btrfs_submit_compressed_write(inode
,
743 async_extent
->ram_size
,
745 ins
.offset
, async_extent
->pages
,
746 async_extent
->nr_pages
);
748 BUG_ON(ret
); /* -ENOMEM */
749 alloc_hint
= ins
.objectid
+ ins
.offset
;
761 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
764 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
765 struct extent_map
*em
;
768 read_lock(&em_tree
->lock
);
769 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
772 * if block start isn't an actual block number then find the
773 * first block in this inode and use that as a hint. If that
774 * block is also bogus then just don't worry about it.
776 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
778 em
= search_extent_mapping(em_tree
, 0, 0);
779 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
780 alloc_hint
= em
->block_start
;
784 alloc_hint
= em
->block_start
;
788 read_unlock(&em_tree
->lock
);
794 * when extent_io.c finds a delayed allocation range in the file,
795 * the call backs end up in this code. The basic idea is to
796 * allocate extents on disk for the range, and create ordered data structs
797 * in ram to track those extents.
799 * locked_page is the page that writepage had locked already. We use
800 * it to make sure we don't do extra locks or unlocks.
802 * *page_started is set to one if we unlock locked_page and do everything
803 * required to start IO on it. It may be clean and already done with
806 static noinline
int cow_file_range(struct inode
*inode
,
807 struct page
*locked_page
,
808 u64 start
, u64 end
, int *page_started
,
809 unsigned long *nr_written
,
812 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
813 struct btrfs_trans_handle
*trans
;
816 unsigned long ram_size
;
819 u64 blocksize
= root
->sectorsize
;
820 struct btrfs_key ins
;
821 struct extent_map
*em
;
822 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
825 BUG_ON(btrfs_is_free_space_inode(root
, inode
));
826 trans
= btrfs_join_transaction(root
);
828 extent_clear_unlock_delalloc(inode
,
829 &BTRFS_I(inode
)->io_tree
,
831 EXTENT_CLEAR_UNLOCK_PAGE
|
832 EXTENT_CLEAR_UNLOCK
|
833 EXTENT_CLEAR_DELALLOC
|
835 EXTENT_SET_WRITEBACK
|
836 EXTENT_END_WRITEBACK
);
837 return PTR_ERR(trans
);
839 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
841 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
842 num_bytes
= max(blocksize
, num_bytes
);
843 disk_num_bytes
= num_bytes
;
846 /* if this is a small write inside eof, kick off defrag */
847 if (num_bytes
< 64 * 1024 &&
848 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
849 btrfs_add_inode_defrag(trans
, inode
);
852 /* lets try to make an inline extent */
853 ret
= cow_file_range_inline(trans
, root
, inode
,
854 start
, end
, 0, 0, NULL
);
856 extent_clear_unlock_delalloc(inode
,
857 &BTRFS_I(inode
)->io_tree
,
859 EXTENT_CLEAR_UNLOCK_PAGE
|
860 EXTENT_CLEAR_UNLOCK
|
861 EXTENT_CLEAR_DELALLOC
|
863 EXTENT_SET_WRITEBACK
|
864 EXTENT_END_WRITEBACK
);
866 *nr_written
= *nr_written
+
867 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
870 } else if (ret
< 0) {
871 btrfs_abort_transaction(trans
, root
, ret
);
876 BUG_ON(disk_num_bytes
>
877 btrfs_super_total_bytes(root
->fs_info
->super_copy
));
879 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
880 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
882 while (disk_num_bytes
> 0) {
885 cur_alloc_size
= disk_num_bytes
;
886 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
887 root
->sectorsize
, 0, alloc_hint
,
890 btrfs_abort_transaction(trans
, root
, ret
);
894 em
= alloc_extent_map();
895 BUG_ON(!em
); /* -ENOMEM */
897 em
->orig_start
= em
->start
;
898 ram_size
= ins
.offset
;
899 em
->len
= ins
.offset
;
901 em
->block_start
= ins
.objectid
;
902 em
->block_len
= ins
.offset
;
903 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
904 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
907 write_lock(&em_tree
->lock
);
908 ret
= add_extent_mapping(em_tree
, em
);
909 write_unlock(&em_tree
->lock
);
910 if (ret
!= -EEXIST
) {
914 btrfs_drop_extent_cache(inode
, start
,
915 start
+ ram_size
- 1, 0);
918 cur_alloc_size
= ins
.offset
;
919 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
920 ram_size
, cur_alloc_size
, 0);
921 BUG_ON(ret
); /* -ENOMEM */
923 if (root
->root_key
.objectid
==
924 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
925 ret
= btrfs_reloc_clone_csums(inode
, start
,
928 btrfs_abort_transaction(trans
, root
, ret
);
933 if (disk_num_bytes
< cur_alloc_size
)
936 /* we're not doing compressed IO, don't unlock the first
937 * page (which the caller expects to stay locked), don't
938 * clear any dirty bits and don't set any writeback bits
940 * Do set the Private2 bit so we know this page was properly
941 * setup for writepage
943 op
= unlock
? EXTENT_CLEAR_UNLOCK_PAGE
: 0;
944 op
|= EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
947 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
948 start
, start
+ ram_size
- 1,
950 disk_num_bytes
-= cur_alloc_size
;
951 num_bytes
-= cur_alloc_size
;
952 alloc_hint
= ins
.objectid
+ ins
.offset
;
953 start
+= cur_alloc_size
;
957 btrfs_end_transaction(trans
, root
);
961 extent_clear_unlock_delalloc(inode
,
962 &BTRFS_I(inode
)->io_tree
,
964 EXTENT_CLEAR_UNLOCK_PAGE
|
965 EXTENT_CLEAR_UNLOCK
|
966 EXTENT_CLEAR_DELALLOC
|
968 EXTENT_SET_WRITEBACK
|
969 EXTENT_END_WRITEBACK
);
975 * work queue call back to started compression on a file and pages
977 static noinline
void async_cow_start(struct btrfs_work
*work
)
979 struct async_cow
*async_cow
;
981 async_cow
= container_of(work
, struct async_cow
, work
);
983 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
984 async_cow
->start
, async_cow
->end
, async_cow
,
987 async_cow
->inode
= NULL
;
991 * work queue call back to submit previously compressed pages
993 static noinline
void async_cow_submit(struct btrfs_work
*work
)
995 struct async_cow
*async_cow
;
996 struct btrfs_root
*root
;
997 unsigned long nr_pages
;
999 async_cow
= container_of(work
, struct async_cow
, work
);
1001 root
= async_cow
->root
;
1002 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
1005 atomic_sub(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
1007 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
1009 waitqueue_active(&root
->fs_info
->async_submit_wait
))
1010 wake_up(&root
->fs_info
->async_submit_wait
);
1012 if (async_cow
->inode
)
1013 submit_compressed_extents(async_cow
->inode
, async_cow
);
1016 static noinline
void async_cow_free(struct btrfs_work
*work
)
1018 struct async_cow
*async_cow
;
1019 async_cow
= container_of(work
, struct async_cow
, work
);
1023 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
1024 u64 start
, u64 end
, int *page_started
,
1025 unsigned long *nr_written
)
1027 struct async_cow
*async_cow
;
1028 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1029 unsigned long nr_pages
;
1031 int limit
= 10 * 1024 * 1042;
1033 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
1034 1, 0, NULL
, GFP_NOFS
);
1035 while (start
< end
) {
1036 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
1037 BUG_ON(!async_cow
); /* -ENOMEM */
1038 async_cow
->inode
= inode
;
1039 async_cow
->root
= root
;
1040 async_cow
->locked_page
= locked_page
;
1041 async_cow
->start
= start
;
1043 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
1046 cur_end
= min(end
, start
+ 512 * 1024 - 1);
1048 async_cow
->end
= cur_end
;
1049 INIT_LIST_HEAD(&async_cow
->extents
);
1051 async_cow
->work
.func
= async_cow_start
;
1052 async_cow
->work
.ordered_func
= async_cow_submit
;
1053 async_cow
->work
.ordered_free
= async_cow_free
;
1054 async_cow
->work
.flags
= 0;
1056 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
1058 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
1060 btrfs_queue_worker(&root
->fs_info
->delalloc_workers
,
1063 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
1064 wait_event(root
->fs_info
->async_submit_wait
,
1065 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
1069 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
1070 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1071 wait_event(root
->fs_info
->async_submit_wait
,
1072 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
1076 *nr_written
+= nr_pages
;
1077 start
= cur_end
+ 1;
1083 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
1084 u64 bytenr
, u64 num_bytes
)
1087 struct btrfs_ordered_sum
*sums
;
1090 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
1091 bytenr
+ num_bytes
- 1, &list
, 0);
1092 if (ret
== 0 && list_empty(&list
))
1095 while (!list_empty(&list
)) {
1096 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1097 list_del(&sums
->list
);
1104 * when nowcow writeback call back. This checks for snapshots or COW copies
1105 * of the extents that exist in the file, and COWs the file as required.
1107 * If no cow copies or snapshots exist, we write directly to the existing
1110 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1111 struct page
*locked_page
,
1112 u64 start
, u64 end
, int *page_started
, int force
,
1113 unsigned long *nr_written
)
1115 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1116 struct btrfs_trans_handle
*trans
;
1117 struct extent_buffer
*leaf
;
1118 struct btrfs_path
*path
;
1119 struct btrfs_file_extent_item
*fi
;
1120 struct btrfs_key found_key
;
1133 u64 ino
= btrfs_ino(inode
);
1135 path
= btrfs_alloc_path();
1139 nolock
= btrfs_is_free_space_inode(root
, inode
);
1142 trans
= btrfs_join_transaction_nolock(root
);
1144 trans
= btrfs_join_transaction(root
);
1146 if (IS_ERR(trans
)) {
1147 btrfs_free_path(path
);
1148 return PTR_ERR(trans
);
1151 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1153 cow_start
= (u64
)-1;
1156 ret
= btrfs_lookup_file_extent(trans
, root
, path
, ino
,
1159 btrfs_abort_transaction(trans
, root
, ret
);
1162 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1163 leaf
= path
->nodes
[0];
1164 btrfs_item_key_to_cpu(leaf
, &found_key
,
1165 path
->slots
[0] - 1);
1166 if (found_key
.objectid
== ino
&&
1167 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1172 leaf
= path
->nodes
[0];
1173 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1174 ret
= btrfs_next_leaf(root
, path
);
1176 btrfs_abort_transaction(trans
, root
, ret
);
1181 leaf
= path
->nodes
[0];
1187 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1189 if (found_key
.objectid
> ino
||
1190 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1191 found_key
.offset
> end
)
1194 if (found_key
.offset
> cur_offset
) {
1195 extent_end
= found_key
.offset
;
1200 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1201 struct btrfs_file_extent_item
);
1202 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1204 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1205 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1206 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1207 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1208 extent_end
= found_key
.offset
+
1209 btrfs_file_extent_num_bytes(leaf
, fi
);
1210 if (extent_end
<= start
) {
1214 if (disk_bytenr
== 0)
1216 if (btrfs_file_extent_compression(leaf
, fi
) ||
1217 btrfs_file_extent_encryption(leaf
, fi
) ||
1218 btrfs_file_extent_other_encoding(leaf
, fi
))
1220 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1222 if (btrfs_extent_readonly(root
, disk_bytenr
))
1224 if (btrfs_cross_ref_exist(trans
, root
, ino
,
1226 extent_offset
, disk_bytenr
))
1228 disk_bytenr
+= extent_offset
;
1229 disk_bytenr
+= cur_offset
- found_key
.offset
;
1230 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1232 * force cow if csum exists in the range.
1233 * this ensure that csum for a given extent are
1234 * either valid or do not exist.
1236 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1239 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1240 extent_end
= found_key
.offset
+
1241 btrfs_file_extent_inline_len(leaf
, fi
);
1242 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1247 if (extent_end
<= start
) {
1252 if (cow_start
== (u64
)-1)
1253 cow_start
= cur_offset
;
1254 cur_offset
= extent_end
;
1255 if (cur_offset
> end
)
1261 btrfs_release_path(path
);
1262 if (cow_start
!= (u64
)-1) {
1263 ret
= cow_file_range(inode
, locked_page
, cow_start
,
1264 found_key
.offset
- 1, page_started
,
1267 btrfs_abort_transaction(trans
, root
, ret
);
1270 cow_start
= (u64
)-1;
1273 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1274 struct extent_map
*em
;
1275 struct extent_map_tree
*em_tree
;
1276 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1277 em
= alloc_extent_map();
1278 BUG_ON(!em
); /* -ENOMEM */
1279 em
->start
= cur_offset
;
1280 em
->orig_start
= em
->start
;
1281 em
->len
= num_bytes
;
1282 em
->block_len
= num_bytes
;
1283 em
->block_start
= disk_bytenr
;
1284 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1285 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1287 write_lock(&em_tree
->lock
);
1288 ret
= add_extent_mapping(em_tree
, em
);
1289 write_unlock(&em_tree
->lock
);
1290 if (ret
!= -EEXIST
) {
1291 free_extent_map(em
);
1294 btrfs_drop_extent_cache(inode
, em
->start
,
1295 em
->start
+ em
->len
- 1, 0);
1297 type
= BTRFS_ORDERED_PREALLOC
;
1299 type
= BTRFS_ORDERED_NOCOW
;
1302 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1303 num_bytes
, num_bytes
, type
);
1304 BUG_ON(ret
); /* -ENOMEM */
1306 if (root
->root_key
.objectid
==
1307 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1308 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1311 btrfs_abort_transaction(trans
, root
, ret
);
1316 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
1317 cur_offset
, cur_offset
+ num_bytes
- 1,
1318 locked_page
, EXTENT_CLEAR_UNLOCK_PAGE
|
1319 EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
1320 EXTENT_SET_PRIVATE2
);
1321 cur_offset
= extent_end
;
1322 if (cur_offset
> end
)
1325 btrfs_release_path(path
);
1327 if (cur_offset
<= end
&& cow_start
== (u64
)-1)
1328 cow_start
= cur_offset
;
1329 if (cow_start
!= (u64
)-1) {
1330 ret
= cow_file_range(inode
, locked_page
, cow_start
, end
,
1331 page_started
, nr_written
, 1);
1333 btrfs_abort_transaction(trans
, root
, ret
);
1340 err
= btrfs_end_transaction_nolock(trans
, root
);
1342 err
= btrfs_end_transaction(trans
, root
);
1347 btrfs_free_path(path
);
1352 * extent_io.c call back to do delayed allocation processing
1354 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1355 u64 start
, u64 end
, int *page_started
,
1356 unsigned long *nr_written
)
1359 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1361 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
)
1362 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1363 page_started
, 1, nr_written
);
1364 else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
)
1365 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1366 page_started
, 0, nr_written
);
1367 else if (!btrfs_test_opt(root
, COMPRESS
) &&
1368 !(BTRFS_I(inode
)->force_compress
) &&
1369 !(BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
))
1370 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1371 page_started
, nr_written
, 1);
1373 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1374 page_started
, nr_written
);
1378 static void btrfs_split_extent_hook(struct inode
*inode
,
1379 struct extent_state
*orig
, u64 split
)
1381 /* not delalloc, ignore it */
1382 if (!(orig
->state
& EXTENT_DELALLOC
))
1385 spin_lock(&BTRFS_I(inode
)->lock
);
1386 BTRFS_I(inode
)->outstanding_extents
++;
1387 spin_unlock(&BTRFS_I(inode
)->lock
);
1391 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1392 * extents so we can keep track of new extents that are just merged onto old
1393 * extents, such as when we are doing sequential writes, so we can properly
1394 * account for the metadata space we'll need.
1396 static void btrfs_merge_extent_hook(struct inode
*inode
,
1397 struct extent_state
*new,
1398 struct extent_state
*other
)
1400 /* not delalloc, ignore it */
1401 if (!(other
->state
& EXTENT_DELALLOC
))
1404 spin_lock(&BTRFS_I(inode
)->lock
);
1405 BTRFS_I(inode
)->outstanding_extents
--;
1406 spin_unlock(&BTRFS_I(inode
)->lock
);
1410 * extent_io.c set_bit_hook, used to track delayed allocation
1411 * bytes in this file, and to maintain the list of inodes that
1412 * have pending delalloc work to be done.
1414 static void btrfs_set_bit_hook(struct inode
*inode
,
1415 struct extent_state
*state
, int *bits
)
1419 * set_bit and clear bit hooks normally require _irqsave/restore
1420 * but in this case, we are only testing for the DELALLOC
1421 * bit, which is only set or cleared with irqs on
1423 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1424 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1425 u64 len
= state
->end
+ 1 - state
->start
;
1426 bool do_list
= !btrfs_is_free_space_inode(root
, inode
);
1428 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1429 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1431 spin_lock(&BTRFS_I(inode
)->lock
);
1432 BTRFS_I(inode
)->outstanding_extents
++;
1433 spin_unlock(&BTRFS_I(inode
)->lock
);
1436 spin_lock(&root
->fs_info
->delalloc_lock
);
1437 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1438 root
->fs_info
->delalloc_bytes
+= len
;
1439 if (do_list
&& list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1440 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1441 &root
->fs_info
->delalloc_inodes
);
1443 spin_unlock(&root
->fs_info
->delalloc_lock
);
1448 * extent_io.c clear_bit_hook, see set_bit_hook for why
1450 static void btrfs_clear_bit_hook(struct inode
*inode
,
1451 struct extent_state
*state
, int *bits
)
1454 * set_bit and clear bit hooks normally require _irqsave/restore
1455 * but in this case, we are only testing for the DELALLOC
1456 * bit, which is only set or cleared with irqs on
1458 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1459 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1460 u64 len
= state
->end
+ 1 - state
->start
;
1461 bool do_list
= !btrfs_is_free_space_inode(root
, inode
);
1463 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1464 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1465 } else if (!(*bits
& EXTENT_DO_ACCOUNTING
)) {
1466 spin_lock(&BTRFS_I(inode
)->lock
);
1467 BTRFS_I(inode
)->outstanding_extents
--;
1468 spin_unlock(&BTRFS_I(inode
)->lock
);
1471 if (*bits
& EXTENT_DO_ACCOUNTING
)
1472 btrfs_delalloc_release_metadata(inode
, len
);
1474 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1476 btrfs_free_reserved_data_space(inode
, len
);
1478 spin_lock(&root
->fs_info
->delalloc_lock
);
1479 root
->fs_info
->delalloc_bytes
-= len
;
1480 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1482 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1483 !list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1484 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1486 spin_unlock(&root
->fs_info
->delalloc_lock
);
1491 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1492 * we don't create bios that span stripes or chunks
1494 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
1495 size_t size
, struct bio
*bio
,
1496 unsigned long bio_flags
)
1498 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1499 struct btrfs_mapping_tree
*map_tree
;
1500 u64 logical
= (u64
)bio
->bi_sector
<< 9;
1505 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1508 length
= bio
->bi_size
;
1509 map_tree
= &root
->fs_info
->mapping_tree
;
1510 map_length
= length
;
1511 ret
= btrfs_map_block(map_tree
, READ
, logical
,
1512 &map_length
, NULL
, 0);
1513 /* Will always return 0 or 1 with map_multi == NULL */
1515 if (map_length
< length
+ size
)
1521 * in order to insert checksums into the metadata in large chunks,
1522 * we wait until bio submission time. All the pages in the bio are
1523 * checksummed and sums are attached onto the ordered extent record.
1525 * At IO completion time the cums attached on the ordered extent record
1526 * are inserted into the btree
1528 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1529 struct bio
*bio
, int mirror_num
,
1530 unsigned long bio_flags
,
1533 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1536 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1537 BUG_ON(ret
); /* -ENOMEM */
1542 * in order to insert checksums into the metadata in large chunks,
1543 * we wait until bio submission time. All the pages in the bio are
1544 * checksummed and sums are attached onto the ordered extent record.
1546 * At IO completion time the cums attached on the ordered extent record
1547 * are inserted into the btree
1549 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1550 int mirror_num
, unsigned long bio_flags
,
1553 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1554 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1558 * extent_io.c submission hook. This does the right thing for csum calculation
1559 * on write, or reading the csums from the tree before a read
1561 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1562 int mirror_num
, unsigned long bio_flags
,
1565 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1570 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1572 if (btrfs_is_free_space_inode(root
, inode
))
1575 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, metadata
);
1579 if (!(rw
& REQ_WRITE
)) {
1580 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1581 return btrfs_submit_compressed_read(inode
, bio
,
1582 mirror_num
, bio_flags
);
1583 } else if (!skip_sum
) {
1584 ret
= btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1589 } else if (!skip_sum
) {
1590 /* csum items have already been cloned */
1591 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1593 /* we're doing a write, do the async checksumming */
1594 return btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1595 inode
, rw
, bio
, mirror_num
,
1596 bio_flags
, bio_offset
,
1597 __btrfs_submit_bio_start
,
1598 __btrfs_submit_bio_done
);
1602 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1606 * given a list of ordered sums record them in the inode. This happens
1607 * at IO completion time based on sums calculated at bio submission time.
1609 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1610 struct inode
*inode
, u64 file_offset
,
1611 struct list_head
*list
)
1613 struct btrfs_ordered_sum
*sum
;
1615 list_for_each_entry(sum
, list
, list
) {
1616 btrfs_csum_file_blocks(trans
,
1617 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1622 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1623 struct extent_state
**cached_state
)
1625 if ((end
& (PAGE_CACHE_SIZE
- 1)) == 0)
1627 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1628 cached_state
, GFP_NOFS
);
1631 /* see btrfs_writepage_start_hook for details on why this is required */
1632 struct btrfs_writepage_fixup
{
1634 struct btrfs_work work
;
1637 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1639 struct btrfs_writepage_fixup
*fixup
;
1640 struct btrfs_ordered_extent
*ordered
;
1641 struct extent_state
*cached_state
= NULL
;
1643 struct inode
*inode
;
1648 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1652 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1653 ClearPageChecked(page
);
1657 inode
= page
->mapping
->host
;
1658 page_start
= page_offset(page
);
1659 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1661 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1664 /* already ordered? We're done */
1665 if (PagePrivate2(page
))
1668 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1670 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1671 page_end
, &cached_state
, GFP_NOFS
);
1673 btrfs_start_ordered_extent(inode
, ordered
, 1);
1674 btrfs_put_ordered_extent(ordered
);
1678 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
1680 mapping_set_error(page
->mapping
, ret
);
1681 end_extent_writepage(page
, ret
, page_start
, page_end
);
1682 ClearPageChecked(page
);
1686 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1687 ClearPageChecked(page
);
1688 set_page_dirty(page
);
1690 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1691 &cached_state
, GFP_NOFS
);
1694 page_cache_release(page
);
1699 * There are a few paths in the higher layers of the kernel that directly
1700 * set the page dirty bit without asking the filesystem if it is a
1701 * good idea. This causes problems because we want to make sure COW
1702 * properly happens and the data=ordered rules are followed.
1704 * In our case any range that doesn't have the ORDERED bit set
1705 * hasn't been properly setup for IO. We kick off an async process
1706 * to fix it up. The async helper will wait for ordered extents, set
1707 * the delalloc bit and make it safe to write the page.
1709 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1711 struct inode
*inode
= page
->mapping
->host
;
1712 struct btrfs_writepage_fixup
*fixup
;
1713 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1715 /* this page is properly in the ordered list */
1716 if (TestClearPagePrivate2(page
))
1719 if (PageChecked(page
))
1722 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1726 SetPageChecked(page
);
1727 page_cache_get(page
);
1728 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
1730 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
1734 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1735 struct inode
*inode
, u64 file_pos
,
1736 u64 disk_bytenr
, u64 disk_num_bytes
,
1737 u64 num_bytes
, u64 ram_bytes
,
1738 u8 compression
, u8 encryption
,
1739 u16 other_encoding
, int extent_type
)
1741 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1742 struct btrfs_file_extent_item
*fi
;
1743 struct btrfs_path
*path
;
1744 struct extent_buffer
*leaf
;
1745 struct btrfs_key ins
;
1749 path
= btrfs_alloc_path();
1753 path
->leave_spinning
= 1;
1756 * we may be replacing one extent in the tree with another.
1757 * The new extent is pinned in the extent map, and we don't want
1758 * to drop it from the cache until it is completely in the btree.
1760 * So, tell btrfs_drop_extents to leave this extent in the cache.
1761 * the caller is expected to unpin it and allow it to be merged
1764 ret
= btrfs_drop_extents(trans
, inode
, file_pos
, file_pos
+ num_bytes
,
1769 ins
.objectid
= btrfs_ino(inode
);
1770 ins
.offset
= file_pos
;
1771 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1772 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
, sizeof(*fi
));
1775 leaf
= path
->nodes
[0];
1776 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1777 struct btrfs_file_extent_item
);
1778 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1779 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1780 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1781 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1782 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1783 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1784 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1785 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1786 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1787 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1789 btrfs_unlock_up_safe(path
, 1);
1790 btrfs_set_lock_blocking(leaf
);
1792 btrfs_mark_buffer_dirty(leaf
);
1794 inode_add_bytes(inode
, num_bytes
);
1796 ins
.objectid
= disk_bytenr
;
1797 ins
.offset
= disk_num_bytes
;
1798 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1799 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
1800 root
->root_key
.objectid
,
1801 btrfs_ino(inode
), file_pos
, &ins
);
1803 btrfs_free_path(path
);
1809 * helper function for btrfs_finish_ordered_io, this
1810 * just reads in some of the csum leaves to prime them into ram
1811 * before we start the transaction. It limits the amount of btree
1812 * reads required while inside the transaction.
1814 /* as ordered data IO finishes, this gets called so we can finish
1815 * an ordered extent if the range of bytes in the file it covers are
1818 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
)
1820 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1821 struct btrfs_trans_handle
*trans
= NULL
;
1822 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
1823 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1824 struct extent_state
*cached_state
= NULL
;
1825 int compress_type
= 0;
1829 ret
= btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
1833 BUG_ON(!ordered_extent
); /* Logic error */
1835 nolock
= btrfs_is_free_space_inode(root
, inode
);
1837 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
1838 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
1839 ret
= btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
1842 trans
= btrfs_join_transaction_nolock(root
);
1844 trans
= btrfs_join_transaction(root
);
1846 return PTR_ERR(trans
);
1847 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1848 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
1849 if (ret
) /* -ENOMEM or corruption */
1850 btrfs_abort_transaction(trans
, root
, ret
);
1855 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
1856 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
1860 trans
= btrfs_join_transaction_nolock(root
);
1862 trans
= btrfs_join_transaction(root
);
1863 if (IS_ERR(trans
)) {
1864 ret
= PTR_ERR(trans
);
1868 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1870 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
1871 compress_type
= ordered_extent
->compress_type
;
1872 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
1873 BUG_ON(compress_type
);
1874 ret
= btrfs_mark_extent_written(trans
, inode
,
1875 ordered_extent
->file_offset
,
1876 ordered_extent
->file_offset
+
1877 ordered_extent
->len
);
1879 BUG_ON(root
== root
->fs_info
->tree_root
);
1880 ret
= insert_reserved_file_extent(trans
, inode
,
1881 ordered_extent
->file_offset
,
1882 ordered_extent
->start
,
1883 ordered_extent
->disk_len
,
1884 ordered_extent
->len
,
1885 ordered_extent
->len
,
1886 compress_type
, 0, 0,
1887 BTRFS_FILE_EXTENT_REG
);
1888 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
1889 ordered_extent
->file_offset
,
1890 ordered_extent
->len
);
1892 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
1893 ordered_extent
->file_offset
+
1894 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
1896 btrfs_abort_transaction(trans
, root
, ret
);
1900 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
1901 &ordered_extent
->list
);
1903 ret
= btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
1904 if (!ret
|| !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
1905 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
1906 if (ret
) { /* -ENOMEM or corruption */
1907 btrfs_abort_transaction(trans
, root
, ret
);
1913 if (root
!= root
->fs_info
->tree_root
)
1914 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
1917 btrfs_end_transaction_nolock(trans
, root
);
1919 btrfs_end_transaction(trans
, root
);
1923 btrfs_put_ordered_extent(ordered_extent
);
1924 /* once for the tree */
1925 btrfs_put_ordered_extent(ordered_extent
);
1929 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
1930 ordered_extent
->file_offset
+
1931 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
1935 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
1936 struct extent_state
*state
, int uptodate
)
1938 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
1940 ClearPagePrivate2(page
);
1941 return btrfs_finish_ordered_io(page
->mapping
->host
, start
, end
);
1945 * when reads are done, we need to check csums to verify the data is correct
1946 * if there's a match, we allow the bio to finish. If not, the code in
1947 * extent_io.c will try to find good copies for us.
1949 static int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
1950 struct extent_state
*state
, int mirror
)
1952 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
1953 struct inode
*inode
= page
->mapping
->host
;
1954 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1956 u64
private = ~(u32
)0;
1958 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1961 if (PageChecked(page
)) {
1962 ClearPageChecked(page
);
1966 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
1969 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
1970 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
1971 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
1976 if (state
&& state
->start
== start
) {
1977 private = state
->private;
1980 ret
= get_state_private(io_tree
, start
, &private);
1982 kaddr
= kmap_atomic(page
);
1986 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
1987 btrfs_csum_final(csum
, (char *)&csum
);
1988 if (csum
!= private)
1991 kunmap_atomic(kaddr
);
1996 printk_ratelimited(KERN_INFO
"btrfs csum failed ino %llu off %llu csum %u "
1998 (unsigned long long)btrfs_ino(page
->mapping
->host
),
1999 (unsigned long long)start
, csum
,
2000 (unsigned long long)private);
2001 memset(kaddr
+ offset
, 1, end
- start
+ 1);
2002 flush_dcache_page(page
);
2003 kunmap_atomic(kaddr
);
2009 struct delayed_iput
{
2010 struct list_head list
;
2011 struct inode
*inode
;
2014 /* JDM: If this is fs-wide, why can't we add a pointer to
2015 * btrfs_inode instead and avoid the allocation? */
2016 void btrfs_add_delayed_iput(struct inode
*inode
)
2018 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2019 struct delayed_iput
*delayed
;
2021 if (atomic_add_unless(&inode
->i_count
, -1, 1))
2024 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
2025 delayed
->inode
= inode
;
2027 spin_lock(&fs_info
->delayed_iput_lock
);
2028 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
2029 spin_unlock(&fs_info
->delayed_iput_lock
);
2032 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
2035 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2036 struct delayed_iput
*delayed
;
2039 spin_lock(&fs_info
->delayed_iput_lock
);
2040 empty
= list_empty(&fs_info
->delayed_iputs
);
2041 spin_unlock(&fs_info
->delayed_iput_lock
);
2045 down_read(&root
->fs_info
->cleanup_work_sem
);
2046 spin_lock(&fs_info
->delayed_iput_lock
);
2047 list_splice_init(&fs_info
->delayed_iputs
, &list
);
2048 spin_unlock(&fs_info
->delayed_iput_lock
);
2050 while (!list_empty(&list
)) {
2051 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
2052 list_del(&delayed
->list
);
2053 iput(delayed
->inode
);
2056 up_read(&root
->fs_info
->cleanup_work_sem
);
2059 enum btrfs_orphan_cleanup_state
{
2060 ORPHAN_CLEANUP_STARTED
= 1,
2061 ORPHAN_CLEANUP_DONE
= 2,
2065 * This is called in transaction commit time. If there are no orphan
2066 * files in the subvolume, it removes orphan item and frees block_rsv
2069 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
2070 struct btrfs_root
*root
)
2072 struct btrfs_block_rsv
*block_rsv
;
2075 if (!list_empty(&root
->orphan_list
) ||
2076 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
2079 spin_lock(&root
->orphan_lock
);
2080 if (!list_empty(&root
->orphan_list
)) {
2081 spin_unlock(&root
->orphan_lock
);
2085 if (root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
) {
2086 spin_unlock(&root
->orphan_lock
);
2090 block_rsv
= root
->orphan_block_rsv
;
2091 root
->orphan_block_rsv
= NULL
;
2092 spin_unlock(&root
->orphan_lock
);
2094 if (root
->orphan_item_inserted
&&
2095 btrfs_root_refs(&root
->root_item
) > 0) {
2096 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
2097 root
->root_key
.objectid
);
2099 root
->orphan_item_inserted
= 0;
2103 WARN_ON(block_rsv
->size
> 0);
2104 btrfs_free_block_rsv(root
, block_rsv
);
2109 * This creates an orphan entry for the given inode in case something goes
2110 * wrong in the middle of an unlink/truncate.
2112 * NOTE: caller of this function should reserve 5 units of metadata for
2115 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2117 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2118 struct btrfs_block_rsv
*block_rsv
= NULL
;
2123 if (!root
->orphan_block_rsv
) {
2124 block_rsv
= btrfs_alloc_block_rsv(root
);
2129 spin_lock(&root
->orphan_lock
);
2130 if (!root
->orphan_block_rsv
) {
2131 root
->orphan_block_rsv
= block_rsv
;
2132 } else if (block_rsv
) {
2133 btrfs_free_block_rsv(root
, block_rsv
);
2137 if (list_empty(&BTRFS_I(inode
)->i_orphan
)) {
2138 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
2141 * For proper ENOSPC handling, we should do orphan
2142 * cleanup when mounting. But this introduces backward
2143 * compatibility issue.
2145 if (!xchg(&root
->orphan_item_inserted
, 1))
2153 if (!BTRFS_I(inode
)->orphan_meta_reserved
) {
2154 BTRFS_I(inode
)->orphan_meta_reserved
= 1;
2157 spin_unlock(&root
->orphan_lock
);
2159 /* grab metadata reservation from transaction handle */
2161 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
2162 BUG_ON(ret
); /* -ENOSPC in reservation; Logic error? JDM */
2165 /* insert an orphan item to track this unlinked/truncated file */
2167 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
2168 if (ret
&& ret
!= -EEXIST
) {
2169 btrfs_abort_transaction(trans
, root
, ret
);
2175 /* insert an orphan item to track subvolume contains orphan files */
2177 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
2178 root
->root_key
.objectid
);
2179 if (ret
&& ret
!= -EEXIST
) {
2180 btrfs_abort_transaction(trans
, root
, ret
);
2188 * We have done the truncate/delete so we can go ahead and remove the orphan
2189 * item for this particular inode.
2191 int btrfs_orphan_del(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2193 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2194 int delete_item
= 0;
2195 int release_rsv
= 0;
2198 spin_lock(&root
->orphan_lock
);
2199 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
2200 list_del_init(&BTRFS_I(inode
)->i_orphan
);
2204 if (BTRFS_I(inode
)->orphan_meta_reserved
) {
2205 BTRFS_I(inode
)->orphan_meta_reserved
= 0;
2208 spin_unlock(&root
->orphan_lock
);
2210 if (trans
&& delete_item
) {
2211 ret
= btrfs_del_orphan_item(trans
, root
, btrfs_ino(inode
));
2212 BUG_ON(ret
); /* -ENOMEM or corruption (JDM: Recheck) */
2216 btrfs_orphan_release_metadata(inode
);
2222 * this cleans up any orphans that may be left on the list from the last use
2225 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
2227 struct btrfs_path
*path
;
2228 struct extent_buffer
*leaf
;
2229 struct btrfs_key key
, found_key
;
2230 struct btrfs_trans_handle
*trans
;
2231 struct inode
*inode
;
2232 u64 last_objectid
= 0;
2233 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
2235 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
2238 path
= btrfs_alloc_path();
2245 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
2246 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
2247 key
.offset
= (u64
)-1;
2250 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2255 * if ret == 0 means we found what we were searching for, which
2256 * is weird, but possible, so only screw with path if we didn't
2257 * find the key and see if we have stuff that matches
2261 if (path
->slots
[0] == 0)
2266 /* pull out the item */
2267 leaf
= path
->nodes
[0];
2268 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2270 /* make sure the item matches what we want */
2271 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
2273 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
2276 /* release the path since we're done with it */
2277 btrfs_release_path(path
);
2280 * this is where we are basically btrfs_lookup, without the
2281 * crossing root thing. we store the inode number in the
2282 * offset of the orphan item.
2285 if (found_key
.offset
== last_objectid
) {
2286 printk(KERN_ERR
"btrfs: Error removing orphan entry, "
2287 "stopping orphan cleanup\n");
2292 last_objectid
= found_key
.offset
;
2294 found_key
.objectid
= found_key
.offset
;
2295 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
2296 found_key
.offset
= 0;
2297 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
2298 ret
= PTR_RET(inode
);
2299 if (ret
&& ret
!= -ESTALE
)
2302 if (ret
== -ESTALE
&& root
== root
->fs_info
->tree_root
) {
2303 struct btrfs_root
*dead_root
;
2304 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2305 int is_dead_root
= 0;
2308 * this is an orphan in the tree root. Currently these
2309 * could come from 2 sources:
2310 * a) a snapshot deletion in progress
2311 * b) a free space cache inode
2312 * We need to distinguish those two, as the snapshot
2313 * orphan must not get deleted.
2314 * find_dead_roots already ran before us, so if this
2315 * is a snapshot deletion, we should find the root
2316 * in the dead_roots list
2318 spin_lock(&fs_info
->trans_lock
);
2319 list_for_each_entry(dead_root
, &fs_info
->dead_roots
,
2321 if (dead_root
->root_key
.objectid
==
2322 found_key
.objectid
) {
2327 spin_unlock(&fs_info
->trans_lock
);
2329 /* prevent this orphan from being found again */
2330 key
.offset
= found_key
.objectid
- 1;
2335 * Inode is already gone but the orphan item is still there,
2336 * kill the orphan item.
2338 if (ret
== -ESTALE
) {
2339 trans
= btrfs_start_transaction(root
, 1);
2340 if (IS_ERR(trans
)) {
2341 ret
= PTR_ERR(trans
);
2344 ret
= btrfs_del_orphan_item(trans
, root
,
2345 found_key
.objectid
);
2346 BUG_ON(ret
); /* -ENOMEM or corruption (JDM: Recheck) */
2347 btrfs_end_transaction(trans
, root
);
2352 * add this inode to the orphan list so btrfs_orphan_del does
2353 * the proper thing when we hit it
2355 spin_lock(&root
->orphan_lock
);
2356 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
2357 spin_unlock(&root
->orphan_lock
);
2359 /* if we have links, this was a truncate, lets do that */
2360 if (inode
->i_nlink
) {
2361 if (!S_ISREG(inode
->i_mode
)) {
2367 ret
= btrfs_truncate(inode
);
2372 /* this will do delete_inode and everything for us */
2377 /* release the path since we're done with it */
2378 btrfs_release_path(path
);
2380 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
2382 if (root
->orphan_block_rsv
)
2383 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
2386 if (root
->orphan_block_rsv
|| root
->orphan_item_inserted
) {
2387 trans
= btrfs_join_transaction(root
);
2389 btrfs_end_transaction(trans
, root
);
2393 printk(KERN_INFO
"btrfs: unlinked %d orphans\n", nr_unlink
);
2395 printk(KERN_INFO
"btrfs: truncated %d orphans\n", nr_truncate
);
2399 printk(KERN_CRIT
"btrfs: could not do orphan cleanup %d\n", ret
);
2400 btrfs_free_path(path
);
2405 * very simple check to peek ahead in the leaf looking for xattrs. If we
2406 * don't find any xattrs, we know there can't be any acls.
2408 * slot is the slot the inode is in, objectid is the objectid of the inode
2410 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
2411 int slot
, u64 objectid
)
2413 u32 nritems
= btrfs_header_nritems(leaf
);
2414 struct btrfs_key found_key
;
2418 while (slot
< nritems
) {
2419 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2421 /* we found a different objectid, there must not be acls */
2422 if (found_key
.objectid
!= objectid
)
2425 /* we found an xattr, assume we've got an acl */
2426 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
)
2430 * we found a key greater than an xattr key, there can't
2431 * be any acls later on
2433 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
2440 * it goes inode, inode backrefs, xattrs, extents,
2441 * so if there are a ton of hard links to an inode there can
2442 * be a lot of backrefs. Don't waste time searching too hard,
2443 * this is just an optimization
2448 /* we hit the end of the leaf before we found an xattr or
2449 * something larger than an xattr. We have to assume the inode
2456 * read an inode from the btree into the in-memory inode
2458 static void btrfs_read_locked_inode(struct inode
*inode
)
2460 struct btrfs_path
*path
;
2461 struct extent_buffer
*leaf
;
2462 struct btrfs_inode_item
*inode_item
;
2463 struct btrfs_timespec
*tspec
;
2464 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2465 struct btrfs_key location
;
2469 bool filled
= false;
2471 ret
= btrfs_fill_inode(inode
, &rdev
);
2475 path
= btrfs_alloc_path();
2479 path
->leave_spinning
= 1;
2480 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
2482 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
2486 leaf
= path
->nodes
[0];
2491 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2492 struct btrfs_inode_item
);
2493 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
2494 set_nlink(inode
, btrfs_inode_nlink(leaf
, inode_item
));
2495 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
2496 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
2497 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
2499 tspec
= btrfs_inode_atime(inode_item
);
2500 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2501 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2503 tspec
= btrfs_inode_mtime(inode_item
);
2504 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2505 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2507 tspec
= btrfs_inode_ctime(inode_item
);
2508 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2509 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2511 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
2512 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
2513 BTRFS_I(inode
)->sequence
= btrfs_inode_sequence(leaf
, inode_item
);
2514 inode
->i_generation
= BTRFS_I(inode
)->generation
;
2516 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
2518 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
2519 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
2522 * try to precache a NULL acl entry for files that don't have
2523 * any xattrs or acls
2525 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
2528 cache_no_acl(inode
);
2530 btrfs_free_path(path
);
2532 switch (inode
->i_mode
& S_IFMT
) {
2534 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2535 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2536 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2537 inode
->i_fop
= &btrfs_file_operations
;
2538 inode
->i_op
= &btrfs_file_inode_operations
;
2541 inode
->i_fop
= &btrfs_dir_file_operations
;
2542 if (root
== root
->fs_info
->tree_root
)
2543 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
2545 inode
->i_op
= &btrfs_dir_inode_operations
;
2548 inode
->i_op
= &btrfs_symlink_inode_operations
;
2549 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2550 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2553 inode
->i_op
= &btrfs_special_inode_operations
;
2554 init_special_inode(inode
, inode
->i_mode
, rdev
);
2558 btrfs_update_iflags(inode
);
2562 btrfs_free_path(path
);
2563 make_bad_inode(inode
);
2567 * given a leaf and an inode, copy the inode fields into the leaf
2569 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
2570 struct extent_buffer
*leaf
,
2571 struct btrfs_inode_item
*item
,
2572 struct inode
*inode
)
2574 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
2575 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
2576 btrfs_set_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
);
2577 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
2578 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
2580 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
2581 inode
->i_atime
.tv_sec
);
2582 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
2583 inode
->i_atime
.tv_nsec
);
2585 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
2586 inode
->i_mtime
.tv_sec
);
2587 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
2588 inode
->i_mtime
.tv_nsec
);
2590 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
2591 inode
->i_ctime
.tv_sec
);
2592 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
2593 inode
->i_ctime
.tv_nsec
);
2595 btrfs_set_inode_nbytes(leaf
, item
, inode_get_bytes(inode
));
2596 btrfs_set_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
);
2597 btrfs_set_inode_sequence(leaf
, item
, BTRFS_I(inode
)->sequence
);
2598 btrfs_set_inode_transid(leaf
, item
, trans
->transid
);
2599 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
2600 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
2601 btrfs_set_inode_block_group(leaf
, item
, 0);
2605 * copy everything in the in-memory inode into the btree.
2607 static noinline
int btrfs_update_inode_item(struct btrfs_trans_handle
*trans
,
2608 struct btrfs_root
*root
, struct inode
*inode
)
2610 struct btrfs_inode_item
*inode_item
;
2611 struct btrfs_path
*path
;
2612 struct extent_buffer
*leaf
;
2615 path
= btrfs_alloc_path();
2619 path
->leave_spinning
= 1;
2620 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
2628 btrfs_unlock_up_safe(path
, 1);
2629 leaf
= path
->nodes
[0];
2630 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2631 struct btrfs_inode_item
);
2633 fill_inode_item(trans
, leaf
, inode_item
, inode
);
2634 btrfs_mark_buffer_dirty(leaf
);
2635 btrfs_set_inode_last_trans(trans
, inode
);
2638 btrfs_free_path(path
);
2643 * copy everything in the in-memory inode into the btree.
2645 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
2646 struct btrfs_root
*root
, struct inode
*inode
)
2651 * If the inode is a free space inode, we can deadlock during commit
2652 * if we put it into the delayed code.
2654 * The data relocation inode should also be directly updated
2657 if (!btrfs_is_free_space_inode(root
, inode
)
2658 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
) {
2659 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
2661 btrfs_set_inode_last_trans(trans
, inode
);
2665 return btrfs_update_inode_item(trans
, root
, inode
);
2668 static noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
2669 struct btrfs_root
*root
, struct inode
*inode
)
2673 ret
= btrfs_update_inode(trans
, root
, inode
);
2675 return btrfs_update_inode_item(trans
, root
, inode
);
2680 * unlink helper that gets used here in inode.c and in the tree logging
2681 * recovery code. It remove a link in a directory with a given name, and
2682 * also drops the back refs in the inode to the directory
2684 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
2685 struct btrfs_root
*root
,
2686 struct inode
*dir
, struct inode
*inode
,
2687 const char *name
, int name_len
)
2689 struct btrfs_path
*path
;
2691 struct extent_buffer
*leaf
;
2692 struct btrfs_dir_item
*di
;
2693 struct btrfs_key key
;
2695 u64 ino
= btrfs_ino(inode
);
2696 u64 dir_ino
= btrfs_ino(dir
);
2698 path
= btrfs_alloc_path();
2704 path
->leave_spinning
= 1;
2705 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
2706 name
, name_len
, -1);
2715 leaf
= path
->nodes
[0];
2716 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2717 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2720 btrfs_release_path(path
);
2722 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
2725 printk(KERN_INFO
"btrfs failed to delete reference to %.*s, "
2726 "inode %llu parent %llu\n", name_len
, name
,
2727 (unsigned long long)ino
, (unsigned long long)dir_ino
);
2728 btrfs_abort_transaction(trans
, root
, ret
);
2732 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
2734 btrfs_abort_transaction(trans
, root
, ret
);
2738 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
2740 if (ret
!= 0 && ret
!= -ENOENT
) {
2741 btrfs_abort_transaction(trans
, root
, ret
);
2745 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
2750 btrfs_free_path(path
);
2754 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
2755 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
2756 btrfs_update_inode(trans
, root
, dir
);
2761 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
2762 struct btrfs_root
*root
,
2763 struct inode
*dir
, struct inode
*inode
,
2764 const char *name
, int name_len
)
2767 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
2769 btrfs_drop_nlink(inode
);
2770 ret
= btrfs_update_inode(trans
, root
, inode
);
2776 /* helper to check if there is any shared block in the path */
2777 static int check_path_shared(struct btrfs_root
*root
,
2778 struct btrfs_path
*path
)
2780 struct extent_buffer
*eb
;
2784 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
2787 if (!path
->nodes
[level
])
2789 eb
= path
->nodes
[level
];
2790 if (!btrfs_block_can_be_shared(root
, eb
))
2792 ret
= btrfs_lookup_extent_info(NULL
, root
, eb
->start
, eb
->len
,
2801 * helper to start transaction for unlink and rmdir.
2803 * unlink and rmdir are special in btrfs, they do not always free space.
2804 * so in enospc case, we should make sure they will free space before
2805 * allowing them to use the global metadata reservation.
2807 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
,
2808 struct dentry
*dentry
)
2810 struct btrfs_trans_handle
*trans
;
2811 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2812 struct btrfs_path
*path
;
2813 struct btrfs_inode_ref
*ref
;
2814 struct btrfs_dir_item
*di
;
2815 struct inode
*inode
= dentry
->d_inode
;
2820 u64 ino
= btrfs_ino(inode
);
2821 u64 dir_ino
= btrfs_ino(dir
);
2824 * 1 for the possible orphan item
2825 * 1 for the dir item
2826 * 1 for the dir index
2827 * 1 for the inode ref
2828 * 1 for the inode ref in the tree log
2829 * 2 for the dir entries in the log
2832 trans
= btrfs_start_transaction(root
, 8);
2833 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
2836 if (ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
2837 return ERR_PTR(-ENOSPC
);
2839 /* check if there is someone else holds reference */
2840 if (S_ISDIR(inode
->i_mode
) && atomic_read(&inode
->i_count
) > 1)
2841 return ERR_PTR(-ENOSPC
);
2843 if (atomic_read(&inode
->i_count
) > 2)
2844 return ERR_PTR(-ENOSPC
);
2846 if (xchg(&root
->fs_info
->enospc_unlink
, 1))
2847 return ERR_PTR(-ENOSPC
);
2849 path
= btrfs_alloc_path();
2851 root
->fs_info
->enospc_unlink
= 0;
2852 return ERR_PTR(-ENOMEM
);
2855 /* 1 for the orphan item */
2856 trans
= btrfs_start_transaction(root
, 1);
2857 if (IS_ERR(trans
)) {
2858 btrfs_free_path(path
);
2859 root
->fs_info
->enospc_unlink
= 0;
2863 path
->skip_locking
= 1;
2864 path
->search_commit_root
= 1;
2866 ret
= btrfs_lookup_inode(trans
, root
, path
,
2867 &BTRFS_I(dir
)->location
, 0);
2873 if (check_path_shared(root
, path
))
2878 btrfs_release_path(path
);
2880 ret
= btrfs_lookup_inode(trans
, root
, path
,
2881 &BTRFS_I(inode
)->location
, 0);
2887 if (check_path_shared(root
, path
))
2892 btrfs_release_path(path
);
2894 if (ret
== 0 && S_ISREG(inode
->i_mode
)) {
2895 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
2901 BUG_ON(ret
== 0); /* Corruption */
2902 if (check_path_shared(root
, path
))
2904 btrfs_release_path(path
);
2912 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
2913 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
2919 if (check_path_shared(root
, path
))
2925 btrfs_release_path(path
);
2927 ref
= btrfs_lookup_inode_ref(trans
, root
, path
,
2928 dentry
->d_name
.name
, dentry
->d_name
.len
,
2934 BUG_ON(!ref
); /* Logic error */
2935 if (check_path_shared(root
, path
))
2937 index
= btrfs_inode_ref_index(path
->nodes
[0], ref
);
2938 btrfs_release_path(path
);
2941 * This is a commit root search, if we can lookup inode item and other
2942 * relative items in the commit root, it means the transaction of
2943 * dir/file creation has been committed, and the dir index item that we
2944 * delay to insert has also been inserted into the commit root. So
2945 * we needn't worry about the delayed insertion of the dir index item
2948 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir_ino
, index
,
2949 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
2954 BUG_ON(ret
== -ENOENT
);
2955 if (check_path_shared(root
, path
))
2960 btrfs_free_path(path
);
2961 /* Migrate the orphan reservation over */
2963 err
= btrfs_block_rsv_migrate(trans
->block_rsv
,
2964 &root
->fs_info
->global_block_rsv
,
2965 trans
->bytes_reserved
);
2968 btrfs_end_transaction(trans
, root
);
2969 root
->fs_info
->enospc_unlink
= 0;
2970 return ERR_PTR(err
);
2973 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
2977 static void __unlink_end_trans(struct btrfs_trans_handle
*trans
,
2978 struct btrfs_root
*root
)
2980 if (trans
->block_rsv
== &root
->fs_info
->global_block_rsv
) {
2981 btrfs_block_rsv_release(root
, trans
->block_rsv
,
2982 trans
->bytes_reserved
);
2983 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
2984 BUG_ON(!root
->fs_info
->enospc_unlink
);
2985 root
->fs_info
->enospc_unlink
= 0;
2987 btrfs_end_transaction(trans
, root
);
2990 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
2992 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2993 struct btrfs_trans_handle
*trans
;
2994 struct inode
*inode
= dentry
->d_inode
;
2996 unsigned long nr
= 0;
2998 trans
= __unlink_start_trans(dir
, dentry
);
3000 return PTR_ERR(trans
);
3002 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
3004 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3005 dentry
->d_name
.name
, dentry
->d_name
.len
);
3009 if (inode
->i_nlink
== 0) {
3010 ret
= btrfs_orphan_add(trans
, inode
);
3016 nr
= trans
->blocks_used
;
3017 __unlink_end_trans(trans
, root
);
3018 btrfs_btree_balance_dirty(root
, nr
);
3022 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
3023 struct btrfs_root
*root
,
3024 struct inode
*dir
, u64 objectid
,
3025 const char *name
, int name_len
)
3027 struct btrfs_path
*path
;
3028 struct extent_buffer
*leaf
;
3029 struct btrfs_dir_item
*di
;
3030 struct btrfs_key key
;
3033 u64 dir_ino
= btrfs_ino(dir
);
3035 path
= btrfs_alloc_path();
3039 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3040 name
, name_len
, -1);
3041 if (IS_ERR_OR_NULL(di
)) {
3049 leaf
= path
->nodes
[0];
3050 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3051 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
3052 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3054 btrfs_abort_transaction(trans
, root
, ret
);
3057 btrfs_release_path(path
);
3059 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
3060 objectid
, root
->root_key
.objectid
,
3061 dir_ino
, &index
, name
, name_len
);
3063 if (ret
!= -ENOENT
) {
3064 btrfs_abort_transaction(trans
, root
, ret
);
3067 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
3069 if (IS_ERR_OR_NULL(di
)) {
3074 btrfs_abort_transaction(trans
, root
, ret
);
3078 leaf
= path
->nodes
[0];
3079 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3080 btrfs_release_path(path
);
3083 btrfs_release_path(path
);
3085 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3087 btrfs_abort_transaction(trans
, root
, ret
);
3091 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3092 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3093 ret
= btrfs_update_inode(trans
, root
, dir
);
3095 btrfs_abort_transaction(trans
, root
, ret
);
3097 btrfs_free_path(path
);
3101 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3103 struct inode
*inode
= dentry
->d_inode
;
3105 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3106 struct btrfs_trans_handle
*trans
;
3107 unsigned long nr
= 0;
3109 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
||
3110 btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
)
3113 trans
= __unlink_start_trans(dir
, dentry
);
3115 return PTR_ERR(trans
);
3117 if (unlikely(btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
3118 err
= btrfs_unlink_subvol(trans
, root
, dir
,
3119 BTRFS_I(inode
)->location
.objectid
,
3120 dentry
->d_name
.name
,
3121 dentry
->d_name
.len
);
3125 err
= btrfs_orphan_add(trans
, inode
);
3129 /* now the directory is empty */
3130 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3131 dentry
->d_name
.name
, dentry
->d_name
.len
);
3133 btrfs_i_size_write(inode
, 0);
3135 nr
= trans
->blocks_used
;
3136 __unlink_end_trans(trans
, root
);
3137 btrfs_btree_balance_dirty(root
, nr
);
3143 * this can truncate away extent items, csum items and directory items.
3144 * It starts at a high offset and removes keys until it can't find
3145 * any higher than new_size
3147 * csum items that cross the new i_size are truncated to the new size
3150 * min_type is the minimum key type to truncate down to. If set to 0, this
3151 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3153 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
3154 struct btrfs_root
*root
,
3155 struct inode
*inode
,
3156 u64 new_size
, u32 min_type
)
3158 struct btrfs_path
*path
;
3159 struct extent_buffer
*leaf
;
3160 struct btrfs_file_extent_item
*fi
;
3161 struct btrfs_key key
;
3162 struct btrfs_key found_key
;
3163 u64 extent_start
= 0;
3164 u64 extent_num_bytes
= 0;
3165 u64 extent_offset
= 0;
3167 u64 mask
= root
->sectorsize
- 1;
3168 u32 found_type
= (u8
)-1;
3171 int pending_del_nr
= 0;
3172 int pending_del_slot
= 0;
3173 int extent_type
= -1;
3176 u64 ino
= btrfs_ino(inode
);
3178 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
3180 path
= btrfs_alloc_path();
3185 if (root
->ref_cows
|| root
== root
->fs_info
->tree_root
)
3186 btrfs_drop_extent_cache(inode
, new_size
& (~mask
), (u64
)-1, 0);
3189 * This function is also used to drop the items in the log tree before
3190 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3191 * it is used to drop the loged items. So we shouldn't kill the delayed
3194 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
3195 btrfs_kill_delayed_inode_items(inode
);
3198 key
.offset
= (u64
)-1;
3202 path
->leave_spinning
= 1;
3203 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3210 /* there are no items in the tree for us to truncate, we're
3213 if (path
->slots
[0] == 0)
3220 leaf
= path
->nodes
[0];
3221 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3222 found_type
= btrfs_key_type(&found_key
);
3224 if (found_key
.objectid
!= ino
)
3227 if (found_type
< min_type
)
3230 item_end
= found_key
.offset
;
3231 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
3232 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3233 struct btrfs_file_extent_item
);
3234 extent_type
= btrfs_file_extent_type(leaf
, fi
);
3235 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3237 btrfs_file_extent_num_bytes(leaf
, fi
);
3238 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3239 item_end
+= btrfs_file_extent_inline_len(leaf
,
3244 if (found_type
> min_type
) {
3247 if (item_end
< new_size
)
3249 if (found_key
.offset
>= new_size
)
3255 /* FIXME, shrink the extent if the ref count is only 1 */
3256 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
3259 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3261 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
3263 u64 orig_num_bytes
=
3264 btrfs_file_extent_num_bytes(leaf
, fi
);
3265 extent_num_bytes
= new_size
-
3266 found_key
.offset
+ root
->sectorsize
- 1;
3267 extent_num_bytes
= extent_num_bytes
&
3268 ~((u64
)root
->sectorsize
- 1);
3269 btrfs_set_file_extent_num_bytes(leaf
, fi
,
3271 num_dec
= (orig_num_bytes
-
3273 if (root
->ref_cows
&& extent_start
!= 0)
3274 inode_sub_bytes(inode
, num_dec
);
3275 btrfs_mark_buffer_dirty(leaf
);
3278 btrfs_file_extent_disk_num_bytes(leaf
,
3280 extent_offset
= found_key
.offset
-
3281 btrfs_file_extent_offset(leaf
, fi
);
3283 /* FIXME blocksize != 4096 */
3284 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
3285 if (extent_start
!= 0) {
3288 inode_sub_bytes(inode
, num_dec
);
3291 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3293 * we can't truncate inline items that have had
3297 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
3298 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
3299 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
3300 u32 size
= new_size
- found_key
.offset
;
3302 if (root
->ref_cows
) {
3303 inode_sub_bytes(inode
, item_end
+ 1 -
3307 btrfs_file_extent_calc_inline_size(size
);
3308 btrfs_truncate_item(trans
, root
, path
,
3310 } else if (root
->ref_cows
) {
3311 inode_sub_bytes(inode
, item_end
+ 1 -
3317 if (!pending_del_nr
) {
3318 /* no pending yet, add ourselves */
3319 pending_del_slot
= path
->slots
[0];
3321 } else if (pending_del_nr
&&
3322 path
->slots
[0] + 1 == pending_del_slot
) {
3323 /* hop on the pending chunk */
3325 pending_del_slot
= path
->slots
[0];
3332 if (found_extent
&& (root
->ref_cows
||
3333 root
== root
->fs_info
->tree_root
)) {
3334 btrfs_set_path_blocking(path
);
3335 ret
= btrfs_free_extent(trans
, root
, extent_start
,
3336 extent_num_bytes
, 0,
3337 btrfs_header_owner(leaf
),
3338 ino
, extent_offset
, 0);
3342 if (found_type
== BTRFS_INODE_ITEM_KEY
)
3345 if (path
->slots
[0] == 0 ||
3346 path
->slots
[0] != pending_del_slot
) {
3347 if (root
->ref_cows
&&
3348 BTRFS_I(inode
)->location
.objectid
!=
3349 BTRFS_FREE_INO_OBJECTID
) {
3353 if (pending_del_nr
) {
3354 ret
= btrfs_del_items(trans
, root
, path
,
3358 btrfs_abort_transaction(trans
,
3364 btrfs_release_path(path
);
3371 if (pending_del_nr
) {
3372 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
3375 btrfs_abort_transaction(trans
, root
, ret
);
3378 btrfs_free_path(path
);
3383 * taken from block_truncate_page, but does cow as it zeros out
3384 * any bytes left in the last page in the file.
3386 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
3388 struct inode
*inode
= mapping
->host
;
3389 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3390 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3391 struct btrfs_ordered_extent
*ordered
;
3392 struct extent_state
*cached_state
= NULL
;
3394 u32 blocksize
= root
->sectorsize
;
3395 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
3396 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
3398 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
3403 if ((offset
& (blocksize
- 1)) == 0)
3405 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
3411 page
= find_or_create_page(mapping
, index
, mask
);
3413 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
3417 page_start
= page_offset(page
);
3418 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3420 if (!PageUptodate(page
)) {
3421 ret
= btrfs_readpage(NULL
, page
);
3423 if (page
->mapping
!= mapping
) {
3425 page_cache_release(page
);
3428 if (!PageUptodate(page
)) {
3433 wait_on_page_writeback(page
);
3435 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
3436 set_page_extent_mapped(page
);
3438 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
3440 unlock_extent_cached(io_tree
, page_start
, page_end
,
3441 &cached_state
, GFP_NOFS
);
3443 page_cache_release(page
);
3444 btrfs_start_ordered_extent(inode
, ordered
, 1);
3445 btrfs_put_ordered_extent(ordered
);
3449 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
3450 EXTENT_DIRTY
| EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
,
3451 0, 0, &cached_state
, GFP_NOFS
);
3453 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
3456 unlock_extent_cached(io_tree
, page_start
, page_end
,
3457 &cached_state
, GFP_NOFS
);
3462 if (offset
!= PAGE_CACHE_SIZE
) {
3464 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
3465 flush_dcache_page(page
);
3468 ClearPageChecked(page
);
3469 set_page_dirty(page
);
3470 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
3475 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
3477 page_cache_release(page
);
3483 * This function puts in dummy file extents for the area we're creating a hole
3484 * for. So if we are truncating this file to a larger size we need to insert
3485 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3486 * the range between oldsize and size
3488 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
3490 struct btrfs_trans_handle
*trans
;
3491 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3492 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3493 struct extent_map
*em
= NULL
;
3494 struct extent_state
*cached_state
= NULL
;
3495 u64 mask
= root
->sectorsize
- 1;
3496 u64 hole_start
= (oldsize
+ mask
) & ~mask
;
3497 u64 block_end
= (size
+ mask
) & ~mask
;
3503 if (size
<= hole_start
)
3507 struct btrfs_ordered_extent
*ordered
;
3508 btrfs_wait_ordered_range(inode
, hole_start
,
3509 block_end
- hole_start
);
3510 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
3512 ordered
= btrfs_lookup_ordered_extent(inode
, hole_start
);
3515 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
3516 &cached_state
, GFP_NOFS
);
3517 btrfs_put_ordered_extent(ordered
);
3520 cur_offset
= hole_start
;
3522 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
3523 block_end
- cur_offset
, 0);
3528 last_byte
= min(extent_map_end(em
), block_end
);
3529 last_byte
= (last_byte
+ mask
) & ~mask
;
3530 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
3532 hole_size
= last_byte
- cur_offset
;
3534 trans
= btrfs_start_transaction(root
, 3);
3535 if (IS_ERR(trans
)) {
3536 err
= PTR_ERR(trans
);
3540 err
= btrfs_drop_extents(trans
, inode
, cur_offset
,
3541 cur_offset
+ hole_size
,
3544 btrfs_abort_transaction(trans
, root
, err
);
3545 btrfs_end_transaction(trans
, root
);
3549 err
= btrfs_insert_file_extent(trans
, root
,
3550 btrfs_ino(inode
), cur_offset
, 0,
3551 0, hole_size
, 0, hole_size
,
3554 btrfs_abort_transaction(trans
, root
, err
);
3555 btrfs_end_transaction(trans
, root
);
3559 btrfs_drop_extent_cache(inode
, hole_start
,
3562 btrfs_update_inode(trans
, root
, inode
);
3563 btrfs_end_transaction(trans
, root
);
3565 free_extent_map(em
);
3567 cur_offset
= last_byte
;
3568 if (cur_offset
>= block_end
)
3572 free_extent_map(em
);
3573 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
3578 static int btrfs_setsize(struct inode
*inode
, loff_t newsize
)
3580 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3581 struct btrfs_trans_handle
*trans
;
3582 loff_t oldsize
= i_size_read(inode
);
3585 if (newsize
== oldsize
)
3588 if (newsize
> oldsize
) {
3589 truncate_pagecache(inode
, oldsize
, newsize
);
3590 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
3594 trans
= btrfs_start_transaction(root
, 1);
3596 return PTR_ERR(trans
);
3598 i_size_write(inode
, newsize
);
3599 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
3600 ret
= btrfs_update_inode(trans
, root
, inode
);
3601 btrfs_end_transaction(trans
, root
);
3605 * We're truncating a file that used to have good data down to
3606 * zero. Make sure it gets into the ordered flush list so that
3607 * any new writes get down to disk quickly.
3610 BTRFS_I(inode
)->ordered_data_close
= 1;
3612 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3613 truncate_setsize(inode
, newsize
);
3614 ret
= btrfs_truncate(inode
);
3620 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
3622 struct inode
*inode
= dentry
->d_inode
;
3623 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3626 if (btrfs_root_readonly(root
))
3629 err
= inode_change_ok(inode
, attr
);
3633 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
3634 err
= btrfs_setsize(inode
, attr
->ia_size
);
3639 if (attr
->ia_valid
) {
3640 setattr_copy(inode
, attr
);
3641 err
= btrfs_dirty_inode(inode
);
3643 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
3644 err
= btrfs_acl_chmod(inode
);
3650 void btrfs_evict_inode(struct inode
*inode
)
3652 struct btrfs_trans_handle
*trans
;
3653 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3654 struct btrfs_block_rsv
*rsv
, *global_rsv
;
3655 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
3659 trace_btrfs_inode_evict(inode
);
3661 truncate_inode_pages(&inode
->i_data
, 0);
3662 if (inode
->i_nlink
&& (btrfs_root_refs(&root
->root_item
) != 0 ||
3663 btrfs_is_free_space_inode(root
, inode
)))
3666 if (is_bad_inode(inode
)) {
3667 btrfs_orphan_del(NULL
, inode
);
3670 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
3671 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
3673 if (root
->fs_info
->log_root_recovering
) {
3674 BUG_ON(!list_empty(&BTRFS_I(inode
)->i_orphan
));
3678 if (inode
->i_nlink
> 0) {
3679 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0);
3683 rsv
= btrfs_alloc_block_rsv(root
);
3685 btrfs_orphan_del(NULL
, inode
);
3688 rsv
->size
= min_size
;
3689 global_rsv
= &root
->fs_info
->global_block_rsv
;
3691 btrfs_i_size_write(inode
, 0);
3694 * This is a bit simpler than btrfs_truncate since
3696 * 1) We've already reserved our space for our orphan item in the
3698 * 2) We're going to delete the inode item, so we don't need to update
3701 * So we just need to reserve some slack space in case we add bytes when
3702 * doing the truncate.
3705 ret
= btrfs_block_rsv_refill_noflush(root
, rsv
, min_size
);
3708 * Try and steal from the global reserve since we will
3709 * likely not use this space anyway, we want to try as
3710 * hard as possible to get this to work.
3713 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
, min_size
);
3716 printk(KERN_WARNING
"Could not get space for a "
3717 "delete, will truncate on mount %d\n", ret
);
3718 btrfs_orphan_del(NULL
, inode
);
3719 btrfs_free_block_rsv(root
, rsv
);
3723 trans
= btrfs_start_transaction(root
, 0);
3724 if (IS_ERR(trans
)) {
3725 btrfs_orphan_del(NULL
, inode
);
3726 btrfs_free_block_rsv(root
, rsv
);
3730 trans
->block_rsv
= rsv
;
3732 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
3736 nr
= trans
->blocks_used
;
3737 btrfs_end_transaction(trans
, root
);
3739 btrfs_btree_balance_dirty(root
, nr
);
3742 btrfs_free_block_rsv(root
, rsv
);
3745 trans
->block_rsv
= root
->orphan_block_rsv
;
3746 ret
= btrfs_orphan_del(trans
, inode
);
3750 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
3751 if (!(root
== root
->fs_info
->tree_root
||
3752 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
3753 btrfs_return_ino(root
, btrfs_ino(inode
));
3755 nr
= trans
->blocks_used
;
3756 btrfs_end_transaction(trans
, root
);
3757 btrfs_btree_balance_dirty(root
, nr
);
3764 * this returns the key found in the dir entry in the location pointer.
3765 * If no dir entries were found, location->objectid is 0.
3767 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
3768 struct btrfs_key
*location
)
3770 const char *name
= dentry
->d_name
.name
;
3771 int namelen
= dentry
->d_name
.len
;
3772 struct btrfs_dir_item
*di
;
3773 struct btrfs_path
*path
;
3774 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3777 path
= btrfs_alloc_path();
3781 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(dir
), name
,
3786 if (IS_ERR_OR_NULL(di
))
3789 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
3791 btrfs_free_path(path
);
3794 location
->objectid
= 0;
3799 * when we hit a tree root in a directory, the btrfs part of the inode
3800 * needs to be changed to reflect the root directory of the tree root. This
3801 * is kind of like crossing a mount point.
3803 static int fixup_tree_root_location(struct btrfs_root
*root
,
3805 struct dentry
*dentry
,
3806 struct btrfs_key
*location
,
3807 struct btrfs_root
**sub_root
)
3809 struct btrfs_path
*path
;
3810 struct btrfs_root
*new_root
;
3811 struct btrfs_root_ref
*ref
;
3812 struct extent_buffer
*leaf
;
3816 path
= btrfs_alloc_path();
3823 ret
= btrfs_find_root_ref(root
->fs_info
->tree_root
, path
,
3824 BTRFS_I(dir
)->root
->root_key
.objectid
,
3825 location
->objectid
);
3832 leaf
= path
->nodes
[0];
3833 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
3834 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(dir
) ||
3835 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
3838 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
3839 (unsigned long)(ref
+ 1),
3840 dentry
->d_name
.len
);
3844 btrfs_release_path(path
);
3846 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
3847 if (IS_ERR(new_root
)) {
3848 err
= PTR_ERR(new_root
);
3852 if (btrfs_root_refs(&new_root
->root_item
) == 0) {
3857 *sub_root
= new_root
;
3858 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
3859 location
->type
= BTRFS_INODE_ITEM_KEY
;
3860 location
->offset
= 0;
3863 btrfs_free_path(path
);
3867 static void inode_tree_add(struct inode
*inode
)
3869 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3870 struct btrfs_inode
*entry
;
3872 struct rb_node
*parent
;
3873 u64 ino
= btrfs_ino(inode
);
3875 p
= &root
->inode_tree
.rb_node
;
3878 if (inode_unhashed(inode
))
3881 spin_lock(&root
->inode_lock
);
3884 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
3886 if (ino
< btrfs_ino(&entry
->vfs_inode
))
3887 p
= &parent
->rb_left
;
3888 else if (ino
> btrfs_ino(&entry
->vfs_inode
))
3889 p
= &parent
->rb_right
;
3891 WARN_ON(!(entry
->vfs_inode
.i_state
&
3892 (I_WILL_FREE
| I_FREEING
)));
3893 rb_erase(parent
, &root
->inode_tree
);
3894 RB_CLEAR_NODE(parent
);
3895 spin_unlock(&root
->inode_lock
);
3899 rb_link_node(&BTRFS_I(inode
)->rb_node
, parent
, p
);
3900 rb_insert_color(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
3901 spin_unlock(&root
->inode_lock
);
3904 static void inode_tree_del(struct inode
*inode
)
3906 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3909 spin_lock(&root
->inode_lock
);
3910 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
3911 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
3912 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
3913 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
3915 spin_unlock(&root
->inode_lock
);
3918 * Free space cache has inodes in the tree root, but the tree root has a
3919 * root_refs of 0, so this could end up dropping the tree root as a
3920 * snapshot, so we need the extra !root->fs_info->tree_root check to
3921 * make sure we don't drop it.
3923 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0 &&
3924 root
!= root
->fs_info
->tree_root
) {
3925 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
3926 spin_lock(&root
->inode_lock
);
3927 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
3928 spin_unlock(&root
->inode_lock
);
3930 btrfs_add_dead_root(root
);
3934 void btrfs_invalidate_inodes(struct btrfs_root
*root
)
3936 struct rb_node
*node
;
3937 struct rb_node
*prev
;
3938 struct btrfs_inode
*entry
;
3939 struct inode
*inode
;
3942 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
3944 spin_lock(&root
->inode_lock
);
3946 node
= root
->inode_tree
.rb_node
;
3950 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
3952 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
3953 node
= node
->rb_left
;
3954 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
3955 node
= node
->rb_right
;
3961 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
3962 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
3966 prev
= rb_next(prev
);
3970 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
3971 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
3972 inode
= igrab(&entry
->vfs_inode
);
3974 spin_unlock(&root
->inode_lock
);
3975 if (atomic_read(&inode
->i_count
) > 1)
3976 d_prune_aliases(inode
);
3978 * btrfs_drop_inode will have it removed from
3979 * the inode cache when its usage count
3984 spin_lock(&root
->inode_lock
);
3988 if (cond_resched_lock(&root
->inode_lock
))
3991 node
= rb_next(node
);
3993 spin_unlock(&root
->inode_lock
);
3996 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
3998 struct btrfs_iget_args
*args
= p
;
3999 inode
->i_ino
= args
->ino
;
4000 BTRFS_I(inode
)->root
= args
->root
;
4001 btrfs_set_inode_space_info(args
->root
, inode
);
4005 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
4007 struct btrfs_iget_args
*args
= opaque
;
4008 return args
->ino
== btrfs_ino(inode
) &&
4009 args
->root
== BTRFS_I(inode
)->root
;
4012 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
4014 struct btrfs_root
*root
)
4016 struct inode
*inode
;
4017 struct btrfs_iget_args args
;
4018 args
.ino
= objectid
;
4021 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
4022 btrfs_init_locked_inode
,
4027 /* Get an inode object given its location and corresponding root.
4028 * Returns in *is_new if the inode was read from disk
4030 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
4031 struct btrfs_root
*root
, int *new)
4033 struct inode
*inode
;
4035 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
4037 return ERR_PTR(-ENOMEM
);
4039 if (inode
->i_state
& I_NEW
) {
4040 BTRFS_I(inode
)->root
= root
;
4041 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
4042 btrfs_read_locked_inode(inode
);
4043 if (!is_bad_inode(inode
)) {
4044 inode_tree_add(inode
);
4045 unlock_new_inode(inode
);
4049 unlock_new_inode(inode
);
4051 inode
= ERR_PTR(-ESTALE
);
4058 static struct inode
*new_simple_dir(struct super_block
*s
,
4059 struct btrfs_key
*key
,
4060 struct btrfs_root
*root
)
4062 struct inode
*inode
= new_inode(s
);
4065 return ERR_PTR(-ENOMEM
);
4067 BTRFS_I(inode
)->root
= root
;
4068 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
4069 BTRFS_I(inode
)->dummy_inode
= 1;
4071 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
4072 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
4073 inode
->i_fop
= &simple_dir_operations
;
4074 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
4075 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4080 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
4082 struct inode
*inode
;
4083 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4084 struct btrfs_root
*sub_root
= root
;
4085 struct btrfs_key location
;
4089 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
4090 return ERR_PTR(-ENAMETOOLONG
);
4092 if (unlikely(d_need_lookup(dentry
))) {
4093 memcpy(&location
, dentry
->d_fsdata
, sizeof(struct btrfs_key
));
4094 kfree(dentry
->d_fsdata
);
4095 dentry
->d_fsdata
= NULL
;
4096 /* This thing is hashed, drop it for now */
4099 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
4103 return ERR_PTR(ret
);
4105 if (location
.objectid
== 0)
4108 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
4109 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
4113 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
4115 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
4116 ret
= fixup_tree_root_location(root
, dir
, dentry
,
4117 &location
, &sub_root
);
4120 inode
= ERR_PTR(ret
);
4122 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
4124 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
4126 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
4128 if (!IS_ERR(inode
) && root
!= sub_root
) {
4129 down_read(&root
->fs_info
->cleanup_work_sem
);
4130 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
4131 ret
= btrfs_orphan_cleanup(sub_root
);
4132 up_read(&root
->fs_info
->cleanup_work_sem
);
4134 inode
= ERR_PTR(ret
);
4140 static int btrfs_dentry_delete(const struct dentry
*dentry
)
4142 struct btrfs_root
*root
;
4143 struct inode
*inode
= dentry
->d_inode
;
4145 if (!inode
&& !IS_ROOT(dentry
))
4146 inode
= dentry
->d_parent
->d_inode
;
4149 root
= BTRFS_I(inode
)->root
;
4150 if (btrfs_root_refs(&root
->root_item
) == 0)
4153 if (btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
4159 static void btrfs_dentry_release(struct dentry
*dentry
)
4161 if (dentry
->d_fsdata
)
4162 kfree(dentry
->d_fsdata
);
4165 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
4166 struct nameidata
*nd
)
4170 ret
= d_splice_alias(btrfs_lookup_dentry(dir
, dentry
), dentry
);
4171 if (unlikely(d_need_lookup(dentry
))) {
4172 spin_lock(&dentry
->d_lock
);
4173 dentry
->d_flags
&= ~DCACHE_NEED_LOOKUP
;
4174 spin_unlock(&dentry
->d_lock
);
4179 unsigned char btrfs_filetype_table
[] = {
4180 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
4183 static int btrfs_real_readdir(struct file
*filp
, void *dirent
,
4186 struct inode
*inode
= filp
->f_dentry
->d_inode
;
4187 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4188 struct btrfs_item
*item
;
4189 struct btrfs_dir_item
*di
;
4190 struct btrfs_key key
;
4191 struct btrfs_key found_key
;
4192 struct btrfs_path
*path
;
4193 struct list_head ins_list
;
4194 struct list_head del_list
;
4196 struct extent_buffer
*leaf
;
4198 unsigned char d_type
;
4203 int key_type
= BTRFS_DIR_INDEX_KEY
;
4207 int is_curr
= 0; /* filp->f_pos points to the current index? */
4209 /* FIXME, use a real flag for deciding about the key type */
4210 if (root
->fs_info
->tree_root
== root
)
4211 key_type
= BTRFS_DIR_ITEM_KEY
;
4213 /* special case for "." */
4214 if (filp
->f_pos
== 0) {
4215 over
= filldir(dirent
, ".", 1,
4216 filp
->f_pos
, btrfs_ino(inode
), DT_DIR
);
4221 /* special case for .., just use the back ref */
4222 if (filp
->f_pos
== 1) {
4223 u64 pino
= parent_ino(filp
->f_path
.dentry
);
4224 over
= filldir(dirent
, "..", 2,
4225 filp
->f_pos
, pino
, DT_DIR
);
4230 path
= btrfs_alloc_path();
4236 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
4237 INIT_LIST_HEAD(&ins_list
);
4238 INIT_LIST_HEAD(&del_list
);
4239 btrfs_get_delayed_items(inode
, &ins_list
, &del_list
);
4242 btrfs_set_key_type(&key
, key_type
);
4243 key
.offset
= filp
->f_pos
;
4244 key
.objectid
= btrfs_ino(inode
);
4246 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4251 leaf
= path
->nodes
[0];
4252 slot
= path
->slots
[0];
4253 if (slot
>= btrfs_header_nritems(leaf
)) {
4254 ret
= btrfs_next_leaf(root
, path
);
4262 item
= btrfs_item_nr(leaf
, slot
);
4263 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
4265 if (found_key
.objectid
!= key
.objectid
)
4267 if (btrfs_key_type(&found_key
) != key_type
)
4269 if (found_key
.offset
< filp
->f_pos
)
4271 if (key_type
== BTRFS_DIR_INDEX_KEY
&&
4272 btrfs_should_delete_dir_index(&del_list
,
4276 filp
->f_pos
= found_key
.offset
;
4279 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
4281 di_total
= btrfs_item_size(leaf
, item
);
4283 while (di_cur
< di_total
) {
4284 struct btrfs_key location
;
4286 if (verify_dir_item(root
, leaf
, di
))
4289 name_len
= btrfs_dir_name_len(leaf
, di
);
4290 if (name_len
<= sizeof(tmp_name
)) {
4291 name_ptr
= tmp_name
;
4293 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
4299 read_extent_buffer(leaf
, name_ptr
,
4300 (unsigned long)(di
+ 1), name_len
);
4302 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
4303 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
4306 /* is this a reference to our own snapshot? If so
4309 * In contrast to old kernels, we insert the snapshot's
4310 * dir item and dir index after it has been created, so
4311 * we won't find a reference to our own snapshot. We
4312 * still keep the following code for backward
4315 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
4316 location
.objectid
== root
->root_key
.objectid
) {
4320 over
= filldir(dirent
, name_ptr
, name_len
,
4321 found_key
.offset
, location
.objectid
,
4325 if (name_ptr
!= tmp_name
)
4330 di_len
= btrfs_dir_name_len(leaf
, di
) +
4331 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
4333 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
4339 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
4342 ret
= btrfs_readdir_delayed_dir_index(filp
, dirent
, filldir
,
4348 /* Reached end of directory/root. Bump pos past the last item. */
4349 if (key_type
== BTRFS_DIR_INDEX_KEY
)
4351 * 32-bit glibc will use getdents64, but then strtol -
4352 * so the last number we can serve is this.
4354 filp
->f_pos
= 0x7fffffff;
4360 if (key_type
== BTRFS_DIR_INDEX_KEY
)
4361 btrfs_put_delayed_items(&ins_list
, &del_list
);
4362 btrfs_free_path(path
);
4366 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
4368 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4369 struct btrfs_trans_handle
*trans
;
4371 bool nolock
= false;
4373 if (BTRFS_I(inode
)->dummy_inode
)
4376 if (btrfs_fs_closing(root
->fs_info
) && btrfs_is_free_space_inode(root
, inode
))
4379 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
4381 trans
= btrfs_join_transaction_nolock(root
);
4383 trans
= btrfs_join_transaction(root
);
4385 return PTR_ERR(trans
);
4387 ret
= btrfs_end_transaction_nolock(trans
, root
);
4389 ret
= btrfs_commit_transaction(trans
, root
);
4395 * This is somewhat expensive, updating the tree every time the
4396 * inode changes. But, it is most likely to find the inode in cache.
4397 * FIXME, needs more benchmarking...there are no reasons other than performance
4398 * to keep or drop this code.
4400 int btrfs_dirty_inode(struct inode
*inode
)
4402 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4403 struct btrfs_trans_handle
*trans
;
4406 if (BTRFS_I(inode
)->dummy_inode
)
4409 trans
= btrfs_join_transaction(root
);
4411 return PTR_ERR(trans
);
4413 ret
= btrfs_update_inode(trans
, root
, inode
);
4414 if (ret
&& ret
== -ENOSPC
) {
4415 /* whoops, lets try again with the full transaction */
4416 btrfs_end_transaction(trans
, root
);
4417 trans
= btrfs_start_transaction(root
, 1);
4419 return PTR_ERR(trans
);
4421 ret
= btrfs_update_inode(trans
, root
, inode
);
4423 btrfs_end_transaction(trans
, root
);
4424 if (BTRFS_I(inode
)->delayed_node
)
4425 btrfs_balance_delayed_items(root
);
4431 * This is a copy of file_update_time. We need this so we can return error on
4432 * ENOSPC for updating the inode in the case of file write and mmap writes.
4434 int btrfs_update_time(struct file
*file
)
4436 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
4437 struct timespec now
;
4439 enum { S_MTIME
= 1, S_CTIME
= 2, S_VERSION
= 4 } sync_it
= 0;
4441 /* First try to exhaust all avenues to not sync */
4442 if (IS_NOCMTIME(inode
))
4445 now
= current_fs_time(inode
->i_sb
);
4446 if (!timespec_equal(&inode
->i_mtime
, &now
))
4449 if (!timespec_equal(&inode
->i_ctime
, &now
))
4452 if (IS_I_VERSION(inode
))
4453 sync_it
|= S_VERSION
;
4458 /* Finally allowed to write? Takes lock. */
4459 if (mnt_want_write_file(file
))
4462 /* Only change inode inside the lock region */
4463 if (sync_it
& S_VERSION
)
4464 inode_inc_iversion(inode
);
4465 if (sync_it
& S_CTIME
)
4466 inode
->i_ctime
= now
;
4467 if (sync_it
& S_MTIME
)
4468 inode
->i_mtime
= now
;
4469 ret
= btrfs_dirty_inode(inode
);
4471 mark_inode_dirty_sync(inode
);
4472 mnt_drop_write(file
->f_path
.mnt
);
4477 * find the highest existing sequence number in a directory
4478 * and then set the in-memory index_cnt variable to reflect
4479 * free sequence numbers
4481 static int btrfs_set_inode_index_count(struct inode
*inode
)
4483 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4484 struct btrfs_key key
, found_key
;
4485 struct btrfs_path
*path
;
4486 struct extent_buffer
*leaf
;
4489 key
.objectid
= btrfs_ino(inode
);
4490 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
4491 key
.offset
= (u64
)-1;
4493 path
= btrfs_alloc_path();
4497 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4500 /* FIXME: we should be able to handle this */
4506 * MAGIC NUMBER EXPLANATION:
4507 * since we search a directory based on f_pos we have to start at 2
4508 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4509 * else has to start at 2
4511 if (path
->slots
[0] == 0) {
4512 BTRFS_I(inode
)->index_cnt
= 2;
4518 leaf
= path
->nodes
[0];
4519 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4521 if (found_key
.objectid
!= btrfs_ino(inode
) ||
4522 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
4523 BTRFS_I(inode
)->index_cnt
= 2;
4527 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
4529 btrfs_free_path(path
);
4534 * helper to find a free sequence number in a given directory. This current
4535 * code is very simple, later versions will do smarter things in the btree
4537 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
4541 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
4542 ret
= btrfs_inode_delayed_dir_index_count(dir
);
4544 ret
= btrfs_set_inode_index_count(dir
);
4550 *index
= BTRFS_I(dir
)->index_cnt
;
4551 BTRFS_I(dir
)->index_cnt
++;
4556 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
4557 struct btrfs_root
*root
,
4559 const char *name
, int name_len
,
4560 u64 ref_objectid
, u64 objectid
,
4561 umode_t mode
, u64
*index
)
4563 struct inode
*inode
;
4564 struct btrfs_inode_item
*inode_item
;
4565 struct btrfs_key
*location
;
4566 struct btrfs_path
*path
;
4567 struct btrfs_inode_ref
*ref
;
4568 struct btrfs_key key
[2];
4574 path
= btrfs_alloc_path();
4576 return ERR_PTR(-ENOMEM
);
4578 inode
= new_inode(root
->fs_info
->sb
);
4580 btrfs_free_path(path
);
4581 return ERR_PTR(-ENOMEM
);
4585 * we have to initialize this early, so we can reclaim the inode
4586 * number if we fail afterwards in this function.
4588 inode
->i_ino
= objectid
;
4591 trace_btrfs_inode_request(dir
);
4593 ret
= btrfs_set_inode_index(dir
, index
);
4595 btrfs_free_path(path
);
4597 return ERR_PTR(ret
);
4601 * index_cnt is ignored for everything but a dir,
4602 * btrfs_get_inode_index_count has an explanation for the magic
4605 BTRFS_I(inode
)->index_cnt
= 2;
4606 BTRFS_I(inode
)->root
= root
;
4607 BTRFS_I(inode
)->generation
= trans
->transid
;
4608 inode
->i_generation
= BTRFS_I(inode
)->generation
;
4609 btrfs_set_inode_space_info(root
, inode
);
4616 key
[0].objectid
= objectid
;
4617 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
4620 key
[1].objectid
= objectid
;
4621 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
4622 key
[1].offset
= ref_objectid
;
4624 sizes
[0] = sizeof(struct btrfs_inode_item
);
4625 sizes
[1] = name_len
+ sizeof(*ref
);
4627 path
->leave_spinning
= 1;
4628 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
4632 inode_init_owner(inode
, dir
, mode
);
4633 inode_set_bytes(inode
, 0);
4634 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4635 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4636 struct btrfs_inode_item
);
4637 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
4639 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
4640 struct btrfs_inode_ref
);
4641 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
4642 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
4643 ptr
= (unsigned long)(ref
+ 1);
4644 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
4646 btrfs_mark_buffer_dirty(path
->nodes
[0]);
4647 btrfs_free_path(path
);
4649 location
= &BTRFS_I(inode
)->location
;
4650 location
->objectid
= objectid
;
4651 location
->offset
= 0;
4652 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
4654 btrfs_inherit_iflags(inode
, dir
);
4656 if (S_ISREG(mode
)) {
4657 if (btrfs_test_opt(root
, NODATASUM
))
4658 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
4659 if (btrfs_test_opt(root
, NODATACOW
) ||
4660 (BTRFS_I(dir
)->flags
& BTRFS_INODE_NODATACOW
))
4661 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
;
4664 insert_inode_hash(inode
);
4665 inode_tree_add(inode
);
4667 trace_btrfs_inode_new(inode
);
4668 btrfs_set_inode_last_trans(trans
, inode
);
4673 BTRFS_I(dir
)->index_cnt
--;
4674 btrfs_free_path(path
);
4676 return ERR_PTR(ret
);
4679 static inline u8
btrfs_inode_type(struct inode
*inode
)
4681 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
4685 * utility function to add 'inode' into 'parent_inode' with
4686 * a give name and a given sequence number.
4687 * if 'add_backref' is true, also insert a backref from the
4688 * inode to the parent directory.
4690 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
4691 struct inode
*parent_inode
, struct inode
*inode
,
4692 const char *name
, int name_len
, int add_backref
, u64 index
)
4695 struct btrfs_key key
;
4696 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
4697 u64 ino
= btrfs_ino(inode
);
4698 u64 parent_ino
= btrfs_ino(parent_inode
);
4700 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4701 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
4704 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
4708 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4709 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
4710 key
.objectid
, root
->root_key
.objectid
,
4711 parent_ino
, index
, name
, name_len
);
4712 } else if (add_backref
) {
4713 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
4717 /* Nothing to clean up yet */
4721 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
4723 btrfs_inode_type(inode
), index
);
4727 btrfs_abort_transaction(trans
, root
, ret
);
4731 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
4733 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
4734 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
4736 btrfs_abort_transaction(trans
, root
, ret
);
4740 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
4743 err
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
4744 key
.objectid
, root
->root_key
.objectid
,
4745 parent_ino
, &local_index
, name
, name_len
);
4747 } else if (add_backref
) {
4751 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
4752 ino
, parent_ino
, &local_index
);
4757 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
4758 struct inode
*dir
, struct dentry
*dentry
,
4759 struct inode
*inode
, int backref
, u64 index
)
4761 int err
= btrfs_add_link(trans
, dir
, inode
,
4762 dentry
->d_name
.name
, dentry
->d_name
.len
,
4769 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
4770 umode_t mode
, dev_t rdev
)
4772 struct btrfs_trans_handle
*trans
;
4773 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4774 struct inode
*inode
= NULL
;
4778 unsigned long nr
= 0;
4781 if (!new_valid_dev(rdev
))
4785 * 2 for inode item and ref
4787 * 1 for xattr if selinux is on
4789 trans
= btrfs_start_transaction(root
, 5);
4791 return PTR_ERR(trans
);
4793 err
= btrfs_find_free_ino(root
, &objectid
);
4797 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4798 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
4800 if (IS_ERR(inode
)) {
4801 err
= PTR_ERR(inode
);
4805 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
4812 * If the active LSM wants to access the inode during
4813 * d_instantiate it needs these. Smack checks to see
4814 * if the filesystem supports xattrs by looking at the
4818 inode
->i_op
= &btrfs_special_inode_operations
;
4819 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
4823 init_special_inode(inode
, inode
->i_mode
, rdev
);
4824 btrfs_update_inode(trans
, root
, inode
);
4825 d_instantiate(dentry
, inode
);
4828 nr
= trans
->blocks_used
;
4829 btrfs_end_transaction(trans
, root
);
4830 btrfs_btree_balance_dirty(root
, nr
);
4832 inode_dec_link_count(inode
);
4838 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
4839 umode_t mode
, struct nameidata
*nd
)
4841 struct btrfs_trans_handle
*trans
;
4842 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4843 struct inode
*inode
= NULL
;
4846 unsigned long nr
= 0;
4851 * 2 for inode item and ref
4853 * 1 for xattr if selinux is on
4855 trans
= btrfs_start_transaction(root
, 5);
4857 return PTR_ERR(trans
);
4859 err
= btrfs_find_free_ino(root
, &objectid
);
4863 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4864 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
4866 if (IS_ERR(inode
)) {
4867 err
= PTR_ERR(inode
);
4871 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
4878 * If the active LSM wants to access the inode during
4879 * d_instantiate it needs these. Smack checks to see
4880 * if the filesystem supports xattrs by looking at the
4883 inode
->i_fop
= &btrfs_file_operations
;
4884 inode
->i_op
= &btrfs_file_inode_operations
;
4886 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
4890 inode
->i_mapping
->a_ops
= &btrfs_aops
;
4891 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
4892 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
4893 d_instantiate(dentry
, inode
);
4896 nr
= trans
->blocks_used
;
4897 btrfs_end_transaction(trans
, root
);
4899 inode_dec_link_count(inode
);
4902 btrfs_btree_balance_dirty(root
, nr
);
4906 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
4907 struct dentry
*dentry
)
4909 struct btrfs_trans_handle
*trans
;
4910 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4911 struct inode
*inode
= old_dentry
->d_inode
;
4913 unsigned long nr
= 0;
4917 /* do not allow sys_link's with other subvols of the same device */
4918 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
4921 if (inode
->i_nlink
== ~0U)
4924 err
= btrfs_set_inode_index(dir
, &index
);
4929 * 2 items for inode and inode ref
4930 * 2 items for dir items
4931 * 1 item for parent inode
4933 trans
= btrfs_start_transaction(root
, 5);
4934 if (IS_ERR(trans
)) {
4935 err
= PTR_ERR(trans
);
4939 btrfs_inc_nlink(inode
);
4940 inode
->i_ctime
= CURRENT_TIME
;
4943 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
4948 struct dentry
*parent
= dentry
->d_parent
;
4949 err
= btrfs_update_inode(trans
, root
, inode
);
4952 d_instantiate(dentry
, inode
);
4953 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
4956 nr
= trans
->blocks_used
;
4957 btrfs_end_transaction(trans
, root
);
4960 inode_dec_link_count(inode
);
4963 btrfs_btree_balance_dirty(root
, nr
);
4967 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
4969 struct inode
*inode
= NULL
;
4970 struct btrfs_trans_handle
*trans
;
4971 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4973 int drop_on_err
= 0;
4976 unsigned long nr
= 1;
4979 * 2 items for inode and ref
4980 * 2 items for dir items
4981 * 1 for xattr if selinux is on
4983 trans
= btrfs_start_transaction(root
, 5);
4985 return PTR_ERR(trans
);
4987 err
= btrfs_find_free_ino(root
, &objectid
);
4991 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4992 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
4993 S_IFDIR
| mode
, &index
);
4994 if (IS_ERR(inode
)) {
4995 err
= PTR_ERR(inode
);
5001 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5005 inode
->i_op
= &btrfs_dir_inode_operations
;
5006 inode
->i_fop
= &btrfs_dir_file_operations
;
5008 btrfs_i_size_write(inode
, 0);
5009 err
= btrfs_update_inode(trans
, root
, inode
);
5013 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
5014 dentry
->d_name
.len
, 0, index
);
5018 d_instantiate(dentry
, inode
);
5022 nr
= trans
->blocks_used
;
5023 btrfs_end_transaction(trans
, root
);
5026 btrfs_btree_balance_dirty(root
, nr
);
5030 /* helper for btfs_get_extent. Given an existing extent in the tree,
5031 * and an extent that you want to insert, deal with overlap and insert
5032 * the new extent into the tree.
5034 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
5035 struct extent_map
*existing
,
5036 struct extent_map
*em
,
5037 u64 map_start
, u64 map_len
)
5041 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
5042 start_diff
= map_start
- em
->start
;
5043 em
->start
= map_start
;
5045 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
5046 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
5047 em
->block_start
+= start_diff
;
5048 em
->block_len
-= start_diff
;
5050 return add_extent_mapping(em_tree
, em
);
5053 static noinline
int uncompress_inline(struct btrfs_path
*path
,
5054 struct inode
*inode
, struct page
*page
,
5055 size_t pg_offset
, u64 extent_offset
,
5056 struct btrfs_file_extent_item
*item
)
5059 struct extent_buffer
*leaf
= path
->nodes
[0];
5062 unsigned long inline_size
;
5066 WARN_ON(pg_offset
!= 0);
5067 compress_type
= btrfs_file_extent_compression(leaf
, item
);
5068 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
5069 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
5070 btrfs_item_nr(leaf
, path
->slots
[0]));
5071 tmp
= kmalloc(inline_size
, GFP_NOFS
);
5074 ptr
= btrfs_file_extent_inline_start(item
);
5076 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
5078 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
5079 ret
= btrfs_decompress(compress_type
, tmp
, page
,
5080 extent_offset
, inline_size
, max_size
);
5082 char *kaddr
= kmap_atomic(page
);
5083 unsigned long copy_size
= min_t(u64
,
5084 PAGE_CACHE_SIZE
- pg_offset
,
5085 max_size
- extent_offset
);
5086 memset(kaddr
+ pg_offset
, 0, copy_size
);
5087 kunmap_atomic(kaddr
);
5094 * a bit scary, this does extent mapping from logical file offset to the disk.
5095 * the ugly parts come from merging extents from the disk with the in-ram
5096 * representation. This gets more complex because of the data=ordered code,
5097 * where the in-ram extents might be locked pending data=ordered completion.
5099 * This also copies inline extents directly into the page.
5102 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
5103 size_t pg_offset
, u64 start
, u64 len
,
5109 u64 extent_start
= 0;
5111 u64 objectid
= btrfs_ino(inode
);
5113 struct btrfs_path
*path
= NULL
;
5114 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5115 struct btrfs_file_extent_item
*item
;
5116 struct extent_buffer
*leaf
;
5117 struct btrfs_key found_key
;
5118 struct extent_map
*em
= NULL
;
5119 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
5120 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
5121 struct btrfs_trans_handle
*trans
= NULL
;
5125 read_lock(&em_tree
->lock
);
5126 em
= lookup_extent_mapping(em_tree
, start
, len
);
5128 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5129 read_unlock(&em_tree
->lock
);
5132 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
5133 free_extent_map(em
);
5134 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
5135 free_extent_map(em
);
5139 em
= alloc_extent_map();
5144 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5145 em
->start
= EXTENT_MAP_HOLE
;
5146 em
->orig_start
= EXTENT_MAP_HOLE
;
5148 em
->block_len
= (u64
)-1;
5151 path
= btrfs_alloc_path();
5157 * Chances are we'll be called again, so go ahead and do
5163 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
5164 objectid
, start
, trans
!= NULL
);
5171 if (path
->slots
[0] == 0)
5176 leaf
= path
->nodes
[0];
5177 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5178 struct btrfs_file_extent_item
);
5179 /* are we inside the extent that was found? */
5180 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5181 found_type
= btrfs_key_type(&found_key
);
5182 if (found_key
.objectid
!= objectid
||
5183 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
5187 found_type
= btrfs_file_extent_type(leaf
, item
);
5188 extent_start
= found_key
.offset
;
5189 compress_type
= btrfs_file_extent_compression(leaf
, item
);
5190 if (found_type
== BTRFS_FILE_EXTENT_REG
||
5191 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5192 extent_end
= extent_start
+
5193 btrfs_file_extent_num_bytes(leaf
, item
);
5194 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
5196 size
= btrfs_file_extent_inline_len(leaf
, item
);
5197 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
5198 ~((u64
)root
->sectorsize
- 1);
5201 if (start
>= extent_end
) {
5203 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
5204 ret
= btrfs_next_leaf(root
, path
);
5211 leaf
= path
->nodes
[0];
5213 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5214 if (found_key
.objectid
!= objectid
||
5215 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
5217 if (start
+ len
<= found_key
.offset
)
5220 em
->len
= found_key
.offset
- start
;
5224 if (found_type
== BTRFS_FILE_EXTENT_REG
||
5225 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5226 em
->start
= extent_start
;
5227 em
->len
= extent_end
- extent_start
;
5228 em
->orig_start
= extent_start
-
5229 btrfs_file_extent_offset(leaf
, item
);
5230 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
5232 em
->block_start
= EXTENT_MAP_HOLE
;
5235 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
5236 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
5237 em
->compress_type
= compress_type
;
5238 em
->block_start
= bytenr
;
5239 em
->block_len
= btrfs_file_extent_disk_num_bytes(leaf
,
5242 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
5243 em
->block_start
= bytenr
;
5244 em
->block_len
= em
->len
;
5245 if (found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
5246 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
5249 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
5253 size_t extent_offset
;
5256 em
->block_start
= EXTENT_MAP_INLINE
;
5257 if (!page
|| create
) {
5258 em
->start
= extent_start
;
5259 em
->len
= extent_end
- extent_start
;
5263 size
= btrfs_file_extent_inline_len(leaf
, item
);
5264 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
5265 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
5266 size
- extent_offset
);
5267 em
->start
= extent_start
+ extent_offset
;
5268 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
5269 ~((u64
)root
->sectorsize
- 1);
5270 em
->orig_start
= EXTENT_MAP_INLINE
;
5271 if (compress_type
) {
5272 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
5273 em
->compress_type
= compress_type
;
5275 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
5276 if (create
== 0 && !PageUptodate(page
)) {
5277 if (btrfs_file_extent_compression(leaf
, item
) !=
5278 BTRFS_COMPRESS_NONE
) {
5279 ret
= uncompress_inline(path
, inode
, page
,
5281 extent_offset
, item
);
5282 BUG_ON(ret
); /* -ENOMEM */
5285 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
5287 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
5288 memset(map
+ pg_offset
+ copy_size
, 0,
5289 PAGE_CACHE_SIZE
- pg_offset
-
5294 flush_dcache_page(page
);
5295 } else if (create
&& PageUptodate(page
)) {
5299 free_extent_map(em
);
5302 btrfs_release_path(path
);
5303 trans
= btrfs_join_transaction(root
);
5306 return ERR_CAST(trans
);
5310 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
5313 btrfs_mark_buffer_dirty(leaf
);
5315 set_extent_uptodate(io_tree
, em
->start
,
5316 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
5319 printk(KERN_ERR
"btrfs unknown found_type %d\n", found_type
);
5326 em
->block_start
= EXTENT_MAP_HOLE
;
5327 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
5329 btrfs_release_path(path
);
5330 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
5331 printk(KERN_ERR
"Btrfs: bad extent! em: [%llu %llu] passed "
5332 "[%llu %llu]\n", (unsigned long long)em
->start
,
5333 (unsigned long long)em
->len
,
5334 (unsigned long long)start
,
5335 (unsigned long long)len
);
5341 write_lock(&em_tree
->lock
);
5342 ret
= add_extent_mapping(em_tree
, em
);
5343 /* it is possible that someone inserted the extent into the tree
5344 * while we had the lock dropped. It is also possible that
5345 * an overlapping map exists in the tree
5347 if (ret
== -EEXIST
) {
5348 struct extent_map
*existing
;
5352 existing
= lookup_extent_mapping(em_tree
, start
, len
);
5353 if (existing
&& (existing
->start
> start
||
5354 existing
->start
+ existing
->len
<= start
)) {
5355 free_extent_map(existing
);
5359 existing
= lookup_extent_mapping(em_tree
, em
->start
,
5362 err
= merge_extent_mapping(em_tree
, existing
,
5365 free_extent_map(existing
);
5367 free_extent_map(em
);
5372 free_extent_map(em
);
5376 free_extent_map(em
);
5381 write_unlock(&em_tree
->lock
);
5384 trace_btrfs_get_extent(root
, em
);
5387 btrfs_free_path(path
);
5389 ret
= btrfs_end_transaction(trans
, root
);
5394 free_extent_map(em
);
5395 return ERR_PTR(err
);
5397 BUG_ON(!em
); /* Error is always set */
5401 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
5402 size_t pg_offset
, u64 start
, u64 len
,
5405 struct extent_map
*em
;
5406 struct extent_map
*hole_em
= NULL
;
5407 u64 range_start
= start
;
5413 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
5418 * if our em maps to a hole, there might
5419 * actually be delalloc bytes behind it
5421 if (em
->block_start
!= EXTENT_MAP_HOLE
)
5427 /* check to see if we've wrapped (len == -1 or similar) */
5436 /* ok, we didn't find anything, lets look for delalloc */
5437 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
5438 end
, len
, EXTENT_DELALLOC
, 1);
5439 found_end
= range_start
+ found
;
5440 if (found_end
< range_start
)
5441 found_end
= (u64
)-1;
5444 * we didn't find anything useful, return
5445 * the original results from get_extent()
5447 if (range_start
> end
|| found_end
<= start
) {
5453 /* adjust the range_start to make sure it doesn't
5454 * go backwards from the start they passed in
5456 range_start
= max(start
,range_start
);
5457 found
= found_end
- range_start
;
5460 u64 hole_start
= start
;
5463 em
= alloc_extent_map();
5469 * when btrfs_get_extent can't find anything it
5470 * returns one huge hole
5472 * make sure what it found really fits our range, and
5473 * adjust to make sure it is based on the start from
5477 u64 calc_end
= extent_map_end(hole_em
);
5479 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
5480 free_extent_map(hole_em
);
5483 hole_start
= max(hole_em
->start
, start
);
5484 hole_len
= calc_end
- hole_start
;
5488 if (hole_em
&& range_start
> hole_start
) {
5489 /* our hole starts before our delalloc, so we
5490 * have to return just the parts of the hole
5491 * that go until the delalloc starts
5493 em
->len
= min(hole_len
,
5494 range_start
- hole_start
);
5495 em
->start
= hole_start
;
5496 em
->orig_start
= hole_start
;
5498 * don't adjust block start at all,
5499 * it is fixed at EXTENT_MAP_HOLE
5501 em
->block_start
= hole_em
->block_start
;
5502 em
->block_len
= hole_len
;
5504 em
->start
= range_start
;
5506 em
->orig_start
= range_start
;
5507 em
->block_start
= EXTENT_MAP_DELALLOC
;
5508 em
->block_len
= found
;
5510 } else if (hole_em
) {
5515 free_extent_map(hole_em
);
5517 free_extent_map(em
);
5518 return ERR_PTR(err
);
5523 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
5524 struct extent_map
*em
,
5527 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5528 struct btrfs_trans_handle
*trans
;
5529 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
5530 struct btrfs_key ins
;
5533 bool insert
= false;
5536 * Ok if the extent map we looked up is a hole and is for the exact
5537 * range we want, there is no reason to allocate a new one, however if
5538 * it is not right then we need to free this one and drop the cache for
5541 if (em
->block_start
!= EXTENT_MAP_HOLE
|| em
->start
!= start
||
5543 free_extent_map(em
);
5546 btrfs_drop_extent_cache(inode
, start
, start
+ len
- 1, 0);
5549 trans
= btrfs_join_transaction(root
);
5551 return ERR_CAST(trans
);
5553 if (start
<= BTRFS_I(inode
)->disk_i_size
&& len
< 64 * 1024)
5554 btrfs_add_inode_defrag(trans
, inode
);
5556 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
5558 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
5559 ret
= btrfs_reserve_extent(trans
, root
, len
, root
->sectorsize
, 0,
5560 alloc_hint
, &ins
, 1);
5567 em
= alloc_extent_map();
5569 em
= ERR_PTR(-ENOMEM
);
5575 em
->orig_start
= em
->start
;
5576 em
->len
= ins
.offset
;
5578 em
->block_start
= ins
.objectid
;
5579 em
->block_len
= ins
.offset
;
5580 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5583 * We need to do this because if we're using the original em we searched
5584 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5587 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
5590 write_lock(&em_tree
->lock
);
5591 ret
= add_extent_mapping(em_tree
, em
);
5592 write_unlock(&em_tree
->lock
);
5595 btrfs_drop_extent_cache(inode
, start
, start
+ em
->len
- 1, 0);
5598 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
5599 ins
.offset
, ins
.offset
, 0);
5601 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
5605 btrfs_end_transaction(trans
, root
);
5610 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5611 * block must be cow'd
5613 static noinline
int can_nocow_odirect(struct btrfs_trans_handle
*trans
,
5614 struct inode
*inode
, u64 offset
, u64 len
)
5616 struct btrfs_path
*path
;
5618 struct extent_buffer
*leaf
;
5619 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5620 struct btrfs_file_extent_item
*fi
;
5621 struct btrfs_key key
;
5629 path
= btrfs_alloc_path();
5633 ret
= btrfs_lookup_file_extent(trans
, root
, path
, btrfs_ino(inode
),
5638 slot
= path
->slots
[0];
5641 /* can't find the item, must cow */
5648 leaf
= path
->nodes
[0];
5649 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
5650 if (key
.objectid
!= btrfs_ino(inode
) ||
5651 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
5652 /* not our file or wrong item type, must cow */
5656 if (key
.offset
> offset
) {
5657 /* Wrong offset, must cow */
5661 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
5662 found_type
= btrfs_file_extent_type(leaf
, fi
);
5663 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
5664 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
5665 /* not a regular extent, must cow */
5668 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
5669 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
5671 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
5672 if (extent_end
< offset
+ len
) {
5673 /* extent doesn't include our full range, must cow */
5677 if (btrfs_extent_readonly(root
, disk_bytenr
))
5681 * look for other files referencing this extent, if we
5682 * find any we must cow
5684 if (btrfs_cross_ref_exist(trans
, root
, btrfs_ino(inode
),
5685 key
.offset
- backref_offset
, disk_bytenr
))
5689 * adjust disk_bytenr and num_bytes to cover just the bytes
5690 * in this extent we are about to write. If there
5691 * are any csums in that range we have to cow in order
5692 * to keep the csums correct
5694 disk_bytenr
+= backref_offset
;
5695 disk_bytenr
+= offset
- key
.offset
;
5696 num_bytes
= min(offset
+ len
, extent_end
) - offset
;
5697 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
5700 * all of the above have passed, it is safe to overwrite this extent
5705 btrfs_free_path(path
);
5709 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
5710 struct buffer_head
*bh_result
, int create
)
5712 struct extent_map
*em
;
5713 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5714 u64 start
= iblock
<< inode
->i_blkbits
;
5715 u64 len
= bh_result
->b_size
;
5716 struct btrfs_trans_handle
*trans
;
5718 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
5723 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5724 * io. INLINE is special, and we could probably kludge it in here, but
5725 * it's still buffered so for safety lets just fall back to the generic
5728 * For COMPRESSED we _have_ to read the entire extent in so we can
5729 * decompress it, so there will be buffering required no matter what we
5730 * do, so go ahead and fallback to buffered.
5732 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5733 * to buffered IO. Don't blame me, this is the price we pay for using
5736 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
5737 em
->block_start
== EXTENT_MAP_INLINE
) {
5738 free_extent_map(em
);
5742 /* Just a good old fashioned hole, return */
5743 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
5744 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
5745 free_extent_map(em
);
5746 /* DIO will do one hole at a time, so just unlock a sector */
5747 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
,
5748 start
+ root
->sectorsize
- 1);
5753 * We don't allocate a new extent in the following cases
5755 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5757 * 2) The extent is marked as PREALLOC. We're good to go here and can
5758 * just use the extent.
5762 len
= em
->len
- (start
- em
->start
);
5766 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
5767 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
5768 em
->block_start
!= EXTENT_MAP_HOLE
)) {
5773 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
5774 type
= BTRFS_ORDERED_PREALLOC
;
5776 type
= BTRFS_ORDERED_NOCOW
;
5777 len
= min(len
, em
->len
- (start
- em
->start
));
5778 block_start
= em
->block_start
+ (start
- em
->start
);
5781 * we're not going to log anything, but we do need
5782 * to make sure the current transaction stays open
5783 * while we look for nocow cross refs
5785 trans
= btrfs_join_transaction(root
);
5789 if (can_nocow_odirect(trans
, inode
, start
, len
) == 1) {
5790 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
5791 block_start
, len
, len
, type
);
5792 btrfs_end_transaction(trans
, root
);
5794 free_extent_map(em
);
5799 btrfs_end_transaction(trans
, root
);
5803 * this will cow the extent, reset the len in case we changed
5806 len
= bh_result
->b_size
;
5807 em
= btrfs_new_extent_direct(inode
, em
, start
, len
);
5810 len
= min(len
, em
->len
- (start
- em
->start
));
5812 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
- 1,
5813 EXTENT_LOCKED
| EXTENT_DELALLOC
| EXTENT_DIRTY
, 1,
5816 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
5818 bh_result
->b_size
= len
;
5819 bh_result
->b_bdev
= em
->bdev
;
5820 set_buffer_mapped(bh_result
);
5821 if (create
&& !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
5822 set_buffer_new(bh_result
);
5824 free_extent_map(em
);
5829 struct btrfs_dio_private
{
5830 struct inode
*inode
;
5837 /* number of bios pending for this dio */
5838 atomic_t pending_bios
;
5843 struct bio
*orig_bio
;
5846 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
5848 struct btrfs_dio_private
*dip
= bio
->bi_private
;
5849 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
5850 struct bio_vec
*bvec
= bio
->bi_io_vec
;
5851 struct inode
*inode
= dip
->inode
;
5852 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5854 u32
*private = dip
->csums
;
5856 start
= dip
->logical_offset
;
5858 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
5859 struct page
*page
= bvec
->bv_page
;
5862 unsigned long flags
;
5864 local_irq_save(flags
);
5865 kaddr
= kmap_atomic(page
);
5866 csum
= btrfs_csum_data(root
, kaddr
+ bvec
->bv_offset
,
5867 csum
, bvec
->bv_len
);
5868 btrfs_csum_final(csum
, (char *)&csum
);
5869 kunmap_atomic(kaddr
);
5870 local_irq_restore(flags
);
5872 flush_dcache_page(bvec
->bv_page
);
5873 if (csum
!= *private) {
5874 printk(KERN_ERR
"btrfs csum failed ino %llu off"
5875 " %llu csum %u private %u\n",
5876 (unsigned long long)btrfs_ino(inode
),
5877 (unsigned long long)start
,
5883 start
+= bvec
->bv_len
;
5886 } while (bvec
<= bvec_end
);
5888 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
5889 dip
->logical_offset
+ dip
->bytes
- 1);
5890 bio
->bi_private
= dip
->private;
5895 /* If we had a csum failure make sure to clear the uptodate flag */
5897 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
5898 dio_end_io(bio
, err
);
5901 static void btrfs_endio_direct_write(struct bio
*bio
, int err
)
5903 struct btrfs_dio_private
*dip
= bio
->bi_private
;
5904 struct inode
*inode
= dip
->inode
;
5905 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5906 struct btrfs_trans_handle
*trans
;
5907 struct btrfs_ordered_extent
*ordered
= NULL
;
5908 struct extent_state
*cached_state
= NULL
;
5909 u64 ordered_offset
= dip
->logical_offset
;
5910 u64 ordered_bytes
= dip
->bytes
;
5916 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
5924 trans
= btrfs_join_transaction(root
);
5925 if (IS_ERR(trans
)) {
5929 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
5931 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
)) {
5932 ret
= btrfs_ordered_update_i_size(inode
, 0, ordered
);
5934 err
= btrfs_update_inode_fallback(trans
, root
, inode
);
5938 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, ordered
->file_offset
,
5939 ordered
->file_offset
+ ordered
->len
- 1, 0,
5942 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
)) {
5943 ret
= btrfs_mark_extent_written(trans
, inode
,
5944 ordered
->file_offset
,
5945 ordered
->file_offset
+
5952 ret
= insert_reserved_file_extent(trans
, inode
,
5953 ordered
->file_offset
,
5959 BTRFS_FILE_EXTENT_REG
);
5960 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
5961 ordered
->file_offset
, ordered
->len
);
5969 add_pending_csums(trans
, inode
, ordered
->file_offset
, &ordered
->list
);
5970 ret
= btrfs_ordered_update_i_size(inode
, 0, ordered
);
5971 if (!ret
|| !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
))
5972 btrfs_update_inode_fallback(trans
, root
, inode
);
5975 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, ordered
->file_offset
,
5976 ordered
->file_offset
+ ordered
->len
- 1,
5977 &cached_state
, GFP_NOFS
);
5979 btrfs_delalloc_release_metadata(inode
, ordered
->len
);
5980 btrfs_end_transaction(trans
, root
);
5981 ordered_offset
= ordered
->file_offset
+ ordered
->len
;
5982 btrfs_put_ordered_extent(ordered
);
5983 btrfs_put_ordered_extent(ordered
);
5987 * our bio might span multiple ordered extents. If we haven't
5988 * completed the accounting for the whole dio, go back and try again
5990 if (ordered_offset
< dip
->logical_offset
+ dip
->bytes
) {
5991 ordered_bytes
= dip
->logical_offset
+ dip
->bytes
-
5996 bio
->bi_private
= dip
->private;
6001 /* If we had an error make sure to clear the uptodate flag */
6003 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
6004 dio_end_io(bio
, err
);
6007 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
6008 struct bio
*bio
, int mirror_num
,
6009 unsigned long bio_flags
, u64 offset
)
6012 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6013 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
6014 BUG_ON(ret
); /* -ENOMEM */
6018 static void btrfs_end_dio_bio(struct bio
*bio
, int err
)
6020 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6023 printk(KERN_ERR
"btrfs direct IO failed ino %llu rw %lu "
6024 "sector %#Lx len %u err no %d\n",
6025 (unsigned long long)btrfs_ino(dip
->inode
), bio
->bi_rw
,
6026 (unsigned long long)bio
->bi_sector
, bio
->bi_size
, err
);
6030 * before atomic variable goto zero, we must make sure
6031 * dip->errors is perceived to be set.
6033 smp_mb__before_atomic_dec();
6036 /* if there are more bios still pending for this dio, just exit */
6037 if (!atomic_dec_and_test(&dip
->pending_bios
))
6041 bio_io_error(dip
->orig_bio
);
6043 set_bit(BIO_UPTODATE
, &dip
->orig_bio
->bi_flags
);
6044 bio_endio(dip
->orig_bio
, 0);
6050 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
6051 u64 first_sector
, gfp_t gfp_flags
)
6053 int nr_vecs
= bio_get_nr_vecs(bdev
);
6054 return btrfs_bio_alloc(bdev
, first_sector
, nr_vecs
, gfp_flags
);
6057 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
6058 int rw
, u64 file_offset
, int skip_sum
,
6059 u32
*csums
, int async_submit
)
6061 int write
= rw
& REQ_WRITE
;
6062 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6066 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
6073 if (write
&& async_submit
) {
6074 ret
= btrfs_wq_submit_bio(root
->fs_info
,
6075 inode
, rw
, bio
, 0, 0,
6077 __btrfs_submit_bio_start_direct_io
,
6078 __btrfs_submit_bio_done
);
6082 * If we aren't doing async submit, calculate the csum of the
6085 ret
= btrfs_csum_one_bio(root
, inode
, bio
, file_offset
, 1);
6088 } else if (!skip_sum
) {
6089 ret
= btrfs_lookup_bio_sums_dio(root
, inode
, bio
,
6090 file_offset
, csums
);
6096 ret
= btrfs_map_bio(root
, rw
, bio
, 0, async_submit
);
6102 static int btrfs_submit_direct_hook(int rw
, struct btrfs_dio_private
*dip
,
6105 struct inode
*inode
= dip
->inode
;
6106 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6107 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
6109 struct bio
*orig_bio
= dip
->orig_bio
;
6110 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
6111 u64 start_sector
= orig_bio
->bi_sector
;
6112 u64 file_offset
= dip
->logical_offset
;
6116 u32
*csums
= dip
->csums
;
6118 int async_submit
= 0;
6119 int write
= rw
& REQ_WRITE
;
6121 map_length
= orig_bio
->bi_size
;
6122 ret
= btrfs_map_block(map_tree
, READ
, start_sector
<< 9,
6123 &map_length
, NULL
, 0);
6129 if (map_length
>= orig_bio
->bi_size
) {
6135 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
6138 bio
->bi_private
= dip
;
6139 bio
->bi_end_io
= btrfs_end_dio_bio
;
6140 atomic_inc(&dip
->pending_bios
);
6142 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
6143 if (unlikely(map_length
< submit_len
+ bvec
->bv_len
||
6144 bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
6145 bvec
->bv_offset
) < bvec
->bv_len
)) {
6147 * inc the count before we submit the bio so
6148 * we know the end IO handler won't happen before
6149 * we inc the count. Otherwise, the dip might get freed
6150 * before we're done setting it up
6152 atomic_inc(&dip
->pending_bios
);
6153 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
,
6154 file_offset
, skip_sum
,
6155 csums
, async_submit
);
6158 atomic_dec(&dip
->pending_bios
);
6162 /* Write's use the ordered csums */
6163 if (!write
&& !skip_sum
)
6164 csums
= csums
+ nr_pages
;
6165 start_sector
+= submit_len
>> 9;
6166 file_offset
+= submit_len
;
6171 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
6172 start_sector
, GFP_NOFS
);
6175 bio
->bi_private
= dip
;
6176 bio
->bi_end_io
= btrfs_end_dio_bio
;
6178 map_length
= orig_bio
->bi_size
;
6179 ret
= btrfs_map_block(map_tree
, READ
, start_sector
<< 9,
6180 &map_length
, NULL
, 0);
6186 submit_len
+= bvec
->bv_len
;
6193 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
, file_offset
, skip_sum
,
6194 csums
, async_submit
);
6202 * before atomic variable goto zero, we must
6203 * make sure dip->errors is perceived to be set.
6205 smp_mb__before_atomic_dec();
6206 if (atomic_dec_and_test(&dip
->pending_bios
))
6207 bio_io_error(dip
->orig_bio
);
6209 /* bio_end_io() will handle error, so we needn't return it */
6213 static void btrfs_submit_direct(int rw
, struct bio
*bio
, struct inode
*inode
,
6216 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6217 struct btrfs_dio_private
*dip
;
6218 struct bio_vec
*bvec
= bio
->bi_io_vec
;
6220 int write
= rw
& REQ_WRITE
;
6223 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
6225 dip
= kmalloc(sizeof(*dip
), GFP_NOFS
);
6232 /* Write's use the ordered csum stuff, so we don't need dip->csums */
6233 if (!write
&& !skip_sum
) {
6234 dip
->csums
= kmalloc(sizeof(u32
) * bio
->bi_vcnt
, GFP_NOFS
);
6242 dip
->private = bio
->bi_private
;
6244 dip
->logical_offset
= file_offset
;
6248 dip
->bytes
+= bvec
->bv_len
;
6250 } while (bvec
<= (bio
->bi_io_vec
+ bio
->bi_vcnt
- 1));
6252 dip
->disk_bytenr
= (u64
)bio
->bi_sector
<< 9;
6253 bio
->bi_private
= dip
;
6255 dip
->orig_bio
= bio
;
6256 atomic_set(&dip
->pending_bios
, 0);
6259 bio
->bi_end_io
= btrfs_endio_direct_write
;
6261 bio
->bi_end_io
= btrfs_endio_direct_read
;
6263 ret
= btrfs_submit_direct_hook(rw
, dip
, skip_sum
);
6268 * If this is a write, we need to clean up the reserved space and kill
6269 * the ordered extent.
6272 struct btrfs_ordered_extent
*ordered
;
6273 ordered
= btrfs_lookup_ordered_extent(inode
, file_offset
);
6274 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
6275 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
6276 btrfs_free_reserved_extent(root
, ordered
->start
,
6278 btrfs_put_ordered_extent(ordered
);
6279 btrfs_put_ordered_extent(ordered
);
6281 bio_endio(bio
, ret
);
6284 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
6285 const struct iovec
*iov
, loff_t offset
,
6286 unsigned long nr_segs
)
6292 unsigned blocksize_mask
= root
->sectorsize
- 1;
6293 ssize_t retval
= -EINVAL
;
6294 loff_t end
= offset
;
6296 if (offset
& blocksize_mask
)
6299 /* Check the memory alignment. Blocks cannot straddle pages */
6300 for (seg
= 0; seg
< nr_segs
; seg
++) {
6301 addr
= (unsigned long)iov
[seg
].iov_base
;
6302 size
= iov
[seg
].iov_len
;
6304 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
6307 /* If this is a write we don't need to check anymore */
6312 * Check to make sure we don't have duplicate iov_base's in this
6313 * iovec, if so return EINVAL, otherwise we'll get csum errors
6314 * when reading back.
6316 for (i
= seg
+ 1; i
< nr_segs
; i
++) {
6317 if (iov
[seg
].iov_base
== iov
[i
].iov_base
)
6325 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
6326 const struct iovec
*iov
, loff_t offset
,
6327 unsigned long nr_segs
)
6329 struct file
*file
= iocb
->ki_filp
;
6330 struct inode
*inode
= file
->f_mapping
->host
;
6331 struct btrfs_ordered_extent
*ordered
;
6332 struct extent_state
*cached_state
= NULL
;
6333 u64 lockstart
, lockend
;
6335 int writing
= rw
& WRITE
;
6337 size_t count
= iov_length(iov
, nr_segs
);
6339 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iov
,
6345 lockend
= offset
+ count
- 1;
6348 ret
= btrfs_delalloc_reserve_space(inode
, count
);
6354 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6357 * We're concerned with the entire range that we're going to be
6358 * doing DIO to, so we need to make sure theres no ordered
6359 * extents in this range.
6361 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
6362 lockend
- lockstart
+ 1);
6365 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6366 &cached_state
, GFP_NOFS
);
6367 btrfs_start_ordered_extent(inode
, ordered
, 1);
6368 btrfs_put_ordered_extent(ordered
);
6373 * we don't use btrfs_set_extent_delalloc because we don't want
6374 * the dirty or uptodate bits
6377 write_bits
= EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
;
6378 ret
= set_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6379 EXTENT_DELALLOC
, NULL
, &cached_state
,
6382 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6383 lockend
, EXTENT_LOCKED
| write_bits
,
6384 1, 0, &cached_state
, GFP_NOFS
);
6389 free_extent_state(cached_state
);
6390 cached_state
= NULL
;
6392 ret
= __blockdev_direct_IO(rw
, iocb
, inode
,
6393 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
6394 iov
, offset
, nr_segs
, btrfs_get_blocks_direct
, NULL
,
6395 btrfs_submit_direct
, 0);
6397 if (ret
< 0 && ret
!= -EIOCBQUEUED
) {
6398 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, offset
,
6399 offset
+ iov_length(iov
, nr_segs
) - 1,
6400 EXTENT_LOCKED
| write_bits
, 1, 0,
6401 &cached_state
, GFP_NOFS
);
6402 } else if (ret
>= 0 && ret
< iov_length(iov
, nr_segs
)) {
6404 * We're falling back to buffered, unlock the section we didn't
6407 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, offset
+ ret
,
6408 offset
+ iov_length(iov
, nr_segs
) - 1,
6409 EXTENT_LOCKED
| write_bits
, 1, 0,
6410 &cached_state
, GFP_NOFS
);
6413 free_extent_state(cached_state
);
6417 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
6418 __u64 start
, __u64 len
)
6420 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
6423 int btrfs_readpage(struct file
*file
, struct page
*page
)
6425 struct extent_io_tree
*tree
;
6426 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6427 return extent_read_full_page(tree
, page
, btrfs_get_extent
, 0);
6430 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
6432 struct extent_io_tree
*tree
;
6435 if (current
->flags
& PF_MEMALLOC
) {
6436 redirty_page_for_writepage(wbc
, page
);
6440 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6441 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
6444 int btrfs_writepages(struct address_space
*mapping
,
6445 struct writeback_control
*wbc
)
6447 struct extent_io_tree
*tree
;
6449 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
6450 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
6454 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
6455 struct list_head
*pages
, unsigned nr_pages
)
6457 struct extent_io_tree
*tree
;
6458 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
6459 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
6462 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
6464 struct extent_io_tree
*tree
;
6465 struct extent_map_tree
*map
;
6468 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6469 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
6470 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
6472 ClearPagePrivate(page
);
6473 set_page_private(page
, 0);
6474 page_cache_release(page
);
6479 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
6481 if (PageWriteback(page
) || PageDirty(page
))
6483 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
6486 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
6488 struct extent_io_tree
*tree
;
6489 struct btrfs_ordered_extent
*ordered
;
6490 struct extent_state
*cached_state
= NULL
;
6491 u64 page_start
= page_offset(page
);
6492 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6496 * we have the page locked, so new writeback can't start,
6497 * and the dirty bit won't be cleared while we are here.
6499 * Wait for IO on this page so that we can safely clear
6500 * the PagePrivate2 bit and do ordered accounting
6502 wait_on_page_writeback(page
);
6504 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
6506 btrfs_releasepage(page
, GFP_NOFS
);
6509 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
6510 ordered
= btrfs_lookup_ordered_extent(page
->mapping
->host
,
6514 * IO on this page will never be started, so we need
6515 * to account for any ordered extents now
6517 clear_extent_bit(tree
, page_start
, page_end
,
6518 EXTENT_DIRTY
| EXTENT_DELALLOC
|
6519 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
, 1, 0,
6520 &cached_state
, GFP_NOFS
);
6522 * whoever cleared the private bit is responsible
6523 * for the finish_ordered_io
6525 if (TestClearPagePrivate2(page
)) {
6526 btrfs_finish_ordered_io(page
->mapping
->host
,
6527 page_start
, page_end
);
6529 btrfs_put_ordered_extent(ordered
);
6530 cached_state
= NULL
;
6531 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
6533 clear_extent_bit(tree
, page_start
, page_end
,
6534 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
6535 EXTENT_DO_ACCOUNTING
, 1, 1, &cached_state
, GFP_NOFS
);
6536 __btrfs_releasepage(page
, GFP_NOFS
);
6538 ClearPageChecked(page
);
6539 if (PagePrivate(page
)) {
6540 ClearPagePrivate(page
);
6541 set_page_private(page
, 0);
6542 page_cache_release(page
);
6547 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6548 * called from a page fault handler when a page is first dirtied. Hence we must
6549 * be careful to check for EOF conditions here. We set the page up correctly
6550 * for a written page which means we get ENOSPC checking when writing into
6551 * holes and correct delalloc and unwritten extent mapping on filesystems that
6552 * support these features.
6554 * We are not allowed to take the i_mutex here so we have to play games to
6555 * protect against truncate races as the page could now be beyond EOF. Because
6556 * vmtruncate() writes the inode size before removing pages, once we have the
6557 * page lock we can determine safely if the page is beyond EOF. If it is not
6558 * beyond EOF, then the page is guaranteed safe against truncation until we
6561 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
6563 struct page
*page
= vmf
->page
;
6564 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
6565 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6566 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6567 struct btrfs_ordered_extent
*ordered
;
6568 struct extent_state
*cached_state
= NULL
;
6570 unsigned long zero_start
;
6577 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
6579 ret
= btrfs_update_time(vma
->vm_file
);
6585 else /* -ENOSPC, -EIO, etc */
6586 ret
= VM_FAULT_SIGBUS
;
6592 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
6595 size
= i_size_read(inode
);
6596 page_start
= page_offset(page
);
6597 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
6599 if ((page
->mapping
!= inode
->i_mapping
) ||
6600 (page_start
>= size
)) {
6601 /* page got truncated out from underneath us */
6604 wait_on_page_writeback(page
);
6606 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
6607 set_page_extent_mapped(page
);
6610 * we can't set the delalloc bits if there are pending ordered
6611 * extents. Drop our locks and wait for them to finish
6613 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
6615 unlock_extent_cached(io_tree
, page_start
, page_end
,
6616 &cached_state
, GFP_NOFS
);
6618 btrfs_start_ordered_extent(inode
, ordered
, 1);
6619 btrfs_put_ordered_extent(ordered
);
6624 * XXX - page_mkwrite gets called every time the page is dirtied, even
6625 * if it was already dirty, so for space accounting reasons we need to
6626 * clear any delalloc bits for the range we are fixing to save. There
6627 * is probably a better way to do this, but for now keep consistent with
6628 * prepare_pages in the normal write path.
6630 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
6631 EXTENT_DIRTY
| EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
,
6632 0, 0, &cached_state
, GFP_NOFS
);
6634 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
6637 unlock_extent_cached(io_tree
, page_start
, page_end
,
6638 &cached_state
, GFP_NOFS
);
6639 ret
= VM_FAULT_SIGBUS
;
6644 /* page is wholly or partially inside EOF */
6645 if (page_start
+ PAGE_CACHE_SIZE
> size
)
6646 zero_start
= size
& ~PAGE_CACHE_MASK
;
6648 zero_start
= PAGE_CACHE_SIZE
;
6650 if (zero_start
!= PAGE_CACHE_SIZE
) {
6652 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
6653 flush_dcache_page(page
);
6656 ClearPageChecked(page
);
6657 set_page_dirty(page
);
6658 SetPageUptodate(page
);
6660 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
6661 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
6663 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
6667 return VM_FAULT_LOCKED
;
6670 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
6675 static int btrfs_truncate(struct inode
*inode
)
6677 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6678 struct btrfs_block_rsv
*rsv
;
6681 struct btrfs_trans_handle
*trans
;
6683 u64 mask
= root
->sectorsize
- 1;
6684 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
6686 ret
= btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
6690 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
6691 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
6694 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6695 * 3 things going on here
6697 * 1) We need to reserve space for our orphan item and the space to
6698 * delete our orphan item. Lord knows we don't want to have a dangling
6699 * orphan item because we didn't reserve space to remove it.
6701 * 2) We need to reserve space to update our inode.
6703 * 3) We need to have something to cache all the space that is going to
6704 * be free'd up by the truncate operation, but also have some slack
6705 * space reserved in case it uses space during the truncate (thank you
6706 * very much snapshotting).
6708 * And we need these to all be seperate. The fact is we can use alot of
6709 * space doing the truncate, and we have no earthly idea how much space
6710 * we will use, so we need the truncate reservation to be seperate so it
6711 * doesn't end up using space reserved for updating the inode or
6712 * removing the orphan item. We also need to be able to stop the
6713 * transaction and start a new one, which means we need to be able to
6714 * update the inode several times, and we have no idea of knowing how
6715 * many times that will be, so we can't just reserve 1 item for the
6716 * entirety of the opration, so that has to be done seperately as well.
6717 * Then there is the orphan item, which does indeed need to be held on
6718 * to for the whole operation, and we need nobody to touch this reserved
6719 * space except the orphan code.
6721 * So that leaves us with
6723 * 1) root->orphan_block_rsv - for the orphan deletion.
6724 * 2) rsv - for the truncate reservation, which we will steal from the
6725 * transaction reservation.
6726 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6727 * updating the inode.
6729 rsv
= btrfs_alloc_block_rsv(root
);
6732 rsv
->size
= min_size
;
6735 * 1 for the truncate slack space
6736 * 1 for the orphan item we're going to add
6737 * 1 for the orphan item deletion
6738 * 1 for updating the inode.
6740 trans
= btrfs_start_transaction(root
, 4);
6741 if (IS_ERR(trans
)) {
6742 err
= PTR_ERR(trans
);
6746 /* Migrate the slack space for the truncate to our reserve */
6747 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
, rsv
,
6751 ret
= btrfs_orphan_add(trans
, inode
);
6753 btrfs_end_transaction(trans
, root
);
6758 * setattr is responsible for setting the ordered_data_close flag,
6759 * but that is only tested during the last file release. That
6760 * could happen well after the next commit, leaving a great big
6761 * window where new writes may get lost if someone chooses to write
6762 * to this file after truncating to zero
6764 * The inode doesn't have any dirty data here, and so if we commit
6765 * this is a noop. If someone immediately starts writing to the inode
6766 * it is very likely we'll catch some of their writes in this
6767 * transaction, and the commit will find this file on the ordered
6768 * data list with good things to send down.
6770 * This is a best effort solution, there is still a window where
6771 * using truncate to replace the contents of the file will
6772 * end up with a zero length file after a crash.
6774 if (inode
->i_size
== 0 && BTRFS_I(inode
)->ordered_data_close
)
6775 btrfs_add_ordered_operation(trans
, root
, inode
);
6778 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
);
6781 * This can only happen with the original transaction we
6782 * started above, every other time we shouldn't have a
6783 * transaction started yet.
6792 /* Just need the 1 for updating the inode */
6793 trans
= btrfs_start_transaction(root
, 1);
6794 if (IS_ERR(trans
)) {
6795 ret
= err
= PTR_ERR(trans
);
6801 trans
->block_rsv
= rsv
;
6803 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
6805 BTRFS_EXTENT_DATA_KEY
);
6806 if (ret
!= -EAGAIN
) {
6811 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
6812 ret
= btrfs_update_inode(trans
, root
, inode
);
6818 nr
= trans
->blocks_used
;
6819 btrfs_end_transaction(trans
, root
);
6821 btrfs_btree_balance_dirty(root
, nr
);
6824 if (ret
== 0 && inode
->i_nlink
> 0) {
6825 trans
->block_rsv
= root
->orphan_block_rsv
;
6826 ret
= btrfs_orphan_del(trans
, inode
);
6829 } else if (ret
&& inode
->i_nlink
> 0) {
6831 * Failed to do the truncate, remove us from the in memory
6834 ret
= btrfs_orphan_del(NULL
, inode
);
6838 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
6839 ret
= btrfs_update_inode(trans
, root
, inode
);
6843 nr
= trans
->blocks_used
;
6844 ret
= btrfs_end_transaction(trans
, root
);
6845 btrfs_btree_balance_dirty(root
, nr
);
6849 btrfs_free_block_rsv(root
, rsv
);
6858 * create a new subvolume directory/inode (helper for the ioctl).
6860 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
6861 struct btrfs_root
*new_root
, u64 new_dirid
)
6863 struct inode
*inode
;
6867 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
6868 new_dirid
, new_dirid
,
6869 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
6872 return PTR_ERR(inode
);
6873 inode
->i_op
= &btrfs_dir_inode_operations
;
6874 inode
->i_fop
= &btrfs_dir_file_operations
;
6876 set_nlink(inode
, 1);
6877 btrfs_i_size_write(inode
, 0);
6879 err
= btrfs_update_inode(trans
, new_root
, inode
);
6885 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
6887 struct btrfs_inode
*ei
;
6888 struct inode
*inode
;
6890 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
6895 ei
->space_info
= NULL
;
6899 ei
->last_sub_trans
= 0;
6900 ei
->logged_trans
= 0;
6901 ei
->delalloc_bytes
= 0;
6902 ei
->disk_i_size
= 0;
6905 ei
->index_cnt
= (u64
)-1;
6906 ei
->last_unlink_trans
= 0;
6908 spin_lock_init(&ei
->lock
);
6909 ei
->outstanding_extents
= 0;
6910 ei
->reserved_extents
= 0;
6912 ei
->ordered_data_close
= 0;
6913 ei
->orphan_meta_reserved
= 0;
6914 ei
->dummy_inode
= 0;
6916 ei
->delalloc_meta_reserved
= 0;
6917 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
6919 ei
->delayed_node
= NULL
;
6921 inode
= &ei
->vfs_inode
;
6922 extent_map_tree_init(&ei
->extent_tree
);
6923 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
);
6924 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
);
6925 ei
->io_tree
.track_uptodate
= 1;
6926 ei
->io_failure_tree
.track_uptodate
= 1;
6927 mutex_init(&ei
->log_mutex
);
6928 mutex_init(&ei
->delalloc_mutex
);
6929 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
6930 INIT_LIST_HEAD(&ei
->i_orphan
);
6931 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
6932 INIT_LIST_HEAD(&ei
->ordered_operations
);
6933 RB_CLEAR_NODE(&ei
->rb_node
);
6938 static void btrfs_i_callback(struct rcu_head
*head
)
6940 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
6941 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
6944 void btrfs_destroy_inode(struct inode
*inode
)
6946 struct btrfs_ordered_extent
*ordered
;
6947 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6949 WARN_ON(!list_empty(&inode
->i_dentry
));
6950 WARN_ON(inode
->i_data
.nrpages
);
6951 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
6952 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
6953 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
6954 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
6957 * This can happen where we create an inode, but somebody else also
6958 * created the same inode and we need to destroy the one we already
6965 * Make sure we're properly removed from the ordered operation
6969 if (!list_empty(&BTRFS_I(inode
)->ordered_operations
)) {
6970 spin_lock(&root
->fs_info
->ordered_extent_lock
);
6971 list_del_init(&BTRFS_I(inode
)->ordered_operations
);
6972 spin_unlock(&root
->fs_info
->ordered_extent_lock
);
6975 spin_lock(&root
->orphan_lock
);
6976 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
6977 printk(KERN_INFO
"BTRFS: inode %llu still on the orphan list\n",
6978 (unsigned long long)btrfs_ino(inode
));
6979 list_del_init(&BTRFS_I(inode
)->i_orphan
);
6981 spin_unlock(&root
->orphan_lock
);
6984 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
6988 printk(KERN_ERR
"btrfs found ordered "
6989 "extent %llu %llu on inode cleanup\n",
6990 (unsigned long long)ordered
->file_offset
,
6991 (unsigned long long)ordered
->len
);
6992 btrfs_remove_ordered_extent(inode
, ordered
);
6993 btrfs_put_ordered_extent(ordered
);
6994 btrfs_put_ordered_extent(ordered
);
6997 inode_tree_del(inode
);
6998 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
7000 btrfs_remove_delayed_node(inode
);
7001 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
7004 int btrfs_drop_inode(struct inode
*inode
)
7006 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7008 if (btrfs_root_refs(&root
->root_item
) == 0 &&
7009 !btrfs_is_free_space_inode(root
, inode
))
7012 return generic_drop_inode(inode
);
7015 static void init_once(void *foo
)
7017 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
7019 inode_init_once(&ei
->vfs_inode
);
7022 void btrfs_destroy_cachep(void)
7024 if (btrfs_inode_cachep
)
7025 kmem_cache_destroy(btrfs_inode_cachep
);
7026 if (btrfs_trans_handle_cachep
)
7027 kmem_cache_destroy(btrfs_trans_handle_cachep
);
7028 if (btrfs_transaction_cachep
)
7029 kmem_cache_destroy(btrfs_transaction_cachep
);
7030 if (btrfs_path_cachep
)
7031 kmem_cache_destroy(btrfs_path_cachep
);
7032 if (btrfs_free_space_cachep
)
7033 kmem_cache_destroy(btrfs_free_space_cachep
);
7036 int btrfs_init_cachep(void)
7038 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode_cache",
7039 sizeof(struct btrfs_inode
), 0,
7040 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
7041 if (!btrfs_inode_cachep
)
7044 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle_cache",
7045 sizeof(struct btrfs_trans_handle
), 0,
7046 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7047 if (!btrfs_trans_handle_cachep
)
7050 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction_cache",
7051 sizeof(struct btrfs_transaction
), 0,
7052 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7053 if (!btrfs_transaction_cachep
)
7056 btrfs_path_cachep
= kmem_cache_create("btrfs_path_cache",
7057 sizeof(struct btrfs_path
), 0,
7058 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7059 if (!btrfs_path_cachep
)
7062 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space_cache",
7063 sizeof(struct btrfs_free_space
), 0,
7064 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7065 if (!btrfs_free_space_cachep
)
7070 btrfs_destroy_cachep();
7074 static int btrfs_getattr(struct vfsmount
*mnt
,
7075 struct dentry
*dentry
, struct kstat
*stat
)
7077 struct inode
*inode
= dentry
->d_inode
;
7078 u32 blocksize
= inode
->i_sb
->s_blocksize
;
7080 generic_fillattr(inode
, stat
);
7081 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
7082 stat
->blksize
= PAGE_CACHE_SIZE
;
7083 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
7084 ALIGN(BTRFS_I(inode
)->delalloc_bytes
, blocksize
)) >> 9;
7089 * If a file is moved, it will inherit the cow and compression flags of the new
7092 static void fixup_inode_flags(struct inode
*dir
, struct inode
*inode
)
7094 struct btrfs_inode
*b_dir
= BTRFS_I(dir
);
7095 struct btrfs_inode
*b_inode
= BTRFS_I(inode
);
7097 if (b_dir
->flags
& BTRFS_INODE_NODATACOW
)
7098 b_inode
->flags
|= BTRFS_INODE_NODATACOW
;
7100 b_inode
->flags
&= ~BTRFS_INODE_NODATACOW
;
7102 if (b_dir
->flags
& BTRFS_INODE_COMPRESS
)
7103 b_inode
->flags
|= BTRFS_INODE_COMPRESS
;
7105 b_inode
->flags
&= ~BTRFS_INODE_COMPRESS
;
7108 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
7109 struct inode
*new_dir
, struct dentry
*new_dentry
)
7111 struct btrfs_trans_handle
*trans
;
7112 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
7113 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
7114 struct inode
*new_inode
= new_dentry
->d_inode
;
7115 struct inode
*old_inode
= old_dentry
->d_inode
;
7116 struct timespec ctime
= CURRENT_TIME
;
7120 u64 old_ino
= btrfs_ino(old_inode
);
7122 if (btrfs_ino(new_dir
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
7125 /* we only allow rename subvolume link between subvolumes */
7126 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
7129 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
7130 (new_inode
&& btrfs_ino(new_inode
) == BTRFS_FIRST_FREE_OBJECTID
))
7133 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
7134 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
7137 * we're using rename to replace one file with another.
7138 * and the replacement file is large. Start IO on it now so
7139 * we don't add too much work to the end of the transaction
7141 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
&&
7142 old_inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
7143 filemap_flush(old_inode
->i_mapping
);
7145 /* close the racy window with snapshot create/destroy ioctl */
7146 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
7147 down_read(&root
->fs_info
->subvol_sem
);
7149 * We want to reserve the absolute worst case amount of items. So if
7150 * both inodes are subvols and we need to unlink them then that would
7151 * require 4 item modifications, but if they are both normal inodes it
7152 * would require 5 item modifications, so we'll assume their normal
7153 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7154 * should cover the worst case number of items we'll modify.
7156 trans
= btrfs_start_transaction(root
, 20);
7157 if (IS_ERR(trans
)) {
7158 ret
= PTR_ERR(trans
);
7163 btrfs_record_root_in_trans(trans
, dest
);
7165 ret
= btrfs_set_inode_index(new_dir
, &index
);
7169 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
7170 /* force full log commit if subvolume involved. */
7171 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
7173 ret
= btrfs_insert_inode_ref(trans
, dest
,
7174 new_dentry
->d_name
.name
,
7175 new_dentry
->d_name
.len
,
7177 btrfs_ino(new_dir
), index
);
7181 * this is an ugly little race, but the rename is required
7182 * to make sure that if we crash, the inode is either at the
7183 * old name or the new one. pinning the log transaction lets
7184 * us make sure we don't allow a log commit to come in after
7185 * we unlink the name but before we add the new name back in.
7187 btrfs_pin_log_trans(root
);
7190 * make sure the inode gets flushed if it is replacing
7193 if (new_inode
&& new_inode
->i_size
&& S_ISREG(old_inode
->i_mode
))
7194 btrfs_add_ordered_operation(trans
, root
, old_inode
);
7196 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
7197 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
7198 old_inode
->i_ctime
= ctime
;
7200 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
7201 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
7203 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
7204 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
7205 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
7206 old_dentry
->d_name
.name
,
7207 old_dentry
->d_name
.len
);
7209 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
7210 old_dentry
->d_inode
,
7211 old_dentry
->d_name
.name
,
7212 old_dentry
->d_name
.len
);
7214 ret
= btrfs_update_inode(trans
, root
, old_inode
);
7217 btrfs_abort_transaction(trans
, root
, ret
);
7222 new_inode
->i_ctime
= CURRENT_TIME
;
7223 if (unlikely(btrfs_ino(new_inode
) ==
7224 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
7225 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
7226 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
7228 new_dentry
->d_name
.name
,
7229 new_dentry
->d_name
.len
);
7230 BUG_ON(new_inode
->i_nlink
== 0);
7232 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
7233 new_dentry
->d_inode
,
7234 new_dentry
->d_name
.name
,
7235 new_dentry
->d_name
.len
);
7237 if (!ret
&& new_inode
->i_nlink
== 0) {
7238 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
7242 btrfs_abort_transaction(trans
, root
, ret
);
7247 fixup_inode_flags(new_dir
, old_inode
);
7249 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
7250 new_dentry
->d_name
.name
,
7251 new_dentry
->d_name
.len
, 0, index
);
7253 btrfs_abort_transaction(trans
, root
, ret
);
7257 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
7258 struct dentry
*parent
= new_dentry
->d_parent
;
7259 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
7260 btrfs_end_log_trans(root
);
7263 btrfs_end_transaction(trans
, root
);
7265 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
7266 up_read(&root
->fs_info
->subvol_sem
);
7272 * some fairly slow code that needs optimization. This walks the list
7273 * of all the inodes with pending delalloc and forces them to disk.
7275 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
7277 struct list_head
*head
= &root
->fs_info
->delalloc_inodes
;
7278 struct btrfs_inode
*binode
;
7279 struct inode
*inode
;
7281 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
7284 spin_lock(&root
->fs_info
->delalloc_lock
);
7285 while (!list_empty(head
)) {
7286 binode
= list_entry(head
->next
, struct btrfs_inode
,
7288 inode
= igrab(&binode
->vfs_inode
);
7290 list_del_init(&binode
->delalloc_inodes
);
7291 spin_unlock(&root
->fs_info
->delalloc_lock
);
7293 filemap_flush(inode
->i_mapping
);
7295 btrfs_add_delayed_iput(inode
);
7300 spin_lock(&root
->fs_info
->delalloc_lock
);
7302 spin_unlock(&root
->fs_info
->delalloc_lock
);
7304 /* the filemap_flush will queue IO into the worker threads, but
7305 * we have to make sure the IO is actually started and that
7306 * ordered extents get created before we return
7308 atomic_inc(&root
->fs_info
->async_submit_draining
);
7309 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
7310 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
7311 wait_event(root
->fs_info
->async_submit_wait
,
7312 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
7313 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
7315 atomic_dec(&root
->fs_info
->async_submit_draining
);
7319 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
7320 const char *symname
)
7322 struct btrfs_trans_handle
*trans
;
7323 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
7324 struct btrfs_path
*path
;
7325 struct btrfs_key key
;
7326 struct inode
*inode
= NULL
;
7334 struct btrfs_file_extent_item
*ei
;
7335 struct extent_buffer
*leaf
;
7336 unsigned long nr
= 0;
7338 name_len
= strlen(symname
) + 1;
7339 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
7340 return -ENAMETOOLONG
;
7343 * 2 items for inode item and ref
7344 * 2 items for dir items
7345 * 1 item for xattr if selinux is on
7347 trans
= btrfs_start_transaction(root
, 5);
7349 return PTR_ERR(trans
);
7351 err
= btrfs_find_free_ino(root
, &objectid
);
7355 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
7356 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
7357 S_IFLNK
|S_IRWXUGO
, &index
);
7358 if (IS_ERR(inode
)) {
7359 err
= PTR_ERR(inode
);
7363 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
7370 * If the active LSM wants to access the inode during
7371 * d_instantiate it needs these. Smack checks to see
7372 * if the filesystem supports xattrs by looking at the
7375 inode
->i_fop
= &btrfs_file_operations
;
7376 inode
->i_op
= &btrfs_file_inode_operations
;
7378 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
7382 inode
->i_mapping
->a_ops
= &btrfs_aops
;
7383 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
7384 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
7389 path
= btrfs_alloc_path();
7395 key
.objectid
= btrfs_ino(inode
);
7397 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
7398 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
7399 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
7403 btrfs_free_path(path
);
7406 leaf
= path
->nodes
[0];
7407 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
7408 struct btrfs_file_extent_item
);
7409 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
7410 btrfs_set_file_extent_type(leaf
, ei
,
7411 BTRFS_FILE_EXTENT_INLINE
);
7412 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
7413 btrfs_set_file_extent_compression(leaf
, ei
, 0);
7414 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
7415 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
7417 ptr
= btrfs_file_extent_inline_start(ei
);
7418 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
7419 btrfs_mark_buffer_dirty(leaf
);
7420 btrfs_free_path(path
);
7422 inode
->i_op
= &btrfs_symlink_inode_operations
;
7423 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
7424 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
7425 inode_set_bytes(inode
, name_len
);
7426 btrfs_i_size_write(inode
, name_len
- 1);
7427 err
= btrfs_update_inode(trans
, root
, inode
);
7433 d_instantiate(dentry
, inode
);
7434 nr
= trans
->blocks_used
;
7435 btrfs_end_transaction(trans
, root
);
7437 inode_dec_link_count(inode
);
7440 btrfs_btree_balance_dirty(root
, nr
);
7444 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
7445 u64 start
, u64 num_bytes
, u64 min_size
,
7446 loff_t actual_len
, u64
*alloc_hint
,
7447 struct btrfs_trans_handle
*trans
)
7449 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7450 struct btrfs_key ins
;
7451 u64 cur_offset
= start
;
7454 bool own_trans
= true;
7458 while (num_bytes
> 0) {
7460 trans
= btrfs_start_transaction(root
, 3);
7461 if (IS_ERR(trans
)) {
7462 ret
= PTR_ERR(trans
);
7467 ret
= btrfs_reserve_extent(trans
, root
, num_bytes
, min_size
,
7468 0, *alloc_hint
, &ins
, 1);
7471 btrfs_end_transaction(trans
, root
);
7475 ret
= insert_reserved_file_extent(trans
, inode
,
7476 cur_offset
, ins
.objectid
,
7477 ins
.offset
, ins
.offset
,
7478 ins
.offset
, 0, 0, 0,
7479 BTRFS_FILE_EXTENT_PREALLOC
);
7481 btrfs_abort_transaction(trans
, root
, ret
);
7483 btrfs_end_transaction(trans
, root
);
7486 btrfs_drop_extent_cache(inode
, cur_offset
,
7487 cur_offset
+ ins
.offset
-1, 0);
7489 num_bytes
-= ins
.offset
;
7490 cur_offset
+= ins
.offset
;
7491 *alloc_hint
= ins
.objectid
+ ins
.offset
;
7493 inode
->i_ctime
= CURRENT_TIME
;
7494 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
7495 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
7496 (actual_len
> inode
->i_size
) &&
7497 (cur_offset
> inode
->i_size
)) {
7498 if (cur_offset
> actual_len
)
7499 i_size
= actual_len
;
7501 i_size
= cur_offset
;
7502 i_size_write(inode
, i_size
);
7503 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
7506 ret
= btrfs_update_inode(trans
, root
, inode
);
7509 btrfs_abort_transaction(trans
, root
, ret
);
7511 btrfs_end_transaction(trans
, root
);
7516 btrfs_end_transaction(trans
, root
);
7521 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
7522 u64 start
, u64 num_bytes
, u64 min_size
,
7523 loff_t actual_len
, u64
*alloc_hint
)
7525 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
7526 min_size
, actual_len
, alloc_hint
,
7530 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
7531 struct btrfs_trans_handle
*trans
, int mode
,
7532 u64 start
, u64 num_bytes
, u64 min_size
,
7533 loff_t actual_len
, u64
*alloc_hint
)
7535 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
7536 min_size
, actual_len
, alloc_hint
, trans
);
7539 static int btrfs_set_page_dirty(struct page
*page
)
7541 return __set_page_dirty_nobuffers(page
);
7544 static int btrfs_permission(struct inode
*inode
, int mask
)
7546 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7547 umode_t mode
= inode
->i_mode
;
7549 if (mask
& MAY_WRITE
&&
7550 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
7551 if (btrfs_root_readonly(root
))
7553 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
7556 return generic_permission(inode
, mask
);
7559 static const struct inode_operations btrfs_dir_inode_operations
= {
7560 .getattr
= btrfs_getattr
,
7561 .lookup
= btrfs_lookup
,
7562 .create
= btrfs_create
,
7563 .unlink
= btrfs_unlink
,
7565 .mkdir
= btrfs_mkdir
,
7566 .rmdir
= btrfs_rmdir
,
7567 .rename
= btrfs_rename
,
7568 .symlink
= btrfs_symlink
,
7569 .setattr
= btrfs_setattr
,
7570 .mknod
= btrfs_mknod
,
7571 .setxattr
= btrfs_setxattr
,
7572 .getxattr
= btrfs_getxattr
,
7573 .listxattr
= btrfs_listxattr
,
7574 .removexattr
= btrfs_removexattr
,
7575 .permission
= btrfs_permission
,
7576 .get_acl
= btrfs_get_acl
,
7578 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
7579 .lookup
= btrfs_lookup
,
7580 .permission
= btrfs_permission
,
7581 .get_acl
= btrfs_get_acl
,
7584 static const struct file_operations btrfs_dir_file_operations
= {
7585 .llseek
= generic_file_llseek
,
7586 .read
= generic_read_dir
,
7587 .readdir
= btrfs_real_readdir
,
7588 .unlocked_ioctl
= btrfs_ioctl
,
7589 #ifdef CONFIG_COMPAT
7590 .compat_ioctl
= btrfs_ioctl
,
7592 .release
= btrfs_release_file
,
7593 .fsync
= btrfs_sync_file
,
7596 static struct extent_io_ops btrfs_extent_io_ops
= {
7597 .fill_delalloc
= run_delalloc_range
,
7598 .submit_bio_hook
= btrfs_submit_bio_hook
,
7599 .merge_bio_hook
= btrfs_merge_bio_hook
,
7600 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
7601 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
7602 .writepage_start_hook
= btrfs_writepage_start_hook
,
7603 .set_bit_hook
= btrfs_set_bit_hook
,
7604 .clear_bit_hook
= btrfs_clear_bit_hook
,
7605 .merge_extent_hook
= btrfs_merge_extent_hook
,
7606 .split_extent_hook
= btrfs_split_extent_hook
,
7610 * btrfs doesn't support the bmap operation because swapfiles
7611 * use bmap to make a mapping of extents in the file. They assume
7612 * these extents won't change over the life of the file and they
7613 * use the bmap result to do IO directly to the drive.
7615 * the btrfs bmap call would return logical addresses that aren't
7616 * suitable for IO and they also will change frequently as COW
7617 * operations happen. So, swapfile + btrfs == corruption.
7619 * For now we're avoiding this by dropping bmap.
7621 static const struct address_space_operations btrfs_aops
= {
7622 .readpage
= btrfs_readpage
,
7623 .writepage
= btrfs_writepage
,
7624 .writepages
= btrfs_writepages
,
7625 .readpages
= btrfs_readpages
,
7626 .direct_IO
= btrfs_direct_IO
,
7627 .invalidatepage
= btrfs_invalidatepage
,
7628 .releasepage
= btrfs_releasepage
,
7629 .set_page_dirty
= btrfs_set_page_dirty
,
7630 .error_remove_page
= generic_error_remove_page
,
7633 static const struct address_space_operations btrfs_symlink_aops
= {
7634 .readpage
= btrfs_readpage
,
7635 .writepage
= btrfs_writepage
,
7636 .invalidatepage
= btrfs_invalidatepage
,
7637 .releasepage
= btrfs_releasepage
,
7640 static const struct inode_operations btrfs_file_inode_operations
= {
7641 .getattr
= btrfs_getattr
,
7642 .setattr
= btrfs_setattr
,
7643 .setxattr
= btrfs_setxattr
,
7644 .getxattr
= btrfs_getxattr
,
7645 .listxattr
= btrfs_listxattr
,
7646 .removexattr
= btrfs_removexattr
,
7647 .permission
= btrfs_permission
,
7648 .fiemap
= btrfs_fiemap
,
7649 .get_acl
= btrfs_get_acl
,
7651 static const struct inode_operations btrfs_special_inode_operations
= {
7652 .getattr
= btrfs_getattr
,
7653 .setattr
= btrfs_setattr
,
7654 .permission
= btrfs_permission
,
7655 .setxattr
= btrfs_setxattr
,
7656 .getxattr
= btrfs_getxattr
,
7657 .listxattr
= btrfs_listxattr
,
7658 .removexattr
= btrfs_removexattr
,
7659 .get_acl
= btrfs_get_acl
,
7661 static const struct inode_operations btrfs_symlink_inode_operations
= {
7662 .readlink
= generic_readlink
,
7663 .follow_link
= page_follow_link_light
,
7664 .put_link
= page_put_link
,
7665 .getattr
= btrfs_getattr
,
7666 .setattr
= btrfs_setattr
,
7667 .permission
= btrfs_permission
,
7668 .setxattr
= btrfs_setxattr
,
7669 .getxattr
= btrfs_getxattr
,
7670 .listxattr
= btrfs_listxattr
,
7671 .removexattr
= btrfs_removexattr
,
7672 .get_acl
= btrfs_get_acl
,
7675 const struct dentry_operations btrfs_dentry_operations
= {
7676 .d_delete
= btrfs_dentry_delete
,
7677 .d_release
= btrfs_dentry_release
,