19 #include <linux/kernel.h>
26 #include <linux/time.h>
28 #include <linux/string.h>
36 #include <linux/xattr.h>
38 #include <linux/falloc.h>
39 #include <linux/slab.h>
80 static unsigned char btrfs_type_by_mode[
S_IFMT >>
S_SHIFT] = {
90 static int btrfs_setsize(
struct inode *
inode, loff_t newsize);
91 static int btrfs_truncate(
struct inode *
inode);
94 struct page *locked_page,
96 unsigned long *nr_written,
int unlock);
119 struct page **compressed_pages)
130 size_t cur_size =
size;
134 if (compressed_size && compressed_pages)
143 key.objectid = btrfs_ino(inode);
146 datasize = btrfs_file_extent_calc_inline_size(cur_size);
149 ret = btrfs_insert_empty_item(trans, root, path, &
key,
155 leaf = path->
nodes[0];
158 btrfs_set_file_extent_generation(leaf, ei, trans->
transid);
160 btrfs_set_file_extent_encryption(leaf, ei, 0);
161 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
162 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
163 ptr = btrfs_file_extent_inline_start(ei);
168 while (compressed_size > 0) {
169 cpage = compressed_pages[
i];
170 cur_size =
min_t(
unsigned long, compressed_size,
179 compressed_size -= cur_size;
181 btrfs_set_file_extent_compression(leaf, ei,
186 btrfs_set_file_extent_compression(leaf, ei, 0);
205 BTRFS_I(inode)->disk_i_size = inode->
i_size;
223 size_t compressed_size,
int compress_type,
224 struct page **compressed_pages)
226 u64 isize = i_size_read(inode);
227 u64 actual_end =
min(end + 1, isize);
228 u64 inline_len = actual_end -
start;
241 (actual_end & (root->
sectorsize - 1)) == 0) ||
243 data_len > root->
fs_info->max_inline) {
251 if (isize > actual_end)
252 inline_len =
min_t(
u64, isize, actual_end);
253 ret = insert_inline_extent(trans, root, inode, start,
254 inline_len, compressed_size,
255 compress_type, compressed_pages);
256 if (ret && ret != -
ENOSPC) {
259 }
else if (ret == -
ENOSPC) {
292 unsigned long nr_pages,
326 static noinline int compress_file_range(
struct inode *inode,
327 struct page *locked_page,
332 struct btrfs_root *root = BTRFS_I(inode)->root;
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;
351 if ((end - start + 1) < 16 * 1024 &&
352 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
355 actual_end =
min_t(
u64, isize, end + 1);
371 if (actual_end <= start)
372 goto cleanup_and_bail_uncompressed;
374 total_compressed = actual_end -
start;
386 total_compressed =
min(total_compressed, max_uncompressed);
387 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
388 num_bytes =
max(blocksize, num_bytes);
399 (BTRFS_I(inode)->force_compress) ||
402 pages = kzalloc(
sizeof(
struct page *) * nr_pages,
GFP_NOFS);
408 if (BTRFS_I(inode)->force_compress)
409 compress_type = BTRFS_I(inode)->force_compress;
413 total_compressed, pages,
414 nr_pages, &nr_pages_ret,
420 unsigned long offset = total_compressed &
422 struct page *page = pages[nr_pages_ret - 1];
441 ret = PTR_ERR(trans);
443 goto cleanup_and_out;
448 if (ret || total_in < (actual_end - start)) {
452 ret = cow_file_range_inline(trans, root, inode,
453 start, end, 0, 0,
NULL);
456 ret = cow_file_range_inline(trans, root, inode,
459 compress_type, pages);
468 &BTRFS_I(inode)->io_tree,
486 total_compressed = (total_compressed + blocksize - 1) &
495 if (total_compressed >= total_in) {
498 num_bytes = total_in;
501 if (!will_compress && pages) {
506 for (i = 0; i < nr_pages_ret; i++) {
512 total_compressed = 0;
517 !(BTRFS_I(inode)->force_compress)) {
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:
552 add_async_extent(async_cow, start, end - start + 1,
561 for (i = 0; i < nr_pages_ret; i++) {
577 if (!trans || IS_ERR(trans))
590 static noinline int submit_compressed_extents(
struct inode *inode,
591 struct async_cow *async_cow)
593 struct async_extent *async_extent;
598 struct btrfs_root *root = BTRFS_I(inode)->root;
603 if (list_empty(&async_cow->
extents))
607 while (!list_empty(&async_cow->
extents)) {
609 struct async_extent,
list);
612 io_tree = &BTRFS_I(inode)->io_tree;
616 if (!async_extent->
pages) {
617 int page_started = 0;
618 unsigned long nr_written = 0;
621 async_extent->
start +
625 ret = cow_file_range(inode, async_cow->
locked_page,
627 async_extent->
start +
629 &page_started, &nr_written, 0);
639 if (!page_started && !ret)
641 inode, async_extent->
start,
642 async_extent->
start +
656 ret = PTR_ERR(trans);
662 0, alloc_hint, &
ins, 1);
663 if (ret && ret != -
ENOSPC)
670 for (i = 0; i < async_extent->
nr_pages; i++) {
678 async_extent->
start +
690 async_extent->
start +
715 async_extent->
start +
732 &BTRFS_I(inode)->io_tree,
734 async_extent->
start +
749 alloc_hint =
ins.objectid +
ins.offset;
761 static u64 get_extent_allocation_hint(
struct inode *inode,
u64 start,
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;
825 BUG_ON(btrfs_is_free_space_inode(inode));
829 &BTRFS_I(inode)->io_tree,
830 start, end, locked_page,
837 return PTR_ERR(trans);
841 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
842 num_bytes =
max(blocksize, num_bytes);
847 if (num_bytes < 64 * 1024 &&
848 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
853 ret = cow_file_range_inline(trans, root, inode,
854 start, end, 0, 0,
NULL);
857 &BTRFS_I(inode)->io_tree,
866 *nr_written = *nr_written +
870 }
else if (ret < 0) {
877 btrfs_super_total_bytes(root->
fs_info->super_copy));
879 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
882 while (disk_num_bytes > 0) {
898 ram_size =
ins.offset;
915 start + ram_size - 1, 0);
918 cur_alloc_size =
ins.offset;
920 ram_size, cur_alloc_size, 0);
933 if (disk_num_bytes < cur_alloc_size)
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;
962 &BTRFS_I(inode)->io_tree,
963 start, end, locked_page,
979 struct async_cow *async_cow;
984 async_cow->
start, async_cow->
end, async_cow,
986 if (num_added == 0) {
997 struct async_cow *async_cow;
999 unsigned long nr_pages;
1001 async_cow =
container_of(work,
struct async_cow, work);
1003 root = async_cow->
root;
1009 waitqueue_active(&root->
fs_info->async_submit_wait))
1012 if (async_cow->
inode)
1013 submit_compressed_extents(async_cow->
inode, async_cow);
1018 struct async_cow *async_cow;
1019 async_cow =
container_of(work,
struct async_cow, work);
1020 if (async_cow->
inode)
1025 static int cow_file_range_async(
struct inode *inode,
struct page *locked_page,
1026 u64 start,
u64 end,
int *page_started,
1027 unsigned long *nr_written)
1029 struct async_cow *async_cow;
1030 struct btrfs_root *root = BTRFS_I(inode)->root;
1031 unsigned long nr_pages;
1033 int limit = 10 * 1024 * 1024;
1037 while (start < end) {
1041 async_cow->
root = root;
1045 if (BTRFS_I(inode)->
flags & BTRFS_INODE_NOCOMPRESS)
1048 cur_end =
min(end, start + 512 * 1024 - 1);
1050 async_cow->
end = cur_end;
1051 INIT_LIST_HEAD(&async_cow->
extents);
1053 async_cow->
work.func = async_cow_start;
1054 async_cow->
work.ordered_func = async_cow_submit;
1055 async_cow->
work.ordered_free = async_cow_free;
1056 async_cow->
work.flags = 0;
1078 *nr_written += nr_pages;
1079 start = cur_end + 1;
1093 bytenr + num_bytes - 1, &
list, 0);
1094 if (ret == 0 && list_empty(&
list))
1097 while (!list_empty(&
list)) {
1112 static noinline int run_delalloc_nocow(
struct inode *inode,
1113 struct page *locked_page,
1115 unsigned long *nr_written)
1117 struct btrfs_root *root = BTRFS_I(inode)->root;
1135 u64 ino = btrfs_ino(inode);
1140 &BTRFS_I(inode)->io_tree,
1141 start, end, locked_page,
1151 nolock = btrfs_is_free_space_inode(inode);
1158 if (IS_ERR(trans)) {
1160 &BTRFS_I(inode)->io_tree,
1161 start, end, locked_page,
1169 return PTR_ERR(trans);
1174 cow_start = (
u64)-1;
1183 if (ret > 0 && path->
slots[0] > 0 && check_prev) {
1184 leaf = path->
nodes[0];
1185 btrfs_item_key_to_cpu(leaf, &found_key,
1186 path->
slots[0] - 1);
1187 if (found_key.objectid == ino &&
1193 leaf = path->
nodes[0];
1194 if (path->
slots[0] >= btrfs_header_nritems(leaf)) {
1202 leaf = path->
nodes[0];
1208 btrfs_item_key_to_cpu(leaf, &found_key, path->
slots[0]);
1210 if (found_key.objectid > ino ||
1212 found_key.offset > end)
1215 if (found_key.offset > cur_offset) {
1216 extent_end = found_key.offset;
1223 extent_type = btrfs_file_extent_type(leaf, fi);
1227 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1228 extent_offset = btrfs_file_extent_offset(leaf, fi);
1229 extent_end = found_key.offset +
1230 btrfs_file_extent_num_bytes(leaf, fi);
1231 if (extent_end <= start) {
1235 if (disk_bytenr == 0)
1237 if (btrfs_file_extent_compression(leaf, fi) ||
1238 btrfs_file_extent_encryption(leaf, fi) ||
1239 btrfs_file_extent_other_encoding(leaf, fi))
1247 extent_offset, disk_bytenr))
1249 disk_bytenr += extent_offset;
1250 disk_bytenr += cur_offset - found_key.offset;
1251 num_bytes =
min(end + 1, extent_end) - cur_offset;
1257 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1261 extent_end = found_key.offset +
1262 btrfs_file_extent_inline_len(leaf, fi);
1268 if (extent_end <= start) {
1273 if (cow_start == (
u64)-1)
1274 cow_start = cur_offset;
1275 cur_offset = extent_end;
1276 if (cur_offset > end)
1283 if (cow_start != (
u64)-1) {
1284 ret = cow_file_range(inode, locked_page, cow_start,
1285 found_key.offset - 1, page_started,
1291 cow_start = (
u64)-1;
1297 em_tree = &BTRFS_I(inode)->extent_tree;
1300 em->
start = cur_offset;
1305 em->
bdev = root->
fs_info->fs_devices->latest_bdev;
1325 num_bytes, num_bytes, type);
1339 cur_offset, cur_offset + num_bytes - 1,
1343 cur_offset = extent_end;
1344 if (cur_offset > end)
1349 if (cur_offset <= end && cow_start == (
u64)-1) {
1350 cow_start = cur_offset;
1354 if (cow_start != (
u64)-1) {
1355 ret = cow_file_range(inode, locked_page, cow_start, end,
1356 page_started, nr_written, 1);
1368 if (ret && cur_offset < end)
1370 &BTRFS_I(inode)->io_tree,
1371 cur_offset, end, locked_page,
1386 static int run_delalloc_range(
struct inode *inode,
struct page *locked_page,
1387 u64 start,
u64 end,
int *page_started,
1388 unsigned long *nr_written)
1391 struct btrfs_root *root = BTRFS_I(inode)->root;
1394 ret = run_delalloc_nocow(inode, locked_page, start, end,
1395 page_started, 1, nr_written);
1397 ret = run_delalloc_nocow(inode, locked_page, start, end,
1398 page_started, 0, nr_written);
1400 !(BTRFS_I(inode)->force_compress) &&
1401 !(BTRFS_I(inode)->
flags & BTRFS_INODE_COMPRESS)) {
1402 ret = cow_file_range(inode, locked_page, start, end,
1403 page_started, nr_written, 1);
1406 &BTRFS_I(inode)->runtime_flags);
1407 ret = cow_file_range_async(inode, locked_page, start, end,
1408 page_started, nr_written);
1413 static void btrfs_split_extent_hook(
struct inode *inode,
1420 spin_lock(&BTRFS_I(inode)->lock);
1421 BTRFS_I(inode)->outstanding_extents++;
1422 spin_unlock(&BTRFS_I(inode)->lock);
1431 static void btrfs_merge_extent_hook(
struct inode *inode,
1439 spin_lock(&BTRFS_I(inode)->lock);
1440 BTRFS_I(inode)->outstanding_extents--;
1441 spin_unlock(&BTRFS_I(inode)->lock);
1449 static void btrfs_set_bit_hook(
struct inode *inode,
1459 struct btrfs_root *root = BTRFS_I(inode)->root;
1461 bool do_list = !btrfs_is_free_space_inode(inode);
1464 *bits &= ~EXTENT_FIRST_DELALLOC;
1466 spin_lock(&BTRFS_I(inode)->lock);
1467 BTRFS_I(inode)->outstanding_extents++;
1468 spin_unlock(&BTRFS_I(inode)->lock);
1471 spin_lock(&root->
fs_info->delalloc_lock);
1472 BTRFS_I(inode)->delalloc_bytes += len;
1473 root->
fs_info->delalloc_bytes += len;
1474 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1476 &root->
fs_info->delalloc_inodes);
1478 spin_unlock(&root->
fs_info->delalloc_lock);
1485 static void btrfs_clear_bit_hook(
struct inode *inode,
1493 if ((state->
state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1494 struct btrfs_root *root = BTRFS_I(inode)->root;
1496 bool do_list = !btrfs_is_free_space_inode(inode);
1498 if (*bits & EXTENT_FIRST_DELALLOC) {
1499 *bits &= ~EXTENT_FIRST_DELALLOC;
1501 spin_lock(&BTRFS_I(inode)->lock);
1502 BTRFS_I(inode)->outstanding_extents--;
1503 spin_unlock(&BTRFS_I(inode)->lock);
1506 if (*bits & EXTENT_DO_ACCOUNTING)
1513 spin_lock(&root->
fs_info->delalloc_lock);
1514 root->
fs_info->delalloc_bytes -= len;
1515 BTRFS_I(inode)->delalloc_bytes -= len;
1517 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
1518 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1519 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1521 spin_unlock(&root->
fs_info->delalloc_lock);
1530 size_t size,
struct bio *bio,
1531 unsigned long bio_flags)
1535 u64 logical = (
u64)bio->bi_sector << 9;
1544 map_tree = &root->
fs_info->mapping_tree;
1547 &map_length,
NULL, 0);
1550 if (map_length <
length + size)
1563 static int __btrfs_submit_bio_start(
struct inode *inode,
int rw,
1564 struct bio *bio,
int mirror_num,
1565 unsigned long bio_flags,
1568 struct btrfs_root *root = BTRFS_I(inode)->root;
1584 static int __btrfs_submit_bio_done(
struct inode *inode,
int rw,
struct bio *bio,
1585 int mirror_num,
unsigned long bio_flags,
1588 struct btrfs_root *root = BTRFS_I(inode)->root;
1596 static int btrfs_submit_bio_hook(
struct inode *inode,
int rw,
struct bio *bio,
1597 int mirror_num,
unsigned long bio_flags,
1600 struct btrfs_root *root = BTRFS_I(inode)->root;
1607 if (btrfs_is_free_space_inode(inode))
1617 mirror_num, bio_flags);
1618 }
else if (!skip_sum) {
1624 }
else if (!skip_sum) {
1630 inode, rw, bio, mirror_num,
1631 bio_flags, bio_offset,
1632 __btrfs_submit_bio_start,
1633 __btrfs_submit_bio_done);
1652 BTRFS_I(inode)->root->
fs_info->csum_root, sum);
1672 static void btrfs_writepage_fixup_worker(
struct btrfs_work *work)
1678 struct inode *
inode;
1687 if (!page->
mapping || !PageDirty(page) || !PageChecked(page)) {
1688 ClearPageChecked(page);
1700 if (PagePrivate2(page))
1706 page_end, &cached_state,
GFP_NOFS);
1715 mapping_set_error(page->
mapping, ret);
1717 ClearPageChecked(page);
1722 ClearPageChecked(page);
1744 static int btrfs_writepage_start_hook(
struct page *page,
u64 start,
u64 end)
1746 struct inode *inode = page->
mapping->host;
1748 struct btrfs_root *root = BTRFS_I(inode)->root;
1751 if (TestClearPagePrivate2(page))
1754 if (PageChecked(page))
1757 fixup = kzalloc(
sizeof(*fixup),
GFP_NOFS);
1761 SetPageChecked(page);
1763 fixup->
work.func = btrfs_writepage_fixup_worker;
1770 struct inode *inode,
u64 file_pos,
1771 u64 disk_bytenr,
u64 disk_num_bytes,
1776 struct btrfs_root *root = BTRFS_I(inode)->root;
1799 file_pos + num_bytes, 0);
1803 ins.objectid = btrfs_ino(inode);
1804 ins.offset = file_pos;
1806 ret = btrfs_insert_empty_item(trans, root, path, &
ins,
sizeof(*fi));
1809 leaf = path->
nodes[0];
1812 btrfs_set_file_extent_generation(leaf, fi, trans->
transid);
1813 btrfs_set_file_extent_type(leaf, fi, extent_type);
1814 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1815 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1816 btrfs_set_file_extent_offset(leaf, fi, 0);
1817 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1818 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1819 btrfs_set_file_extent_compression(leaf, fi, compression);
1820 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1821 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
1833 btrfs_ino(inode), file_pos, &
ins);
1852 struct inode *inode = ordered_extent->
inode;
1853 struct btrfs_root *root = BTRFS_I(inode)->root;
1857 int compress_type = 0;
1861 nolock = btrfs_is_free_space_inode(inode);
1876 if (IS_ERR(trans)) {
1877 ret = PTR_ERR(trans);
1897 if (IS_ERR(trans)) {
1898 ret = PTR_ERR(trans);
1911 ordered_extent->
len);
1914 ret = insert_reserved_file_extent(trans, inode,
1916 ordered_extent->
start,
1918 ordered_extent->
len,
1919 ordered_extent->
len,
1920 compress_type, 0, 0,
1931 add_pending_csums(trans, inode, ordered_extent->
file_offset,
1932 &ordered_extent->
list);
1942 btrfs_set_inode_last_trans(trans, inode);
1948 ordered_extent->
len - 1, &cached_state,
GFP_NOFS);
1950 if (root != root->
fs_info->tree_root)
1974 static void finish_ordered_fn(
struct btrfs_work *work)
1978 btrfs_finish_ordered_io(ordered_extent);
1981 static int btrfs_writepage_end_io_hook(
struct page *page,
u64 start,
u64 end,
1984 struct inode *inode = page->
mapping->host;
1985 struct btrfs_root *root = BTRFS_I(inode)->root;
1989 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
1991 ClearPagePrivate2(page);
1993 end - start + 1, uptodate))
1996 ordered_extent->
work.func = finish_ordered_fn;
1997 ordered_extent->
work.flags = 0;
1999 if (btrfs_is_free_space_inode(inode))
2000 workers = &root->
fs_info->endio_freespace_worker;
2002 workers = &root->
fs_info->endio_write_workers;
2013 static int btrfs_readpage_end_io_hook(
struct page *page,
u64 start,
u64 end,
2017 struct inode *inode = page->
mapping->host;
2022 struct btrfs_root *root = BTRFS_I(inode)->root;
2025 if (PageChecked(page)) {
2026 ClearPageChecked(page);
2040 if (state && state->
start == start) {
2052 if (csum !=
private)
2062 (
unsigned long long)btrfs_ino(page->
mapping->host),
2063 (
unsigned long long)start, csum,
2064 (
unsigned long long)
private);
2065 memset(kaddr + offset, 1, end - start + 1);
2085 if (atomic_add_unless(&inode->
i_count, -1, 1))
2113 while (!list_empty(&list)) {
2157 btrfs_root_refs(&root->
root_item) > 0) {
2179 struct btrfs_root *root = BTRFS_I(inode)->root;
2194 }
else if (block_rsv) {
2200 &BTRFS_I(inode)->runtime_flags)) {
2217 &BTRFS_I(inode)->runtime_flags))
2230 if (ret && ret != -
EEXIST) {
2232 &BTRFS_I(inode)->runtime_flags);
2243 if (ret && ret != -
EEXIST) {
2257 struct btrfs_root *root = BTRFS_I(inode)->root;
2258 int delete_item = 0;
2259 int release_rsv = 0;
2264 &BTRFS_I(inode)->runtime_flags))
2268 &BTRFS_I(inode)->runtime_flags))
2272 if (trans && delete_item) {
2295 struct inode *
inode;
2296 u64 last_objectid = 0;
2297 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2325 if (path->
slots[0] == 0)
2331 leaf = path->
nodes[0];
2332 btrfs_item_key_to_cpu(leaf, &found_key, path->
slots[0]);
2349 if (found_key.
offset == last_objectid) {
2351 "stopping orphan cleanup\n");
2356 last_objectid = found_key.
offset;
2362 ret = PTR_RET(inode);
2363 if (ret && ret != -
ESTALE)
2369 int is_dead_root = 0;
2385 if (dead_root->
root_key.objectid ==
2404 if (IS_ERR(trans)) {
2405 ret = PTR_ERR(trans);
2422 &BTRFS_I(inode)->runtime_flags);
2432 ret = btrfs_truncate(inode);
2483 while (slot < nritems) {
2484 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2487 if (found_key.objectid != objectid)
2523 static void btrfs_read_locked_inode(
struct inode *inode)
2529 struct btrfs_root *root = BTRFS_I(inode)->root;
2534 bool filled =
false;
2551 leaf = path->
nodes[0];
2558 inode->
i_mode = btrfs_inode_mode(leaf, inode_item);
2559 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
2560 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
2561 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
2562 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
2564 tspec = btrfs_inode_atime(inode_item);
2565 inode->
i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2566 inode->
i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2568 tspec = btrfs_inode_mtime(inode_item);
2569 inode->
i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2570 inode->
i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2572 tspec = btrfs_inode_ctime(inode_item);
2573 inode->
i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2574 inode->
i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2577 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
2578 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
2586 if (BTRFS_I(inode)->last_trans == root->
fs_info->generation)
2588 &BTRFS_I(inode)->runtime_flags);
2590 inode->
i_version = btrfs_inode_sequence(leaf, inode_item);
2593 rdev = btrfs_inode_rdev(leaf, inode_item);
2595 BTRFS_I(inode)->index_cnt = (
u64)-1;
2596 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
2602 maybe_acls = acls_after_inode_item(leaf, path->
slots[0],
2605 cache_no_acl(inode);
2613 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2615 inode->
i_op = &btrfs_file_inode_operations;
2618 inode->
i_fop = &btrfs_dir_file_operations;
2619 if (root == root->
fs_info->tree_root)
2620 inode->
i_op = &btrfs_dir_ro_inode_operations;
2622 inode->
i_op = &btrfs_dir_inode_operations;
2625 inode->
i_op = &btrfs_symlink_inode_operations;
2626 inode->
i_mapping->a_ops = &btrfs_symlink_aops;
2630 inode->
i_op = &btrfs_special_inode_operations;
2649 struct inode *inode)
2651 btrfs_set_inode_uid(leaf, item, i_uid_read(inode));
2652 btrfs_set_inode_gid(leaf, item, i_gid_read(inode));
2653 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
2654 btrfs_set_inode_mode(leaf, item, inode->
i_mode);
2655 btrfs_set_inode_nlink(leaf, item, inode->
i_nlink);
2657 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2659 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2662 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2664 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2667 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2669 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2673 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->
generation);
2674 btrfs_set_inode_sequence(leaf, item, inode->
i_version);
2675 btrfs_set_inode_transid(leaf, item, trans->
transid);
2676 btrfs_set_inode_rdev(leaf, item, inode->
i_rdev);
2677 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->
flags);
2678 btrfs_set_inode_block_group(leaf, item, 0);
2685 struct btrfs_root *root,
struct inode *inode)
2706 leaf = path->
nodes[0];
2710 fill_inode_item(trans, leaf, inode_item, inode);
2712 btrfs_set_inode_last_trans(trans, inode);
2723 struct btrfs_root *root,
struct inode *inode)
2734 if (!btrfs_is_free_space_inode(inode)
2740 btrfs_set_inode_last_trans(trans, inode);
2744 return btrfs_update_inode_item(trans, root, inode);
2749 struct inode *inode)
2755 return btrfs_update_inode_item(trans, root, inode);
2766 struct inode *dir,
struct inode *inode,
2775 u64 ino = btrfs_ino(inode);
2776 u64 dir_ino = btrfs_ino(dir);
2786 name, name_len, -1);
2795 leaf = path->
nodes[0];
2796 btrfs_dir_item_key_to_cpu(leaf, di, &
key);
2806 "inode %llu parent %llu\n", name_len, name,
2807 (
unsigned long long)ino, (
unsigned long long)dir_ino);
2820 if (ret != 0 && ret != -
ENOENT) {
2834 btrfs_i_size_write(dir, dir->
i_size - name_len * 2);
2835 inode_inc_iversion(inode);
2836 inode_inc_iversion(dir);
2845 struct inode *dir,
struct inode *inode,
2846 const char *name,
int name_len)
2849 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2859 static int check_path_shared(
struct btrfs_root *root,
2869 if (!path->
nodes[level])
2893 struct btrfs_root *root = BTRFS_I(dir)->root;
2896 struct inode *inode = dentry->
d_inode;
2901 u64 ino = btrfs_ino(inode);
2902 u64 dir_ino = btrfs_ino(dir);
2914 if (!IS_ERR(trans) || PTR_ERR(trans) != -
ENOSPC)
2932 root->
fs_info->enospc_unlink = 0;
2938 if (IS_ERR(trans)) {
2940 root->
fs_info->enospc_unlink = 0;
2954 if (check_path_shared(root, path))
2968 if (check_path_shared(root, path))
2983 if (check_path_shared(root, path))
3000 if (check_path_shared(root, path))
3009 dentry->
d_name.len, ino, dir_ino, 0,
3016 if (check_path_shared(root, path))
3036 if (check_path_shared(root, path))
3045 &root->
fs_info->global_block_rsv,
3050 root->
fs_info->enospc_unlink = 0;
3051 return ERR_PTR(err);
3066 root->
fs_info->enospc_unlink = 0;
3071 static int btrfs_unlink(
struct inode *dir,
struct dentry *dentry)
3073 struct btrfs_root *root = BTRFS_I(dir)->root;
3075 struct inode *inode = dentry->
d_inode;
3077 unsigned long nr = 0;
3079 trans = __unlink_start_trans(dir, dentry);
3081 return PTR_ERR(trans);
3098 __unlink_end_trans(trans, root);
3105 struct inode *dir,
u64 objectid,
3106 const char *name,
int name_len)
3114 u64 dir_ino = btrfs_ino(dir);
3121 name, name_len, -1);
3122 if (IS_ERR_OR_NULL(di)) {
3130 leaf = path->
nodes[0];
3131 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3142 dir_ino, &index, name, name_len);
3150 if (IS_ERR_OR_NULL(di)) {
3159 leaf = path->
nodes[0];
3160 btrfs_item_key_to_cpu(leaf, &key, path->
slots[0]);
3172 btrfs_i_size_write(dir, dir->
i_size - name_len * 2);
3173 inode_inc_iversion(dir);
3183 static int btrfs_rmdir(
struct inode *dir,
struct dentry *dentry)
3185 struct inode *inode = dentry->
d_inode;
3187 struct btrfs_root *root = BTRFS_I(dir)->root;
3189 unsigned long nr = 0;
3196 trans = __unlink_start_trans(dir, dentry);
3198 return PTR_ERR(trans);
3216 btrfs_i_size_write(inode, 0);
3219 __unlink_end_trans(trans, root);
3238 struct inode *inode,
3239 u64 new_size,
u32 min_type)
3246 u64 extent_start = 0;
3247 u64 extent_num_bytes = 0;
3248 u64 extent_offset = 0;
3251 u32 found_type = (
u8)-1;
3254 int pending_del_nr = 0;
3255 int pending_del_slot = 0;
3256 int extent_type = -1;
3259 u64 ino = btrfs_ino(inode);
3282 if (min_type == 0 && root == BTRFS_I(inode)->root)
3301 if (path->
slots[0] == 0)
3308 leaf = path->
nodes[0];
3309 btrfs_item_key_to_cpu(leaf, &found_key, path->
slots[0]);
3310 found_type = btrfs_key_type(&found_key);
3315 if (found_type < min_type)
3318 item_end = found_key.
offset;
3322 extent_type = btrfs_file_extent_type(leaf, fi);
3325 btrfs_file_extent_num_bytes(leaf, fi);
3327 item_end += btrfs_file_extent_inline_len(leaf,
3332 if (found_type > min_type) {
3335 if (item_end < new_size)
3337 if (found_key.
offset >= new_size)
3349 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
3351 u64 orig_num_bytes =
3352 btrfs_file_extent_num_bytes(leaf, fi);
3353 extent_num_bytes = new_size -
3355 extent_num_bytes = extent_num_bytes &
3357 btrfs_set_file_extent_num_bytes(leaf, fi,
3359 num_dec = (orig_num_bytes -
3361 if (root->
ref_cows && extent_start != 0)
3366 btrfs_file_extent_disk_num_bytes(leaf,
3368 extent_offset = found_key.
offset -
3369 btrfs_file_extent_offset(leaf, fi);
3372 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
3373 if (extent_start != 0) {
3385 btrfs_file_extent_compression(leaf, fi) == 0 &&
3386 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3387 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
3395 btrfs_file_extent_calc_inline_size(size);
3405 if (!pending_del_nr) {
3407 pending_del_slot = path->
slots[0];
3409 }
else if (pending_del_nr &&
3410 path->
slots[0] + 1 == pending_del_slot) {
3413 pending_del_slot = path->
slots[0];
3420 if (found_extent && (root->
ref_cows ||
3421 root == root->
fs_info->tree_root)) {
3424 extent_num_bytes, 0,
3425 btrfs_header_owner(leaf),
3426 ino, extent_offset, 0);
3433 if (path->
slots[0] == 0 ||
3434 path->
slots[0] != pending_del_slot) {
3435 if (pending_del_nr) {
3453 if (pending_del_nr) {
3479 struct btrfs_root *root = BTRFS_I(inode)->root;
3488 gfp_t mask = btrfs_alloc_write_mask(mapping);
3493 if ((offset & (blocksize - 1)) == 0 &&
3494 (!len || ((len & (blocksize - 1)) == 0)))
3511 if (!PageUptodate(page)) {
3514 if (page->
mapping != mapping) {
3519 if (!PageUptodate(page)) {
3524 wait_on_page_writeback(page);
3554 if (offset != PAGE_CACHE_SIZE) {
3556 len = PAGE_CACHE_SIZE -
offset;
3559 memset(kaddr, 0, offset);
3561 memset(kaddr + offset, 0, len);
3565 ClearPageChecked(page);
3588 struct btrfs_root *root = BTRFS_I(inode)->root;
3594 u64 hole_start = (oldsize +
mask) & ~mask;
3595 u64 block_end = (size +
mask) & ~mask;
3601 if (size <= hole_start)
3607 block_end - hole_start);
3618 cur_offset = hole_start;
3621 block_end - cur_offset, 0);
3626 last_byte =
min(extent_map_end(em), block_end);
3627 last_byte = (last_byte +
mask) & ~mask;
3630 hole_size = last_byte - cur_offset;
3633 if (IS_ERR(trans)) {
3634 err = PTR_ERR(trans);
3640 cur_offset + hole_size, 1);
3648 btrfs_ino(inode), cur_offset, 0,
3649 0, hole_size, 0, hole_size,
3658 cur_offset + hole_size - 1, 0);
3662 &BTRFS_I(inode)->runtime_flags);
3665 hole_em->
start = cur_offset;
3666 hole_em->
len = hole_size;
3671 hole_em->
bdev = root->
fs_info->fs_devices->latest_bdev;
3679 list_move(&hole_em->
list,
3695 cur_offset = last_byte;
3696 if (cur_offset >= block_end)
3706 static int btrfs_setsize(
struct inode *inode, loff_t newsize)
3708 struct btrfs_root *root = BTRFS_I(inode)->root;
3710 loff_t oldsize = i_size_read(inode);
3713 if (newsize == oldsize)
3716 if (newsize > oldsize) {
3724 return PTR_ERR(trans);
3726 i_size_write(inode, newsize);
3739 &BTRFS_I(inode)->runtime_flags);
3743 ret = btrfs_truncate(inode);
3749 static int btrfs_setattr(
struct dentry *dentry,
struct iattr *
attr)
3751 struct inode *inode = dentry->
d_inode;
3752 struct btrfs_root *root = BTRFS_I(inode)->root;
3755 if (btrfs_root_readonly(root))
3763 err = btrfs_setsize(inode, attr->
ia_size);
3770 inode_inc_iversion(inode);
3783 struct btrfs_root *root = BTRFS_I(inode)->root;
3785 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
3789 trace_btrfs_inode_evict(inode);
3793 btrfs_is_free_space_inode(inode)))
3803 if (root->
fs_info->log_root_recovering) {
3805 &BTRFS_I(inode)->runtime_flags));
3819 rsv->
size = min_size;
3821 global_rsv = &root->
fs_info->global_block_rsv;
3823 btrfs_i_size_write(inode, 0);
3844 "delete, will truncate on mount %d\n", ret);
3851 if (IS_ERR(trans)) {
3882 if (!(root == root->
fs_info->tree_root ||
3898 static int btrfs_inode_by_name(
struct inode *dir,
struct dentry *dentry,
3901 const char *name = dentry->
d_name.name;
3905 struct btrfs_root *root = BTRFS_I(dir)->root;
3917 if (IS_ERR_OR_NULL(di))
3920 btrfs_dir_item_key_to_cpu(path->
nodes[0], di, location);
3934 static int fixup_tree_root_location(
struct btrfs_root *root,
3936 struct dentry *dentry,
3955 BTRFS_I(dir)->root->
root_key.objectid,
3963 leaf = path->
nodes[0];
3965 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
3966 btrfs_root_ref_name_len(leaf, ref) != dentry->
d_name.len)
3970 (
unsigned long)(ref + 1),
3978 if (IS_ERR(new_root)) {
3979 err = PTR_ERR(new_root);
3983 if (btrfs_root_refs(&new_root->
root_item) == 0) {
3988 *sub_root = new_root;
3998 static void inode_tree_add(
struct inode *inode)
4000 struct btrfs_root *root = BTRFS_I(inode)->root;
4004 u64 ino = btrfs_ino(inode);
4009 if (inode_unhashed(inode))
4019 else if (ino > btrfs_ino(&entry->
vfs_inode))
4030 rb_link_node(&BTRFS_I(inode)->
rb_node, parent, p);
4035 static void inode_tree_del(
struct inode *inode)
4037 struct btrfs_root *root = BTRFS_I(inode)->root;
4054 if (empty && btrfs_root_refs(&root->
root_item) == 0 &&
4055 root != root->
fs_info->tree_root) {
4070 struct inode *
inode;
4083 if (objectid < btrfs_ino(&entry->
vfs_inode))
4085 else if (objectid > btrfs_ino(&entry->
vfs_inode))
4093 if (objectid <= btrfs_ino(&entry->
vfs_inode)) {
4102 objectid = btrfs_ino(&entry->
vfs_inode) + 1;
4127 static int btrfs_init_locked_inode(
struct inode *inode,
void *p)
4131 BTRFS_I(inode)->root = args->
root;
4135 static int btrfs_find_actor(
struct inode *inode,
void *opaque)
4138 return args->
ino == btrfs_ino(inode) &&
4139 args->
root == BTRFS_I(inode)->root;
4142 static struct inode *btrfs_iget_locked(
struct super_block *
s,
4146 struct inode *
inode;
4152 btrfs_init_locked_inode,
4163 struct inode *
inode;
4165 inode = btrfs_iget_locked(s, location->
objectid, root);
4170 BTRFS_I(inode)->root = root;
4171 memcpy(&BTRFS_I(inode)->location, location,
sizeof(*location));
4172 btrfs_read_locked_inode(inode);
4174 inode_tree_add(inode);
4181 inode = ERR_PTR(-
ESTALE);
4188 static struct inode *new_simple_dir(
struct super_block *s,
4197 BTRFS_I(inode)->root = root;
4198 memcpy(&BTRFS_I(inode)->location, key,
sizeof(*key));
4202 inode->
i_op = &btrfs_dir_ro_inode_operations;
4212 struct inode *
inode;
4213 struct btrfs_root *root = BTRFS_I(dir)->root;
4222 if (
unlikely(d_need_lookup(dentry))) {
4229 ret = btrfs_inode_by_name(dir, dentry, &location);
4233 return ERR_PTR(ret);
4245 index = srcu_read_lock(&root->
fs_info->subvol_srcu);
4246 ret = fixup_tree_root_location(root, dir, dentry,
4247 &location, &sub_root);
4250 inode = ERR_PTR(ret);
4252 inode = new_simple_dir(dir->
i_sb, &location, sub_root);
4256 srcu_read_unlock(&root->
fs_info->subvol_srcu, index);
4258 if (!IS_ERR(inode) && root != sub_root) {
4264 inode = ERR_PTR(ret);
4270 static int btrfs_dentry_delete(
const struct dentry *dentry)
4273 struct inode *inode = dentry->
d_inode;
4275 if (!inode && !
IS_ROOT(dentry))
4279 root = BTRFS_I(inode)->root;
4280 if (btrfs_root_refs(&root->
root_item) == 0)
4289 static void btrfs_dentry_release(
struct dentry *dentry)
4295 static struct dentry *btrfs_lookup(
struct inode *dir,
struct dentry *dentry,
4301 if (
unlikely(d_need_lookup(dentry))) {
4302 spin_lock(&dentry->
d_lock);
4304 spin_unlock(&dentry->
d_lock);
4313 static int btrfs_real_readdir(
struct file *filp,
void *
dirent,
4316 struct inode *inode = filp->f_dentry->d_inode;
4317 struct btrfs_root *root = BTRFS_I(inode)->root;
4328 unsigned char d_type;
4340 if (root->
fs_info->tree_root == root)
4344 if (filp->
f_pos == 0) {
4345 over = filldir(dirent,
".", 1,
4352 if (filp->
f_pos == 1) {
4354 over = filldir(dirent,
"..", 2,
4367 INIT_LIST_HEAD(&ins_list);
4368 INIT_LIST_HEAD(&del_list);
4372 btrfs_set_key_type(&key, key_type);
4381 leaf = path->
nodes[0];
4382 slot = path->
slots[0];
4383 if (slot >= btrfs_header_nritems(leaf)) {
4392 item = btrfs_item_nr(leaf, slot);
4393 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4395 if (found_key.objectid != key.
objectid)
4397 if (btrfs_key_type(&found_key) != key_type)
4399 if (found_key.offset < filp->
f_pos)
4406 filp->
f_pos = found_key.offset;
4411 di_total = btrfs_item_size(leaf, item);
4413 while (di_cur < di_total) {
4419 name_len = btrfs_dir_name_len(leaf, di);
4420 if (name_len <=
sizeof(tmp_name)) {
4421 name_ptr = tmp_name;
4430 (
unsigned long)(di + 1), name_len);
4432 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4433 btrfs_dir_item_key_to_cpu(leaf, di, &location);
4450 over = filldir(dirent, name_ptr, name_len,
4451 found_key.offset, location.
objectid,
4455 if (name_ptr != tmp_name)
4460 di_len = btrfs_dir_name_len(leaf, di) +
4461 btrfs_dir_data_len(leaf, di) +
sizeof(*di);
4484 filp->
f_pos = 0x7fffffff;
4498 struct btrfs_root *root = BTRFS_I(inode)->root;
4501 bool nolock =
false;
4506 if (btrfs_fs_closing(root->
fs_info) && btrfs_is_free_space_inode(inode))
4515 return PTR_ERR(trans);
4529 struct btrfs_root *root = BTRFS_I(inode)->root;
4538 return PTR_ERR(trans);
4541 if (ret && ret == -
ENOSPC) {
4546 return PTR_ERR(trans);
4551 if (BTRFS_I(inode)->delayed_node)
4561 static int btrfs_update_time(
struct inode *inode,
struct timespec *now,
4564 struct btrfs_root *root = BTRFS_I(inode)->root;
4566 if (btrfs_root_readonly(root))
4570 inode_inc_iversion(inode);
4585 static int btrfs_set_inode_index_count(
struct inode *inode)
4587 struct btrfs_root *root = BTRFS_I(inode)->root;
4615 if (path->
slots[0] == 0) {
4616 BTRFS_I(inode)->index_cnt = 2;
4622 leaf = path->
nodes[0];
4623 btrfs_item_key_to_cpu(leaf, &found_key, path->
slots[0]);
4625 if (found_key.objectid != btrfs_ino(inode) ||
4627 BTRFS_I(inode)->index_cnt = 2;
4631 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4645 if (BTRFS_I(dir)->index_cnt == (
u64)-1) {
4648 ret = btrfs_set_inode_index_count(dir);
4654 *index = BTRFS_I(dir)->index_cnt;
4655 BTRFS_I(dir)->index_cnt++;
4663 const char *name,
int name_len,
4664 u64 ref_objectid,
u64 objectid,
4667 struct inode *
inode;
4695 trace_btrfs_inode_request(dir);
4701 return ERR_PTR(ret);
4709 BTRFS_I(inode)->index_cnt = 2;
4710 BTRFS_I(inode)->root = root;
4711 BTRFS_I(inode)->generation = trans->
transid;
4739 key[1].
offset = ref_objectid;
4742 sizes[1] = name_len +
sizeof(*ref);
4755 sizeof(*inode_item));
4756 fill_inode_item(trans, path->
nodes[0], inode_item, inode);
4760 btrfs_set_inode_ref_name_len(path->
nodes[0], ref, name_len);
4761 btrfs_set_inode_ref_index(path->
nodes[0], ref, *index);
4762 ptr = (
unsigned long)(ref + 1);
4768 location = &BTRFS_I(inode)->location;
4779 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
4783 insert_inode_hash(inode);
4784 inode_tree_add(inode);
4786 trace_btrfs_inode_new(inode);
4787 btrfs_set_inode_last_trans(trans, inode);
4794 BTRFS_I(dir)->index_cnt--;
4797 return ERR_PTR(ret);
4800 static inline u8 btrfs_inode_type(
struct inode *inode)
4812 struct inode *parent_inode,
struct inode *inode,
4813 const char *name,
int name_len,
int add_backref,
u64 index)
4817 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
4818 u64 ino = btrfs_ino(inode);
4832 parent_ino, index, name, name_len);
4833 }
else if (add_backref) {
4844 btrfs_inode_type(inode), index);
4852 btrfs_i_size_write(parent_inode, parent_inode->
i_size +
4854 inode_inc_iversion(parent_inode);
4867 parent_ino, &local_index, name, name_len);
4869 }
else if (add_backref) {
4874 ino, parent_ino, &local_index);
4880 struct inode *dir,
struct dentry *dentry,
4881 struct inode *inode,
int backref,
u64 index)
4891 static int btrfs_mknod(
struct inode *dir,
struct dentry *dentry,
4895 struct btrfs_root *root = BTRFS_I(dir)->root;
4896 struct inode *inode =
NULL;
4900 unsigned long nr = 0;
4903 if (!new_valid_dev(rdev))
4913 return PTR_ERR(trans);
4919 inode = btrfs_new_inode(trans, root, dir, dentry->
d_name.name,
4920 dentry->
d_name.len, btrfs_ino(dir), objectid,
4922 if (IS_ERR(inode)) {
4923 err = PTR_ERR(inode);
4927 err = btrfs_init_inode_security(trans, inode, dir, &dentry->
d_name);
4940 inode->
i_op = &btrfs_special_inode_operations;
4941 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
4954 inode_dec_link_count(inode);
4960 static int btrfs_create(
struct inode *dir,
struct dentry *dentry,
4964 struct btrfs_root *root = BTRFS_I(dir)->root;
4965 struct inode *inode =
NULL;
4968 unsigned long nr = 0;
4979 return PTR_ERR(trans);
4985 inode = btrfs_new_inode(trans, root, dir, dentry->
d_name.name,
4986 dentry->
d_name.len, btrfs_ino(dir), objectid,
4988 if (IS_ERR(inode)) {
4989 err = PTR_ERR(inode);
4993 err = btrfs_init_inode_security(trans, inode, dir, &dentry->
d_name);
5006 inode->
i_op = &btrfs_file_inode_operations;
5008 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
5014 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5021 inode_dec_link_count(inode);
5028 static int btrfs_link(
struct dentry *old_dentry,
struct inode *dir,
5029 struct dentry *dentry)
5032 struct btrfs_root *root = BTRFS_I(dir)->root;
5033 struct inode *inode = old_dentry->
d_inode;
5035 unsigned long nr = 0;
5056 if (IS_ERR(trans)) {
5057 err = PTR_ERR(trans);
5062 inode_inc_iversion(inode);
5066 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
5071 struct dentry *parent = dentry->
d_parent;
5083 inode_dec_link_count(inode);
5090 static int btrfs_mkdir(
struct inode *dir,
struct dentry *dentry,
umode_t mode)
5092 struct inode *inode =
NULL;
5094 struct btrfs_root *root = BTRFS_I(dir)->root;
5096 int drop_on_err = 0;
5099 unsigned long nr = 1;
5108 return PTR_ERR(trans);
5114 inode = btrfs_new_inode(trans, root, dir, dentry->
d_name.name,
5115 dentry->
d_name.len, btrfs_ino(dir), objectid,
5117 if (IS_ERR(inode)) {
5118 err = PTR_ERR(inode);
5124 err = btrfs_init_inode_security(trans, inode, dir, &dentry->
d_name);
5128 inode->
i_op = &btrfs_dir_inode_operations;
5129 inode->
i_fop = &btrfs_dir_file_operations;
5131 btrfs_i_size_write(inode, 0);
5137 dentry->
d_name.len, 0, index);
5160 u64 map_start,
u64 map_len)
5164 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5165 start_diff = map_start - em->
start;
5166 em->
start = map_start;
5177 struct inode *inode,
struct page *page,
5178 size_t pg_offset,
u64 extent_offset,
5185 unsigned long inline_size;
5190 compress_type = btrfs_file_extent_compression(leaf, item);
5191 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5192 inline_size = btrfs_file_extent_inline_item_len(leaf,
5193 btrfs_item_nr(leaf, path->
slots[0]));
5197 ptr = btrfs_file_extent_inline_start(item);
5203 extent_offset, inline_size, max_size);
5206 unsigned long copy_size =
min_t(
u64,
5208 max_size - extent_offset);
5209 memset(kaddr + pg_offset, 0, copy_size);
5226 size_t pg_offset,
u64 start,
u64 len,
5232 u64 extent_start = 0;
5234 u64 objectid = btrfs_ino(inode);
5237 struct btrfs_root *root = BTRFS_I(inode)->root;
5251 em->
bdev = root->
fs_info->fs_devices->latest_bdev;
5267 em->
bdev = root->
fs_info->fs_devices->latest_bdev;
5287 objectid, start, trans !=
NULL);
5294 if (path->
slots[0] == 0)
5299 leaf = path->
nodes[0];
5303 btrfs_item_key_to_cpu(leaf, &found_key, path->
slots[0]);
5304 found_type = btrfs_key_type(&found_key);
5305 if (found_key.
objectid != objectid ||
5310 found_type = btrfs_file_extent_type(leaf, item);
5311 extent_start = found_key.
offset;
5312 compress_type = btrfs_file_extent_compression(leaf, item);
5315 extent_end = extent_start +
5316 btrfs_file_extent_num_bytes(leaf, item);
5319 size = btrfs_file_extent_inline_len(leaf, item);
5320 extent_end = (extent_start + size + root->
sectorsize - 1) &
5324 if (start >= extent_end) {
5326 if (path->
slots[0] >= btrfs_header_nritems(leaf)) {
5334 leaf = path->
nodes[0];
5336 btrfs_item_key_to_cpu(leaf, &found_key, path->
slots[0]);
5337 if (found_key.
objectid != objectid ||
5340 if (start + len <= found_key.
offset)
5349 em->
start = extent_start;
5350 em->
len = extent_end - extent_start;
5352 btrfs_file_extent_offset(leaf, item);
5353 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5362 em->
block_len = btrfs_file_extent_disk_num_bytes(leaf,
5365 bytenr += btrfs_file_extent_offset(leaf, item);
5376 size_t extent_offset;
5380 if (!page || create) {
5381 em->
start = extent_start;
5382 em->
len = extent_end - extent_start;
5386 size = btrfs_file_extent_inline_len(leaf, item);
5387 extent_offset =
page_offset(page) + pg_offset - extent_start;
5389 size - extent_offset);
5390 em->
start = extent_start + extent_offset;
5394 if (compress_type) {
5398 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
5399 if (create == 0 && !PageUptodate(page)) {
5400 if (btrfs_file_extent_compression(leaf, item) !=
5402 ret = uncompress_inline(path, inode, page,
5404 extent_offset, item);
5411 memset(map + pg_offset + copy_size, 0,
5418 }
else if (create && PageUptodate(page)) {
5429 return ERR_CAST(trans);
5453 if (em->
start > start || extent_map_end(em) <= start) {
5455 "[%llu %llu]\n", (
unsigned long long)em->
start,
5456 (
unsigned long long)em->
len,
5457 (
unsigned long long)start,
5458 (
unsigned long long)len);
5476 if (existing && (existing->
start > start ||
5477 existing->
start + existing->
len <= start)) {
5485 err = merge_extent_mapping(em_tree, existing,
5508 trace_btrfs_get_extent(root, em);
5519 return ERR_PTR(err);
5526 size_t pg_offset,
u64 start,
u64 len,
5562 end, len, EXTENT_DELALLOC, 1);
5563 found_end = range_start + found;
5564 if (found_end < range_start)
5565 found_end = (
u64)-1;
5571 if (range_start > end || found_end <= start) {
5580 range_start =
max(start,range_start);
5601 u64 calc_end = extent_map_end(hole_em);
5603 if (calc_end <= start || (hole_em->
start > end)) {
5607 hole_start =
max(hole_em->
start, start);
5608 hole_len = calc_end - hole_start;
5612 if (hole_em && range_start > hole_start) {
5618 range_start - hole_start);
5619 em->
start = hole_start;
5634 }
else if (hole_em) {
5642 return ERR_PTR(err);
5647 static struct extent_map *btrfs_new_extent_direct(
struct inode *inode,
5651 struct btrfs_root *root = BTRFS_I(inode)->root;
5657 bool insert =
false;
5675 return ERR_CAST(trans);
5677 if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024)
5682 alloc_hint = get_extent_allocation_hint(inode, start, len);
5684 alloc_hint, &
ins, 1);
5704 em->
bdev = root->
fs_info->fs_devices->latest_bdev;
5723 ins.offset,
ins.offset, 0);
5738 struct inode *inode,
u64 offset,
u64 len)
5743 struct btrfs_root *root = BTRFS_I(inode)->root;
5762 slot = path->
slots[0];
5772 leaf = path->
nodes[0];
5773 btrfs_item_key_to_cpu(leaf, &key, slot);
5774 if (key.
objectid != btrfs_ino(inode) ||
5780 if (key.
offset > offset) {
5786 found_type = btrfs_file_extent_type(leaf, fi);
5792 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5793 backref_offset = btrfs_file_extent_offset(leaf, fi);
5795 extent_end = key.
offset + btrfs_file_extent_num_bytes(leaf, fi);
5796 if (extent_end < offset + len) {
5809 key.
offset - backref_offset, disk_bytenr))
5818 disk_bytenr += backref_offset;
5819 disk_bytenr += offset - key.
offset;
5820 num_bytes =
min(offset + len, extent_end) -
offset;
5821 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5833 static int lock_extent_direct(
struct inode *inode,
u64 lockstart,
u64 lockend,
5848 lockend - lockstart + 1);
5857 if (!ordered && (!writing ||
5894 static struct extent_map *create_pinned_em(
struct inode *inode,
u64 start,
5901 struct btrfs_root *root = BTRFS_I(inode)->root;
5904 em_tree = &BTRFS_I(inode)->extent_tree;
5914 em->
bdev = root->
fs_info->fs_devices->latest_bdev;
5925 }
while (ret == -
EEXIST);
5929 return ERR_PTR(ret);
5936 static int btrfs_get_blocks_direct(
struct inode *inode,
sector_t iblock,
5937 struct buffer_head *bh_result,
int create)
5940 struct btrfs_root *root = BTRFS_I(inode)->root;
5943 u64 lockstart, lockend;
5944 u64 len = bh_result->b_size;
5959 lockend = start + len - 1;
5965 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
5970 lockend, EXTENT_DELALLOC,
NULL,
6022 lockstart = start + len;
6027 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6049 if (can_nocow_odirect(trans, inode, start, len) == 1) {
6054 em = create_pinned_em(inode, start, len,
6056 block_start, len, type);
6064 block_start, len, len, type);
6079 len = bh_result->b_size;
6080 em = btrfs_new_extent_direct(inode, em, start, len);
6089 bh_result->b_size = len;
6090 bh_result->b_bdev = em->
bdev;
6091 set_buffer_mapped(bh_result);
6094 set_buffer_new(bh_result);
6100 if (start + len > i_size_read(inode))
6101 i_size_write(inode, start + len);
6109 if (lockstart < lockend) {
6110 if (create && len < lockend - lockstart) {
6112 lockstart + len - 1,
6120 lockstart + len, lockend,
6121 unlock_bits | EXTENT_DO_ACCOUNTING |
6125 lockend, unlock_bits, 1, 0,
6141 unlock_bits, 1, 0, &cached_state,
GFP_NOFS);
6161 static void btrfs_endio_direct_read(
struct bio *bio,
int err)
6164 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6165 struct bio_vec *bvec = bio->bi_io_vec;
6166 struct inode *inode = dip->
inode;
6167 struct btrfs_root *root = BTRFS_I(inode)->root;
6172 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6173 struct page *page = bvec->bv_page;
6177 unsigned long flags;
6185 csum, bvec->bv_len);
6191 if (csum !=
private) {
6194 " %llu csum %u private %u\n",
6195 (
unsigned long long)btrfs_ino(inode),
6196 (
unsigned long long)start,
6197 csum, (
unsigned)
private);
6202 start += bvec->bv_len;
6204 }
while (bvec <= bvec_end);
6208 bio->bi_private = dip->
private;
6214 clear_bit(BIO_UPTODATE, &bio->bi_flags);
6218 static void btrfs_endio_direct_write(
struct bio *bio,
int err)
6221 struct inode *inode = dip->
inode;
6222 struct btrfs_root *root = BTRFS_I(inode)->root;
6233 ordered_bytes, !err);
6237 ordered->
work.func = finish_ordered_fn;
6238 ordered->
work.flags = 0;
6246 if (ordered_offset < dip->logical_offset + dip->
bytes) {
6253 bio->bi_private = dip->
private;
6259 clear_bit(BIO_UPTODATE, &bio->bi_flags);
6263 static int __btrfs_submit_bio_start_direct_io(
struct inode *inode,
int rw,
6264 struct bio *bio,
int mirror_num,
6265 unsigned long bio_flags,
u64 offset)
6268 struct btrfs_root *root = BTRFS_I(inode)->root;
6274 static void btrfs_end_dio_bio(
struct bio *bio,
int err)
6280 "sector %#Lx len %u err no %d\n",
6281 (
unsigned long long)btrfs_ino(dip->
inode), bio->bi_rw,
6282 (
unsigned long long)bio->bi_sector, bio->bi_size, err);
6306 static struct bio *btrfs_dio_bio_alloc(
struct block_device *bdev,
6313 static inline int __btrfs_submit_dio_bio(
struct bio *bio,
struct inode *inode,
6314 int rw,
u64 file_offset,
int skip_sum,
6318 struct btrfs_root *root = BTRFS_I(inode)->root;
6332 if (write && async_submit) {
6334 inode, rw, bio, 0, 0,
6336 __btrfs_submit_bio_start_direct_io,
6337 __btrfs_submit_bio_done);
6347 }
else if (!skip_sum) {
6363 struct inode *inode = dip->
inode;
6364 struct btrfs_root *root = BTRFS_I(inode)->root;
6367 struct bio *orig_bio = dip->
orig_bio;
6368 struct bio_vec *bvec = orig_bio->bi_io_vec;
6375 int async_submit = 0;
6377 map_length = orig_bio->bi_size;
6379 &map_length,
NULL, 0);
6385 if (map_length >= orig_bio->bi_size) {
6391 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector,
GFP_NOFS);
6394 bio->bi_private = dip;
6395 bio->bi_end_io = btrfs_end_dio_bio;
6398 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6399 if (
unlikely(map_length < submit_len + bvec->bv_len ||
6401 bvec->bv_offset) < bvec->bv_len)) {
6409 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6410 file_offset, skip_sum,
6418 start_sector += submit_len >> 9;
6419 file_offset += submit_len;
6424 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6428 bio->bi_private = dip;
6429 bio->bi_end_io = btrfs_end_dio_bio;
6431 map_length = orig_bio->bi_size;
6433 &map_length,
NULL, 0);
6439 submit_len += bvec->bv_len;
6446 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
6466 static void btrfs_submit_direct(
int rw,
struct bio *bio,
struct inode *inode,
6469 struct btrfs_root *root = BTRFS_I(inode)->root;
6471 struct bio_vec *bvec = bio->bi_io_vec;
6484 dip->
private = bio->bi_private;
6490 dip->
bytes += bvec->bv_len;
6492 }
while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6495 bio->bi_private = dip;
6501 bio->bi_end_io = btrfs_endio_direct_write;
6503 bio->bi_end_io = btrfs_endio_direct_read;
6505 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6527 const struct iovec *iov, loff_t offset,
6528 unsigned long nr_segs)
6534 unsigned blocksize_mask = root->
sectorsize - 1;
6538 if (offset & blocksize_mask)
6542 for (seg = 0; seg < nr_segs; seg++) {
6543 addr = (
unsigned long)iov[seg].iov_base;
6546 if ((addr & blocksize_mask) || (size & blocksize_mask))
6558 for (i = seg + 1; i < nr_segs; i++) {
6559 if (iov[seg].iov_base == iov[i].iov_base)
6568 static ssize_t btrfs_direct_IO(
int rw,
struct kiocb *iocb,
6569 const struct iovec *iov, loff_t offset,
6570 unsigned long nr_segs)
6573 struct inode *inode = file->
f_mapping->host;
6575 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
6580 BTRFS_I(inode)->root->
fs_info->fs_devices->latest_bdev,
6581 iov, offset, nr_segs, btrfs_get_blocks_direct,
NULL,
6582 btrfs_submit_direct, 0);
6594 tree = &BTRFS_I(page->
mapping->host)->io_tree;
6608 tree = &BTRFS_I(page->
mapping->host)->io_tree;
6617 tree = &BTRFS_I(mapping->
host)->io_tree;
6623 struct list_head *pages,
unsigned nr_pages)
6626 tree = &BTRFS_I(mapping->
host)->io_tree;
6630 static int __btrfs_releasepage(
struct page *page,
gfp_t gfp_flags)
6636 tree = &BTRFS_I(page->
mapping->host)->io_tree;
6637 map = &BTRFS_I(page->
mapping->host)->extent_tree;
6640 ClearPagePrivate(page);
6641 set_page_private(page, 0);
6647 static int btrfs_releasepage(
struct page *page,
gfp_t gfp_flags)
6649 if (PageWriteback(page) || PageDirty(page))
6651 return __btrfs_releasepage(page, gfp_flags &
GFP_NOFS);
6654 static void btrfs_invalidatepage(
struct page *page,
unsigned long offset)
6656 struct inode *inode = page->
mapping->host;
6670 wait_on_page_writeback(page);
6672 tree = &BTRFS_I(inode)->io_tree;
6693 if (TestClearPagePrivate2(page) &&
6695 PAGE_CACHE_SIZE, 1)) {
6696 btrfs_finish_ordered_io(ordered);
6699 cached_state =
NULL;
6706 __btrfs_releasepage(page,
GFP_NOFS);
6708 ClearPageChecked(page);
6709 if (PagePrivate(page)) {
6710 ClearPagePrivate(page);
6711 set_page_private(page, 0);
6733 struct page *page = vmf->page;
6734 struct inode *inode = fdentry(vma->
vm_file)->d_inode;
6735 struct btrfs_root *root = BTRFS_I(inode)->root;
6740 unsigned long zero_start;
6747 sb_start_pagefault(inode->
i_sb);
6757 ret = VM_FAULT_SIGBUS;
6763 ret = VM_FAULT_NOPAGE;
6766 size = i_size_read(inode);
6768 page_end = page_start + PAGE_CACHE_SIZE - 1;
6771 (page_start >= size)) {
6775 wait_on_page_writeback(page);
6811 ret = VM_FAULT_SIGBUS;
6817 if (page_start + PAGE_CACHE_SIZE > size)
6822 if (zero_start != PAGE_CACHE_SIZE) {
6824 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6828 ClearPageChecked(page);
6830 SetPageUptodate(page);
6832 BTRFS_I(inode)->last_trans = root->
fs_info->generation;
6833 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6834 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
6840 sb_end_pagefault(inode->
i_sb);
6841 return VM_FAULT_LOCKED;
6847 sb_end_pagefault(inode->
i_sb);
6851 static int btrfs_truncate(
struct inode *inode)
6853 struct btrfs_root *root = BTRFS_I(inode)->root;
6860 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
6908 rsv->
size = min_size;
6918 if (IS_ERR(trans)) {
6919 err = PTR_ERR(trans);
6952 &BTRFS_I(inode)->runtime_flags))
6986 if (IS_ERR(trans)) {
6987 ret = err = PTR_ERR(trans);
6998 if (ret == 0 && inode->
i_nlink > 0) {
7003 }
else if (ret && inode->
i_nlink > 0) {
7037 struct inode *
inode;
7041 inode = btrfs_new_inode(trans, new_root,
NULL,
"..", 2,
7042 new_dirid, new_dirid,
7046 return PTR_ERR(inode);
7047 inode->
i_op = &btrfs_dir_inode_operations;
7048 inode->
i_fop = &btrfs_dir_file_operations;
7051 btrfs_i_size_write(inode, 0);
7062 struct inode *
inode;
7094 ei->
io_tree.track_uptodate = 1;
7115 struct btrfs_root *root = BTRFS_I(inode)->root;
7119 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7120 WARN_ON(BTRFS_I(inode)->reserved_extents);
7121 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7122 WARN_ON(BTRFS_I(inode)->csum_bytes);
7137 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7138 spin_lock(&root->
fs_info->ordered_extent_lock);
7139 list_del_init(&BTRFS_I(inode)->ordered_operations);
7140 spin_unlock(&root->
fs_info->ordered_extent_lock);
7144 &BTRFS_I(inode)->runtime_flags)) {
7146 (
unsigned long long)btrfs_ino(inode));
7156 "extent %llu %llu on inode cleanup\n",
7158 (
unsigned long long)ordered->
len);
7164 inode_tree_del(inode);
7173 struct btrfs_root *root = BTRFS_I(inode)->root;
7175 if (btrfs_root_refs(&root->
root_item) == 0 &&
7176 !btrfs_is_free_space_inode(inode))
7179 return generic_drop_inode(inode);
7182 static void init_once(
void *
foo)
7196 if (btrfs_inode_cachep)
7198 if (btrfs_trans_handle_cachep)
7200 if (btrfs_transaction_cachep)
7202 if (btrfs_path_cachep)
7204 if (btrfs_free_space_cachep)
7213 if (!btrfs_inode_cachep)
7219 if (!btrfs_trans_handle_cachep)
7225 if (!btrfs_transaction_cachep)
7231 if (!btrfs_path_cachep)
7237 if (!btrfs_free_space_cachep)
7246 static int btrfs_getattr(
struct vfsmount *mnt,
7247 struct dentry *dentry,
struct kstat *
stat)
7249 struct inode *inode = dentry->
d_inode;
7250 u32 blocksize = inode->
i_sb->s_blocksize;
7253 stat->
dev = BTRFS_I(inode)->root->anon_dev;
7256 ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9;
7264 static void fixup_inode_flags(
struct inode *dir,
struct inode *inode)
7269 if (b_dir->
flags & BTRFS_INODE_NODATACOW)
7272 b_inode->
flags &= ~BTRFS_INODE_NODATACOW;
7274 if (b_dir->
flags & BTRFS_INODE_COMPRESS) {
7276 b_inode->
flags &= ~BTRFS_INODE_NOCOMPRESS;
7278 b_inode->
flags &= ~(BTRFS_INODE_COMPRESS |
7283 static int btrfs_rename(
struct inode *old_dir,
struct dentry *old_dentry,
7284 struct inode *new_dir,
struct dentry *new_dentry)
7287 struct btrfs_root *root = BTRFS_I(old_dir)->root;
7290 struct inode *old_inode = old_dentry->
d_inode;
7295 u64 old_ino = btrfs_ino(old_inode);
7332 if (IS_ERR(trans)) {
7333 ret = PTR_ERR(trans);
7352 btrfs_ino(new_dir), index);
7371 inode_inc_iversion(old_dir);
7372 inode_inc_iversion(new_dir);
7373 inode_inc_iversion(old_inode);
7382 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7387 ret = __btrfs_unlink_inode(trans, root, old_dir,
7400 inode_inc_iversion(new_inode);
7402 if (
unlikely(btrfs_ino(new_inode) ==
7404 root_objectid = BTRFS_I(new_inode)->location.objectid;
7416 if (!ret && new_inode->
i_nlink == 0) {
7426 fixup_inode_flags(new_dir, old_inode);
7430 new_dentry->
d_name.len, 0, index);
7437 struct dentry *parent = new_dentry->
d_parent;
7458 struct inode *
inode;
7463 spin_lock(&root->
fs_info->delalloc_lock);
7464 while (!list_empty(head)) {
7470 spin_unlock(&root->
fs_info->delalloc_lock);
7479 spin_lock(&root->
fs_info->delalloc_lock);
7481 spin_unlock(&root->
fs_info->delalloc_lock);
7498 static int btrfs_symlink(
struct inode *dir,
struct dentry *dentry,
7499 const char *symname)
7502 struct btrfs_root *root = BTRFS_I(dir)->root;
7505 struct inode *inode =
NULL;
7515 unsigned long nr = 0;
7517 name_len =
strlen(symname) + 1;
7528 return PTR_ERR(trans);
7534 inode = btrfs_new_inode(trans, root, dir, dentry->
d_name.name,
7535 dentry->
d_name.len, btrfs_ino(dir), objectid,
7537 if (IS_ERR(inode)) {
7538 err = PTR_ERR(inode);
7542 err = btrfs_init_inode_security(trans, inode, dir, &dentry->
d_name);
7555 inode->
i_op = &btrfs_file_inode_operations;
7557 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
7563 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
7577 datasize = btrfs_file_extent_calc_inline_size(name_len);
7578 err = btrfs_insert_empty_item(trans, root, path, &key,
7585 leaf = path->
nodes[0];
7588 btrfs_set_file_extent_generation(leaf, ei, trans->
transid);
7589 btrfs_set_file_extent_type(leaf, ei,
7591 btrfs_set_file_extent_encryption(leaf, ei, 0);
7592 btrfs_set_file_extent_compression(leaf, ei, 0);
7593 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7594 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7596 ptr = btrfs_file_extent_inline_start(ei);
7601 inode->
i_op = &btrfs_symlink_inode_operations;
7602 inode->
i_mapping->a_ops = &btrfs_symlink_aops;
7605 btrfs_i_size_write(inode, name_len - 1);
7616 inode_dec_link_count(inode);
7623 static int __btrfs_prealloc_file_range(
struct inode *inode,
int mode,
7625 loff_t actual_len,
u64 *alloc_hint,
7630 struct btrfs_root *root = BTRFS_I(inode)->root;
7635 bool own_trans =
true;
7639 while (num_bytes > 0) {
7642 if (IS_ERR(trans)) {
7643 ret = PTR_ERR(trans);
7649 0, *alloc_hint, &
ins, 1);
7656 ret = insert_reserved_file_extent(trans, inode,
7657 cur_offset,
ins.objectid,
7659 ins.offset, 0, 0, 0,
7668 cur_offset +
ins.offset -1, 0);
7673 &BTRFS_I(inode)->runtime_flags);
7677 em->
start = cur_offset;
7682 em->
bdev = root->
fs_info->fs_devices->latest_bdev;
7690 list_move(&em->
list,
7696 cur_offset +
ins.offset - 1,
7701 num_bytes -=
ins.offset;
7702 cur_offset +=
ins.offset;
7703 *alloc_hint =
ins.objectid +
ins.offset;
7705 inode_inc_iversion(inode);
7709 (actual_len > inode->
i_size) &&
7710 (cur_offset > inode->
i_size)) {
7711 if (cur_offset > actual_len)
7712 i_size = actual_len;
7714 i_size = cur_offset;
7715 i_size_write(inode, i_size);
7736 loff_t actual_len,
u64 *alloc_hint)
7738 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7739 min_size, actual_len, alloc_hint,
7746 loff_t actual_len,
u64 *alloc_hint)
7748 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7749 min_size, actual_len, alloc_hint, trans);
7752 static int btrfs_set_page_dirty(
struct page *page)
7757 static int btrfs_permission(
struct inode *inode,
int mask)
7759 struct btrfs_root *root = BTRFS_I(inode)->root;
7764 if (btrfs_root_readonly(root))
7773 .getattr = btrfs_getattr,
7774 .lookup = btrfs_lookup,
7775 .create = btrfs_create,
7776 .unlink = btrfs_unlink,
7778 .mkdir = btrfs_mkdir,
7779 .rmdir = btrfs_rmdir,
7780 .rename = btrfs_rename,
7781 .symlink = btrfs_symlink,
7782 .setattr = btrfs_setattr,
7783 .mknod = btrfs_mknod,
7788 .permission = btrfs_permission,
7792 .lookup = btrfs_lookup,
7793 .permission = btrfs_permission,
7800 .readdir = btrfs_real_readdir,
7802 #ifdef CONFIG_COMPAT
7810 .fill_delalloc = run_delalloc_range,
7811 .submit_bio_hook = btrfs_submit_bio_hook,
7813 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
7814 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
7815 .writepage_start_hook = btrfs_writepage_start_hook,
7816 .set_bit_hook = btrfs_set_bit_hook,
7817 .clear_bit_hook = btrfs_clear_bit_hook,
7818 .merge_extent_hook = btrfs_merge_extent_hook,
7819 .split_extent_hook = btrfs_split_extent_hook,
7836 .writepage = btrfs_writepage,
7838 .readpages = btrfs_readpages,
7839 .direct_IO = btrfs_direct_IO,
7840 .invalidatepage = btrfs_invalidatepage,
7841 .releasepage = btrfs_releasepage,
7842 .set_page_dirty = btrfs_set_page_dirty,
7848 .writepage = btrfs_writepage,
7849 .invalidatepage = btrfs_invalidatepage,
7850 .releasepage = btrfs_releasepage,
7854 .getattr = btrfs_getattr,
7855 .setattr = btrfs_setattr,
7860 .permission = btrfs_permission,
7861 .fiemap = btrfs_fiemap,
7863 .update_time = btrfs_update_time,
7866 .getattr = btrfs_getattr,
7867 .setattr = btrfs_setattr,
7868 .permission = btrfs_permission,
7874 .update_time = btrfs_update_time,
7880 .getattr = btrfs_getattr,
7881 .setattr = btrfs_setattr,
7882 .permission = btrfs_permission,
7888 .update_time = btrfs_update_time,
7892 .d_delete = btrfs_dentry_delete,
7893 .d_release = btrfs_dentry_release,