Blame quota-4.01-Recognize-units-at-block-limits-by-setquota.patch

f989b9
From 0ada9c13f9b8299ff607b66c37022ce2a3c4444b Mon Sep 17 00:00:00 2001
f989b9
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
f989b9
Date: Wed, 9 Jan 2013 17:00:44 +0100
f989b9
Subject: [PATCH 1/5] Recognize units at block limits by setquota
f989b9
MIME-Version: 1.0
f989b9
Content-Type: text/plain; charset=UTF-8
f989b9
Content-Transfer-Encoding: 8bit
f989b9
f989b9
This patch allows to specify suffixes at block limits on setquota
f989b9
command line. Binary units K, M, G, T are implemented. Numeric value
f989b9
without suffix is equivatent to kibibytes as before. This is
f989b9
complementary functionality to `quota -s'.
f989b9
f989b9
Signed-off-by: Petr Písař <ppisar@redhat.com>
f989b9
Signed-off-by: Jan Kara <jack@suse.cz>
f989b9
---
f989b9
 quota.h    |  2 ++
f989b9
 quotasys.c | 31 +++++++++++++++++++++++++++++++
f989b9
 quotasys.h |  3 +++
f989b9
 setquota.8 |  7 +++++++
f989b9
 setquota.c | 17 +++++++++++++++--
f989b9
 5 files changed, 58 insertions(+), 2 deletions(-)
f989b9
f989b9
diff --git a/quota.h b/quota.h
f989b9
index ac034d0..6787eab 100644
f989b9
--- a/quota.h
f989b9
+++ b/quota.h
f989b9
@@ -2,9 +2,11 @@
f989b9
 #define GUARD_QUOTA_H
f989b9
 
f989b9
 #include <sys types.h="">
f989b9
+#include <stdint.h>
f989b9
 
f989b9
 typedef u_int32_t qid_t;	/* Type in which we store ids in memory */
f989b9
 typedef int64_t qsize_t;	/* Type in which we store size limitations */
f989b9
+#define QSIZE_MAX INT64_MAX /* Maximum value storable in qsize_t */
f989b9
 
f989b9
 #define MAXQUOTAS 2
f989b9
 #define USRQUOTA  0		/* element used for user quotas */
f989b9
diff --git a/quotasys.c b/quotasys.c
f989b9
index 03f678a..5c1464f 100644
f989b9
--- a/quotasys.c
f989b9
+++ b/quotasys.c
f989b9
@@ -367,6 +367,37 @@ void space2str(qsize_t space, char *buf, int format)
f989b9
 }
f989b9
 
f989b9
 /*
f989b9
+ * Convert block number with unit from string to quota blocks.
f989b9
+ * Return NULL on success, static error message otherwise.
f989b9
+ */
f989b9
+const char *str2space(const char *string, qsize_t *space)
f989b9
+{
f989b9
+	char *unit;
f989b9
+	unsigned long long int number;
f989b9
+	int unit_shift;
f989b9
+       
f989b9
+	number = strtoull(string, &unit, 0);
f989b9
+	if (ULLONG_MAX == number)
f989b9
+		return _("Integer overflow while parsing space number.");
f989b9
+
f989b9
+	if (!unit || unit[0] == '\0' || !strcmp(unit, _("K")))
f989b9
+		unit_shift = 0;
f989b9
+	else if (!strcmp(unit, _("M")))
f989b9
+		unit_shift = 10;
f989b9
+	else if (!strcmp(unit, _("G")))
f989b9
+		unit_shift = 20;
f989b9
+	else if (!strcmp(unit, _("T")))
f989b9
+		unit_shift = 30;
f989b9
+	else
f989b9
+		return _("Unknown space binary unit. "
f989b9
+			"Valid units are K, M, G, T.");
f989b9
+	if (number > (QSIZE_MAX >> unit_shift))
f989b9
+		return _("Integer overflow while interpreting space unit.");
f989b9
+	*space = number << unit_shift;
f989b9
+	return NULL;
f989b9
+}
f989b9
+
f989b9
+/*
f989b9
  *  Convert number to some nice short form for printing
f989b9
  */
f989b9
 void number2str(unsigned long long num, char *buf, int format)
f989b9
diff --git a/quotasys.h b/quotasys.h
f989b9
index 1cebf7e..0cc2c4c 100644
f989b9
--- a/quotasys.h
f989b9
+++ b/quotasys.h
f989b9
@@ -103,6 +103,9 @@ int str2timeunits(time_t, char *, time_t *);
f989b9
 /* Convert number in quota blocks to short printable form */
f989b9
 void space2str(qsize_t, char *, int);
f989b9
 
f989b9
+/* Convert block number with unit from string to quota blocks */
f989b9
+const char *str2space(const char *string, qsize_t *space);
f989b9
+
f989b9
 /* Convert number to short printable form */
f989b9
 void number2str(unsigned long long, char *, int);
f989b9
 
f989b9
diff --git a/setquota.8 b/setquota.8
f989b9
index db9d054..e4511c4 100644
f989b9
--- a/setquota.8
f989b9
+++ b/setquota.8
f989b9
@@ -182,6 +182,13 @@ Go through all filesystems with quota in
f989b9
 .B /etc/mtab
f989b9
 and perform setting.
f989b9
 .PP
f989b9
+.I block-softlimit
f989b9
+and
f989b9
+.I block-hardlimit
f989b9
+are interpreted as multiples of kibibyte (1024 bytes) blocks by default.
f989b9
+Symbols K, M, G, and T can be appended to numeric value to express kibibytes,
f989b9
+mebibytes, gibibytes, and tebibytes.
f989b9
+.PP
f989b9
 To disable a quota, set the corresponding parameter to 0. To change quotas
f989b9
 for several filesystems, invoke once for each filesystem.
f989b9
 .PP
f989b9
diff --git a/setquota.c b/setquota.c
f989b9
index f609dc7..ccac7f7 100644
f989b9
--- a/setquota.c
f989b9
+++ b/setquota.c
f989b9
@@ -93,6 +93,19 @@ static qsize_t parse_unum(char *str, char *msg)
f989b9
 	return ret;
f989b9
 }
f989b9
 
f989b9
+/* Convert block size to number - print errstr message in case of failure */
f989b9
+static qsize_t parse_blocksize(const char *str, const char *msg)
f989b9
+{
f989b9
+	qsize_t ret;
f989b9
+	const char *error = str2space(str, &ret);
f989b9
+
f989b9
+	if (error) {
f989b9
+		errstr(_("%s: %s: %s\n"), msg, str, error);
f989b9
+		usage();
f989b9
+	}
f989b9
+	return ret;
f989b9
+}
f989b9
+
f989b9
 /* Convert our flags to quota type */
f989b9
 static inline int flag2type(int flags)
f989b9
 {
f989b9
@@ -226,8 +239,8 @@ static void parse_options(int argcnt, char **argstr)
f989b9
 	if (!(flags & (FL_GRACE | FL_BATCH))) {
f989b9
 		id = name2id(argstr[optind++], flag2type(flags), !!(flags & FL_NUMNAMES), NULL);
f989b9
 		if (!(flags & (FL_GRACE | FL_INDIVIDUAL_GRACE | FL_PROTO))) {
f989b9
-			toset.dqb_bsoftlimit = parse_unum(argstr[optind++], _("Bad block softlimit"));
f989b9
-			toset.dqb_bhardlimit = parse_unum(argstr[optind++], _("Bad block hardlimit"));
f989b9
+			toset.dqb_bsoftlimit = parse_blocksize(argstr[optind++], _("Bad block softlimit"));
f989b9
+			toset.dqb_bhardlimit = parse_blocksize(argstr[optind++], _("Bad block hardlimit"));
f989b9
 			toset.dqb_isoftlimit = parse_unum(argstr[optind++], _("Bad inode softlimit"));
f989b9
 			toset.dqb_ihardlimit = parse_unum(argstr[optind++], _("Bad inode hardlimit"));
f989b9
 		}
f989b9
-- 
f989b9
1.8.1.4
f989b9