Skip to content

Commit 70bc708

Browse files
asjkdave
authored andcommitted
btrfs: refactor btrfs_free_stale_devices() to get return value
Preparatory patch to add ioctl that allows to forget a device (ie. reverse of scan). Refactors btrfs_free_stale_devices() to obtain return status. As this function can fail if it can't find the given path (returns -ENOENT) or trying to delete a mounted device (returns -EBUSY). Signed-off-by: Anand Jain <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent e4319cd commit 70bc708

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

fs/btrfs/volumes.c

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -734,59 +734,75 @@ static void pending_bios_fn(struct btrfs_work *work)
734734
run_scheduled_bios(device);
735735
}
736736

737+
static bool device_path_matched(const char *path, struct btrfs_device *device)
738+
{
739+
int found;
740+
741+
rcu_read_lock();
742+
found = strcmp(rcu_str_deref(device->name), path);
743+
rcu_read_unlock();
744+
745+
return found == 0;
746+
}
747+
737748
/*
738749
* Search and remove all stale (devices which are not mounted) devices.
739750
* When both inputs are NULL, it will search and release all stale devices.
740751
* path: Optional. When provided will it release all unmounted devices
741752
* matching this path only.
742753
* skip_dev: Optional. Will skip this device when searching for the stale
743754
* devices.
755+
* Return: 0 for success or if @path is NULL.
756+
* -EBUSY if @path is a mounted device.
757+
* -ENOENT if @path does not match any device in the list.
744758
*/
745-
static void btrfs_free_stale_devices(const char *path,
759+
static int btrfs_free_stale_devices(const char *path,
746760
struct btrfs_device *skip_device)
747761
{
748762
struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;
749763
struct btrfs_device *device, *tmp_device;
764+
int ret = 0;
765+
766+
if (path)
767+
ret = -ENOENT;
750768

751769
list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
752-
mutex_lock(&fs_devices->device_list_mutex);
753-
if (fs_devices->opened) {
754-
mutex_unlock(&fs_devices->device_list_mutex);
755-
continue;
756-
}
757770

771+
mutex_lock(&fs_devices->device_list_mutex);
758772
list_for_each_entry_safe(device, tmp_device,
759773
&fs_devices->devices, dev_list) {
760-
int not_found = 0;
761-
762774
if (skip_device && skip_device == device)
763775
continue;
764776
if (path && !device->name)
765777
continue;
766-
767-
rcu_read_lock();
768-
if (path)
769-
not_found = strcmp(rcu_str_deref(device->name),
770-
path);
771-
rcu_read_unlock();
772-
if (not_found)
778+
if (path && !device_path_matched(path, device))
773779
continue;
780+
if (fs_devices->opened) {
781+
/* for an already deleted device return 0 */
782+
if (path && ret != 0)
783+
ret = -EBUSY;
784+
break;
785+
}
774786

775787
/* delete the stale device */
776788
fs_devices->num_devices--;
777789
list_del(&device->dev_list);
778790
btrfs_free_device(device);
779791

792+
ret = 0;
780793
if (fs_devices->num_devices == 0)
781794
break;
782795
}
783796
mutex_unlock(&fs_devices->device_list_mutex);
797+
784798
if (fs_devices->num_devices == 0) {
785799
btrfs_sysfs_remove_fsid(fs_devices);
786800
list_del(&fs_devices->fs_list);
787801
free_fs_devices(fs_devices);
788802
}
789803
}
804+
805+
return ret;
790806
}
791807

792808
static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,

0 commit comments

Comments
 (0)