Coverage Report

Created: 2026-02-14 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/samba/source3/lib/sysquotas_xfs.c
Line
Count
Source
1
/*
2
   Unix SMB/CIFS implementation.
3
   System QUOTA function wrappers for XFS
4
   Copyright (C) Stefan (metze) Metzmacher  2003
5
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3 of the License, or
9
   (at your option) any later version.
10
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
*/
19
20
21
#include "includes.h"
22
23
#undef DBGC_CLASS
24
0
#define DBGC_CLASS DBGC_QUOTA
25
26
#if defined(HAVE_SYS_QUOTAS) && defined(HAVE_XFS_QUOTAS)
27
28
#ifdef HAVE_SYS_QUOTA_H
29
#include <sys/quota.h>
30
#endif
31
32
/* this one should actually come from glibc: */
33
/* #include "samba_linux_quota.h" */
34
35
#ifdef HAVE_XFS_XQM_H
36
#include <xfs/xqm.h>
37
#endif
38
39
#define HAVE_GROUP_QUOTA
40
41
/* on IRIX */
42
#ifndef Q_XQUOTAON
43
#define Q_XQUOTAON Q_QUOTAON
44
#endif /* Q_XQUOTAON */
45
#ifndef Q_XQUOTAOFF
46
#define Q_XQUOTAOFF Q_QUOTAOFF
47
#endif /* Q_XQUOTAOFF */
48
#ifndef Q_XGETQSTAT
49
#define Q_XGETQSTAT Q_GETQSTAT
50
#endif /* Q_XGETQSTAT */
51
52
/* currently doesn't support Group and Project quotas on IRIX
53
 */
54
55
#ifndef QCMD
56
#define QCMD(x,y) x
57
#endif
58
59
/*
60
 * IRIX has BBSIZE in <sys/param.h>
61
 */
62
#ifndef BBSHIFT
63
#define BBSHIFT   9
64
#endif /* BBSHIFT */
65
#ifndef BBSIZE
66
#define BBSIZE    (1<<BBSHIFT)
67
#endif /* BBSIZE */
68
69
/****************************************************************************
70
 Abstract out the XFS Quota Manager quota get call.
71
****************************************************************************/
72
int sys_get_xfs_quota(const char *path, const char *bdev, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
73
0
{
74
0
  int ret = -1;
75
0
  uint32_t qflags = 0;
76
0
  uint64_t bsize = (uint64_t)BBSIZE;
77
0
  struct fs_disk_quota D;
78
0
  struct fs_quota_stat F;
79
0
  ZERO_STRUCT(D);
80
0
  ZERO_STRUCT(F);
81
82
0
  if (!bdev||!dp)
83
0
    smb_panic("sys_get_xfs_quota: called with NULL pointer");
84
85
0
  ZERO_STRUCT(*dp);
86
0
  dp->qtype = qtype;
87
88
0
  switch (qtype) {
89
0
    case SMB_USER_QUOTA_TYPE:
90
0
      DEBUG(10,("sys_get_xfs_quota: path[%s] bdev[%s] SMB_USER_QUOTA_TYPE uid[%u]\n",
91
0
        path, bdev, (unsigned)id.uid));
92
93
0
      ret=quotactl(QCMD(Q_XGETQUOTA,USRQUOTA), bdev, id.uid, (caddr_t)&D);
94
      /* XFS fails with ENOENT if the user has no
95
       * quota. Our protocol in that case is to
96
       * succeed and return 0 as quota.
97
       */
98
0
      if (ret != 0 && errno != ENOENT) {
99
0
        return ret;
100
0
      }
101
102
0
      ret = 0;
103
0
      break;
104
0
#ifdef HAVE_GROUP_QUOTA
105
0
    case SMB_GROUP_QUOTA_TYPE:
106
0
      DEBUG(10,("sys_get_xfs_quota: path[%s] bdev[%s] SMB_GROUP_QUOTA_TYPE gid[%u]\n",
107
0
        path, bdev, (unsigned)id.gid));
108
109
0
      ret=quotactl(QCMD(Q_XGETQUOTA,GRPQUOTA), bdev, id.gid, (caddr_t)&D);
110
      /* XFS fails with ENOENT if the user has no
111
       * quota. Our protocol in that case is to
112
       * succeed and return 0 as quota.
113
       */
114
0
      if (ret != 0 && errno != ENOENT) {
115
0
        return ret;
116
0
      }
117
0
      break;
118
0
#endif /* HAVE_GROUP_QUOTA */
119
0
    case SMB_USER_FS_QUOTA_TYPE:
120
0
      DEBUG(10,("sys_get_xfs_quota: path[%s] bdev[%s] SMB_USER_FS_QUOTA_TYPE (uid[%u])\n",
121
0
        path, bdev, (unsigned)id.uid));
122
123
0
      quotactl(QCMD(Q_XGETQSTAT,USRQUOTA), bdev, -1, (caddr_t)&F);
124
125
0
      if (F.qs_flags & XFS_QUOTA_UDQ_ENFD) {
126
0
        qflags |= QUOTAS_DENY_DISK;
127
0
      }
128
0
      else if (F.qs_flags & XFS_QUOTA_UDQ_ACCT) {
129
0
        qflags |= QUOTAS_ENABLED;
130
0
      }
131
132
0
      ret = 0;
133
134
0
      break;
135
0
#ifdef HAVE_GROUP_QUOTA
136
0
    case SMB_GROUP_FS_QUOTA_TYPE:
137
0
      DEBUG(10,("sys_get_xfs_quota: path[%s] bdev[%s] SMB_GROUP_FS_QUOTA_TYPE (gid[%u])\n",
138
0
        path, bdev, (unsigned)id.gid));
139
140
0
      quotactl(QCMD(Q_XGETQSTAT,GRPQUOTA), bdev, -1, (caddr_t)&F);
141
142
0
      if (F.qs_flags & XFS_QUOTA_GDQ_ENFD) {
143
0
        qflags |= QUOTAS_DENY_DISK;
144
0
      }
145
0
      else if (F.qs_flags & XFS_QUOTA_GDQ_ACCT) {
146
0
        qflags |= QUOTAS_ENABLED;
147
0
      }
148
149
0
      ret = 0;
150
151
0
      break;
152
0
#endif /* HAVE_GROUP_QUOTA */
153
0
    default:
154
0
      errno = ENOSYS;
155
0
      return -1;
156
0
  }
157
158
0
  dp->bsize = bsize;
159
0
  dp->softlimit = (uint64_t)D.d_blk_softlimit;
160
0
  dp->hardlimit = (uint64_t)D.d_blk_hardlimit;
161
0
  dp->ihardlimit = (uint64_t)D.d_ino_hardlimit;
162
0
  dp->isoftlimit = (uint64_t)D.d_ino_softlimit;
163
0
  dp->curinodes = (uint64_t)D.d_icount;
164
0
  dp->curblocks = (uint64_t)D.d_bcount;
165
0
  dp->qflags = qflags;
166
167
0
  return ret;
168
0
}
169
170
/****************************************************************************
171
 Abstract out the XFS Quota Manager quota set call.
172
****************************************************************************/
173
int sys_set_xfs_quota(const char *path, const char *bdev, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
174
0
{
175
0
  int ret = -1;
176
0
  uint32_t qflags = 0;
177
0
  uint64_t bsize = (uint64_t)BBSIZE;
178
0
  struct fs_disk_quota D;
179
0
  struct fs_quota_stat F;
180
0
  int q_on = 0;
181
0
  int q_off = 0;
182
0
  ZERO_STRUCT(D);
183
0
  ZERO_STRUCT(F);
184
185
0
  if (!bdev||!dp)
186
0
    smb_panic("sys_set_xfs_quota: called with NULL pointer");
187
188
0
  if (bsize == dp->bsize) {
189
0
    D.d_blk_softlimit = dp->softlimit;
190
0
    D.d_blk_hardlimit = dp->hardlimit;
191
0
  } else {
192
0
    D.d_blk_softlimit = (dp->softlimit*dp->bsize)/bsize;
193
0
    D.d_blk_hardlimit = (dp->hardlimit*dp->bsize)/bsize;
194
0
  }
195
0
  D.d_ino_hardlimit = dp->ihardlimit;
196
0
  D.d_ino_softlimit = dp->isoftlimit;
197
198
0
  qflags = dp->qflags;
199
200
0
  switch (qtype) {
201
0
    case SMB_USER_QUOTA_TYPE:
202
0
      DEBUG(10,("sys_set_xfs_quota: path[%s] bdev[%s] SMB_USER_QUOTA_TYPE uid[%u]\n",
203
0
        path, bdev, (unsigned)id.uid));
204
205
0
      D.d_fieldmask |= FS_DQ_LIMIT_MASK;
206
0
      ret = quotactl(QCMD(Q_XSETQLIM,USRQUOTA), bdev, id.uid, (caddr_t)&D);
207
0
      break;
208
0
#ifdef HAVE_GROUP_QUOTA
209
0
    case SMB_GROUP_QUOTA_TYPE:
210
0
      DEBUG(10,("sys_set_xfs_quota: path[%s] bdev[%s] SMB_GROUP_QUOTA_TYPE gid[%u]\n",
211
0
        path, bdev, (unsigned)id.gid));
212
213
0
      D.d_fieldmask |= FS_DQ_LIMIT_MASK;
214
0
      ret = quotactl(QCMD(Q_XSETQLIM,GRPQUOTA), bdev, id.gid, (caddr_t)&D);
215
0
      break;
216
0
#endif /* HAVE_GROUP_QUOTA */
217
0
    case SMB_USER_FS_QUOTA_TYPE:
218
0
      DEBUG(10,("sys_set_xfs_quota: path[%s] bdev[%s] SMB_USER_FS_QUOTA_TYPE (uid[%u])\n",
219
0
        path, bdev, (unsigned)id.uid));
220
221
0
      quotactl(QCMD(Q_XGETQSTAT,USRQUOTA), bdev, -1, (caddr_t)&F);
222
223
0
      if (qflags & QUOTAS_DENY_DISK) {
224
0
        if (!(F.qs_flags & XFS_QUOTA_UDQ_ENFD))
225
0
          q_on |= XFS_QUOTA_UDQ_ENFD;
226
0
        if (!(F.qs_flags & XFS_QUOTA_UDQ_ACCT))
227
0
          q_on |= XFS_QUOTA_UDQ_ACCT;
228
229
0
        if (q_on != 0) {
230
0
          ret = quotactl(QCMD(Q_XQUOTAON,USRQUOTA),bdev, -1, (caddr_t)&q_on);
231
0
        } else {
232
0
          ret = 0;
233
0
        }
234
235
0
      } else if (qflags & QUOTAS_ENABLED) {
236
0
        if (F.qs_flags & XFS_QUOTA_UDQ_ENFD)
237
0
          q_off |= XFS_QUOTA_UDQ_ENFD;
238
239
0
        if (q_off != 0) {
240
0
          ret = quotactl(QCMD(Q_XQUOTAOFF,USRQUOTA),bdev, -1, (caddr_t)&q_off);
241
0
        } else {
242
0
          ret = 0;
243
0
        }
244
245
0
        if (!(F.qs_flags & XFS_QUOTA_UDQ_ACCT))
246
0
          q_on |= XFS_QUOTA_UDQ_ACCT;
247
248
0
        if (q_on != 0) {
249
0
          ret = quotactl(QCMD(Q_XQUOTAON,USRQUOTA),bdev, -1, (caddr_t)&q_on);
250
0
        } else {
251
0
          ret = 0;
252
0
        }
253
0
      } else {
254
0
        ret = -1;
255
0
      }
256
257
0
      break;
258
0
#ifdef HAVE_GROUP_QUOTA
259
0
    case SMB_GROUP_FS_QUOTA_TYPE:
260
0
      DEBUG(10,("sys_set_xfs_quota: path[%s] bdev[%s] SMB_GROUP_FS_QUOTA_TYPE (gid[%u])\n",
261
0
        path, bdev, (unsigned)id.gid));
262
263
0
      quotactl(QCMD(Q_XGETQSTAT,GRPQUOTA), bdev, -1, (caddr_t)&F);
264
265
0
      if (qflags & QUOTAS_DENY_DISK) {
266
0
        if (!(F.qs_flags & XFS_QUOTA_GDQ_ENFD))
267
0
          q_on |= XFS_QUOTA_GDQ_ENFD;
268
0
        if (!(F.qs_flags & XFS_QUOTA_GDQ_ACCT))
269
0
          q_on |= XFS_QUOTA_GDQ_ACCT;
270
271
0
        if (q_on != 0) {
272
0
          ret = quotactl(QCMD(Q_XQUOTAON,GRPQUOTA),bdev, -1, (caddr_t)&q_on);
273
0
        } else {
274
0
          ret = 0;
275
0
        }
276
277
0
      } else if (qflags & QUOTAS_ENABLED) {
278
0
        if (F.qs_flags & XFS_QUOTA_GDQ_ENFD)
279
0
          q_off |= XFS_QUOTA_GDQ_ENFD;
280
281
0
        if (q_off != 0) {
282
0
          ret = quotactl(QCMD(Q_XQUOTAOFF,GRPQUOTA),bdev, -1, (caddr_t)&q_off);
283
0
        } else {
284
0
          ret = 0;
285
0
        }
286
287
0
        if (!(F.qs_flags & XFS_QUOTA_GDQ_ACCT))
288
0
          q_on |= XFS_QUOTA_GDQ_ACCT;
289
290
0
        if (q_on != 0) {
291
0
          ret = quotactl(QCMD(Q_XQUOTAON,GRPQUOTA),bdev, -1, (caddr_t)&q_on);
292
0
        } else {
293
0
          ret = 0;
294
0
        }
295
0
      } else {
296
0
        ret = -1;
297
0
      }
298
299
0
      break;
300
0
#endif /* HAVE_GROUP_QUOTA */
301
0
    default:
302
0
      errno = ENOSYS;
303
0
      return -1;
304
0
  }
305
306
0
  return ret;
307
0
}
308
309
#else /* HAVE_XFS_QUOTAS */
310
 void dummy_sysquotas_xfs(void);
311
312
 void dummy_sysquotas_xfs(void){}
313
#endif /* HAVE_XFS_QUOTAS */