aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs')
-rw-r--r--fs/btrfs/btrfs_inode.h6
-rw-r--r--fs/btrfs/ctree.h10
-rw-r--r--fs/btrfs/extent-tree.c77
-rw-r--r--fs/btrfs/file-item.c4
-rw-r--r--fs/btrfs/file.c49
-rw-r--r--fs/btrfs/free-space-cache.c20
-rw-r--r--fs/btrfs/inode.c52
-rw-r--r--fs/btrfs/ioctl.c36
-rw-r--r--fs/btrfs/transaction.c4
-rw-r--r--fs/btrfs/tree-log.c28
-rw-r--r--fs/btrfs/volumes.c51
-rw-r--r--fs/btrfs/volumes.h2
-rw-r--r--fs/btrfs/xattr.c9
13 files changed, 277 insertions, 71 deletions
diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h
index 502b9e988679..d9f99a16edd6 100644
--- a/fs/btrfs/btrfs_inode.h
+++ b/fs/btrfs/btrfs_inode.h
@@ -176,7 +176,11 @@ static inline u64 btrfs_ino(struct inode *inode)
176{ 176{
177 u64 ino = BTRFS_I(inode)->location.objectid; 177 u64 ino = BTRFS_I(inode)->location.objectid;
178 178
179 if (ino <= BTRFS_FIRST_FREE_OBJECTID) 179 /*
180 * !ino: btree_inode
181 * type == BTRFS_ROOT_ITEM_KEY: subvol dir
182 */
183 if (!ino || BTRFS_I(inode)->location.type == BTRFS_ROOT_ITEM_KEY)
180 ino = inode->i_ino; 184 ino = inode->i_ino;
181 return ino; 185 return ino;
182} 186}
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 0469263e327e..03912c5c6f49 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -1415,17 +1415,15 @@ void btrfs_set_##name(struct extent_buffer *eb, type *s, u##bits val);
1415#define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \ 1415#define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \
1416static inline u##bits btrfs_##name(struct extent_buffer *eb) \ 1416static inline u##bits btrfs_##name(struct extent_buffer *eb) \
1417{ \ 1417{ \
1418 type *p = kmap_atomic(eb->first_page, KM_USER0); \ 1418 type *p = page_address(eb->first_page); \
1419 u##bits res = le##bits##_to_cpu(p->member); \ 1419 u##bits res = le##bits##_to_cpu(p->member); \
1420 kunmap_atomic(p, KM_USER0); \
1421 return res; \ 1420 return res; \
1422} \ 1421} \
1423static inline void btrfs_set_##name(struct extent_buffer *eb, \ 1422static inline void btrfs_set_##name(struct extent_buffer *eb, \
1424 u##bits val) \ 1423 u##bits val) \
1425{ \ 1424{ \
1426 type *p = kmap_atomic(eb->first_page, KM_USER0); \ 1425 type *p = page_address(eb->first_page); \
1427 p->member = cpu_to_le##bits(val); \ 1426 p->member = cpu_to_le##bits(val); \
1428 kunmap_atomic(p, KM_USER0); \
1429} 1427}
1430 1428
1431#define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \ 1429#define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \
@@ -2367,8 +2365,8 @@ static inline int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
2367int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path); 2365int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
2368int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path); 2366int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path);
2369int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf); 2367int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf);
2370int btrfs_drop_snapshot(struct btrfs_root *root, 2368void btrfs_drop_snapshot(struct btrfs_root *root,
2371 struct btrfs_block_rsv *block_rsv, int update_ref); 2369 struct btrfs_block_rsv *block_rsv, int update_ref);
2372int btrfs_drop_subtree(struct btrfs_trans_handle *trans, 2370int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
2373 struct btrfs_root *root, 2371 struct btrfs_root *root,
2374 struct extent_buffer *node, 2372 struct extent_buffer *node,
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 66bac226944e..f5be06a2462f 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -1782,6 +1782,9 @@ static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1782 1782
1783 1783
1784 for (i = 0; i < multi->num_stripes; i++, stripe++) { 1784 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1785 if (!stripe->dev->can_discard)
1786 continue;
1787
1785 ret = btrfs_issue_discard(stripe->dev->bdev, 1788 ret = btrfs_issue_discard(stripe->dev->bdev,
1786 stripe->physical, 1789 stripe->physical,
1787 stripe->length); 1790 stripe->length);
@@ -1789,11 +1792,16 @@ static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1789 discarded_bytes += stripe->length; 1792 discarded_bytes += stripe->length;
1790 else if (ret != -EOPNOTSUPP) 1793 else if (ret != -EOPNOTSUPP)
1791 break; 1794 break;
1795
1796 /*
1797 * Just in case we get back EOPNOTSUPP for some reason,
1798 * just ignore the return value so we don't screw up
1799 * people calling discard_extent.
1800 */
1801 ret = 0;
1792 } 1802 }
1793 kfree(multi); 1803 kfree(multi);
1794 } 1804 }
1795 if (discarded_bytes && ret == -EOPNOTSUPP)
1796 ret = 0;
1797 1805
1798 if (actual_bytes) 1806 if (actual_bytes)
1799 *actual_bytes = discarded_bytes; 1807 *actual_bytes = discarded_bytes;
@@ -6269,8 +6277,8 @@ static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
6269 * also make sure backrefs for the shared block and all lower level 6277 * also make sure backrefs for the shared block and all lower level
6270 * blocks are properly updated. 6278 * blocks are properly updated.
6271 */ 6279 */
6272int btrfs_drop_snapshot(struct btrfs_root *root, 6280void btrfs_drop_snapshot(struct btrfs_root *root,
6273 struct btrfs_block_rsv *block_rsv, int update_ref) 6281 struct btrfs_block_rsv *block_rsv, int update_ref)
6274{ 6282{
6275 struct btrfs_path *path; 6283 struct btrfs_path *path;
6276 struct btrfs_trans_handle *trans; 6284 struct btrfs_trans_handle *trans;
@@ -6283,13 +6291,16 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
6283 int level; 6291 int level;
6284 6292
6285 path = btrfs_alloc_path(); 6293 path = btrfs_alloc_path();
6286 if (!path) 6294 if (!path) {
6287 return -ENOMEM; 6295 err = -ENOMEM;
6296 goto out;
6297 }
6288 6298
6289 wc = kzalloc(sizeof(*wc), GFP_NOFS); 6299 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6290 if (!wc) { 6300 if (!wc) {
6291 btrfs_free_path(path); 6301 btrfs_free_path(path);
6292 return -ENOMEM; 6302 err = -ENOMEM;
6303 goto out;
6293 } 6304 }
6294 6305
6295 trans = btrfs_start_transaction(tree_root, 0); 6306 trans = btrfs_start_transaction(tree_root, 0);
@@ -6318,7 +6329,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
6318 path->lowest_level = 0; 6329 path->lowest_level = 0;
6319 if (ret < 0) { 6330 if (ret < 0) {
6320 err = ret; 6331 err = ret;
6321 goto out; 6332 goto out_free;
6322 } 6333 }
6323 WARN_ON(ret > 0); 6334 WARN_ON(ret > 0);
6324 6335
@@ -6425,11 +6436,14 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
6425 free_extent_buffer(root->commit_root); 6436 free_extent_buffer(root->commit_root);
6426 kfree(root); 6437 kfree(root);
6427 } 6438 }
6428out: 6439out_free:
6429 btrfs_end_transaction_throttle(trans, tree_root); 6440 btrfs_end_transaction_throttle(trans, tree_root);
6430 kfree(wc); 6441 kfree(wc);
6431 btrfs_free_path(path); 6442 btrfs_free_path(path);
6432 return err; 6443out:
6444 if (err)
6445 btrfs_std_error(root->fs_info, err);
6446 return;
6433} 6447}
6434 6448
6435/* 6449/*
@@ -6720,6 +6734,10 @@ int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
6720 struct btrfs_space_info *space_info; 6734 struct btrfs_space_info *space_info;
6721 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 6735 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6722 struct btrfs_device *device; 6736 struct btrfs_device *device;
6737 u64 min_free;
6738 u64 dev_min = 1;
6739 u64 dev_nr = 0;
6740 int index;
6723 int full = 0; 6741 int full = 0;
6724 int ret = 0; 6742 int ret = 0;
6725 6743
@@ -6729,8 +6747,10 @@ int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
6729 if (!block_group) 6747 if (!block_group)
6730 return -1; 6748 return -1;
6731 6749
6750 min_free = btrfs_block_group_used(&block_group->item);
6751
6732 /* no bytes used, we're good */ 6752 /* no bytes used, we're good */
6733 if (!btrfs_block_group_used(&block_group->item)) 6753 if (!min_free)
6734 goto out; 6754 goto out;
6735 6755
6736 space_info = block_group->space_info; 6756 space_info = block_group->space_info;
@@ -6746,10 +6766,9 @@ int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
6746 * all of the extents from this block group. If we can, we're good 6766 * all of the extents from this block group. If we can, we're good
6747 */ 6767 */
6748 if ((space_info->total_bytes != block_group->key.offset) && 6768 if ((space_info->total_bytes != block_group->key.offset) &&
6749 (space_info->bytes_used + space_info->bytes_reserved + 6769 (space_info->bytes_used + space_info->bytes_reserved +
6750 space_info->bytes_pinned + space_info->bytes_readonly + 6770 space_info->bytes_pinned + space_info->bytes_readonly +
6751 btrfs_block_group_used(&block_group->item) < 6771 min_free < space_info->total_bytes)) {
6752 space_info->total_bytes)) {
6753 spin_unlock(&space_info->lock); 6772 spin_unlock(&space_info->lock);
6754 goto out; 6773 goto out;
6755 } 6774 }
@@ -6766,9 +6785,31 @@ int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
6766 if (full) 6785 if (full)
6767 goto out; 6786 goto out;
6768 6787
6788 /*
6789 * index:
6790 * 0: raid10
6791 * 1: raid1
6792 * 2: dup
6793 * 3: raid0
6794 * 4: single
6795 */
6796 index = get_block_group_index(block_group);
6797 if (index == 0) {
6798 dev_min = 4;
6799 /* Divide by 2 */
6800 min_free >>= 1;
6801 } else if (index == 1) {
6802 dev_min = 2;
6803 } else if (index == 2) {
6804 /* Multiply by 2 */
6805 min_free <<= 1;
6806 } else if (index == 3) {
6807 dev_min = fs_devices->rw_devices;
6808 do_div(min_free, dev_min);
6809 }
6810
6769 mutex_lock(&root->fs_info->chunk_mutex); 6811 mutex_lock(&root->fs_info->chunk_mutex);
6770 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { 6812 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
6771 u64 min_free = btrfs_block_group_used(&block_group->item);
6772 u64 dev_offset; 6813 u64 dev_offset;
6773 6814
6774 /* 6815 /*
@@ -6779,7 +6820,11 @@ int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
6779 ret = find_free_dev_extent(NULL, device, min_free, 6820 ret = find_free_dev_extent(NULL, device, min_free,
6780 &dev_offset, NULL); 6821 &dev_offset, NULL);
6781 if (!ret) 6822 if (!ret)
6823 dev_nr++;
6824
6825 if (dev_nr >= dev_min)
6782 break; 6826 break;
6827
6783 ret = -1; 6828 ret = -1;
6784 } 6829 }
6785 } 6830 }
diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c
index b910694f61ed..a1cb7821becd 100644
--- a/fs/btrfs/file-item.c
+++ b/fs/btrfs/file-item.c
@@ -183,8 +183,10 @@ static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
183 * read from the commit root and sidestep a nasty deadlock 183 * read from the commit root and sidestep a nasty deadlock
184 * between reading the free space cache and updating the csum tree. 184 * between reading the free space cache and updating the csum tree.
185 */ 185 */
186 if (btrfs_is_free_space_inode(root, inode)) 186 if (btrfs_is_free_space_inode(root, inode)) {
187 path->search_commit_root = 1; 187 path->search_commit_root = 1;
188 path->skip_locking = 1;
189 }
188 190
189 disk_bytenr = (u64)bio->bi_sector << 9; 191 disk_bytenr = (u64)bio->bi_sector << 9;
190 if (dio) 192 if (dio)
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 658d66959abe..a381cd22f518 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -150,6 +150,8 @@ int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
150 spin_lock(&root->fs_info->defrag_inodes_lock); 150 spin_lock(&root->fs_info->defrag_inodes_lock);
151 if (!BTRFS_I(inode)->in_defrag) 151 if (!BTRFS_I(inode)->in_defrag)
152 __btrfs_add_inode_defrag(inode, defrag); 152 __btrfs_add_inode_defrag(inode, defrag);
153 else
154 kfree(defrag);
153 spin_unlock(&root->fs_info->defrag_inodes_lock); 155 spin_unlock(&root->fs_info->defrag_inodes_lock);
154 return 0; 156 return 0;
155} 157}
@@ -1073,12 +1075,6 @@ static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
1073 start_pos = pos & ~((u64)root->sectorsize - 1); 1075 start_pos = pos & ~((u64)root->sectorsize - 1);
1074 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT; 1076 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
1075 1077
1076 if (start_pos > inode->i_size) {
1077 err = btrfs_cont_expand(inode, i_size_read(inode), start_pos);
1078 if (err)
1079 return err;
1080 }
1081
1082again: 1078again:
1083 for (i = 0; i < num_pages; i++) { 1079 for (i = 0; i < num_pages; i++) {
1084 pages[i] = find_or_create_page(inode->i_mapping, index + i, 1080 pages[i] = find_or_create_page(inode->i_mapping, index + i,
@@ -1336,6 +1332,7 @@ static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
1336 struct inode *inode = fdentry(file)->d_inode; 1332 struct inode *inode = fdentry(file)->d_inode;
1337 struct btrfs_root *root = BTRFS_I(inode)->root; 1333 struct btrfs_root *root = BTRFS_I(inode)->root;
1338 loff_t *ppos = &iocb->ki_pos; 1334 loff_t *ppos = &iocb->ki_pos;
1335 u64 start_pos;
1339 ssize_t num_written = 0; 1336 ssize_t num_written = 0;
1340 ssize_t err = 0; 1337 ssize_t err = 0;
1341 size_t count, ocount; 1338 size_t count, ocount;
@@ -1384,6 +1381,15 @@ static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
1384 file_update_time(file); 1381 file_update_time(file);
1385 BTRFS_I(inode)->sequence++; 1382 BTRFS_I(inode)->sequence++;
1386 1383
1384 start_pos = round_down(pos, root->sectorsize);
1385 if (start_pos > i_size_read(inode)) {
1386 err = btrfs_cont_expand(inode, i_size_read(inode), start_pos);
1387 if (err) {
1388 mutex_unlock(&inode->i_mutex);
1389 goto out;
1390 }
1391 }
1392
1387 if (unlikely(file->f_flags & O_DIRECT)) { 1393 if (unlikely(file->f_flags & O_DIRECT)) {
1388 num_written = __btrfs_direct_write(iocb, iov, nr_segs, 1394 num_written = __btrfs_direct_write(iocb, iov, nr_segs,
1389 pos, ppos, count, ocount); 1395 pos, ppos, count, ocount);
@@ -1638,11 +1644,15 @@ static long btrfs_fallocate(struct file *file, int mode,
1638 1644
1639 cur_offset = alloc_start; 1645 cur_offset = alloc_start;
1640 while (1) { 1646 while (1) {
1647 u64 actual_end;
1648
1641 em = btrfs_get_extent(inode, NULL, 0, cur_offset, 1649 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
1642 alloc_end - cur_offset, 0); 1650 alloc_end - cur_offset, 0);
1643 BUG_ON(IS_ERR_OR_NULL(em)); 1651 BUG_ON(IS_ERR_OR_NULL(em));
1644 last_byte = min(extent_map_end(em), alloc_end); 1652 last_byte = min(extent_map_end(em), alloc_end);
1653 actual_end = min_t(u64, extent_map_end(em), offset + len);
1645 last_byte = (last_byte + mask) & ~mask; 1654 last_byte = (last_byte + mask) & ~mask;
1655
1646 if (em->block_start == EXTENT_MAP_HOLE || 1656 if (em->block_start == EXTENT_MAP_HOLE ||
1647 (cur_offset >= inode->i_size && 1657 (cur_offset >= inode->i_size &&
1648 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { 1658 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
@@ -1655,6 +1665,16 @@ static long btrfs_fallocate(struct file *file, int mode,
1655 free_extent_map(em); 1665 free_extent_map(em);
1656 break; 1666 break;
1657 } 1667 }
1668 } else if (actual_end > inode->i_size &&
1669 !(mode & FALLOC_FL_KEEP_SIZE)) {
1670 /*
1671 * We didn't need to allocate any more space, but we
1672 * still extended the size of the file so we need to
1673 * update i_size.
1674 */
1675 inode->i_ctime = CURRENT_TIME;
1676 i_size_write(inode, actual_end);
1677 btrfs_ordered_update_i_size(inode, actual_end, NULL);
1658 } 1678 }
1659 free_extent_map(em); 1679 free_extent_map(em);
1660 1680
@@ -1797,6 +1817,11 @@ static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int origin)
1797 goto out; 1817 goto out;
1798 case SEEK_DATA: 1818 case SEEK_DATA:
1799 case SEEK_HOLE: 1819 case SEEK_HOLE:
1820 if (offset >= i_size_read(inode)) {
1821 mutex_unlock(&inode->i_mutex);
1822 return -ENXIO;
1823 }
1824
1800 ret = find_desired_extent(inode, &offset, origin); 1825 ret = find_desired_extent(inode, &offset, origin);
1801 if (ret) { 1826 if (ret) {
1802 mutex_unlock(&inode->i_mutex); 1827 mutex_unlock(&inode->i_mutex);
@@ -1804,10 +1829,14 @@ static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int origin)
1804 } 1829 }
1805 } 1830 }
1806 1831
1807 if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET)) 1832 if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET)) {
1808 return -EINVAL; 1833 offset = -EINVAL;
1809 if (offset > inode->i_sb->s_maxbytes) 1834 goto out;
1810 return -EINVAL; 1835 }
1836 if (offset > inode->i_sb->s_maxbytes) {
1837 offset = -EINVAL;
1838 goto out;
1839 }
1811 1840
1812 /* Special lock needed here? */ 1841 /* Special lock needed here? */
1813 if (offset != file->f_pos) { 1842 if (offset != file->f_pos) {
diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
index 6377713f639c..41ac927401d0 100644
--- a/fs/btrfs/free-space-cache.c
+++ b/fs/btrfs/free-space-cache.c
@@ -190,9 +190,11 @@ int btrfs_truncate_free_space_cache(struct btrfs_root *root,
190 struct btrfs_path *path, 190 struct btrfs_path *path,
191 struct inode *inode) 191 struct inode *inode)
192{ 192{
193 struct btrfs_block_rsv *rsv;
193 loff_t oldsize; 194 loff_t oldsize;
194 int ret = 0; 195 int ret = 0;
195 196
197 rsv = trans->block_rsv;
196 trans->block_rsv = root->orphan_block_rsv; 198 trans->block_rsv = root->orphan_block_rsv;
197 ret = btrfs_block_rsv_check(trans, root, 199 ret = btrfs_block_rsv_check(trans, root,
198 root->orphan_block_rsv, 200 root->orphan_block_rsv,
@@ -210,6 +212,8 @@ int btrfs_truncate_free_space_cache(struct btrfs_root *root,
210 */ 212 */
211 ret = btrfs_truncate_inode_items(trans, root, inode, 213 ret = btrfs_truncate_inode_items(trans, root, inode,
212 0, BTRFS_EXTENT_DATA_KEY); 214 0, BTRFS_EXTENT_DATA_KEY);
215
216 trans->block_rsv = rsv;
213 if (ret) { 217 if (ret) {
214 WARN_ON(1); 218 WARN_ON(1);
215 return ret; 219 return ret;
@@ -1168,9 +1172,9 @@ static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
1168 div64_u64(extent_bytes, (sizeof(struct btrfs_free_space))); 1172 div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
1169} 1173}
1170 1174
1171static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl, 1175static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1172 struct btrfs_free_space *info, u64 offset, 1176 struct btrfs_free_space *info,
1173 u64 bytes) 1177 u64 offset, u64 bytes)
1174{ 1178{
1175 unsigned long start, count; 1179 unsigned long start, count;
1176 1180
@@ -1181,6 +1185,13 @@ static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1181 bitmap_clear(info->bitmap, start, count); 1185 bitmap_clear(info->bitmap, start, count);
1182 1186
1183 info->bytes -= bytes; 1187 info->bytes -= bytes;
1188}
1189
1190static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1191 struct btrfs_free_space *info, u64 offset,
1192 u64 bytes)
1193{
1194 __bitmap_clear_bits(ctl, info, offset, bytes);
1184 ctl->free_space -= bytes; 1195 ctl->free_space -= bytes;
1185} 1196}
1186 1197
@@ -1984,7 +1995,7 @@ static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
1984 return 0; 1995 return 0;
1985 1996
1986 ret = search_start; 1997 ret = search_start;
1987 bitmap_clear_bits(ctl, entry, ret, bytes); 1998 __bitmap_clear_bits(ctl, entry, ret, bytes);
1988 1999
1989 return ret; 2000 return ret;
1990} 2001}
@@ -2039,7 +2050,6 @@ u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2039 continue; 2050 continue;
2040 } 2051 }
2041 } else { 2052 } else {
2042
2043 ret = entry->offset; 2053 ret = entry->offset;
2044 2054
2045 entry->offset += bytes; 2055 entry->offset += bytes;
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 15fceefbca0a..b2d004ad66a0 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -1786,7 +1786,7 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1786 &ordered_extent->list); 1786 &ordered_extent->list);
1787 1787
1788 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); 1788 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1789 if (!ret) { 1789 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1790 ret = btrfs_update_inode(trans, root, inode); 1790 ret = btrfs_update_inode(trans, root, inode);
1791 BUG_ON(ret); 1791 BUG_ON(ret);
1792 } 1792 }
@@ -3510,15 +3510,19 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
3510 err = btrfs_drop_extents(trans, inode, cur_offset, 3510 err = btrfs_drop_extents(trans, inode, cur_offset,
3511 cur_offset + hole_size, 3511 cur_offset + hole_size,
3512 &hint_byte, 1); 3512 &hint_byte, 1);
3513 if (err) 3513 if (err) {
3514 btrfs_end_transaction(trans, root);
3514 break; 3515 break;
3516 }
3515 3517
3516 err = btrfs_insert_file_extent(trans, root, 3518 err = btrfs_insert_file_extent(trans, root,
3517 btrfs_ino(inode), cur_offset, 0, 3519 btrfs_ino(inode), cur_offset, 0,
3518 0, hole_size, 0, hole_size, 3520 0, hole_size, 0, hole_size,
3519 0, 0, 0); 3521 0, 0, 0);
3520 if (err) 3522 if (err) {
3523 btrfs_end_transaction(trans, root);
3521 break; 3524 break;
3525 }
3522 3526
3523 btrfs_drop_extent_cache(inode, hole_start, 3527 btrfs_drop_extent_cache(inode, hole_start,
3524 last_byte - 1, 0); 3528 last_byte - 1, 0);
@@ -3952,7 +3956,6 @@ struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
3952 struct btrfs_root *root, int *new) 3956 struct btrfs_root *root, int *new)
3953{ 3957{
3954 struct inode *inode; 3958 struct inode *inode;
3955 int bad_inode = 0;
3956 3959
3957 inode = btrfs_iget_locked(s, location->objectid, root); 3960 inode = btrfs_iget_locked(s, location->objectid, root);
3958 if (!inode) 3961 if (!inode)
@@ -3968,15 +3971,12 @@ struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
3968 if (new) 3971 if (new)
3969 *new = 1; 3972 *new = 1;
3970 } else { 3973 } else {
3971 bad_inode = 1; 3974 unlock_new_inode(inode);
3975 iput(inode);
3976 inode = ERR_PTR(-ESTALE);
3972 } 3977 }
3973 } 3978 }
3974 3979
3975 if (bad_inode) {
3976 iput(inode);
3977 inode = ERR_PTR(-ESTALE);
3978 }
3979
3980 return inode; 3980 return inode;
3981} 3981}
3982 3982
@@ -4018,7 +4018,8 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
4018 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key)); 4018 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4019 kfree(dentry->d_fsdata); 4019 kfree(dentry->d_fsdata);
4020 dentry->d_fsdata = NULL; 4020 dentry->d_fsdata = NULL;
4021 d_clear_need_lookup(dentry); 4021 /* This thing is hashed, drop it for now */
4022 d_drop(dentry);
4022 } else { 4023 } else {
4023 ret = btrfs_inode_by_name(dir, dentry, &location); 4024 ret = btrfs_inode_by_name(dir, dentry, &location);
4024 } 4025 }
@@ -4085,7 +4086,15 @@ static void btrfs_dentry_release(struct dentry *dentry)
4085static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, 4086static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4086 struct nameidata *nd) 4087 struct nameidata *nd)
4087{ 4088{
4088 return d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry); 4089 struct dentry *ret;
4090
4091 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4092 if (unlikely(d_need_lookup(dentry))) {
4093 spin_lock(&dentry->d_lock);
4094 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4095 spin_unlock(&dentry->d_lock);
4096 }
4097 return ret;
4089} 4098}
4090 4099
4091unsigned char btrfs_filetype_table[] = { 4100unsigned char btrfs_filetype_table[] = {
@@ -4125,7 +4134,8 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
4125 4134
4126 /* special case for "." */ 4135 /* special case for "." */
4127 if (filp->f_pos == 0) { 4136 if (filp->f_pos == 0) {
4128 over = filldir(dirent, ".", 1, 1, btrfs_ino(inode), DT_DIR); 4137 over = filldir(dirent, ".", 1,
4138 filp->f_pos, btrfs_ino(inode), DT_DIR);
4129 if (over) 4139 if (over)
4130 return 0; 4140 return 0;
4131 filp->f_pos = 1; 4141 filp->f_pos = 1;
@@ -4134,7 +4144,7 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
4134 if (filp->f_pos == 1) { 4144 if (filp->f_pos == 1) {
4135 u64 pino = parent_ino(filp->f_path.dentry); 4145 u64 pino = parent_ino(filp->f_path.dentry);
4136 over = filldir(dirent, "..", 2, 4146 over = filldir(dirent, "..", 2,
4137 2, pino, DT_DIR); 4147 filp->f_pos, pino, DT_DIR);
4138 if (over) 4148 if (over)
4139 return 0; 4149 return 0;
4140 filp->f_pos = 2; 4150 filp->f_pos = 2;
@@ -5823,7 +5833,7 @@ again:
5823 5833
5824 add_pending_csums(trans, inode, ordered->file_offset, &ordered->list); 5834 add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
5825 ret = btrfs_ordered_update_i_size(inode, 0, ordered); 5835 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5826 if (!ret) 5836 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags))
5827 btrfs_update_inode(trans, root, inode); 5837 btrfs_update_inode(trans, root, inode);
5828 ret = 0; 5838 ret = 0;
5829out_unlock: 5839out_unlock:
@@ -7354,11 +7364,15 @@ static int btrfs_set_page_dirty(struct page *page)
7354static int btrfs_permission(struct inode *inode, int mask) 7364static int btrfs_permission(struct inode *inode, int mask)
7355{ 7365{
7356 struct btrfs_root *root = BTRFS_I(inode)->root; 7366 struct btrfs_root *root = BTRFS_I(inode)->root;
7367 umode_t mode = inode->i_mode;
7357 7368
7358 if (btrfs_root_readonly(root) && (mask & MAY_WRITE)) 7369 if (mask & MAY_WRITE &&
7359 return -EROFS; 7370 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7360 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE)) 7371 if (btrfs_root_readonly(root))
7361 return -EACCES; 7372 return -EROFS;
7373 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7374 return -EACCES;
7375 }
7362 return generic_permission(inode, mask); 7376 return generic_permission(inode, mask);
7363} 7377}
7364 7378
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 7cf013349941..538f65a79ec5 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -2177,6 +2177,11 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2177 if (!(src_file->f_mode & FMODE_READ)) 2177 if (!(src_file->f_mode & FMODE_READ))
2178 goto out_fput; 2178 goto out_fput;
2179 2179
2180 /* don't make the dst file partly checksummed */
2181 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2182 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2183 goto out_fput;
2184
2180 ret = -EISDIR; 2185 ret = -EISDIR;
2181 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) 2186 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
2182 goto out_fput; 2187 goto out_fput;
@@ -2220,6 +2225,16 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2220 !IS_ALIGNED(destoff, bs)) 2225 !IS_ALIGNED(destoff, bs))
2221 goto out_unlock; 2226 goto out_unlock;
2222 2227
2228 if (destoff > inode->i_size) {
2229 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2230 if (ret)
2231 goto out_unlock;
2232 }
2233
2234 /* truncate page cache pages from target inode range */
2235 truncate_inode_pages_range(&inode->i_data, destoff,
2236 PAGE_CACHE_ALIGN(destoff + len) - 1);
2237
2223 /* do any pending delalloc/csum calc on src, one way or 2238 /* do any pending delalloc/csum calc on src, one way or
2224 another, and lock file content */ 2239 another, and lock file content */
2225 while (1) { 2240 while (1) {
@@ -2313,7 +2328,12 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2313 else 2328 else
2314 new_key.offset = destoff; 2329 new_key.offset = destoff;
2315 2330
2316 trans = btrfs_start_transaction(root, 1); 2331 /*
2332 * 1 - adjusting old extent (we may have to split it)
2333 * 1 - add new extent
2334 * 1 - inode update
2335 */
2336 trans = btrfs_start_transaction(root, 3);
2317 if (IS_ERR(trans)) { 2337 if (IS_ERR(trans)) {
2318 ret = PTR_ERR(trans); 2338 ret = PTR_ERR(trans);
2319 goto out; 2339 goto out;
@@ -2321,14 +2341,21 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2321 2341
2322 if (type == BTRFS_FILE_EXTENT_REG || 2342 if (type == BTRFS_FILE_EXTENT_REG ||
2323 type == BTRFS_FILE_EXTENT_PREALLOC) { 2343 type == BTRFS_FILE_EXTENT_PREALLOC) {
2344 /*
2345 * a | --- range to clone ---| b
2346 * | ------------- extent ------------- |
2347 */
2348
2349 /* substract range b */
2350 if (key.offset + datal > off + len)
2351 datal = off + len - key.offset;
2352
2353 /* substract range a */
2324 if (off > key.offset) { 2354 if (off > key.offset) {
2325 datao += off - key.offset; 2355 datao += off - key.offset;
2326 datal -= off - key.offset; 2356 datal -= off - key.offset;
2327 } 2357 }
2328 2358
2329 if (key.offset + datal > off + len)
2330 datal = off + len - key.offset;
2331
2332 ret = btrfs_drop_extents(trans, inode, 2359 ret = btrfs_drop_extents(trans, inode,
2333 new_key.offset, 2360 new_key.offset,
2334 new_key.offset + datal, 2361 new_key.offset + datal,
@@ -2425,7 +2452,6 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2425 if (endoff > inode->i_size) 2452 if (endoff > inode->i_size)
2426 btrfs_i_size_write(inode, endoff); 2453 btrfs_i_size_write(inode, endoff);
2427 2454
2428 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
2429 ret = btrfs_update_inode(trans, root, inode); 2455 ret = btrfs_update_inode(trans, root, inode);
2430 BUG_ON(ret); 2456 BUG_ON(ret);
2431 btrfs_end_transaction(trans, root); 2457 btrfs_end_transaction(trans, root);
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index 7dc36fab4afc..e24b7964a155 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -884,6 +884,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
884 struct btrfs_root *tree_root = fs_info->tree_root; 884 struct btrfs_root *tree_root = fs_info->tree_root;
885 struct btrfs_root *root = pending->root; 885 struct btrfs_root *root = pending->root;
886 struct btrfs_root *parent_root; 886 struct btrfs_root *parent_root;
887 struct btrfs_block_rsv *rsv;
887 struct inode *parent_inode; 888 struct inode *parent_inode;
888 struct dentry *parent; 889 struct dentry *parent;
889 struct dentry *dentry; 890 struct dentry *dentry;
@@ -895,6 +896,8 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
895 u64 objectid; 896 u64 objectid;
896 u64 root_flags; 897 u64 root_flags;
897 898
899 rsv = trans->block_rsv;
900
898 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 901 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
899 if (!new_root_item) { 902 if (!new_root_item) {
900 pending->error = -ENOMEM; 903 pending->error = -ENOMEM;
@@ -1002,6 +1005,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1002 btrfs_orphan_post_snapshot(trans, pending); 1005 btrfs_orphan_post_snapshot(trans, pending);
1003fail: 1006fail:
1004 kfree(new_root_item); 1007 kfree(new_root_item);
1008 trans->block_rsv = rsv;
1005 btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1); 1009 btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
1006 return 0; 1010 return 0;
1007} 1011}
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index babee65f8eda..786639fca067 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -799,14 +799,15 @@ static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
799 struct extent_buffer *eb, int slot, 799 struct extent_buffer *eb, int slot,
800 struct btrfs_key *key) 800 struct btrfs_key *key)
801{ 801{
802 struct inode *dir;
803 int ret;
804 struct btrfs_inode_ref *ref; 802 struct btrfs_inode_ref *ref;
803 struct btrfs_dir_item *di;
804 struct inode *dir;
805 struct inode *inode; 805 struct inode *inode;
806 char *name;
807 int namelen;
808 unsigned long ref_ptr; 806 unsigned long ref_ptr;
809 unsigned long ref_end; 807 unsigned long ref_end;
808 char *name;
809 int namelen;
810 int ret;
810 int search_done = 0; 811 int search_done = 0;
811 812
812 /* 813 /*
@@ -909,6 +910,25 @@ again:
909 } 910 }
910 btrfs_release_path(path); 911 btrfs_release_path(path);
911 912
913 /* look for a conflicting sequence number */
914 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
915 btrfs_inode_ref_index(eb, ref),
916 name, namelen, 0);
917 if (di && !IS_ERR(di)) {
918 ret = drop_one_dir_item(trans, root, path, dir, di);
919 BUG_ON(ret);
920 }
921 btrfs_release_path(path);
922
923 /* look for a conflicing name */
924 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
925 name, namelen, 0);
926 if (di && !IS_ERR(di)) {
927 ret = drop_one_dir_item(trans, root, path, dir, di);
928 BUG_ON(ret);
929 }
930 btrfs_release_path(path);
931
912insert: 932insert:
913 /* insert our name */ 933 /* insert our name */
914 ret = btrfs_add_link(trans, dir, inode, name, namelen, 0, 934 ret = btrfs_add_link(trans, dir, inode, name, namelen, 0,
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 53875ae73ad4..f2a4cc79da61 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -142,6 +142,7 @@ static noinline int run_scheduled_bios(struct btrfs_device *device)
142 unsigned long limit; 142 unsigned long limit;
143 unsigned long last_waited = 0; 143 unsigned long last_waited = 0;
144 int force_reg = 0; 144 int force_reg = 0;
145 int sync_pending = 0;
145 struct blk_plug plug; 146 struct blk_plug plug;
146 147
147 /* 148 /*
@@ -229,6 +230,22 @@ loop_lock:
229 230
230 BUG_ON(atomic_read(&cur->bi_cnt) == 0); 231 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
231 232
233 /*
234 * if we're doing the sync list, record that our
235 * plug has some sync requests on it
236 *
237 * If we're doing the regular list and there are
238 * sync requests sitting around, unplug before
239 * we add more
240 */
241 if (pending_bios == &device->pending_sync_bios) {
242 sync_pending = 1;
243 } else if (sync_pending) {
244 blk_finish_plug(&plug);
245 blk_start_plug(&plug);
246 sync_pending = 0;
247 }
248
232 submit_bio(cur->bi_rw, cur); 249 submit_bio(cur->bi_rw, cur);
233 num_run++; 250 num_run++;
234 batch_run++; 251 batch_run++;
@@ -500,6 +517,9 @@ static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
500 fs_devices->rw_devices--; 517 fs_devices->rw_devices--;
501 } 518 }
502 519
520 if (device->can_discard)
521 fs_devices->num_can_discard--;
522
503 new_device = kmalloc(sizeof(*new_device), GFP_NOFS); 523 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
504 BUG_ON(!new_device); 524 BUG_ON(!new_device);
505 memcpy(new_device, device, sizeof(*new_device)); 525 memcpy(new_device, device, sizeof(*new_device));
@@ -508,6 +528,7 @@ static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
508 new_device->bdev = NULL; 528 new_device->bdev = NULL;
509 new_device->writeable = 0; 529 new_device->writeable = 0;
510 new_device->in_fs_metadata = 0; 530 new_device->in_fs_metadata = 0;
531 new_device->can_discard = 0;
511 list_replace_rcu(&device->dev_list, &new_device->dev_list); 532 list_replace_rcu(&device->dev_list, &new_device->dev_list);
512 533
513 call_rcu(&device->rcu, free_device); 534 call_rcu(&device->rcu, free_device);
@@ -547,6 +568,7 @@ int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
547static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, 568static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
548 fmode_t flags, void *holder) 569 fmode_t flags, void *holder)
549{ 570{
571 struct request_queue *q;
550 struct block_device *bdev; 572 struct block_device *bdev;
551 struct list_head *head = &fs_devices->devices; 573 struct list_head *head = &fs_devices->devices;
552 struct btrfs_device *device; 574 struct btrfs_device *device;
@@ -603,6 +625,12 @@ static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
603 seeding = 0; 625 seeding = 0;
604 } 626 }
605 627
628 q = bdev_get_queue(bdev);
629 if (blk_queue_discard(q)) {
630 device->can_discard = 1;
631 fs_devices->num_can_discard++;
632 }
633
606 device->bdev = bdev; 634 device->bdev = bdev;
607 device->in_fs_metadata = 0; 635 device->in_fs_metadata = 0;
608 device->mode = flags; 636 device->mode = flags;
@@ -835,6 +863,7 @@ int find_free_dev_extent(struct btrfs_trans_handle *trans,
835 863
836 max_hole_start = search_start; 864 max_hole_start = search_start;
837 max_hole_size = 0; 865 max_hole_size = 0;
866 hole_size = 0;
838 867
839 if (search_start >= search_end) { 868 if (search_start >= search_end) {
840 ret = -ENOSPC; 869 ret = -ENOSPC;
@@ -917,7 +946,14 @@ next:
917 cond_resched(); 946 cond_resched();
918 } 947 }
919 948
920 hole_size = search_end- search_start; 949 /*
950 * At this point, search_start should be the end of
951 * allocated dev extents, and when shrinking the device,
952 * search_end may be smaller than search_start.
953 */
954 if (search_end > search_start)
955 hole_size = search_end - search_start;
956
921 if (hole_size > max_hole_size) { 957 if (hole_size > max_hole_size) {
922 max_hole_start = search_start; 958 max_hole_start = search_start;
923 max_hole_size = hole_size; 959 max_hole_size = hole_size;
@@ -1543,6 +1579,7 @@ error:
1543 1579
1544int btrfs_init_new_device(struct btrfs_root *root, char *device_path) 1580int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1545{ 1581{
1582 struct request_queue *q;
1546 struct btrfs_trans_handle *trans; 1583 struct btrfs_trans_handle *trans;
1547 struct btrfs_device *device; 1584 struct btrfs_device *device;
1548 struct block_device *bdev; 1585 struct block_device *bdev;
@@ -1612,6 +1649,9 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1612 1649
1613 lock_chunks(root); 1650 lock_chunks(root);
1614 1651
1652 q = bdev_get_queue(bdev);
1653 if (blk_queue_discard(q))
1654 device->can_discard = 1;
1615 device->writeable = 1; 1655 device->writeable = 1;
1616 device->work.func = pending_bios_fn; 1656 device->work.func = pending_bios_fn;
1617 generate_random_uuid(device->uuid); 1657 generate_random_uuid(device->uuid);
@@ -1647,6 +1687,8 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1647 root->fs_info->fs_devices->num_devices++; 1687 root->fs_info->fs_devices->num_devices++;
1648 root->fs_info->fs_devices->open_devices++; 1688 root->fs_info->fs_devices->open_devices++;
1649 root->fs_info->fs_devices->rw_devices++; 1689 root->fs_info->fs_devices->rw_devices++;
1690 if (device->can_discard)
1691 root->fs_info->fs_devices->num_can_discard++;
1650 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; 1692 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1651 1693
1652 if (!blk_queue_nonrot(bdev_get_queue(bdev))) 1694 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
@@ -2413,9 +2455,10 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2413 total_avail = device->total_bytes - device->bytes_used; 2455 total_avail = device->total_bytes - device->bytes_used;
2414 else 2456 else
2415 total_avail = 0; 2457 total_avail = 0;
2416 /* avail is off by max(alloc_start, 1MB), but that is the same 2458
2417 * for all devices, so it doesn't hurt the sorting later on 2459 /* If there is no space on this device, skip it. */
2418 */ 2460 if (total_avail == 0)
2461 continue;
2419 2462
2420 ret = find_free_dev_extent(trans, device, 2463 ret = find_free_dev_extent(trans, device,
2421 max_stripe_size * dev_stripes, 2464 max_stripe_size * dev_stripes,
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 7c12d61ae7ae..6d866db4e177 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -48,6 +48,7 @@ struct btrfs_device {
48 int writeable; 48 int writeable;
49 int in_fs_metadata; 49 int in_fs_metadata;
50 int missing; 50 int missing;
51 int can_discard;
51 52
52 spinlock_t io_lock; 53 spinlock_t io_lock;
53 54
@@ -104,6 +105,7 @@ struct btrfs_fs_devices {
104 u64 rw_devices; 105 u64 rw_devices;
105 u64 missing_devices; 106 u64 missing_devices;
106 u64 total_rw_bytes; 107 u64 total_rw_bytes;
108 u64 num_can_discard;
107 struct block_device *latest_bdev; 109 struct block_device *latest_bdev;
108 110
109 /* all of the devices in the FS, protected by a mutex 111 /* all of the devices in the FS, protected by a mutex
diff --git a/fs/btrfs/xattr.c b/fs/btrfs/xattr.c
index d733b9cfea34..69565e5fc6a0 100644
--- a/fs/btrfs/xattr.c
+++ b/fs/btrfs/xattr.c
@@ -116,6 +116,12 @@ static int do_setxattr(struct btrfs_trans_handle *trans,
116 if (ret) 116 if (ret)
117 goto out; 117 goto out;
118 btrfs_release_path(path); 118 btrfs_release_path(path);
119
120 /*
121 * remove the attribute
122 */
123 if (!value)
124 goto out;
119 } 125 }
120 126
121again: 127again:
@@ -158,6 +164,9 @@ out:
158 return ret; 164 return ret;
159} 165}
160 166
167/*
168 * @value: "" makes the attribute to empty, NULL removes it
169 */
161int __btrfs_setxattr(struct btrfs_trans_handle *trans, 170int __btrfs_setxattr(struct btrfs_trans_handle *trans,
162 struct inode *inode, const char *name, 171 struct inode *inode, const char *name,
163 const void *value, size_t size, int flags) 172 const void *value, size_t size, int flags)