Util-Linux Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH 2/3] fstrim: properly de-duplicate fstrim -A
@ 2019-06-10 19:59 Stanislav Brabec
  0 siblings, 0 replies; only message in thread
From: Stanislav Brabec @ 2019-06-10 19:59 UTC (permalink / raw)
  To: util-linux

fstab can contain tag based mounts. De-duplication by source has to be
done after resolving the full source path.

Perform the table iteration twice. First time, prepare for
de-duplication, second time perform the TRIM itself.

Signed-off-by: Stanislav Brabec <sbrabec@suse.cz>
---
 sys-utils/fstrim.c | 43 ++++++++++++++++++++++++++++++-------------
 1 file changed, 30 insertions(+), 13 deletions(-)

diff --git a/sys-utils/fstrim.c b/sys-utils/fstrim.c
index ff029015e..0491e2b54 100644
--- a/sys-utils/fstrim.c
+++ b/sys-utils/fstrim.c
@@ -241,10 +241,6 @@ static int fstrim_all(struct fstrim_control *ctl)
 	mnt_init_debug(0);
 	ul_path_init_debug();
 
-	itr = mnt_new_iter(MNT_ITER_BACKWARD);
-	if (!itr)
-		err(MNT_EX_FAIL, _("failed to initialize libmount iterator"));
-
 	if (ctl->fstab)
 		filename = mnt_get_fstab_path();
 
@@ -255,9 +251,6 @@ static int fstrim_all(struct fstrim_control *ctl)
 	/* de-duplicate by mountpoints */
 	mnt_table_uniq_fs(tab, 0, uniq_fs_target_cmp);
 
-	/* de-duplicate by source */
-	mnt_table_uniq_fs(tab, MNT_UNIQ_FORWARD, uniq_fs_source_cmp);
-
 	if (ctl->fstab) {
 		char *rootdev = NULL;
 
@@ -281,26 +274,50 @@ static int fstrim_all(struct fstrim_control *ctl)
 		}
 	}
 
+	itr = mnt_new_iter(MNT_ITER_BACKWARD);
+	if (!itr)
+		err(MNT_EX_FAIL, _("failed to initialize libmount iterator"));
+
 	while (mnt_table_next_fs(tab, itr, &fs) == 0) {
 		const char *src = mnt_fs_get_srcpath(fs),
 			   *tgt = mnt_fs_get_target(fs);
-		char *path;
-		int rc = 1;
 
-		if (!tgt || mnt_fs_is_pseudofs(fs) || mnt_fs_is_netfs(fs))
+		if (!tgt || mnt_fs_is_pseudofs(fs) || mnt_fs_is_netfs(fs)) {
+			mnt_table_remove_fs(tab, fs);
 			continue;
+		}
 
 		/* convert LABEL= (etc.) from fstab to paths */
 		if (!src && cache) {
 			const char *spec = mnt_fs_get_source(fs);
 
-			if (!spec)
+			if (!spec) {
+				mnt_table_remove_fs(tab, fs);
 				continue;
+			}
 			src = mnt_resolve_spec(spec, cache);
+			mnt_fs_set_source(fs, src);
 		}
 
-		if (!src || *src != '/')
+		if (!src || *src != '/') {
+			mnt_table_remove_fs(tab, fs);
 			continue;
+		}
+	}
+	mnt_free_iter(itr);
+
+	/* de-duplicate by source */
+	mnt_table_uniq_fs(tab, MNT_UNIQ_FORWARD, uniq_fs_source_cmp);
+
+	itr = mnt_new_iter(MNT_ITER_BACKWARD);
+	if (!itr)
+		err(MNT_EX_FAIL, _("failed to initialize libmount iterator"));
+
+	while (mnt_table_next_fs(tab, itr, &fs) == 0) {
+		const char *src = mnt_fs_get_srcpath(fs),
+			   *tgt = mnt_fs_get_target(fs);
+		char *path;
+		int rc = 1;
 
 		/* Is it really accessible mountpoint? Not all mountpoints are
 		 * accessible (maybe over mounted by another filesystem) */
@@ -329,10 +346,10 @@ static int fstrim_all(struct fstrim_control *ctl)
 		else if (rc == 1 && !ctl->quiet)
 			warnx(_("%s: the discard operation is not supported"), tgt);
 	}
+	mnt_free_iter(itr);
 
 	ul_unref_path(wholedisk);
 	mnt_unref_table(tab);
-	mnt_free_iter(itr);
 	mnt_unref_cache(cache);
 
 	if (cnt && cnt == cnt_err)
-- 
2.21.0

-- 
Best Regards / S pozdravem,

Stanislav Brabec
software developer
---------------------------------------------------------------------
SUSE LINUX, s. r. o.                         e-mail: sbrabec@suse.com
Køi¾íkova 148/34 (Corso IIa)                    tel: +420 284 084 060
186 00 Praha 8-Karlín                          fax:  +420 284 084 001
Czech Republic                                    http://www.suse.cz/
PGP: 830B 40D5 9E05 35D8 5E27 6FA3 717C 209F A04F CD76

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, back to index

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-10 19:59 [PATCH 2/3] fstrim: properly de-duplicate fstrim -A Stanislav Brabec

Util-Linux Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/util-linux/0 util-linux/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 util-linux util-linux/ https://lore.kernel.org/util-linux \
		util-linux@vger.kernel.org util-linux@archiver.kernel.org
	public-inbox-index util-linux


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.util-linux


AGPL code for this site: git clone https://public-inbox.org/ public-inbox