sysquotas_4B: enable for jfs/quota.h on AIX
[samba.git] / source3 / lib / sysquotas_4B.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * System QUOTA function wrappers for QUOTACTL_4B
4
5  * Copyright (C) 2011 James Peach.
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include "includes.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_QUOTA
26
27 #ifndef HAVE_SYS_QUOTAS
28 #undef HAVE_QUOTACTL_4B
29 #endif
30
31 #ifdef HAVE_QUOTACTL_4B
32 /* int quotactl(const char *path, int cmd, int id, char *addr)
33  *
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.
36  */
37
38 #ifdef HAVE_SYS_TYPES_H
39 #include <sys/types.h>
40 #endif
41
42 #ifdef HAVE_SYS_QUOTA_H
43 #include <sys/quota.h>
44 #endif
45
46 #ifdef HAVE_UFS_UFS_QUOTA_H
47 #include <ufs/ufs/quota.h>
48 #endif
49
50 #ifdef HAVE_JFS_QUOTA_H
51 #include <jfs/quota.h>
52 #endif
53
54 #if defined(DARWINOS)
55 /* WorkARound broken HFS access checks in hfs_quotactl. Darwin only(?) */
56 #define HFS_QUOTACTL_WAR 1
57 #endif
58
59 #ifdef HAVE_STRUCT_DQBLK_DQB_CURBYTES
60 /* we handle the byte vs. block count dynamically via QUOTABLOCK_SIZE 1 */
61 #define dqb_curblocks dqb_curbytes
62 #endif
63
64 static void xlate_qblk_to_smb(const struct dqblk * const qblk,
65                         SMB_DISK_QUOTA *dp)
66 {
67         ZERO_STRUCTP(dp);
68
69         DEBUG(10, ("unix softlimit=%u hardlimit=%u curblock=%u\n",
70             (unsigned)qblk->dqb_bsoftlimit, (unsigned)qblk->dqb_bhardlimit,
71             (unsigned)qblk->dqb_curblocks));
72
73         DEBUGADD(10, ("unix softinodes=%u hardinodes=%u curinodes=%u\n",
74             (unsigned)qblk->dqb_isoftlimit, (unsigned)qblk->dqb_ihardlimit,
75             (unsigned)qblk->dqb_curinodes));
76
77         dp->bsize = QUOTABLOCK_SIZE;
78
79         dp->softlimit = qblk->dqb_bsoftlimit;
80         dp->hardlimit = qblk->dqb_bhardlimit;
81         dp->curblocks = qblk->dqb_curblocks;
82 /* our Darwin quotas used to never return 0byte usage but this is probably not needed,
83  * let's comment this out for now
84 #ifdef HAVE_STRUCT_DQBLK_DQB_CURBYTES
85         if (dp->curblocks == 0) {
86                 dp->curblocks = 1;
87         }
88 #endif
89  */
90
91         dp->ihardlimit = qblk->dqb_ihardlimit;
92         dp->isoftlimit = qblk->dqb_isoftlimit;
93         dp->curinodes = qblk->dqb_curinodes;
94
95         dp->qflags = QUOTAS_ENABLED | QUOTAS_DENY_DISK;
96
97         DEBUG(10, ("softlimit=%u hardlimit=%u curblock=%u\n",
98             (unsigned)dp->softlimit, (unsigned)dp->hardlimit,
99             (unsigned)dp->curblocks));
100
101         DEBUGADD(10, ("softinodes=%u hardinodes=%u curinodes=%u\n",
102             (unsigned)dp->isoftlimit, (unsigned)dp->ihardlimit,
103             (unsigned)dp->curinodes));
104
105 }
106
107 static void xlate_smb_to_qblk(const SMB_DISK_QUOTA * const dp,
108                         struct dqblk *qblk)
109 {
110         ZERO_STRUCTP(qblk);
111
112         if (dp->bsize == QUOTABLOCK_SIZE) {
113                 qblk->dqb_bsoftlimit = dp->softlimit;
114                 qblk->dqb_bhardlimit = dp->hardlimit;
115         } else {
116                 qblk->dqb_bsoftlimit = dp->softlimit * dp->bsize / QUOTABLOCK_SIZE;
117                 qblk->dqb_bhardlimit = dp->hardlimit * dp->bsize / QUOTABLOCK_SIZE;
118         }
119         qblk->dqb_ihardlimit = dp->ihardlimit;
120         qblk->dqb_isoftlimit = dp->isoftlimit;
121 }
122
123 static int sys_quotactl_4B(const char * path, int cmd,
124                 int id, struct dqblk *qblk)
125 {
126         int ret;
127
128         /* NB: We must test GRPQUOTA here, because USRQUOTA is 0. */
129         DEBUG(10, ("%s quota for %s ID %u on %s\n",
130                     (cmd & QCMD(Q_GETQUOTA, 0)) ? "getting" : "setting",
131                     (cmd & QCMD(0, GRPQUOTA)) ? "group" : "user",
132                     (unsigned)id, path));
133
134 #ifdef HFS_QUOTACTL_WAR
135         become_root();
136 #endif  /* HFS_QUOTACTL_WAR */
137
138         ret = quotactl(path, cmd, id, qblk);
139         if (ret == -1) {
140                 /* ENOTSUP means quota support is not compiled in. EINVAL
141                  * means that quotas are not configured (commonly).
142                  */
143                 if (errno != ENOTSUP && errno != EINVAL) {
144                         DEBUG(0, ("failed to %s quota for %s ID %u on %s: %s\n",
145                                     (cmd & QCMD(Q_GETQUOTA, 0)) ? "get" : "set",
146                                     (cmd & QCMD(0, GRPQUOTA)) ? "group" : "user",
147                                     (unsigned)id, path, strerror(errno)));
148                 }
149
150 #ifdef HFS_QUOTACTL_WAR
151                 unbecome_root();
152 #endif  /* HFS_QUOTACTL_WAR */
153
154
155                 return -1;
156         }
157
158 #ifdef HFS_QUOTACTL_WAR
159                 unbecome_root();
160 #endif  /* HFS_QUOTACTL_WAR */
161
162         return 0;
163 }
164
165 int sys_get_vfs_quota(const char *path, const char *bdev,
166         enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
167 {
168         int ret;
169         struct dqblk qblk;
170
171         ZERO_STRUCT(qblk);
172
173         switch (qtype) {
174         case SMB_USER_QUOTA_TYPE:
175                 /* Get quota for provided UID. */
176                 ret = sys_quotactl_4B(path, QCMD(Q_GETQUOTA, USRQUOTA),
177                                         id.uid, &qblk);
178                 break;
179         case SMB_USER_FS_QUOTA_TYPE:
180                 /* Get quota for current UID. */
181                 ret = sys_quotactl_4B(path, QCMD(Q_GETQUOTA, USRQUOTA),
182                                         geteuid(), &qblk);
183                 break;
184         case SMB_GROUP_QUOTA_TYPE:
185                 /* Get quota for provided GID. */
186                 ret = sys_quotactl_4B(path, QCMD(Q_GETQUOTA, GRPQUOTA),
187                                         id.gid, &qblk);
188                 break;
189         case SMB_GROUP_FS_QUOTA_TYPE:
190                 /* Get quota for current GID. */
191                 ret = sys_quotactl_4B(path, QCMD(Q_GETQUOTA, GRPQUOTA),
192                                         getegid(), &qblk);
193                 break;
194         default:
195                 DEBUG(0, ("cannot get unsupported quota type: %u\n",
196                             (unsigned)qtype));
197                 errno = ENOSYS;
198                 return -1;
199         }
200
201         if (ret == -1) {
202                 return -1;
203         }
204
205         xlate_qblk_to_smb(&qblk, dp);
206         dp->qtype = qtype;
207
208         return ret;
209 }
210
211 int sys_set_vfs_quota(const char *path, const char *bdev,
212         enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
213 {
214         struct dqblk qblk;
215
216         xlate_smb_to_qblk(dp, &qblk);
217
218         switch (qtype) {
219         case SMB_USER_QUOTA_TYPE:
220                 /* Set quota for provided UID. */
221                 return sys_quotactl_4B(path, QCMD(Q_SETQUOTA, USRQUOTA),
222                                         id.uid, &qblk);
223         case SMB_USER_FS_QUOTA_TYPE:
224                 /* Set quota for current UID. */
225                 return sys_quotactl_4B(path, QCMD(Q_SETQUOTA, USRQUOTA),
226                                         geteuid(), &qblk);
227         case SMB_GROUP_QUOTA_TYPE:
228                 /* Set quota for provided GID. */
229                 return sys_quotactl_4B(path, QCMD(Q_SETQUOTA, GRPQUOTA),
230                                         id.gid, &qblk);
231         case SMB_GROUP_FS_QUOTA_TYPE:
232                 /* Set quota for current GID. */
233                 return sys_quotactl_4B(path, QCMD(Q_SETQUOTA, GRPQUOTA),
234                                         getegid(), &qblk);
235         default:
236                 DEBUG(0, ("cannot set unsupported quota type: %u\n",
237                             (unsigned)qtype));
238                 errno = ENOSYS;
239                 return -1;
240         }
241 }
242
243 #endif /* HAVE_QUOTACTL_4B */