Blob Blame Raw
From cfa10b0f972005b38ed294bca66cebf2f65298ec Mon Sep 17 00:00:00 2001
From: Eric Sandeen <sandeen@redhat.com>
Date: Thu, 24 May 2018 14:48:33 -0500
Subject: [PATCH] xfs_io: add label command

This adds an online get/set/clear label command to xfs_io.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Reviewed-by: Dave Chinner <dchinner@redhat.com>
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
---
 io/Makefile       |   6 +--
 io/init.c         |   1 +
 io/io.h           |   1 +
 io/label.c        | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 man/man8/xfs_io.8 |  13 +++++++
 5 files changed, 126 insertions(+), 3 deletions(-)
 create mode 100644 io/label.c

Index: xfsprogs-4.5.0/io/Makefile
===================================================================
--- xfsprogs-4.5.0.orig/io/Makefile
+++ xfsprogs-4.5.0/io/Makefile
@@ -9,7 +9,7 @@ LTCOMMAND = xfs_io
 LSRCFILES = xfs_bmap.sh xfs_freeze.sh xfs_mkfile.sh
 HFILES = init.h io.h
 CFILES = init.c \
-	attr.c bmap.c file.c freeze.c fsync.c getrusage.c imap.c link.c \
+	attr.c bmap.c file.c freeze.c fsync.c getrusage.c imap.c label.c link.c \
 	mmap.c open.c parent.c pread.c prealloc.c pwrite.c seek.c shutdown.c \
 	sync.c truncate.c reflink.c
 
Index: xfsprogs-4.5.0/io/init.c
===================================================================
--- xfsprogs-4.5.0.orig/io/init.c
+++ xfsprogs-4.5.0/io/init.c
@@ -65,6 +65,7 @@ init_commands(void)
 	help_init();
 	imap_init();
 	inject_init();
+	label_init();
 	seek_init();
 	madvise_init();
 	mincore_init();
Index: xfsprogs-4.5.0/io/io.h
===================================================================
--- xfsprogs-4.5.0.orig/io/io.h
+++ xfsprogs-4.5.0/io/io.h
@@ -102,6 +102,7 @@ extern void		getrusage_init(void);
 extern void		help_init(void);
 extern void		imap_init(void);
 extern void		inject_init(void);
+extern void		label_init(void);
 extern void		mmap_init(void);
 extern void		open_init(void);
 extern void		parent_init(void);
Index: xfsprogs-4.5.0/io/label.c
===================================================================
--- /dev/null
+++ xfsprogs-4.5.0/io/label.c
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2018 Red Hat, Inc.
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software Foundation,
+ * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include <sys/ioctl.h>
+#include <sys/mount.h>
+#include "platform_defs.h"
+#include "libxfs.h"
+#include "path.h"
+#include "command.h"
+#include "init.h"
+#include "io.h"
+
+#ifndef FS_IOC_GETFSLABEL
+/* Max chars for the interface; fs limits may differ */
+#define FSLABEL_MAX 256
+#define FS_IOC_GETFSLABEL		_IOR(0x94, 49, char[FSLABEL_MAX])
+#define FS_IOC_SETFSLABEL		_IOW(0x94, 50, char[FSLABEL_MAX])
+#endif
+
+static cmdinfo_t label_cmd;
+
+static void
+label_help(void)
+{
+	printf(_(
+"\n"
+" Manipulate or query the filesystem label while mounted.\n"
+"\n"
+" With no arguments, displays the current filesystem label.\n"
+" -s newlabel -- set the filesystem label to newlabel\n"
+" -c          -- clear the filesystem label (sets to NULL string)\n"
+"\n"));
+}
+
+static int
+label_f(
+	int		argc,
+	char		**argv)
+{
+	int		c;
+	int		error;
+	char		label[FSLABEL_MAX];
+
+	if (argc == 1) {
+		memset(label, 0, sizeof(label));
+		error = ioctl(file->fd, FS_IOC_GETFSLABEL, &label);
+		goto out;
+	}
+
+	while ((c = getopt(argc, argv, "cs:")) != EOF) {
+		switch (c) {
+		case 'c':
+			label[0] = '\0';
+			break;
+		case 's':
+			strncpy(label, optarg, sizeof(label));
+			break;
+		default:
+			return command_usage(&label_cmd);
+		}
+	}
+
+	/* Check for trailing arguments */
+	if (argc != optind)
+		return command_usage(&label_cmd);
+
+	error = ioctl(file->fd, FS_IOC_SETFSLABEL, label);
+out:
+	if (error) {
+		perror("label");
+		exitcode = 1;
+	} else {
+		printf("label = \"%s\"\n", label);
+	}
+
+	return 0;
+}
+
+void
+label_init(void)
+{
+	label_cmd.name = "label";
+	label_cmd.cfunc = label_f;
+	label_cmd.argmin = 0;
+	label_cmd.argmax = 3;
+	label_cmd.args = _("[-s label|-c]");
+	label_cmd.flags = CMD_NOMAP_OK | CMD_FOREIGN_OK;
+	label_cmd.oneline =
+		_("query, set, or clear the filesystem label while mounted");
+	label_cmd.help = label_help;
+
+	add_command(&label_cmd);
+}
Index: xfsprogs-4.5.0/man/man8/xfs_io.8
===================================================================
--- xfsprogs-4.5.0.orig/man/man8/xfs_io.8
+++ xfsprogs-4.5.0/man/man8/xfs_io.8
@@ -812,7 +812,19 @@ verbose output will be printed.
 .IP
 .B [NOTE: Not currently operational on Linux.]
 .PD
-
+.TP
+.BI "label" " " "[ -c | -s " label " ] "
+On filesystems that support online label manipulation, get, set, or clear the
+filesystem label.  With no options, print the current filesystem label.  The
+.B \-c
+option clears the filesystem label by setting it to the null string.  The
+.BI "\-s " label
+option sets the filesystem label to
+.IR label .
+If the label is longer than the filesystem will accept,
+.B xfs_io
+will print an error message.  XFS filesystem labels can be at most 12
+characters long.
 .SH SEE ALSO
 .BR mkfs.xfs (8),
 .BR xfsctl (3),