2 * Unix SMB/CIFS implementation.
3 * System QUOTA function wrappers for QUOTACTL_4B
5 * Copyright (C) 2011 James Peach.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #define DBGC_CLASS DBGC_QUOTA
27 #ifndef HAVE_SYS_QUOTAS
28 #undef HAVE_QUOTACTL_4B
31 #ifdef HAVE_QUOTACTL_4B
32 /* int quotactl(const char *path, int cmd, int id, char *addr)
34 * This is used by many (all?) BSD-derived systems. This implementation has
35 * been developed and tested on Darwin, but may also work on other BSD systems.
38 #ifdef HAVE_SYS_TYPES_H
39 #include <sys/types.h>
42 #ifdef HAVE_SYS_QUOTA_H
43 #include <sys/quota.h>
46 /* WorkARound broken HFS access checks in hfs_quotactl. */
47 #define HFS_QUOTACTL_WAR 1
49 static void xlate_qblk_to_smb(const struct dqblk * const qblk,
54 DEBUG(10, ("unix softlimit=%u hardlimit=%u curblock=%u\n",
55 (unsigned)qblk->dqb_bsoftlimit, (unsigned)qblk->dqb_bhardlimit,
56 (unsigned)qblk->dqb_curbytes));
58 DEBUGADD(10, ("unix softinodes=%u hardinodes=%u curinodes=%u\n",
59 (unsigned)qblk->dqb_isoftlimit, (unsigned)qblk->dqb_ihardlimit,
60 (unsigned)qblk->dqb_curinodes));
62 /* On Darwin, quotas are counted in bytes. We report them
63 * in 512b blocks because various callers have assumptions
64 * about the block size.
66 #define XLATE_TO_BLOCKS(bytes) (((bytes) + 1) / 512)
69 dp->softlimit = XLATE_TO_BLOCKS(qblk->dqb_bsoftlimit);
70 dp->hardlimit = XLATE_TO_BLOCKS(qblk->dqb_bhardlimit);
71 dp->curblocks = XLATE_TO_BLOCKS(qblk->dqb_curbytes);
72 #undef XLATE_TO_BLOCKS
74 dp->ihardlimit = qblk->dqb_ihardlimit;
75 dp->isoftlimit = qblk->dqb_isoftlimit;
76 dp->curinodes = qblk->dqb_curinodes;
78 dp->qflags = QUOTAS_ENABLED | QUOTAS_DENY_DISK;
80 DEBUG(10, ("softlimit=%u hardlimit=%u curblock=%u\n",
81 (unsigned)dp->softlimit, (unsigned)dp->hardlimit,
82 (unsigned)dp->curblocks));
84 DEBUGADD(10, ("softinodes=%u hardinodes=%u curinodes=%u\n",
85 (unsigned)dp->isoftlimit, (unsigned)dp->ihardlimit,
86 (unsigned)dp->curinodes));
90 static void xlate_smb_to_qblk(const SMB_DISK_QUOTA * const dp,
95 /* On Darwin, quotas are counted in bytes. */
96 qblk->dqb_bsoftlimit = dp->softlimit * dp->bsize;
97 qblk->dqb_bhardlimit = dp->hardlimit * dp->bsize;
98 qblk->dqb_ihardlimit = dp->ihardlimit;
99 qblk->dqb_isoftlimit = dp->isoftlimit;
102 static int sys_quotactl_4B(const char * path, int cmd,
103 int id, struct dqblk *qblk)
107 /* NB: We must test GRPQUOTA here, because USRQUOTA is 0. */
108 DEBUG(10, ("%s quota for %s ID %u on %s\n",
109 (cmd & QCMD(Q_GETQUOTA, 0)) ? "getting" : "setting",
110 (cmd & QCMD(0, GRPQUOTA)) ? "group" : "user",
111 (unsigned)id, path));
113 #ifdef HFS_QUOTACTL_WAR
115 #endif /* HFS_QUOTACTL_WAR */
117 ret = quotactl(path, cmd, id, qblk);
119 /* ENOTSUP means quota support is not compiled in. EINVAL
120 * means that quotas are not configured (commonly).
122 if (errno != ENOTSUP && errno != EINVAL) {
123 DEBUG(0, ("failed to %s quota for %s ID %u on %s: %s\n",
124 (cmd & QCMD(Q_GETQUOTA, 0)) ? "get" : "set",
125 (cmd & QCMD(0, GRPQUOTA)) ? "group" : "user",
126 (unsigned)id, path, strerror(errno)));
129 #ifdef HFS_QUOTACTL_WAR
131 #endif /* HFS_QUOTACTL_WAR */
137 #ifdef HFS_QUOTACTL_WAR
139 #endif /* HFS_QUOTACTL_WAR */
144 int sys_get_vfs_quota(const char *path, const char *bdev,
145 enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
153 case SMB_USER_QUOTA_TYPE:
154 /* Get quota for provided UID. */
155 ret = sys_quotactl_4B(path, QCMD(Q_GETQUOTA, USRQUOTA),
158 case SMB_USER_FS_QUOTA_TYPE:
159 /* Get quota for current UID. */
160 ret = sys_quotactl_4B(path, QCMD(Q_GETQUOTA, USRQUOTA),
163 case SMB_GROUP_QUOTA_TYPE:
164 /* Get quota for provided GID. */
165 ret = sys_quotactl_4B(path, QCMD(Q_GETQUOTA, GRPQUOTA),
168 case SMB_GROUP_FS_QUOTA_TYPE:
169 /* Get quota for current GID. */
170 ret = sys_quotactl_4B(path, QCMD(Q_GETQUOTA, GRPQUOTA),
174 DEBUG(0, ("cannot get unsupported quota type: %u\n",
184 xlate_qblk_to_smb(&qblk, dp);
190 int sys_set_vfs_quota(const char *path, const char *bdev,
191 enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
195 xlate_smb_to_qblk(dp, &qblk);
198 case SMB_USER_QUOTA_TYPE:
199 /* Set quota for provided UID. */
200 return sys_quotactl_4B(path, QCMD(Q_SETQUOTA, USRQUOTA),
202 case SMB_USER_FS_QUOTA_TYPE:
203 /* Set quota for current UID. */
204 return sys_quotactl_4B(path, QCMD(Q_SETQUOTA, USRQUOTA),
206 case SMB_GROUP_QUOTA_TYPE:
207 /* Set quota for provided GID. */
208 return sys_quotactl_4B(path, QCMD(Q_SETQUOTA, GRPQUOTA),
210 case SMB_GROUP_FS_QUOTA_TYPE:
211 /* Set quota for current GID. */
212 return sys_quotactl_4B(path, QCMD(Q_SETQUOTA, GRPQUOTA),
215 DEBUG(0, ("cannot set unsupported quota type: %u\n",
222 #endif /* HAVE_QUOTACTL_4B */