From: Anand Jain <anand.jain@oracle.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH 1/2 v14] btrfs: refactor btrfs_free_stale_devices() to get return value
Date: Fri, 4 Jan 2019 13:31:53 +0800 [thread overview]
Message-ID: <1546579914-14971-3-git-send-email-anand.jain@oracle.com> (raw)
In-Reply-To: <1546579914-14971-1-git-send-email-anand.jain@oracle.com>
Preparatory patch to add forget cli.
Re-factors 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 <anand.jain@oracle.com>
---
v14: born.
A split from the patch 2/2.
Use -ENOENT.
Document return value in the function btrfs_free_stale_devices() header.
Adds a code comment.
Ensures it still returns 0 if it was able to delete a device but followed
by a path matched to a mounted device (a very remote corner case).
if (path && ret != 0)
ret = -EBUSY;
fs/btrfs/volumes.c | 48 +++++++++++++++++++++++++++++++++++-------------
1 file changed, 35 insertions(+), 13 deletions(-)
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 2576b1a379c9..d8ac63e3666d 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -734,6 +734,19 @@ static void pending_bios_fn(struct btrfs_work *work)
run_scheduled_bios(device);
}
+static bool device_path_matched(const char *path, struct btrfs_device *device)
+{
+ int found;
+
+ rcu_read_lock();
+ found = strcmp(rcu_str_deref(device->name), path);
+ rcu_read_unlock();
+
+ if (!found)
+ return true;
+ return false;
+}
+
/*
* Search and remove all stale (devices which are not mounted) devices.
* When both inputs are NULL, it will search and release all stale devices.
@@ -741,45 +754,52 @@ static void pending_bios_fn(struct btrfs_work *work)
* matching this path only.
* skip_dev: Optional. Will skip this device when searching for the stale
* devices.
+ * Return: 0 for success or if path is NULL.
+ * -EBUSY if path is a mounted device.
+ * -ENOENT if path does not match with any device in the list.
*/
-static void btrfs_free_stale_devices(const char *path,
+static int btrfs_free_stale_devices(const char *path,
struct btrfs_device *skip_device)
{
struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;
struct btrfs_device *device, *tmp_device;
+ int ret;
+
+ if (path)
+ ret = -ENOENT;
+ else
+ ret = 0;
list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
+
mutex_lock(&fs_devices->device_list_mutex);
- if (fs_devices->opened) {
- mutex_unlock(&fs_devices->device_list_mutex);
- continue;
- }
list_for_each_entry_safe(device, tmp_device,
&fs_devices->devices, dev_list) {
- int not_found = 0;
if (skip_device && skip_device == device)
continue;
if (path && !device->name)
continue;
-
- rcu_read_lock();
- if (path)
- not_found = strcmp(rcu_str_deref(device->name),
- path);
- rcu_read_unlock();
- if (not_found)
+ if (path && !device_path_matched(path, device))
continue;
+ if (fs_devices->opened) {
+ /* if already deleted a device return 0 */
+ if (path && ret != 0)
+ ret = -EBUSY;
+ break;
+ }
/* delete the stale device */
fs_devices->num_devices--;
list_del(&device->dev_list);
btrfs_free_device(device);
+ ret = 0;
if (fs_devices->num_devices == 0)
break;
}
+
mutex_unlock(&fs_devices->device_list_mutex);
if (fs_devices->num_devices == 0) {
btrfs_sysfs_remove_fsid(fs_devices);
@@ -787,6 +807,8 @@ static void btrfs_free_stale_devices(const char *path,
free_fs_devices(fs_devices);
}
}
+
+ return ret;
}
static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
--
1.8.3.1
next prev parent reply other threads:[~2019-01-04 5:33 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-04 5:31 [PATCH v14] Add cli and ioctl to forget scanned device(s) Anand Jain
2019-01-04 5:31 ` [PATCH v14] btrfs-progs: add cli to forget one or all scanned devices Anand Jain
2019-01-04 5:31 ` Anand Jain [this message]
2019-01-18 17:47 ` [PATCH 1/2 v14] btrfs: refactor btrfs_free_stale_devices() to get return value David Sterba
2019-01-04 5:31 ` [PATCH 2/2 v14] btrfs: introduce feature to forget a btrfs device Anand Jain
2019-01-04 15:13 ` [PATCH v14] Add cli and ioctl to forget scanned device(s) David Sterba
2019-01-07 17:28 ` David Sterba
2019-01-08 5:16 ` Anand Jain
2019-02-06 15:28 ` David Sterba
2019-02-06 15:33 ` David Sterba
2019-02-06 22:13 ` Anand Jain
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1546579914-14971-3-git-send-email-anand.jain@oracle.com \
--to=anand.jain@oracle.com \
--cc=linux-btrfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).