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

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