aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs
diff options
context:
space:
mode:
authorChris Mason <chris.mason@oracle.com>2008-05-13 13:46:40 -0400
committerChris Mason <chris.mason@oracle.com>2008-09-25 11:04:03 -0400
commitdfe25020689bb2d318782d2c9c7141203583fc70 (patch)
treee106059546d7755af0d3b86bf47c113ca8927d7d /fs/btrfs
parent1259ab75c62462b8ffad90067b5e1f6312786a18 (diff)
Btrfs: Add mount -o degraded to allow mounts to continue with missing devices
Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs/btrfs')
-rw-r--r--fs/btrfs/ctree.h3
-rw-r--r--fs/btrfs/disk-io.c49
-rw-r--r--fs/btrfs/disk-io.h3
-rw-r--r--fs/btrfs/super.c25
-rw-r--r--fs/btrfs/volumes.c279
-rw-r--r--fs/btrfs/volumes.h3
6 files changed, 253 insertions, 109 deletions
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 6c2c2c4e4d2a..a9377fb16c37 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -657,6 +657,7 @@ struct btrfs_root {
657#define BTRFS_MOUNT_NODATACOW (1 << 1) 657#define BTRFS_MOUNT_NODATACOW (1 << 1)
658#define BTRFS_MOUNT_NOBARRIER (1 << 2) 658#define BTRFS_MOUNT_NOBARRIER (1 << 2)
659#define BTRFS_MOUNT_SSD (1 << 3) 659#define BTRFS_MOUNT_SSD (1 << 3)
660#define BTRFS_MOUNT_DEGRADED (1 << 4)
660 661
661#define btrfs_clear_opt(o, opt) ((o) &= ~BTRFS_MOUNT_##opt) 662#define btrfs_clear_opt(o, opt) ((o) &= ~BTRFS_MOUNT_##opt)
662#define btrfs_set_opt(o, opt) ((o) |= BTRFS_MOUNT_##opt) 663#define btrfs_set_opt(o, opt) ((o) |= BTRFS_MOUNT_##opt)
@@ -1606,4 +1607,6 @@ int btrfs_delete_xattrs(struct btrfs_trans_handle *trans,
1606 struct btrfs_root *root, struct inode *inode); 1607 struct btrfs_root *root, struct inode *inode);
1607/* super.c */ 1608/* super.c */
1608u64 btrfs_parse_size(char *str); 1609u64 btrfs_parse_size(char *str);
1610int btrfs_parse_options(char *options, struct btrfs_root *root,
1611 char **subvol_name);
1609#endif 1612#endif
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 574b1245964e..38b0d9ecda6a 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -614,21 +614,6 @@ int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
614 return ret; 614 return ret;
615} 615}
616 616
617static int close_all_devices(struct btrfs_fs_info *fs_info)
618{
619 struct list_head *list;
620 struct list_head *next;
621 struct btrfs_device *device;
622
623 list = &fs_info->fs_devices->devices;
624 list_for_each(next, list) {
625 device = list_entry(next, struct btrfs_device, dev_list);
626 close_bdev_excl(device->bdev);
627 device->bdev = NULL;
628 }
629 return 0;
630}
631
632struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root, 617struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
633 u64 bytenr, u32 blocksize) 618 u64 bytenr, u32 blocksize)
634{ 619{
@@ -927,6 +912,8 @@ static int btrfs_congested_fn(void *congested_data, int bdi_bits)
927 912
928 list_for_each(cur, &info->fs_devices->devices) { 913 list_for_each(cur, &info->fs_devices->devices) {
929 device = list_entry(cur, struct btrfs_device, dev_list); 914 device = list_entry(cur, struct btrfs_device, dev_list);
915 if (!device->bdev)
916 continue;
930 bdi = blk_get_backing_dev_info(device->bdev); 917 bdi = blk_get_backing_dev_info(device->bdev);
931 if (bdi && bdi_congested(bdi, bdi_bits)) { 918 if (bdi && bdi_congested(bdi, bdi_bits)) {
932 ret = 1; 919 ret = 1;
@@ -1140,7 +1127,8 @@ static void btrfs_async_submit_work(struct work_struct *work)
1140} 1127}
1141 1128
1142struct btrfs_root *open_ctree(struct super_block *sb, 1129struct btrfs_root *open_ctree(struct super_block *sb,
1143 struct btrfs_fs_devices *fs_devices) 1130 struct btrfs_fs_devices *fs_devices,
1131 char *options)
1144{ 1132{
1145 u32 sectorsize; 1133 u32 sectorsize;
1146 u32 nodesize; 1134 u32 nodesize;
@@ -1276,12 +1264,19 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1276 if (!btrfs_super_root(disk_super)) 1264 if (!btrfs_super_root(disk_super))
1277 goto fail_sb_buffer; 1265 goto fail_sb_buffer;
1278 1266
1279 if (btrfs_super_num_devices(disk_super) != fs_devices->num_devices) { 1267 btrfs_parse_options(options, tree_root, NULL);
1268
1269 if (btrfs_super_num_devices(disk_super) > fs_devices->num_devices) {
1280 printk("Btrfs: wanted %llu devices, but found %llu\n", 1270 printk("Btrfs: wanted %llu devices, but found %llu\n",
1281 (unsigned long long)btrfs_super_num_devices(disk_super), 1271 (unsigned long long)btrfs_super_num_devices(disk_super),
1282 (unsigned long long)fs_devices->num_devices); 1272 (unsigned long long)fs_devices->num_devices);
1283 goto fail_sb_buffer; 1273 if (btrfs_test_opt(tree_root, DEGRADED))
1274 printk("continuing in degraded mode\n");
1275 else {
1276 goto fail_sb_buffer;
1277 }
1284 } 1278 }
1279
1285 fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super); 1280 fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
1286 1281
1287 nodesize = btrfs_super_nodesize(disk_super); 1282 nodesize = btrfs_super_nodesize(disk_super);
@@ -1329,6 +1324,8 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1329 ret = btrfs_read_chunk_tree(chunk_root); 1324 ret = btrfs_read_chunk_tree(chunk_root);
1330 BUG_ON(ret); 1325 BUG_ON(ret);
1331 1326
1327 btrfs_close_extra_devices(fs_devices);
1328
1332 blocksize = btrfs_level_size(tree_root, 1329 blocksize = btrfs_level_size(tree_root,
1333 btrfs_super_root_level(disk_super)); 1330 btrfs_super_root_level(disk_super));
1334 1331
@@ -1374,7 +1371,7 @@ fail_sb_buffer:
1374fail_iput: 1371fail_iput:
1375 iput(fs_info->btree_inode); 1372 iput(fs_info->btree_inode);
1376fail: 1373fail:
1377 close_all_devices(fs_info); 1374 btrfs_close_devices(fs_info->fs_devices);
1378 btrfs_mapping_tree_free(&fs_info->mapping_tree); 1375 btrfs_mapping_tree_free(&fs_info->mapping_tree);
1379 1376
1380 kfree(extent_root); 1377 kfree(extent_root);
@@ -1429,6 +1426,13 @@ int write_all_supers(struct btrfs_root *root)
1429 dev_item = &sb->dev_item; 1426 dev_item = &sb->dev_item;
1430 list_for_each(cur, head) { 1427 list_for_each(cur, head) {
1431 dev = list_entry(cur, struct btrfs_device, dev_list); 1428 dev = list_entry(cur, struct btrfs_device, dev_list);
1429 if (!dev->bdev) {
1430 total_errors++;
1431 continue;
1432 }
1433 if (!dev->in_fs_metadata)
1434 continue;
1435
1432 btrfs_set_stack_device_type(dev_item, dev->type); 1436 btrfs_set_stack_device_type(dev_item, dev->type);
1433 btrfs_set_stack_device_id(dev_item, dev->devid); 1437 btrfs_set_stack_device_id(dev_item, dev->devid);
1434 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes); 1438 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
@@ -1482,6 +1486,11 @@ int write_all_supers(struct btrfs_root *root)
1482 1486
1483 list_for_each(cur, head) { 1487 list_for_each(cur, head) {
1484 dev = list_entry(cur, struct btrfs_device, dev_list); 1488 dev = list_entry(cur, struct btrfs_device, dev_list);
1489 if (!dev->bdev)
1490 continue;
1491 if (!dev->in_fs_metadata)
1492 continue;
1493
1485 BUG_ON(!dev->pending_io); 1494 BUG_ON(!dev->pending_io);
1486 bh = dev->pending_io; 1495 bh = dev->pending_io;
1487 wait_on_buffer(bh); 1496 wait_on_buffer(bh);
@@ -1631,7 +1640,7 @@ int close_ctree(struct btrfs_root *root)
1631 kfree(hasher); 1640 kfree(hasher);
1632 } 1641 }
1633#endif 1642#endif
1634 close_all_devices(fs_info); 1643 btrfs_close_devices(fs_info->fs_devices);
1635 btrfs_mapping_tree_free(&fs_info->mapping_tree); 1644 btrfs_mapping_tree_free(&fs_info->mapping_tree);
1636 1645
1637#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23) 1646#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
diff --git a/fs/btrfs/disk-io.h b/fs/btrfs/disk-io.h
index 30d1ed293c25..2bc64fefe6ea 100644
--- a/fs/btrfs/disk-io.h
+++ b/fs/btrfs/disk-io.h
@@ -33,7 +33,8 @@ struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
33int clean_tree_block(struct btrfs_trans_handle *trans, 33int clean_tree_block(struct btrfs_trans_handle *trans,
34 struct btrfs_root *root, struct extent_buffer *buf); 34 struct btrfs_root *root, struct extent_buffer *buf);
35struct btrfs_root *open_ctree(struct super_block *sb, 35struct btrfs_root *open_ctree(struct super_block *sb,
36 struct btrfs_fs_devices *fs_devices); 36 struct btrfs_fs_devices *fs_devices,
37 char *options);
37int close_ctree(struct btrfs_root *root); 38int close_ctree(struct btrfs_root *root);
38int write_ctree_super(struct btrfs_trans_handle *trans, 39int write_ctree_super(struct btrfs_trans_handle *trans,
39 struct btrfs_root *root); 40 struct btrfs_root *root);
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 020e5a83e31f..273a5b511f50 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -65,11 +65,13 @@ static void btrfs_put_super (struct super_block * sb)
65} 65}
66 66
67enum { 67enum {
68 Opt_subvol, Opt_nodatasum, Opt_nodatacow, Opt_max_extent, 68 Opt_degraded, Opt_subvol, Opt_nodatasum, Opt_nodatacow,
69 Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd, Opt_err, 69 Opt_max_extent, Opt_max_inline, Opt_alloc_start, Opt_nobarrier,
70 Opt_ssd, Opt_err,
70}; 71};
71 72
72static match_table_t tokens = { 73static match_table_t tokens = {
74 {Opt_degraded, "degraded"},
73 {Opt_subvol, "subvol=%s"}, 75 {Opt_subvol, "subvol=%s"},
74 {Opt_nodatasum, "nodatasum"}, 76 {Opt_nodatasum, "nodatasum"},
75 {Opt_nodatacow, "nodatacow"}, 77 {Opt_nodatacow, "nodatacow"},
@@ -106,9 +108,8 @@ u64 btrfs_parse_size(char *str)
106 return res; 108 return res;
107} 109}
108 110
109static int parse_options (char * options, 111int btrfs_parse_options(char *options, struct btrfs_root *root,
110 struct btrfs_root *root, 112 char **subvol_name)
111 char **subvol_name)
112{ 113{
113 char * p; 114 char * p;
114 struct btrfs_fs_info *info = NULL; 115 struct btrfs_fs_info *info = NULL;
@@ -135,6 +136,12 @@ static int parse_options (char * options,
135 136
136 token = match_token(p, tokens, args); 137 token = match_token(p, tokens, args);
137 switch (token) { 138 switch (token) {
139 case Opt_degraded:
140 if (info) {
141 printk("btrfs: allowing degraded mounts\n");
142 btrfs_set_opt(info->mount_opt, DEGRADED);
143 }
144 break;
138 case Opt_subvol: 145 case Opt_subvol:
139 if (subvol_name) { 146 if (subvol_name) {
140 *subvol_name = match_strdup(&args[0]); 147 *subvol_name = match_strdup(&args[0]);
@@ -234,7 +241,7 @@ static int btrfs_fill_super(struct super_block * sb,
234 sb->s_xattr = btrfs_xattr_handlers; 241 sb->s_xattr = btrfs_xattr_handlers;
235 sb->s_time_gran = 1; 242 sb->s_time_gran = 1;
236 243
237 tree_root = open_ctree(sb, fs_devices); 244 tree_root = open_ctree(sb, fs_devices, (char *)data);
238 245
239 if (IS_ERR(tree_root)) { 246 if (IS_ERR(tree_root)) {
240 printk("btrfs: open_ctree failed\n"); 247 printk("btrfs: open_ctree failed\n");
@@ -267,8 +274,6 @@ static int btrfs_fill_super(struct super_block * sb,
267 goto fail_close; 274 goto fail_close;
268 } 275 }
269 276
270 parse_options((char *)data, tree_root, NULL);
271
272 /* this does the super kobj at the same time */ 277 /* this does the super kobj at the same time */
273 err = btrfs_sysfs_add_super(tree_root->fs_info); 278 err = btrfs_sysfs_add_super(tree_root->fs_info);
274 if (err) 279 if (err)
@@ -341,7 +346,7 @@ int btrfs_get_sb_bdev(struct file_system_type *fs_type,
341 if (error) 346 if (error)
342 return error; 347 return error;
343 348
344 bdev = fs_devices->lowest_bdev; 349 bdev = fs_devices->latest_bdev;
345 btrfs_lock_volumes(); 350 btrfs_lock_volumes();
346 s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices); 351 s = sget(fs_type, btrfs_test_super, set_anon_super, fs_devices);
347 btrfs_unlock_volumes(); 352 btrfs_unlock_volumes();
@@ -411,7 +416,7 @@ static int btrfs_get_sb(struct file_system_type *fs_type,
411 int ret; 416 int ret;
412 char *subvol_name = NULL; 417 char *subvol_name = NULL;
413 418
414 parse_options((char *)data, NULL, &subvol_name); 419 btrfs_parse_options((char *)data, NULL, &subvol_name);
415 ret = btrfs_get_sb_bdev(fs_type, flags, dev_name, data, mnt, 420 ret = btrfs_get_sb_bdev(fs_type, flags, dev_name, data, mnt,
416 subvol_name ? subvol_name : "default"); 421 subvol_name ? subvol_name : "default");
417 if (subvol_name) 422 if (subvol_name)
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 5fc7fb481474..43f74d17bcea 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -73,6 +73,7 @@ int btrfs_cleanup_fs_uuids(void)
73 close_bdev_excl(dev->bdev); 73 close_bdev_excl(dev->bdev);
74 } 74 }
75 list_del(&dev->dev_list); 75 list_del(&dev->dev_list);
76 kfree(dev->name);
76 kfree(dev); 77 kfree(dev);
77 } 78 }
78 } 79 }
@@ -127,7 +128,6 @@ static int device_list_add(const char *path,
127 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE); 128 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
128 fs_devices->latest_devid = devid; 129 fs_devices->latest_devid = devid;
129 fs_devices->latest_trans = found_transid; 130 fs_devices->latest_trans = found_transid;
130 fs_devices->lowest_devid = (u64)-1;
131 fs_devices->num_devices = 0; 131 fs_devices->num_devices = 0;
132 device = NULL; 132 device = NULL;
133 } else { 133 } else {
@@ -159,13 +159,35 @@ static int device_list_add(const char *path,
159 fs_devices->latest_devid = devid; 159 fs_devices->latest_devid = devid;
160 fs_devices->latest_trans = found_transid; 160 fs_devices->latest_trans = found_transid;
161 } 161 }
162 if (fs_devices->lowest_devid > devid) {
163 fs_devices->lowest_devid = devid;
164 }
165 *fs_devices_ret = fs_devices; 162 *fs_devices_ret = fs_devices;
166 return 0; 163 return 0;
167} 164}
168 165
166int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
167{
168 struct list_head *head = &fs_devices->devices;
169 struct list_head *cur;
170 struct btrfs_device *device;
171
172 mutex_lock(&uuid_mutex);
173again:
174 list_for_each(cur, head) {
175 device = list_entry(cur, struct btrfs_device, dev_list);
176 if (!device->in_fs_metadata) {
177printk("getting rid of extra dev %s\n", device->name);
178 if (device->bdev)
179 close_bdev_excl(device->bdev);
180 list_del(&device->dev_list);
181 list_del(&device->dev_alloc_list);
182 fs_devices->num_devices--;
183 kfree(device->name);
184 kfree(device);
185 goto again;
186 }
187 }
188 mutex_unlock(&uuid_mutex);
189 return 0;
190}
169int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) 191int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
170{ 192{
171 struct list_head *head = &fs_devices->devices; 193 struct list_head *head = &fs_devices->devices;
@@ -179,6 +201,7 @@ int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
179 close_bdev_excl(device->bdev); 201 close_bdev_excl(device->bdev);
180 } 202 }
181 device->bdev = NULL; 203 device->bdev = NULL;
204 device->in_fs_metadata = 0;
182 } 205 }
183 mutex_unlock(&uuid_mutex); 206 mutex_unlock(&uuid_mutex);
184 return 0; 207 return 0;
@@ -199,6 +222,9 @@ int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
199 if (device->bdev) 222 if (device->bdev)
200 continue; 223 continue;
201 224
225 if (!device->name)
226 continue;
227
202 bdev = open_bdev_excl(device->name, flags, holder); 228 bdev = open_bdev_excl(device->name, flags, holder);
203 229
204 if (IS_ERR(bdev)) { 230 if (IS_ERR(bdev)) {
@@ -209,10 +235,8 @@ int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
209 set_blocksize(bdev, 4096); 235 set_blocksize(bdev, 4096);
210 if (device->devid == fs_devices->latest_devid) 236 if (device->devid == fs_devices->latest_devid)
211 fs_devices->latest_bdev = bdev; 237 fs_devices->latest_bdev = bdev;
212 if (device->devid == fs_devices->lowest_devid) {
213 fs_devices->lowest_bdev = bdev;
214 }
215 device->bdev = bdev; 238 device->bdev = bdev;
239 device->in_fs_metadata = 0;
216 240
217 } 241 }
218 mutex_unlock(&uuid_mutex); 242 mutex_unlock(&uuid_mutex);
@@ -439,7 +463,8 @@ int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
439 } 463 }
440 BUG_ON(ret); 464 BUG_ON(ret);
441 465
442 device->bytes_used -= btrfs_dev_extent_length(leaf, extent); 466 if (device->bytes_used > 0)
467 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
443 ret = btrfs_del_item(trans, root, path); 468 ret = btrfs_del_item(trans, root, path);
444 BUG_ON(ret); 469 BUG_ON(ret);
445 470
@@ -460,6 +485,7 @@ int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
460 struct extent_buffer *leaf; 485 struct extent_buffer *leaf;
461 struct btrfs_key key; 486 struct btrfs_key key;
462 487
488 WARN_ON(!device->in_fs_metadata);
463 path = btrfs_alloc_path(); 489 path = btrfs_alloc_path();
464 if (!path) 490 if (!path)
465 return -ENOMEM; 491 return -ENOMEM;
@@ -674,8 +700,6 @@ static int btrfs_rm_dev_item(struct btrfs_root *root,
674 700
675 next_dev = list_entry(fs_devices->devices.next, struct btrfs_device, 701 next_dev = list_entry(fs_devices->devices.next, struct btrfs_device,
676 dev_list); 702 dev_list);
677 if (bdev == fs_devices->lowest_bdev)
678 fs_devices->lowest_bdev = next_dev->bdev;
679 if (bdev == root->fs_info->sb->s_bdev) 703 if (bdev == root->fs_info->sb->s_bdev)
680 root->fs_info->sb->s_bdev = next_dev->bdev; 704 root->fs_info->sb->s_bdev = next_dev->bdev;
681 if (bdev == fs_devices->latest_bdev) 705 if (bdev == fs_devices->latest_bdev)
@@ -698,7 +722,7 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
698{ 722{
699 struct btrfs_device *device; 723 struct btrfs_device *device;
700 struct block_device *bdev; 724 struct block_device *bdev;
701 struct buffer_head *bh; 725 struct buffer_head *bh = NULL;
702 struct btrfs_super_block *disk_super; 726 struct btrfs_super_block *disk_super;
703 u64 all_avail; 727 u64 all_avail;
704 u64 devid; 728 u64 devid;
@@ -712,47 +736,73 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
712 root->fs_info->avail_metadata_alloc_bits; 736 root->fs_info->avail_metadata_alloc_bits;
713 737
714 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && 738 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
715 root->fs_info->fs_devices->num_devices <= 4) { 739 btrfs_super_num_devices(&root->fs_info->super_copy) <= 4) {
716 printk("btrfs: unable to go below four devices on raid10\n"); 740 printk("btrfs: unable to go below four devices on raid10\n");
717 ret = -EINVAL; 741 ret = -EINVAL;
718 goto out; 742 goto out;
719 } 743 }
720 744
721 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && 745 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
722 root->fs_info->fs_devices->num_devices <= 2) { 746 btrfs_super_num_devices(&root->fs_info->super_copy) <= 2) {
723 printk("btrfs: unable to go below two devices on raid1\n"); 747 printk("btrfs: unable to go below two devices on raid1\n");
724 ret = -EINVAL; 748 ret = -EINVAL;
725 goto out; 749 goto out;
726 } 750 }
727 751
728 bdev = open_bdev_excl(device_path, 0, root->fs_info->bdev_holder); 752 if (strcmp(device_path, "missing") == 0) {
729 if (IS_ERR(bdev)) { 753 struct list_head *cur;
730 ret = PTR_ERR(bdev); 754 struct list_head *devices;
731 goto out; 755 struct btrfs_device *tmp;
732 }
733 756
734 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096); 757 device = NULL;
735 if (!bh) { 758 devices = &root->fs_info->fs_devices->devices;
736 ret = -EIO; 759 list_for_each(cur, devices) {
737 goto error_close; 760 tmp = list_entry(cur, struct btrfs_device, dev_list);
738 } 761 if (tmp->in_fs_metadata && !tmp->bdev) {
739 disk_super = (struct btrfs_super_block *)bh->b_data; 762 device = tmp;
740 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, 763 break;
741 sizeof(disk_super->magic))) { 764 }
742 ret = -ENOENT; 765 }
743 goto error_brelse; 766 bdev = NULL;
744 } 767 bh = NULL;
745 if (memcmp(disk_super->fsid, root->fs_info->fsid, BTRFS_FSID_SIZE)) { 768 disk_super = NULL;
746 ret = -ENOENT; 769 if (!device) {
747 goto error_brelse; 770 printk("btrfs: no missing devices found to remove\n");
748 } 771 goto out;
749 devid = le64_to_cpu(disk_super->dev_item.devid); 772 }
750 device = btrfs_find_device(root, devid, NULL); 773
751 if (!device) { 774 } else {
752 ret = -ENOENT; 775 bdev = open_bdev_excl(device_path, 0,
753 goto error_brelse; 776 root->fs_info->bdev_holder);
754 } 777 if (IS_ERR(bdev)) {
778 ret = PTR_ERR(bdev);
779 goto out;
780 }
781
782 bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
783 if (!bh) {
784 ret = -EIO;
785 goto error_close;
786 }
787 disk_super = (struct btrfs_super_block *)bh->b_data;
788 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
789 sizeof(disk_super->magic))) {
790 ret = -ENOENT;
791 goto error_brelse;
792 }
793 if (memcmp(disk_super->fsid, root->fs_info->fsid,
794 BTRFS_FSID_SIZE)) {
795 ret = -ENOENT;
796 goto error_brelse;
797 }
798 devid = le64_to_cpu(disk_super->dev_item.devid);
799 device = btrfs_find_device(root, devid, NULL);
800 if (!device) {
801 ret = -ENOENT;
802 goto error_brelse;
803 }
755 804
805 }
756 root->fs_info->fs_devices->num_devices--; 806 root->fs_info->fs_devices->num_devices--;
757 807
758 ret = btrfs_shrink_device(device, 0); 808 ret = btrfs_shrink_device(device, 0);
@@ -764,19 +814,25 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
764 if (ret) 814 if (ret)
765 goto error_brelse; 815 goto error_brelse;
766 816
767 /* make sure this device isn't detected as part of the FS anymore */ 817 if (bh) {
768 memset(&disk_super->magic, 0, sizeof(disk_super->magic)); 818 /* make sure this device isn't detected as part of
769 set_buffer_dirty(bh); 819 * the FS anymore
770 sync_dirty_buffer(bh); 820 */
771 821 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
772 brelse(bh); 822 set_buffer_dirty(bh);
773 823 sync_dirty_buffer(bh);
774 /* one close for the device struct or super_block */
775 close_bdev_excl(device->bdev);
776 824
777 /* one close for us */ 825 brelse(bh);
778 close_bdev_excl(device->bdev); 826 }
779 827
828 if (device->bdev) {
829 /* one close for the device struct or super_block */
830 close_bdev_excl(device->bdev);
831 }
832 if (bdev) {
833 /* one close for us */
834 close_bdev_excl(bdev);
835 }
780 kfree(device->name); 836 kfree(device->name);
781 kfree(device); 837 kfree(device);
782 ret = 0; 838 ret = 0;
@@ -785,7 +841,8 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
785error_brelse: 841error_brelse:
786 brelse(bh); 842 brelse(bh);
787error_close: 843error_close:
788 close_bdev_excl(bdev); 844 if (bdev)
845 close_bdev_excl(bdev);
789out: 846out:
790 mutex_unlock(&uuid_mutex); 847 mutex_unlock(&uuid_mutex);
791 mutex_unlock(&root->fs_info->fs_mutex); 848 mutex_unlock(&root->fs_info->fs_mutex);
@@ -839,6 +896,7 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
839 device->total_bytes = i_size_read(bdev->bd_inode); 896 device->total_bytes = i_size_read(bdev->bd_inode);
840 device->dev_root = root->fs_info->dev_root; 897 device->dev_root = root->fs_info->dev_root;
841 device->bdev = bdev; 898 device->bdev = bdev;
899 device->in_fs_metadata = 1;
842 900
843 ret = btrfs_add_device(trans, root, device); 901 ret = btrfs_add_device(trans, root, device);
844 if (ret) 902 if (ret)
@@ -1041,8 +1099,10 @@ int btrfs_relocate_chunk(struct btrfs_root *root,
1041 map->stripes[i].physical); 1099 map->stripes[i].physical);
1042 BUG_ON(ret); 1100 BUG_ON(ret);
1043 1101
1044 ret = btrfs_update_device(trans, map->stripes[i].dev); 1102 if (map->stripes[i].dev) {
1045 BUG_ON(ret); 1103 ret = btrfs_update_device(trans, map->stripes[i].dev);
1104 BUG_ON(ret);
1105 }
1046 } 1106 }
1047 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid, 1107 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1048 chunk_offset); 1108 chunk_offset);
@@ -1415,10 +1475,13 @@ again:
1415 while(index < num_stripes) { 1475 while(index < num_stripes) {
1416 device = list_entry(cur, struct btrfs_device, dev_alloc_list); 1476 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
1417 1477
1418 avail = device->total_bytes - device->bytes_used; 1478 if (device->total_bytes > device->bytes_used)
1479 avail = device->total_bytes - device->bytes_used;
1480 else
1481 avail = 0;
1419 cur = cur->next; 1482 cur = cur->next;
1420 1483
1421 if (avail >= min_free) { 1484 if (device->in_fs_metadata && avail >= min_free) {
1422 u64 ignored_start = 0; 1485 u64 ignored_start = 0;
1423 ret = find_free_dev_extent(trans, device, path, 1486 ret = find_free_dev_extent(trans, device, path,
1424 min_free, 1487 min_free,
@@ -1430,7 +1493,7 @@ again:
1430 if (type & BTRFS_BLOCK_GROUP_DUP) 1493 if (type & BTRFS_BLOCK_GROUP_DUP)
1431 index++; 1494 index++;
1432 } 1495 }
1433 } else if (avail > max_avail) 1496 } else if (device->in_fs_metadata && avail > max_avail)
1434 max_avail = avail; 1497 max_avail = avail;
1435 if (cur == dev_list) 1498 if (cur == dev_list)
1436 break; 1499 break;
@@ -1610,6 +1673,22 @@ int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
1610 return ret; 1673 return ret;
1611} 1674}
1612 1675
1676static int find_live_mirror(struct map_lookup *map, int first, int num,
1677 int optimal)
1678{
1679 int i;
1680 if (map->stripes[optimal].dev->bdev)
1681 return optimal;
1682 for (i = first; i < first + num; i++) {
1683 if (map->stripes[i].dev->bdev)
1684 return i;
1685 }
1686 /* we couldn't find one that doesn't fail. Just return something
1687 * and the io error handling code will clean up eventually
1688 */
1689 return optimal;
1690}
1691
1613static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw, 1692static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1614 u64 logical, u64 *length, 1693 u64 logical, u64 *length,
1615 struct btrfs_multi_bio **multi_ret, 1694 struct btrfs_multi_bio **multi_ret,
@@ -1712,8 +1791,11 @@ again:
1712 num_stripes = map->num_stripes; 1791 num_stripes = map->num_stripes;
1713 else if (mirror_num) 1792 else if (mirror_num)
1714 stripe_index = mirror_num - 1; 1793 stripe_index = mirror_num - 1;
1715 else 1794 else {
1716 stripe_index = current->pid % map->num_stripes; 1795 stripe_index = find_live_mirror(map, 0,
1796 map->num_stripes,
1797 current->pid % map->num_stripes);
1798 }
1717 1799
1718 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { 1800 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1719 if (rw & (1 << BIO_RW)) 1801 if (rw & (1 << BIO_RW))
@@ -1731,8 +1813,11 @@ again:
1731 num_stripes = map->sub_stripes; 1813 num_stripes = map->sub_stripes;
1732 else if (mirror_num) 1814 else if (mirror_num)
1733 stripe_index += mirror_num - 1; 1815 stripe_index += mirror_num - 1;
1734 else 1816 else {
1735 stripe_index += current->pid % map->sub_stripes; 1817 stripe_index = find_live_mirror(map, stripe_index,
1818 map->sub_stripes, stripe_index +
1819 current->pid % map->sub_stripes);
1820 }
1736 } else { 1821 } else {
1737 /* 1822 /*
1738 * after this do_div call, stripe_nr is the number of stripes 1823 * after this do_div call, stripe_nr is the number of stripes
@@ -1749,9 +1834,11 @@ again:
1749 struct backing_dev_info *bdi; 1834 struct backing_dev_info *bdi;
1750 1835
1751 device = map->stripes[stripe_index].dev; 1836 device = map->stripes[stripe_index].dev;
1752 bdi = blk_get_backing_dev_info(device->bdev); 1837 if (device->bdev) {
1753 if (bdi->unplug_io_fn) { 1838 bdi = blk_get_backing_dev_info(device->bdev);
1754 bdi->unplug_io_fn(bdi, unplug_page); 1839 if (bdi->unplug_io_fn) {
1840 bdi->unplug_io_fn(bdi, unplug_page);
1841 }
1755 } 1842 }
1756 } else { 1843 } else {
1757 multi->stripes[i].physical = 1844 multi->stripes[i].physical =
@@ -1880,12 +1967,21 @@ int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
1880 } 1967 }
1881 bio->bi_sector = multi->stripes[dev_nr].physical >> 9; 1968 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
1882 dev = multi->stripes[dev_nr].dev; 1969 dev = multi->stripes[dev_nr].dev;
1883 1970 if (dev && dev->bdev) {
1884 bio->bi_bdev = dev->bdev; 1971 bio->bi_bdev = dev->bdev;
1885 spin_lock(&dev->io_lock); 1972 spin_lock(&dev->io_lock);
1886 dev->total_ios++; 1973 dev->total_ios++;
1887 spin_unlock(&dev->io_lock); 1974 spin_unlock(&dev->io_lock);
1888 submit_bio(rw, bio); 1975 submit_bio(rw, bio);
1976 } else {
1977 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
1978 bio->bi_sector = logical >> 9;
1979#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1980 bio_endio(bio, bio->bi_size, -EIO);
1981#else
1982 bio_endio(bio, -EIO);
1983#endif
1984 }
1889 dev_nr++; 1985 dev_nr++;
1890 } 1986 }
1891 if (total_devs == 1) 1987 if (total_devs == 1)
@@ -1901,6 +1997,27 @@ struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
1901 return __find_device(head, devid, uuid); 1997 return __find_device(head, devid, uuid);
1902} 1998}
1903 1999
2000static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
2001 u64 devid, u8 *dev_uuid)
2002{
2003 struct btrfs_device *device;
2004 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2005
2006 device = kzalloc(sizeof(*device), GFP_NOFS);
2007 list_add(&device->dev_list,
2008 &fs_devices->devices);
2009 list_add(&device->dev_alloc_list,
2010 &fs_devices->alloc_list);
2011 device->barriers = 1;
2012 device->dev_root = root->fs_info->dev_root;
2013 device->devid = devid;
2014 fs_devices->num_devices++;
2015 spin_lock_init(&device->io_lock);
2016 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
2017 return device;
2018}
2019
2020
1904static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, 2021static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1905 struct extent_buffer *leaf, 2022 struct extent_buffer *leaf,
1906 struct btrfs_chunk *chunk) 2023 struct btrfs_chunk *chunk)
@@ -1965,11 +2082,22 @@ static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1965 btrfs_stripe_dev_uuid_nr(chunk, i), 2082 btrfs_stripe_dev_uuid_nr(chunk, i),
1966 BTRFS_UUID_SIZE); 2083 BTRFS_UUID_SIZE);
1967 map->stripes[i].dev = btrfs_find_device(root, devid, uuid); 2084 map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
1968 if (!map->stripes[i].dev) { 2085
2086 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
1969 kfree(map); 2087 kfree(map);
1970 free_extent_map(em); 2088 free_extent_map(em);
1971 return -EIO; 2089 return -EIO;
1972 } 2090 }
2091 if (!map->stripes[i].dev) {
2092 map->stripes[i].dev =
2093 add_missing_dev(root, devid, uuid);
2094 if (!map->stripes[i].dev) {
2095 kfree(map);
2096 free_extent_map(em);
2097 return -EIO;
2098 }
2099 }
2100 map->stripes[i].dev->in_fs_metadata = 1;
1973 } 2101 }
1974 2102
1975 spin_lock(&map_tree->map_tree.lock); 2103 spin_lock(&map_tree->map_tree.lock);
@@ -2016,20 +2144,15 @@ static int read_one_dev(struct btrfs_root *root,
2016 BTRFS_UUID_SIZE); 2144 BTRFS_UUID_SIZE);
2017 device = btrfs_find_device(root, devid, dev_uuid); 2145 device = btrfs_find_device(root, devid, dev_uuid);
2018 if (!device) { 2146 if (!device) {
2019 printk("warning devid %Lu not found already\n", devid); 2147 printk("warning devid %Lu missing\n", devid);
2020 device = kzalloc(sizeof(*device), GFP_NOFS); 2148 device = add_missing_dev(root, devid, dev_uuid);
2021 if (!device) 2149 if (!device)
2022 return -ENOMEM; 2150 return -ENOMEM;
2023 list_add(&device->dev_list,
2024 &root->fs_info->fs_devices->devices);
2025 list_add(&device->dev_alloc_list,
2026 &root->fs_info->fs_devices->alloc_list);
2027 device->barriers = 1;
2028 spin_lock_init(&device->io_lock);
2029 } 2151 }
2030 2152
2031 fill_device_from_item(leaf, dev_item, device); 2153 fill_device_from_item(leaf, dev_item, device);
2032 device->dev_root = root->fs_info->dev_root; 2154 device->dev_root = root->fs_info->dev_root;
2155 device->in_fs_metadata = 1;
2033 ret = 0; 2156 ret = 0;
2034#if 0 2157#if 0
2035 ret = btrfs_open_device(device); 2158 ret = btrfs_open_device(device);
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 0f94a69e6eb6..454fe8103329 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -27,8 +27,10 @@ struct btrfs_device {
27 struct list_head dev_alloc_list; 27 struct list_head dev_alloc_list;
28 struct btrfs_root *dev_root; 28 struct btrfs_root *dev_root;
29 struct buffer_head *pending_io; 29 struct buffer_head *pending_io;
30 u64 generation;
30 31
31 int barriers; 32 int barriers;
33 int in_fs_metadata;
32 34
33 spinlock_t io_lock; 35 spinlock_t io_lock;
34 36
@@ -122,6 +124,7 @@ int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
122int btrfs_scan_one_device(const char *path, int flags, void *holder, 124int btrfs_scan_one_device(const char *path, int flags, void *holder,
123 struct btrfs_fs_devices **fs_devices_ret); 125 struct btrfs_fs_devices **fs_devices_ret);
124int btrfs_close_devices(struct btrfs_fs_devices *fs_devices); 126int btrfs_close_devices(struct btrfs_fs_devices *fs_devices);
127int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices);
125int btrfs_add_device(struct btrfs_trans_handle *trans, 128int btrfs_add_device(struct btrfs_trans_handle *trans,
126 struct btrfs_root *root, 129 struct btrfs_root *root,
127 struct btrfs_device *device); 130 struct btrfs_device *device);