inode.c (879c1cfc3104b632dea85d07490f1b55e54275a9) inode.c (1832a6d5ee3b1af61001cadba9e10da9e91af4a4)
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
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.
7 *
8 * This program is distributed in the hope that it will be useful,

--- 58 unchanged lines hidden (view full) ---

67 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
68 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
69 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
70 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
71 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
72 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
73};
74
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
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.
7 *
8 * This program is distributed in the hope that it will be useful,

--- 58 unchanged lines hidden (view full) ---

67 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
68 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
69 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
70 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
71 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
72 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
73};
74
75int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
76 int for_del)
77{
78 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
79 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
80 u64 thresh;
81 int ret = 0;
82
83 if (for_del)
84 thresh = (total * 90) / 100;
85 else
86 thresh = (total * 85) / 100;
87
88 spin_lock(&root->fs_info->delalloc_lock);
89 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
90 ret = -ENOSPC;
91 spin_unlock(&root->fs_info->delalloc_lock);
92 return ret;
93}
94
75static int cow_file_range(struct inode *inode, u64 start, u64 end)
76{
77 struct btrfs_root *root = BTRFS_I(inode)->root;
78 struct btrfs_trans_handle *trans;
79 u64 alloc_hint = 0;
80 u64 num_bytes;
81 u64 cur_alloc_size;
82 u64 blocksize = root->sectorsize;

--- 36 unchanged lines hidden (view full) ---

119}
120
121static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
122{
123 u64 extent_start;
124 u64 extent_end;
125 u64 bytenr;
126 u64 cow_end;
95static int cow_file_range(struct inode *inode, u64 start, u64 end)
96{
97 struct btrfs_root *root = BTRFS_I(inode)->root;
98 struct btrfs_trans_handle *trans;
99 u64 alloc_hint = 0;
100 u64 num_bytes;
101 u64 cur_alloc_size;
102 u64 blocksize = root->sectorsize;

--- 36 unchanged lines hidden (view full) ---

139}
140
141static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
142{
143 u64 extent_start;
144 u64 extent_end;
145 u64 bytenr;
146 u64 cow_end;
147 u64 loops = 0;
127 struct btrfs_root *root = BTRFS_I(inode)->root;
128 struct extent_buffer *leaf;
129 int found_type;
130 struct btrfs_path *path;
131 struct btrfs_file_extent_item *item;
132 int ret;
133 int err;
134 struct btrfs_key found_key;

--- 29 unchanged lines hidden (view full) ---

164
165 found_type = btrfs_file_extent_type(leaf, item);
166 extent_start = found_key.offset;
167 if (found_type == BTRFS_FILE_EXTENT_REG) {
168 extent_end = extent_start +
169 btrfs_file_extent_num_bytes(leaf, item);
170 err = 0;
171
148 struct btrfs_root *root = BTRFS_I(inode)->root;
149 struct extent_buffer *leaf;
150 int found_type;
151 struct btrfs_path *path;
152 struct btrfs_file_extent_item *item;
153 int ret;
154 int err;
155 struct btrfs_key found_key;

--- 29 unchanged lines hidden (view full) ---

185
186 found_type = btrfs_file_extent_type(leaf, item);
187 extent_start = found_key.offset;
188 if (found_type == BTRFS_FILE_EXTENT_REG) {
189 extent_end = extent_start +
190 btrfs_file_extent_num_bytes(leaf, item);
191 err = 0;
192
193 if (loops && start != extent_start)
194 goto not_found;
195
172 if (start < extent_start || start >= extent_end)
173 goto not_found;
174
175 cow_end = min(end, extent_end - 1);
176 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
177 if (bytenr == 0)
178 goto not_found;
179

--- 6 unchanged lines hidden (view full) ---

186 goto not_found;
187 }
188loop:
189 if (start > end) {
190 btrfs_free_path(path);
191 return 0;
192 }
193 btrfs_release_path(root, path);
196 if (start < extent_start || start >= extent_end)
197 goto not_found;
198
199 cow_end = min(end, extent_end - 1);
200 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
201 if (bytenr == 0)
202 goto not_found;
203

--- 6 unchanged lines hidden (view full) ---

210 goto not_found;
211 }
212loop:
213 if (start > end) {
214 btrfs_free_path(path);
215 return 0;
216 }
217 btrfs_release_path(root, path);
218 loops++;
194 goto again;
195
196not_found:
197 cow_file_range(inode, start, cow_end);
198 start = cow_end + 1;
199 goto loop;
200}
201
202static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
203{
204 struct btrfs_root *root = BTRFS_I(inode)->root;
219 goto again;
220
221not_found:
222 cow_file_range(inode, start, cow_end);
223 start = cow_end + 1;
224 goto loop;
225}
226
227static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
228{
229 struct btrfs_root *root = BTRFS_I(inode)->root;
230 u64 num_bytes;
205 int ret;
206
207 mutex_lock(&root->fs_info->fs_mutex);
208 if (btrfs_test_opt(root, NODATACOW))
209 ret = run_delalloc_nocow(inode, start, end);
210 else
211 ret = cow_file_range(inode, start, end);
231 int ret;
232
233 mutex_lock(&root->fs_info->fs_mutex);
234 if (btrfs_test_opt(root, NODATACOW))
235 ret = run_delalloc_nocow(inode, start, end);
236 else
237 ret = cow_file_range(inode, start, end);
238
239 spin_lock(&root->fs_info->delalloc_lock);
240 num_bytes = end + 1 - start;
241 if (root->fs_info->delalloc_bytes < num_bytes) {
242 printk("delalloc accounting error total %llu sub %llu\n",
243 root->fs_info->delalloc_bytes, num_bytes);
244 } else {
245 root->fs_info->delalloc_bytes -= num_bytes;
246 }
247 spin_unlock(&root->fs_info->delalloc_lock);
248
212 mutex_unlock(&root->fs_info->fs_mutex);
213 return ret;
214}
215
216int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
217{
218 struct inode *inode = page->mapping->host;
219 struct btrfs_root *root = BTRFS_I(inode)->root;

--- 322 unchanged lines hidden (view full) ---

542 return ret;
543}
544
545static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
546{
547 struct btrfs_root *root;
548 struct btrfs_trans_handle *trans;
549 int ret;
249 mutex_unlock(&root->fs_info->fs_mutex);
250 return ret;
251}
252
253int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
254{
255 struct inode *inode = page->mapping->host;
256 struct btrfs_root *root = BTRFS_I(inode)->root;

--- 322 unchanged lines hidden (view full) ---

579 return ret;
580}
581
582static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
583{
584 struct btrfs_root *root;
585 struct btrfs_trans_handle *trans;
586 int ret;
550 unsigned long nr;
587 unsigned long nr = 0;
551
552 root = BTRFS_I(dir)->root;
553 mutex_lock(&root->fs_info->fs_mutex);
588
589 root = BTRFS_I(dir)->root;
590 mutex_lock(&root->fs_info->fs_mutex);
591
592 ret = btrfs_check_free_space(root, 1, 1);
593 if (ret)
594 goto fail;
595
554 trans = btrfs_start_transaction(root, 1);
555
556 btrfs_set_trans_block_group(trans, dir);
557 ret = btrfs_unlink_trans(trans, root, dir, dentry);
558 nr = trans->blocks_used;
559
560 btrfs_end_transaction(trans, root);
596 trans = btrfs_start_transaction(root, 1);
597
598 btrfs_set_trans_block_group(trans, dir);
599 ret = btrfs_unlink_trans(trans, root, dir, dentry);
600 nr = trans->blocks_used;
601
602 btrfs_end_transaction(trans, root);
603fail:
561 mutex_unlock(&root->fs_info->fs_mutex);
562 btrfs_btree_balance_dirty(root, nr);
604 mutex_unlock(&root->fs_info->fs_mutex);
605 btrfs_btree_balance_dirty(root, nr);
563
564 return ret;
565}
566
567static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
568{
569 struct inode *inode = dentry->d_inode;
606 return ret;
607}
608
609static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
610{
611 struct inode *inode = dentry->d_inode;
570 int err;
612 int err = 0;
571 int ret;
572 struct btrfs_root *root = BTRFS_I(dir)->root;
573 struct btrfs_trans_handle *trans;
613 int ret;
614 struct btrfs_root *root = BTRFS_I(dir)->root;
615 struct btrfs_trans_handle *trans;
574 unsigned long nr;
616 unsigned long nr = 0;
575
576 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
577 return -ENOTEMPTY;
578
579 mutex_lock(&root->fs_info->fs_mutex);
617
618 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
619 return -ENOTEMPTY;
620
621 mutex_lock(&root->fs_info->fs_mutex);
622 ret = btrfs_check_free_space(root, 1, 1);
623 if (ret)
624 goto fail;
625
580 trans = btrfs_start_transaction(root, 1);
581 btrfs_set_trans_block_group(trans, dir);
582
583 /* now the directory is empty */
584 err = btrfs_unlink_trans(trans, root, dir, dentry);
585 if (!err) {
586 inode->i_size = 0;
587 }
588
589 nr = trans->blocks_used;
590 ret = btrfs_end_transaction(trans, root);
626 trans = btrfs_start_transaction(root, 1);
627 btrfs_set_trans_block_group(trans, dir);
628
629 /* now the directory is empty */
630 err = btrfs_unlink_trans(trans, root, dir, dentry);
631 if (!err) {
632 inode->i_size = 0;
633 }
634
635 nr = trans->blocks_used;
636 ret = btrfs_end_transaction(trans, root);
637fail:
591 mutex_unlock(&root->fs_info->fs_mutex);
592 btrfs_btree_balance_dirty(root, nr);
593
594 if (ret && !err)
595 err = ret;
596 return err;
597}
598

--- 188 unchanged lines hidden (view full) ---

787 inode->i_sb->s_dirt = 1;
788 return ret;
789}
790
791static int btrfs_cow_one_page(struct inode *inode, struct page *page,
792 size_t zero_start)
793{
794 char *kaddr;
638 mutex_unlock(&root->fs_info->fs_mutex);
639 btrfs_btree_balance_dirty(root, nr);
640
641 if (ret && !err)
642 err = ret;
643 return err;
644}
645

--- 188 unchanged lines hidden (view full) ---

834 inode->i_sb->s_dirt = 1;
835 return ret;
836}
837
838static int btrfs_cow_one_page(struct inode *inode, struct page *page,
839 size_t zero_start)
840{
841 char *kaddr;
795 int ret = 0;
796 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
842 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
843 struct btrfs_root *root = BTRFS_I(inode)->root;
797 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
798 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
844 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
845 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
846 u64 existing_delalloc;
847 u64 delalloc_start;
848 int ret = 0;
799
800 WARN_ON(!PageLocked(page));
801 set_page_extent_mapped(page);
802
803 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
849
850 WARN_ON(!PageLocked(page));
851 set_page_extent_mapped(page);
852
853 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
854 delalloc_start = page_start;
855 existing_delalloc = count_range_bits(&BTRFS_I(inode)->extent_tree,
856 &delalloc_start, page_end,
857 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
804 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
805 page_end, GFP_NOFS);
858 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
859 page_end, GFP_NOFS);
860
861 spin_lock(&root->fs_info->delalloc_lock);
862 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE - existing_delalloc;
863 spin_unlock(&root->fs_info->delalloc_lock);
864
806 if (zero_start != PAGE_CACHE_SIZE) {
807 kaddr = kmap(page);
808 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
809 flush_dcache_page(page);
810 kunmap(page);
811 }
812 set_page_dirty(page);
813 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);

--- 62 unchanged lines hidden (view full) ---

876 u64 pos = (inode->i_size + mask) & ~mask;
877 u64 block_end = attr->ia_size | mask;
878 u64 hole_size;
879 u64 alloc_hint = 0;
880
881 if (attr->ia_size <= pos)
882 goto out;
883
865 if (zero_start != PAGE_CACHE_SIZE) {
866 kaddr = kmap(page);
867 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
868 flush_dcache_page(page);
869 kunmap(page);
870 }
871 set_page_dirty(page);
872 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);

--- 62 unchanged lines hidden (view full) ---

935 u64 pos = (inode->i_size + mask) & ~mask;
936 u64 block_end = attr->ia_size | mask;
937 u64 hole_size;
938 u64 alloc_hint = 0;
939
940 if (attr->ia_size <= pos)
941 goto out;
942
943 mutex_lock(&root->fs_info->fs_mutex);
944 err = btrfs_check_free_space(root, 1, 0);
945 mutex_unlock(&root->fs_info->fs_mutex);
946 if (err)
947 goto fail;
948
884 btrfs_truncate_page(inode->i_mapping, inode->i_size);
885
886 lock_extent(em_tree, pos, block_end, GFP_NOFS);
887 hole_size = (attr->ia_size - pos + mask) & ~mask;
888
889 mutex_lock(&root->fs_info->fs_mutex);
890 trans = btrfs_start_transaction(root, 1);
891 btrfs_set_trans_block_group(trans, inode);

--- 9 unchanged lines hidden (view full) ---

901 btrfs_end_transaction(trans, root);
902 mutex_unlock(&root->fs_info->fs_mutex);
903 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
904 if (err)
905 return err;
906 }
907out:
908 err = inode_setattr(inode, attr);
949 btrfs_truncate_page(inode->i_mapping, inode->i_size);
950
951 lock_extent(em_tree, pos, block_end, GFP_NOFS);
952 hole_size = (attr->ia_size - pos + mask) & ~mask;
953
954 mutex_lock(&root->fs_info->fs_mutex);
955 trans = btrfs_start_transaction(root, 1);
956 btrfs_set_trans_block_group(trans, inode);

--- 9 unchanged lines hidden (view full) ---

966 btrfs_end_transaction(trans, root);
967 mutex_unlock(&root->fs_info->fs_mutex);
968 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
969 if (err)
970 return err;
971 }
972out:
973 err = inode_setattr(inode, attr);
909
974fail:
910 return err;
911}
912void btrfs_delete_inode(struct inode *inode)
913{
914 struct btrfs_trans_handle *trans;
915 struct btrfs_root *root = BTRFS_I(inode)->root;
916 unsigned long nr;
917 int ret;

--- 517 unchanged lines hidden (view full) ---

1435 return err;
1436}
1437
1438static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1439 int mode, dev_t rdev)
1440{
1441 struct btrfs_trans_handle *trans;
1442 struct btrfs_root *root = BTRFS_I(dir)->root;
975 return err;
976}
977void btrfs_delete_inode(struct inode *inode)
978{
979 struct btrfs_trans_handle *trans;
980 struct btrfs_root *root = BTRFS_I(inode)->root;
981 unsigned long nr;
982 int ret;

--- 517 unchanged lines hidden (view full) ---

1500 return err;
1501}
1502
1503static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1504 int mode, dev_t rdev)
1505{
1506 struct btrfs_trans_handle *trans;
1507 struct btrfs_root *root = BTRFS_I(dir)->root;
1443 struct inode *inode;
1508 struct inode *inode = NULL;
1444 int err;
1445 int drop_inode = 0;
1446 u64 objectid;
1509 int err;
1510 int drop_inode = 0;
1511 u64 objectid;
1447 unsigned long nr;
1512 unsigned long nr = 0;
1448
1449 if (!new_valid_dev(rdev))
1450 return -EINVAL;
1451
1452 mutex_lock(&root->fs_info->fs_mutex);
1513
1514 if (!new_valid_dev(rdev))
1515 return -EINVAL;
1516
1517 mutex_lock(&root->fs_info->fs_mutex);
1518 err = btrfs_check_free_space(root, 1, 0);
1519 if (err)
1520 goto fail;
1521
1453 trans = btrfs_start_transaction(root, 1);
1454 btrfs_set_trans_block_group(trans, dir);
1455
1456 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1457 if (err) {
1458 err = -ENOSPC;
1459 goto out_unlock;
1460 }

--- 14 unchanged lines hidden (view full) ---

1475 btrfs_update_inode(trans, root, inode);
1476 }
1477 dir->i_sb->s_dirt = 1;
1478 btrfs_update_inode_block_group(trans, inode);
1479 btrfs_update_inode_block_group(trans, dir);
1480out_unlock:
1481 nr = trans->blocks_used;
1482 btrfs_end_transaction(trans, root);
1522 trans = btrfs_start_transaction(root, 1);
1523 btrfs_set_trans_block_group(trans, dir);
1524
1525 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1526 if (err) {
1527 err = -ENOSPC;
1528 goto out_unlock;
1529 }

--- 14 unchanged lines hidden (view full) ---

1544 btrfs_update_inode(trans, root, inode);
1545 }
1546 dir->i_sb->s_dirt = 1;
1547 btrfs_update_inode_block_group(trans, inode);
1548 btrfs_update_inode_block_group(trans, dir);
1549out_unlock:
1550 nr = trans->blocks_used;
1551 btrfs_end_transaction(trans, root);
1552fail:
1483 mutex_unlock(&root->fs_info->fs_mutex);
1484
1485 if (drop_inode) {
1486 inode_dec_link_count(inode);
1487 iput(inode);
1488 }
1489 btrfs_btree_balance_dirty(root, nr);
1490 return err;
1491}
1492
1493static int btrfs_create(struct inode *dir, struct dentry *dentry,
1494 int mode, struct nameidata *nd)
1495{
1496 struct btrfs_trans_handle *trans;
1497 struct btrfs_root *root = BTRFS_I(dir)->root;
1553 mutex_unlock(&root->fs_info->fs_mutex);
1554
1555 if (drop_inode) {
1556 inode_dec_link_count(inode);
1557 iput(inode);
1558 }
1559 btrfs_btree_balance_dirty(root, nr);
1560 return err;
1561}
1562
1563static int btrfs_create(struct inode *dir, struct dentry *dentry,
1564 int mode, struct nameidata *nd)
1565{
1566 struct btrfs_trans_handle *trans;
1567 struct btrfs_root *root = BTRFS_I(dir)->root;
1498 struct inode *inode;
1568 struct inode *inode = NULL;
1499 int err;
1500 int drop_inode = 0;
1569 int err;
1570 int drop_inode = 0;
1501 unsigned long nr;
1571 unsigned long nr = 0;
1502 u64 objectid;
1503
1504 mutex_lock(&root->fs_info->fs_mutex);
1572 u64 objectid;
1573
1574 mutex_lock(&root->fs_info->fs_mutex);
1575 err = btrfs_check_free_space(root, 1, 0);
1576 if (err)
1577 goto fail;
1505 trans = btrfs_start_transaction(root, 1);
1506 btrfs_set_trans_block_group(trans, dir);
1507
1508 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1509 if (err) {
1510 err = -ENOSPC;
1511 goto out_unlock;
1512 }

--- 17 unchanged lines hidden (view full) ---

1530 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
1531 }
1532 dir->i_sb->s_dirt = 1;
1533 btrfs_update_inode_block_group(trans, inode);
1534 btrfs_update_inode_block_group(trans, dir);
1535out_unlock:
1536 nr = trans->blocks_used;
1537 btrfs_end_transaction(trans, root);
1578 trans = btrfs_start_transaction(root, 1);
1579 btrfs_set_trans_block_group(trans, dir);
1580
1581 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1582 if (err) {
1583 err = -ENOSPC;
1584 goto out_unlock;
1585 }

--- 17 unchanged lines hidden (view full) ---

1603 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
1604 }
1605 dir->i_sb->s_dirt = 1;
1606 btrfs_update_inode_block_group(trans, inode);
1607 btrfs_update_inode_block_group(trans, dir);
1608out_unlock:
1609 nr = trans->blocks_used;
1610 btrfs_end_transaction(trans, root);
1611fail:
1538 mutex_unlock(&root->fs_info->fs_mutex);
1539
1540 if (drop_inode) {
1541 inode_dec_link_count(inode);
1542 iput(inode);
1543 }
1544 btrfs_btree_balance_dirty(root, nr);
1545 return err;
1546}
1547
1548static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1549 struct dentry *dentry)
1550{
1551 struct btrfs_trans_handle *trans;
1552 struct btrfs_root *root = BTRFS_I(dir)->root;
1553 struct inode *inode = old_dentry->d_inode;
1612 mutex_unlock(&root->fs_info->fs_mutex);
1613
1614 if (drop_inode) {
1615 inode_dec_link_count(inode);
1616 iput(inode);
1617 }
1618 btrfs_btree_balance_dirty(root, nr);
1619 return err;
1620}
1621
1622static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1623 struct dentry *dentry)
1624{
1625 struct btrfs_trans_handle *trans;
1626 struct btrfs_root *root = BTRFS_I(dir)->root;
1627 struct inode *inode = old_dentry->d_inode;
1554 unsigned long nr;
1628 unsigned long nr = 0;
1555 int err;
1556 int drop_inode = 0;
1557
1558 if (inode->i_nlink == 0)
1559 return -ENOENT;
1560
1561#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1562 inode->i_nlink++;
1563#else
1564 inc_nlink(inode);
1565#endif
1566 mutex_lock(&root->fs_info->fs_mutex);
1629 int err;
1630 int drop_inode = 0;
1631
1632 if (inode->i_nlink == 0)
1633 return -ENOENT;
1634
1635#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1636 inode->i_nlink++;
1637#else
1638 inc_nlink(inode);
1639#endif
1640 mutex_lock(&root->fs_info->fs_mutex);
1641 err = btrfs_check_free_space(root, 1, 0);
1642 if (err)
1643 goto fail;
1567 trans = btrfs_start_transaction(root, 1);
1568
1569 btrfs_set_trans_block_group(trans, dir);
1570 atomic_inc(&inode->i_count);
1571 err = btrfs_add_nondir(trans, dentry, inode);
1572
1573 if (err)
1574 drop_inode = 1;
1575
1576 dir->i_sb->s_dirt = 1;
1577 btrfs_update_inode_block_group(trans, dir);
1578 err = btrfs_update_inode(trans, root, inode);
1579
1580 if (err)
1581 drop_inode = 1;
1582
1583 nr = trans->blocks_used;
1584 btrfs_end_transaction(trans, root);
1644 trans = btrfs_start_transaction(root, 1);
1645
1646 btrfs_set_trans_block_group(trans, dir);
1647 atomic_inc(&inode->i_count);
1648 err = btrfs_add_nondir(trans, dentry, inode);
1649
1650 if (err)
1651 drop_inode = 1;
1652
1653 dir->i_sb->s_dirt = 1;
1654 btrfs_update_inode_block_group(trans, dir);
1655 err = btrfs_update_inode(trans, root, inode);
1656
1657 if (err)
1658 drop_inode = 1;
1659
1660 nr = trans->blocks_used;
1661 btrfs_end_transaction(trans, root);
1662fail:
1585 mutex_unlock(&root->fs_info->fs_mutex);
1586
1587 if (drop_inode) {
1588 inode_dec_link_count(inode);
1589 iput(inode);
1590 }
1591 btrfs_btree_balance_dirty(root, nr);
1592 return err;

--- 5 unchanged lines hidden (view full) ---

1598 struct btrfs_trans_handle *trans;
1599 struct btrfs_root *root = BTRFS_I(dir)->root;
1600 int err = 0;
1601 int drop_on_err = 0;
1602 u64 objectid;
1603 unsigned long nr = 1;
1604
1605 mutex_lock(&root->fs_info->fs_mutex);
1663 mutex_unlock(&root->fs_info->fs_mutex);
1664
1665 if (drop_inode) {
1666 inode_dec_link_count(inode);
1667 iput(inode);
1668 }
1669 btrfs_btree_balance_dirty(root, nr);
1670 return err;

--- 5 unchanged lines hidden (view full) ---

1676 struct btrfs_trans_handle *trans;
1677 struct btrfs_root *root = BTRFS_I(dir)->root;
1678 int err = 0;
1679 int drop_on_err = 0;
1680 u64 objectid;
1681 unsigned long nr = 1;
1682
1683 mutex_lock(&root->fs_info->fs_mutex);
1684 err = btrfs_check_free_space(root, 1, 0);
1685 if (err)
1686 goto out_unlock;
1687
1606 trans = btrfs_start_transaction(root, 1);
1607 btrfs_set_trans_block_group(trans, dir);
1608
1609 if (IS_ERR(trans)) {
1610 err = PTR_ERR(trans);
1611 goto out_unlock;
1612 }
1613

--- 250 unchanged lines hidden (view full) ---

1864static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
1865{
1866 return extent_bmap(mapping, iblock, btrfs_get_extent);
1867}
1868
1869static int btrfs_prepare_write(struct file *file, struct page *page,
1870 unsigned from, unsigned to)
1871{
1688 trans = btrfs_start_transaction(root, 1);
1689 btrfs_set_trans_block_group(trans, dir);
1690
1691 if (IS_ERR(trans)) {
1692 err = PTR_ERR(trans);
1693 goto out_unlock;
1694 }
1695

--- 250 unchanged lines hidden (view full) ---

1946static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
1947{
1948 return extent_bmap(mapping, iblock, btrfs_get_extent);
1949}
1950
1951static int btrfs_prepare_write(struct file *file, struct page *page,
1952 unsigned from, unsigned to)
1953{
1954 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1955 int err;
1956
1957 mutex_lock(&root->fs_info->fs_mutex);
1958 err = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
1959 mutex_lock(&root->fs_info->fs_mutex);
1960 if (err)
1961 return -ENOSPC;
1962
1872 return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree,
1873 page->mapping->host, page, from, to,
1874 btrfs_get_extent);
1875}
1876
1877int btrfs_readpage(struct file *file, struct page *page)
1878{
1879 struct extent_map_tree *tree;
1880 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1881 return extent_read_full_page(tree, page, btrfs_get_extent);
1882}
1963 return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree,
1964 page->mapping->host, page, from, to,
1965 btrfs_get_extent);
1966}
1967
1968int btrfs_readpage(struct file *file, struct page *page)
1969{
1970 struct extent_map_tree *tree;
1971 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1972 return extent_read_full_page(tree, page, btrfs_get_extent);
1973}
1974
1883static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1884{
1885 struct extent_map_tree *tree;
1886
1887
1888 if (current->flags & PF_MEMALLOC) {
1889 redirty_page_for_writepage(wbc, page);
1890 unlock_page(page);

--- 58 unchanged lines hidden (view full) ---

1949 * vmtruncate() writes the inode size before removing pages, once we have the
1950 * page lock we can determine safely if the page is beyond EOF. If it is not
1951 * beyond EOF, then the page is guaranteed safe against truncation until we
1952 * unlock the page.
1953 */
1954int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1955{
1956 struct inode *inode = fdentry(vma->vm_file)->d_inode;
1975static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1976{
1977 struct extent_map_tree *tree;
1978
1979
1980 if (current->flags & PF_MEMALLOC) {
1981 redirty_page_for_writepage(wbc, page);
1982 unlock_page(page);

--- 58 unchanged lines hidden (view full) ---

2041 * vmtruncate() writes the inode size before removing pages, once we have the
2042 * page lock we can determine safely if the page is beyond EOF. If it is not
2043 * beyond EOF, then the page is guaranteed safe against truncation until we
2044 * unlock the page.
2045 */
2046int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2047{
2048 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2049 struct btrfs_root *root = BTRFS_I(inode)->root;
1957 unsigned long end;
1958 loff_t size;
2050 unsigned long end;
2051 loff_t size;
1959 int ret = -EINVAL;
2052 int ret;
1960 u64 page_start;
1961
2053 u64 page_start;
2054
2055 mutex_lock(&root->fs_info->fs_mutex);
2056 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2057 mutex_lock(&root->fs_info->fs_mutex);
2058 if (ret)
2059 goto out;
2060
2061 ret = -EINVAL;
2062
1962 down_read(&BTRFS_I(inode)->root->snap_sem);
1963 lock_page(page);
1964 wait_on_page_writeback(page);
1965 size = i_size_read(inode);
1966 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1967
1968 if ((page->mapping != inode->i_mapping) ||
1969 (page_start > size)) {

--- 7 unchanged lines hidden (view full) ---

1977 else
1978 end = PAGE_CACHE_SIZE;
1979
1980 ret = btrfs_cow_one_page(inode, page, end);
1981
1982out_unlock:
1983 up_read(&BTRFS_I(inode)->root->snap_sem);
1984 unlock_page(page);
2063 down_read(&BTRFS_I(inode)->root->snap_sem);
2064 lock_page(page);
2065 wait_on_page_writeback(page);
2066 size = i_size_read(inode);
2067 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2068
2069 if ((page->mapping != inode->i_mapping) ||
2070 (page_start > size)) {

--- 7 unchanged lines hidden (view full) ---

2078 else
2079 end = PAGE_CACHE_SIZE;
2080
2081 ret = btrfs_cow_one_page(inode, page, end);
2082
2083out_unlock:
2084 up_read(&BTRFS_I(inode)->root->snap_sem);
2085 unlock_page(page);
2086out:
1985 return ret;
1986}
1987
1988static void btrfs_truncate(struct inode *inode)
1989{
1990 struct btrfs_root *root = BTRFS_I(inode)->root;
1991 int ret;
1992 struct btrfs_trans_handle *trans;

--- 48 unchanged lines hidden (view full) ---

2041 struct inode *dir;
2042 int ret;
2043 int err;
2044 u64 objectid;
2045 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2046 unsigned long nr = 1;
2047
2048 mutex_lock(&root->fs_info->fs_mutex);
2087 return ret;
2088}
2089
2090static void btrfs_truncate(struct inode *inode)
2091{
2092 struct btrfs_root *root = BTRFS_I(inode)->root;
2093 int ret;
2094 struct btrfs_trans_handle *trans;

--- 48 unchanged lines hidden (view full) ---

2143 struct inode *dir;
2144 int ret;
2145 int err;
2146 u64 objectid;
2147 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2148 unsigned long nr = 1;
2149
2150 mutex_lock(&root->fs_info->fs_mutex);
2151 ret = btrfs_check_free_space(root, 1, 0);
2152 if (ret)
2153 goto fail_commit;
2154
2049 trans = btrfs_start_transaction(root, 1);
2050 BUG_ON(!trans);
2051
2052 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2053 0, &objectid);
2054 if (ret)
2055 goto fail;
2056

--- 100 unchanged lines hidden (view full) ---

2157{
2158 struct btrfs_trans_handle *trans;
2159 struct btrfs_key key;
2160 struct btrfs_root_item new_root_item;
2161 struct extent_buffer *tmp;
2162 int ret;
2163 int err;
2164 u64 objectid;
2155 trans = btrfs_start_transaction(root, 1);
2156 BUG_ON(!trans);
2157
2158 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2159 0, &objectid);
2160 if (ret)
2161 goto fail;
2162

--- 100 unchanged lines hidden (view full) ---

2263{
2264 struct btrfs_trans_handle *trans;
2265 struct btrfs_key key;
2266 struct btrfs_root_item new_root_item;
2267 struct extent_buffer *tmp;
2268 int ret;
2269 int err;
2270 u64 objectid;
2165 unsigned long nr;
2271 unsigned long nr = 0;
2166
2167 if (!root->ref_cows)
2168 return -EINVAL;
2169
2170 down_write(&root->snap_sem);
2171 freeze_bdev(root->fs_info->sb->s_bdev);
2172 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
2173
2174 mutex_lock(&root->fs_info->fs_mutex);
2272
2273 if (!root->ref_cows)
2274 return -EINVAL;
2275
2276 down_write(&root->snap_sem);
2277 freeze_bdev(root->fs_info->sb->s_bdev);
2278 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
2279
2280 mutex_lock(&root->fs_info->fs_mutex);
2281 ret = btrfs_check_free_space(root, 1, 0);
2282 if (ret)
2283 goto fail_unlock;
2284
2175 trans = btrfs_start_transaction(root, 1);
2176 BUG_ON(!trans);
2177
2178 ret = btrfs_update_inode(trans, root, root->inode);
2179 if (ret)
2180 goto fail;
2181
2182 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,

--- 41 unchanged lines hidden (view full) ---

2224 if (ret)
2225 goto fail;
2226fail:
2227 nr = trans->blocks_used;
2228 err = btrfs_commit_transaction(trans, root);
2229
2230 if (err && !ret)
2231 ret = err;
2285 trans = btrfs_start_transaction(root, 1);
2286 BUG_ON(!trans);
2287
2288 ret = btrfs_update_inode(trans, root, root->inode);
2289 if (ret)
2290 goto fail;
2291
2292 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,

--- 41 unchanged lines hidden (view full) ---

2334 if (ret)
2335 goto fail;
2336fail:
2337 nr = trans->blocks_used;
2338 err = btrfs_commit_transaction(trans, root);
2339
2340 if (err && !ret)
2341 ret = err;
2232
2342fail_unlock:
2233 mutex_unlock(&root->fs_info->fs_mutex);
2234 up_write(&root->snap_sem);
2235 btrfs_btree_balance_dirty(root, nr);
2236 return ret;
2237}
2238
2239static unsigned long force_ra(struct address_space *mapping,
2240 struct file_ra_state *ra, struct file *file,

--- 9 unchanged lines hidden (view full) ---

2250 req_size = min(last_index - offset + 1, (pgoff_t)128);
2251 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2252 return offset + req_size;
2253#endif
2254}
2255
2256int btrfs_defrag_file(struct file *file) {
2257 struct inode *inode = fdentry(file)->d_inode;
2343 mutex_unlock(&root->fs_info->fs_mutex);
2344 up_write(&root->snap_sem);
2345 btrfs_btree_balance_dirty(root, nr);
2346 return ret;
2347}
2348
2349static unsigned long force_ra(struct address_space *mapping,
2350 struct file_ra_state *ra, struct file *file,

--- 9 unchanged lines hidden (view full) ---

2360 req_size = min(last_index - offset + 1, (pgoff_t)128);
2361 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2362 return offset + req_size;
2363#endif
2364}
2365
2366int btrfs_defrag_file(struct file *file) {
2367 struct inode *inode = fdentry(file)->d_inode;
2368 struct btrfs_root *root = BTRFS_I(inode)->root;
2258 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2259 struct page *page;
2260 unsigned long last_index;
2261 unsigned long ra_index = 0;
2262 u64 page_start;
2263 u64 page_end;
2264 unsigned long i;
2369 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2370 struct page *page;
2371 unsigned long last_index;
2372 unsigned long ra_index = 0;
2373 u64 page_start;
2374 u64 page_end;
2375 unsigned long i;
2376 int ret;
2265
2377
2378 mutex_lock(&root->fs_info->fs_mutex);
2379 ret = btrfs_check_free_space(root, inode->i_size, 0);
2380 mutex_unlock(&root->fs_info->fs_mutex);
2381 if (ret)
2382 return -ENOSPC;
2383
2266 mutex_lock(&inode->i_mutex);
2267 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2268 for (i = 0; i <= last_index; i++) {
2269 if (i == ra_index) {
2270 ra_index = force_ra(inode->i_mapping, &file->f_ra,
2271 file, ra_index, last_index);
2272 }
2273 page = grab_cache_page(inode->i_mapping, i);

--- 243 unchanged lines hidden (view full) ---

2517 int ret;
2518
2519 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2520 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2521 return -ENOTEMPTY;
2522 }
2523
2524 mutex_lock(&root->fs_info->fs_mutex);
2384 mutex_lock(&inode->i_mutex);
2385 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2386 for (i = 0; i <= last_index; i++) {
2387 if (i == ra_index) {
2388 ra_index = force_ra(inode->i_mapping, &file->f_ra,
2389 file, ra_index, last_index);
2390 }
2391 page = grab_cache_page(inode->i_mapping, i);

--- 243 unchanged lines hidden (view full) ---

2635 int ret;
2636
2637 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2638 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2639 return -ENOTEMPTY;
2640 }
2641
2642 mutex_lock(&root->fs_info->fs_mutex);
2643 ret = btrfs_check_free_space(root, 1, 0);
2644 if (ret)
2645 goto out_unlock;
2646
2525 trans = btrfs_start_transaction(root, 1);
2526
2527 btrfs_set_trans_block_group(trans, new_dir);
2528 path = btrfs_alloc_path();
2529 if (!path) {
2530 ret = -ENOMEM;
2531 goto out_fail;
2532 }

--- 15 unchanged lines hidden (view full) ---

2548 }
2549 ret = btrfs_add_link(trans, new_dentry, old_inode);
2550 if (ret)
2551 goto out_fail;
2552
2553out_fail:
2554 btrfs_free_path(path);
2555 btrfs_end_transaction(trans, root);
2647 trans = btrfs_start_transaction(root, 1);
2648
2649 btrfs_set_trans_block_group(trans, new_dir);
2650 path = btrfs_alloc_path();
2651 if (!path) {
2652 ret = -ENOMEM;
2653 goto out_fail;
2654 }

--- 15 unchanged lines hidden (view full) ---

2670 }
2671 ret = btrfs_add_link(trans, new_dentry, old_inode);
2672 if (ret)
2673 goto out_fail;
2674
2675out_fail:
2676 btrfs_free_path(path);
2677 btrfs_end_transaction(trans, root);
2678out_unlock:
2556 mutex_unlock(&root->fs_info->fs_mutex);
2557 return ret;
2558}
2559
2560static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2561 const char *symname)
2562{
2563 struct btrfs_trans_handle *trans;
2564 struct btrfs_root *root = BTRFS_I(dir)->root;
2565 struct btrfs_path *path;
2566 struct btrfs_key key;
2679 mutex_unlock(&root->fs_info->fs_mutex);
2680 return ret;
2681}
2682
2683static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2684 const char *symname)
2685{
2686 struct btrfs_trans_handle *trans;
2687 struct btrfs_root *root = BTRFS_I(dir)->root;
2688 struct btrfs_path *path;
2689 struct btrfs_key key;
2567 struct inode *inode;
2690 struct inode *inode = NULL;
2568 int err;
2569 int drop_inode = 0;
2570 u64 objectid;
2571 int name_len;
2572 int datasize;
2573 unsigned long ptr;
2574 struct btrfs_file_extent_item *ei;
2575 struct extent_buffer *leaf;
2691 int err;
2692 int drop_inode = 0;
2693 u64 objectid;
2694 int name_len;
2695 int datasize;
2696 unsigned long ptr;
2697 struct btrfs_file_extent_item *ei;
2698 struct extent_buffer *leaf;
2576 unsigned long nr;
2699 unsigned long nr = 0;
2577
2578 name_len = strlen(symname) + 1;
2579 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2580 return -ENAMETOOLONG;
2700
2701 name_len = strlen(symname) + 1;
2702 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2703 return -ENAMETOOLONG;
2704
2581 mutex_lock(&root->fs_info->fs_mutex);
2705 mutex_lock(&root->fs_info->fs_mutex);
2706 err = btrfs_check_free_space(root, 1, 0);
2707 if (err)
2708 goto out_fail;
2709
2582 trans = btrfs_start_transaction(root, 1);
2583 btrfs_set_trans_block_group(trans, dir);
2584
2585 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2586 if (err) {
2587 err = -ENOSPC;
2588 goto out_unlock;
2589 }

--- 50 unchanged lines hidden (view full) ---

2640 inode->i_size = name_len - 1;
2641 err = btrfs_update_inode(trans, root, inode);
2642 if (err)
2643 drop_inode = 1;
2644
2645out_unlock:
2646 nr = trans->blocks_used;
2647 btrfs_end_transaction(trans, root);
2710 trans = btrfs_start_transaction(root, 1);
2711 btrfs_set_trans_block_group(trans, dir);
2712
2713 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2714 if (err) {
2715 err = -ENOSPC;
2716 goto out_unlock;
2717 }

--- 50 unchanged lines hidden (view full) ---

2768 inode->i_size = name_len - 1;
2769 err = btrfs_update_inode(trans, root, inode);
2770 if (err)
2771 drop_inode = 1;
2772
2773out_unlock:
2774 nr = trans->blocks_used;
2775 btrfs_end_transaction(trans, root);
2776out_fail:
2648 mutex_unlock(&root->fs_info->fs_mutex);
2649 if (drop_inode) {
2650 inode_dec_link_count(inode);
2651 iput(inode);
2652 }
2653 btrfs_btree_balance_dirty(root, nr);
2654 return err;
2655}

--- 80 unchanged lines hidden ---
2777 mutex_unlock(&root->fs_info->fs_mutex);
2778 if (drop_inode) {
2779 inode_dec_link_count(inode);
2780 iput(inode);
2781 }
2782 btrfs_btree_balance_dirty(root, nr);
2783 return err;
2784}

--- 80 unchanged lines hidden ---