cd814c
From 1be8727ae92a78a5e7fde5ecc6c59740ca2eabe4 Mon Sep 17 00:00:00 2001
cd814c
From: Abhijith Das <adas@redhat.com>
cd814c
Date: Tue, 1 Feb 2011 17:16:12 +0100
cd814c
Subject: [PATCH] Add quotasync tool
cd814c
cd814c
Add tool allowing to sync quotas for some / all filesystems.
cd814c
cd814c
Signed-off-by: Jan Kara <jack@suse.cz>
cd814c
cd814c
Petr Pisar: Remove STRIP from Makefile.in
cd814c
---
cd814c
 Makefile.in |    8 ++-
cd814c
 quotasync.c |  127 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cd814c
 2 files changed, 132 insertions(+), 3 deletions(-)
cd814c
 create mode 100644 quotasync.c
cd814c
cd814c
diff --git a/Makefile.in b/Makefile.in
cd814c
index aeaa79f..0b343a3 100644
cd814c
--- a/Makefile.in
cd814c
+++ b/Makefile.in
cd814c
@@ -1,5 +1,5 @@
cd814c
-PROGS         = quotacheck quotaon quota quot repquota warnquota quotastats xqmstats edquota setquota convertquota rpc.rquotad @QUOTA_NETLINK_PROG@
cd814c
-SOURCES       = bylabel.c common.c convertquota.c edquota.c pot.c quot.c quota.c quotacheck.c quotacheck_v1.c quotacheck_v2.c quotaio.c quotaio_rpc.c quotaio_v1.c quotaio_v2.c quotaio_tree.c quotaio_xfs.c quotaio_meta.c quotaio_generic.c quotaon.c quotaon_xfs.c quotaops.c quotastats.c quotasys.c repquota.c rquota_client.c rquota_server.c rquota_svc.c setquota.c warnquota.c xqmstats.c svc_socket.c
cd814c
+PROGS         = quotacheck quotaon quota quot repquota warnquota quotastats xqmstats edquota setquota convertquota rpc.rquotad quotasync @QUOTA_NETLINK_PROG@
cd814c
+SOURCES       = bylabel.c common.c convertquota.c edquota.c pot.c quot.c quota.c quotacheck.c quotacheck_v1.c quotacheck_v2.c quotaio.c quotaio_rpc.c quotaio_v1.c quotaio_v2.c quotaio_tree.c quotaio_xfs.c quotaio_meta.c quotaio_generic.c quotaon.c quotaon_xfs.c quotaops.c quotastats.c quotasys.c repquota.c rquota_client.c rquota_server.c rquota_svc.c setquota.c warnquota.c xqmstats.c svc_socket.c quotasync.c
cd814c
 CFLAGS        = @CFLAGS@ -D_GNU_SOURCE -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
cd814c
 CPPFLAGS      = @CPPFLAGS@
cd814c
 EXT2LIBS      = @EXT2LIBS@
cd814c
@@ -94,7 +94,7 @@ install: all @INSTMO@
cd814c
 	-mkdir -p $(ROOTDIR)$(mandir)/man8
cd814c
 	-$(INSTALL) -m 755 -d $(ROOTDIR)$(includedir)/rpcsvc
cd814c
 	-$(INSTALL) -m 644 rquota.h rquota.x $(ROOTDIR)$(includedir)/rpcsvc
cd814c
-	-$(INSTALL) -m $(DEF_SBIN_MODE) quota $(ROOTDIR)$(bindir)
cd814c
+	-$(INSTALL) -m $(DEF_SBIN_MODE) quota quotasync $(ROOTDIR)$(bindir)
cd814c
 	-$(INSTALL) -m $(DEF_SBIN_MODE) rpc.rquotad $(ROOTDIR)$(sbindir)
cd814c
 ifneq ($(NETLINKLIBS),)
cd814c
 	-$(INSTALL) -m $(DEF_SBIN_MODE) quota_nld $(ROOTDIR)$(sbindir)
cd814c
@@ -111,6 +111,8 @@ quotacheck: quotacheck.o quotacheck_v1.o quotacheck_v2.o quotaops.o $(LIBOBJS)
cd814c
 
cd814c
 quota: quota.o quotaops.o $(LIBOBJS)
cd814c
 
cd814c
+quotasync: quotasync.o $(LIBOBJS)
cd814c
+
cd814c
 quot: quot.o $(LIBOBJS)
cd814c
 
cd814c
 repquota: repquota.o $(LIBOBJS)
cd814c
diff --git a/quotasync.c b/quotasync.c
cd814c
new file mode 100644
cd814c
index 0000000..0ce44c9
cd814c
--- /dev/null
cd814c
+++ b/quotasync.c
cd814c
@@ -0,0 +1,127 @@
cd814c
+#include <stdio.h>
cd814c
+#include <stdlib.h>
cd814c
+#include <unistd.h>
cd814c
+#include <fcntl.h>
cd814c
+#include <string.h>
cd814c
+#include <errno.h>
cd814c
+#include <getopt.h>
cd814c
+
cd814c
+#include "pot.h"
cd814c
+#include "common.h"
cd814c
+#include "quotasys.h"
cd814c
+#include "quotaio.h"
cd814c
+
cd814c
+#define FL_USER 1		/* sync user quotas */
cd814c
+#define FL_GROUP 2		/* sync group quotas */
cd814c
+#define FL_ALL 4		/* sync quotas on all filesystems */
cd814c
+
cd814c
+int flags, fmt = -1;
cd814c
+char **mnt;
cd814c
+int mntcnt;
cd814c
+char *progname;
cd814c
+
cd814c
+static void usage(void)
cd814c
+{
cd814c
+	errstr(_("Utility for syncing quotas.\nUsage:\n%s [-ug] -a | mntpoint...\n\n"), progname);
cd814c
+	fprintf(stderr, _("Bugs to %s\n"), MY_EMAIL);
cd814c
+	exit(1);
cd814c
+}
cd814c
+
cd814c
+static void parse_options(int argcnt, char **argstr)
cd814c
+{
cd814c
+	int ret;
cd814c
+	struct option long_opts[] = {
cd814c
+		{ "user", 0, NULL, 'u' },
cd814c
+		{ "group", 0, NULL, 'g' },
cd814c
+		{ "all", 0, NULL, 'a' },
cd814c
+		{ "version", 0, NULL, 'V' },
cd814c
+		{ "help", 0, NULL, 'h' },
cd814c
+		{ NULL, 0, NULL, 0 }
cd814c
+	};
cd814c
+
cd814c
+	while ((ret = getopt_long(argcnt, argstr, "ahugV", long_opts, NULL)) != -1) {
cd814c
+		switch (ret) {
cd814c
+			case '?':
cd814c
+			case 'h':
cd814c
+				usage();
cd814c
+			case 'V':
cd814c
+				version();
cd814c
+				exit(0);
cd814c
+			case 'u':
cd814c
+				flags |= FL_USER;
cd814c
+				break;
cd814c
+			case 'g':
cd814c
+				flags |= FL_GROUP;
cd814c
+				break;
cd814c
+			case 'a':
cd814c
+				flags |= FL_ALL;
cd814c
+				break;
cd814c
+		}
cd814c
+	}
cd814c
+
cd814c
+	if ((flags & FL_ALL && optind != argcnt) ||
cd814c
+	    (!(flags & FL_ALL) && optind == argcnt)) {
cd814c
+		fputs(_("Bad number of arguments.\n"), stderr);
cd814c
+		usage();
cd814c
+	}
cd814c
+	if (!(flags & FL_ALL)) {
cd814c
+		mnt = argstr + optind;
cd814c
+		mntcnt = argcnt - optind;
cd814c
+	}
cd814c
+	if (!(flags & (FL_USER | FL_GROUP)))
cd814c
+		flags |= FL_USER;
cd814c
+}
cd814c
+
cd814c
+static int sync_one(int type, char *dev)
cd814c
+{
cd814c
+	int qcmd = QCMD(Q_SYNC, type);
cd814c
+
cd814c
+	return quotactl(qcmd, dev, 0, NULL);
cd814c
+}
cd814c
+
cd814c
+int syncquotas(int type)
cd814c
+{
cd814c
+	struct quota_handle **handles, *h;
cd814c
+	int i, ret = 0;
cd814c
+
cd814c
+	if (flags & FL_ALL) {
cd814c
+		if (sync_one(type, NULL) < 0)
cd814c
+			errstr(_("%s quota sync failed: %s\n"), type2name(type),
cd814c
+					strerror(errno));
cd814c
+		return -1;
cd814c
+	}
cd814c
+
cd814c
+	handles = create_handle_list(mntcnt, mnt, type, fmt,
cd814c
+				     IOI_READONLY, MS_LOCALONLY | MS_NO_AUTOFS);
cd814c
+
cd814c
+	for (i = 0; handles[i]; i++) {
cd814c
+		h = handles[i];
cd814c
+		if (sync_one(type, h->qh_quotadev)) {
cd814c
+			errstr(_("%s quota sync failed for %s: %s\n"),
cd814c
+				type2name(type), h->qh_quotadev, strerror(errno));
cd814c
+			ret = -1;
cd814c
+		}
cd814c
+	}
cd814c
+	dispose_handle_list(handles);
cd814c
+
cd814c
+	return ret;
cd814c
+}
cd814c
+
cd814c
+int main(int argc, char **argv)
cd814c
+{
cd814c
+	int ret = 0;
cd814c
+
cd814c
+	gettexton();
cd814c
+	progname = basename(argv[0]);
cd814c
+
cd814c
+	parse_options(argc, argv);
cd814c
+	init_kernel_interface();
cd814c
+
cd814c
+	if (flags & FL_USER)
cd814c
+		if (syncquotas(USRQUOTA))
cd814c
+			ret = 1;
cd814c
+	if (flags & FL_GROUP)
cd814c
+		if (syncquotas(GRPQUOTA))
cd814c
+			ret = 1;
cd814c
+	return ret;
cd814c
+}
cd814c
-- 
cd814c
1.7.4
cd814c