s3: add sysquotas_4B support
authorBjörn Jacke <bj@sernet.de>
Sun, 1 Jul 2012 12:35:55 +0000 (14:35 +0200)
committerBjoern Jacke <bj@sernet.de>
Sat, 1 Sep 2012 23:00:40 +0000 (01:00 +0200)
this is from James Peach's darwin patch, that exists since a couple of years
already.

Autobuild-User(master): Björn Jacke <bj@sernet.de>
Autobuild-Date(master): Sun Sep  2 01:00:41 CEST 2012 on sn-devel-104

source3/Makefile.in
source3/configure.in
source3/lib/sysquotas.c
source3/lib/sysquotas_4B.c [new file with mode: 0644]
source3/tests/sysquotas.c

index c71d588604866bb529cd824e7031ba84de2cd294..b448c02bfb8fb125e3d3da1bfd405750f69b8161 100644 (file)
@@ -951,7 +951,7 @@ SMBD_OBJ_SRV = smbd/server_reload.o \
               rpc_server/lsasd.o \
               printing/printspoolss.o printing/spoolssd.o \
               lib/sysquotas.o lib/sysquotas_linux.o \
-              lib/sysquotas_xfs.o lib/sysquotas_4A.o \
+              lib/sysquotas_xfs.o lib/sysquotas_4A.o lib/sysquotas_4B.o \
               lib/sysquotas_nfs.o \
               lib/smbd_shim.o \
               lib/background.o \
index db7df4d59c64850237031716b6695f40f35f7347..67ccad6ed134596564e2aae280be2fba387ba611 100644 (file)
@@ -4707,7 +4707,6 @@ AC_TRY_RUN_STRICT([
 #include "${srcdir-.}/../tests/sysquotas.c"],[$CFLAGS $Werror_FLAGS],[$CPPFLAGS],[$LDFLAGS],
            samba_cv_HAVE_QUOTACTL_4B=yes,samba_cv_HAVE_QUOTACTL_4B=no,samba_cv_HAVE_QUOTACTL_4B=cross)])
 if test x"$samba_cv_HAVE_QUOTACTL_4B" = x"yes"; then
-    echo "int quotactl(const char *path, int cmd, int id, char *addr) is not reworked for the new sys_quota api"
     samba_cv_SYSQUOTA_FOUND=yes;
     AC_DEFINE(HAVE_QUOTACTL_4B,1,[Whether int quotactl(const char *path, int cmd, int id, char *addr) is available])
     samba_cv_sysquotas_file="lib/sysquotas_4B.c"
index 08f8444e2f1de4ff5829098ce159bdc38125fe6b..ecd5147d86550b905e9a90e1b3ea3b7aec67b084 100644 (file)
@@ -30,8 +30,6 @@
 /*#endif HAVE_QUOTACTL_4A */
 #elif defined(HAVE_QUOTACTL_4B)
 
-#error HAVE_QUOTACTL_4B not implemeted
-
 /*#endif HAVE_QUOTACTL_4B */
 #elif defined(HAVE_QUOTACTL_3)
 
diff --git a/source3/lib/sysquotas_4B.c b/source3/lib/sysquotas_4B.c
new file mode 100644 (file)
index 0000000..9badd3b
--- /dev/null
@@ -0,0 +1,222 @@
+/*
+ * Unix SMB/CIFS implementation.
+ * System QUOTA function wrappers for QUOTACTL_4B
+
+ * Copyright (C) 2011 James Peach.
+ *
+ * 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will 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 to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include "includes.h"
+
+#undef DBGC_CLASS
+#define DBGC_CLASS DBGC_QUOTA
+
+#ifndef HAVE_SYS_QUOTAS
+#undef HAVE_QUOTACTL_4B
+#endif
+
+#ifdef HAVE_QUOTACTL_4B
+/* int quotactl(const char *path, int cmd, int id, char *addr)
+ *
+ * This is used by many (all?) BSD-derived systems. This implementation has
+ * been developed and tested on Darwin, but may also work on other BSD systems.
+ */
+
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+
+#ifdef HAVE_SYS_QUOTA_H
+#include <sys/quota.h>
+#endif
+
+/* WorkARound broken HFS access checks in hfs_quotactl. */
+#define HFS_QUOTACTL_WAR 1
+
+static void xlate_qblk_to_smb(const struct dqblk * const qblk,
+                       SMB_DISK_QUOTA *dp)
+{
+       ZERO_STRUCTP(dp);
+
+       DEBUG(10, ("unix softlimit=%u hardlimit=%u curblock=%u\n",
+           (unsigned)qblk->dqb_bsoftlimit, (unsigned)qblk->dqb_bhardlimit,
+           (unsigned)qblk->dqb_curbytes));
+
+       DEBUGADD(10, ("unix softinodes=%u hardinodes=%u curinodes=%u\n",
+           (unsigned)qblk->dqb_isoftlimit, (unsigned)qblk->dqb_ihardlimit,
+           (unsigned)qblk->dqb_curinodes));
+
+       /* On Darwin, quotas are counted in bytes. We report them
+        * in 512b blocks because various callers have assumptions
+        * about the block size.
+        */
+#define XLATE_TO_BLOCKS(bytes) (((bytes) + 1) / 512)
+       dp->bsize = 512;
+
+       dp->softlimit = XLATE_TO_BLOCKS(qblk->dqb_bsoftlimit);
+       dp->hardlimit = XLATE_TO_BLOCKS(qblk->dqb_bhardlimit);
+       dp->curblocks = XLATE_TO_BLOCKS(qblk->dqb_curbytes);
+#undef XLATE_TO_BLOCKS
+
+       dp->ihardlimit = qblk->dqb_ihardlimit;
+       dp->isoftlimit = qblk->dqb_isoftlimit;
+       dp->curinodes = qblk->dqb_curinodes;
+
+       dp->qflags = QUOTAS_ENABLED | QUOTAS_DENY_DISK;
+
+       DEBUG(10, ("softlimit=%u hardlimit=%u curblock=%u\n",
+           (unsigned)dp->softlimit, (unsigned)dp->hardlimit,
+           (unsigned)dp->curblocks));
+
+       DEBUGADD(10, ("softinodes=%u hardinodes=%u curinodes=%u\n",
+           (unsigned)dp->isoftlimit, (unsigned)dp->ihardlimit,
+           (unsigned)dp->curinodes));
+
+}
+
+static void xlate_smb_to_qblk(const SMB_DISK_QUOTA * const dp,
+                       struct dqblk *qblk)
+{
+       ZERO_STRUCTP(qblk);
+
+       /* On Darwin, quotas are counted in bytes. */
+       qblk->dqb_bsoftlimit = dp->softlimit * dp->bsize;
+       qblk->dqb_bhardlimit = dp->hardlimit * dp->bsize;
+       qblk->dqb_ihardlimit = dp->ihardlimit;
+       qblk->dqb_isoftlimit = dp->isoftlimit;
+}
+
+static int sys_quotactl_4B(const char * path, int cmd,
+               int id, struct dqblk *qblk)
+{
+       int ret;
+
+       /* NB: We must test GRPQUOTA here, because USRQUOTA is 0. */
+       DEBUG(10, ("%s quota for %s ID %u on %s\n",
+                   (cmd & QCMD(Q_GETQUOTA, 0)) ? "getting" : "setting",
+                   (cmd & QCMD(0, GRPQUOTA)) ? "group" : "user",
+                   (unsigned)id, path));
+
+#ifdef HFS_QUOTACTL_WAR
+       become_root();
+#endif  /* HFS_QUOTACTL_WAR */
+
+       ret = quotactl(path, cmd, id, qblk);
+       if (ret == -1) {
+               /* ENOTSUP means quota support is not compiled in. EINVAL
+                * means that quotas are not configured (commonly).
+                */
+               if (errno != ENOTSUP && errno != EINVAL) {
+                       DEBUG(0, ("failed to %s quota for %s ID %u on %s: %s\n",
+                                   (cmd & QCMD(Q_GETQUOTA, 0)) ? "get" : "set",
+                                   (cmd & QCMD(0, GRPQUOTA)) ? "group" : "user",
+                                   (unsigned)id, path, strerror(errno)));
+               }
+
+#ifdef HFS_QUOTACTL_WAR
+               unbecome_root();
+#endif  /* HFS_QUOTACTL_WAR */
+
+
+               return -1;
+       }
+
+#ifdef HFS_QUOTACTL_WAR
+               unbecome_root();
+#endif  /* HFS_QUOTACTL_WAR */
+
+       return 0;
+}
+
+int sys_get_vfs_quota(const char *path, const char *bdev,
+       enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
+{
+       int ret;
+       struct dqblk qblk;
+
+       ZERO_STRUCT(qblk);
+
+       switch (qtype) {
+       case SMB_USER_QUOTA_TYPE:
+               /* Get quota for provided UID. */
+               ret = sys_quotactl_4B(path, QCMD(Q_GETQUOTA, USRQUOTA),
+                                       id.uid, &qblk);
+               break;
+       case SMB_USER_FS_QUOTA_TYPE:
+               /* Get quota for current UID. */
+               ret = sys_quotactl_4B(path, QCMD(Q_GETQUOTA, USRQUOTA),
+                                       geteuid(), &qblk);
+               break;
+       case SMB_GROUP_QUOTA_TYPE:
+               /* Get quota for provided GID. */
+               ret = sys_quotactl_4B(path, QCMD(Q_GETQUOTA, GRPQUOTA),
+                                       id.gid, &qblk);
+               break;
+       case SMB_GROUP_FS_QUOTA_TYPE:
+               /* Get quota for current GID. */
+               ret = sys_quotactl_4B(path, QCMD(Q_GETQUOTA, GRPQUOTA),
+                                       getegid(), &qblk);
+               break;
+       default:
+               DEBUG(0, ("cannot get unsupported quota type: %u\n",
+                           (unsigned)qtype));
+               errno = ENOSYS;
+               return -1;
+       }
+
+       if (ret == -1) {
+               return -1;
+       }
+
+       xlate_qblk_to_smb(&qblk, dp);
+       dp->qtype = qtype;
+
+       return ret;
+}
+
+int sys_set_vfs_quota(const char *path, const char *bdev,
+       enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
+{
+       struct dqblk qblk;
+
+       xlate_smb_to_qblk(dp, &qblk);
+
+       switch (qtype) {
+       case SMB_USER_QUOTA_TYPE:
+               /* Set quota for provided UID. */
+               return sys_quotactl_4B(path, QCMD(Q_SETQUOTA, USRQUOTA),
+                                       id.uid, &qblk);
+       case SMB_USER_FS_QUOTA_TYPE:
+               /* Set quota for current UID. */
+               return sys_quotactl_4B(path, QCMD(Q_SETQUOTA, USRQUOTA),
+                                       geteuid(), &qblk);
+       case SMB_GROUP_QUOTA_TYPE:
+               /* Set quota for provided GID. */
+               return sys_quotactl_4B(path, QCMD(Q_SETQUOTA, GRPQUOTA),
+                                       id.gid, &qblk);
+       case SMB_GROUP_FS_QUOTA_TYPE:
+               /* Set quota for current GID. */
+               return sys_quotactl_4B(path, QCMD(Q_SETQUOTA, GRPQUOTA),
+                                       getegid(), &qblk);
+       default:
+               DEBUG(0, ("cannot set unsupported quota type: %u\n",
+                           (unsigned)qtype));
+               errno = ENOSYS;
+               return -1;
+       }
+}
+
+#endif /* HAVE_QUOTACTL_4B */
index bf16c8ba6e104ef2bb673288791f8763e11171f0..53d3a67aa1bc73fefde5d81f1324f590428545b0 100644 (file)
 #elif defined(HAVE_QUOTACTL_4B)
 /* int quotactl(const char *path, int cmd, int id, char *addr); */
 
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+
 #ifdef HAVE_SYS_QUOTA_H
 #include <sys/quota.h>
 #else /* *BSD */