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