2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
10 #include <sys/param.h>
15 #include <atalk/afp.h>
21 #ifndef NO_QUOTA_SUPPORT
22 #ifdef NEED_QUOTACTL_WRAPPER
23 int quotactl(int cmd, const char *special, int id, caddr_t addr)
25 return syscall(__NR_quotactl, cmd, special, id, addr);
30 #if defined(USE_MNTTAB_H) || defined(__svr4__)
32 * Return the mount point associated with the filesystem
33 * on which "file" resides. Returns NULL on failure.
43 static struct mnttab mnt;
45 if ( stat( file, &sb ) < 0 ) {
50 if (( mtab = fopen( "/etc/mnttab", "r" )) == NULL ) {
54 while ( getmntent( mtab, &mnt ) == 0 ) {
56 if ( (stat( mnt.mnt_special, &sb ) == 0) && (devno == sb.st_rdev)) {
58 return mnt.mnt_mountp;
61 /* check for nfs. we probably should use
62 * strcmp(mnt.mnt_fstype, MNTTYPE_NFS), but that's not as fast. */
63 if ((stat(mnt.mnt_mountp, &sb) == 0) && (devno == sb.st_dev) &&
64 strchr(mnt.mnt_special, ':')) {
67 return mnt.mnt_special;
78 * Return the block-special device name associated with the filesystem
79 * on which "file" resides. Returns NULL on failure.
87 static struct fs_data fsd;
89 if ( getmnt(0, &fsd, 0, STAT_ONE, file ) < 0 ) {
90 syslog(LOG_INFO, "special: getmnt %s: %m", file );
94 /* XXX: does this really detect an nfs mounted fs? */
95 if (strchr(fsd.fd_req.devname, ':'))
97 return( fsd.fd_req.devname );
101 #if defined(USE_MOUNT_H) || defined(BSD4_4) || defined(_IBMR2)
108 static struct statfs sfs;
110 if ( statfs( file, &sfs ) < 0 ) {
114 /* XXX: make sure this really detects an nfs mounted fs */
115 if (strchr(sfs.f_mntfromname, ':'))
117 return( sfs.f_mntfromname );
132 if ( stat( file, &sb ) < 0 ) {
137 if (( mtab = setmntent( "/etc/mtab", "r" )) == NULL ) {
141 while (( mnt = getmntent( mtab )) != NULL ) {
142 /* check for local fs */
143 if ( (stat( mnt->mnt_fsname, &sb ) == 0) && devno == sb.st_rdev) {
145 return( mnt->mnt_fsname );
148 /* check for an nfs mount entry. the alternative is to use
149 * strcmp(mnt->mnt_type, MNTTYPE_NFS) instead of the strchr. */
150 if ((stat(mnt->mnt_dir, &sb) == 0) && (devno == sb.st_dev) &&
151 strchr(mnt->mnt_fsname, ':')) {
154 return( mnt->mnt_fsname );
164 #endif /* __svr4__ */
167 static int getfsquota(vol, uid, dq)
177 qc.addr = (caddr_t)dq;
178 if ( ioctl( vol->v_qfd, Q_QUOTACTL, &qc ) < 0 ) {
179 return( AFPERR_PARAM );
184 if ( quota( Q_GETDLIM, uid, vol->v_gvs, dq ) != 0 ) {
185 return( AFPERR_PARAM );
194 #define QCMD(a,b) (a)
197 /* for group quotas. we only use these if the user belongs
201 memset(&dqg, 0, sizeof(dqg));
204 if ( quotactl( vol->v_gvs, QCMD(Q_GETQUOTA,USRQUOTA),
205 uid, (char *)dq ) != 0 ) {
206 return( AFPERR_PARAM );
210 quotactl(vol->v_gvs, QCMD(Q_GETQUOTA, GRPQUOTA),
211 groups[0], (char *) &dqg);
214 if ( quotactl(QCMD(Q_GETQUOTA, USRQUOTA), vol->v_gvs, uid,
215 (caddr_t) dq ) != 0 ) {
216 return( AFPERR_PARAM );
220 quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), vol->v_gvs,
221 groups[0], (char *) &dqg);
224 /* set stuff up for group quotas if necessary */
226 /* max(user blocks, group blocks) */
227 if (dqg.dqb_curblocks && (dq->dqb_curblocks < dqg.dqb_curblocks))
228 dq->dqb_curblocks = dqg.dqb_curblocks;
230 /* min(user limit, group limit) */
231 if (dqg.dqb_bhardlimit && (!dq->dqb_bhardlimit ||
232 (dq->dqb_bhardlimit > dqg.dqb_bhardlimit)))
233 dq->dqb_bhardlimit = dqg.dqb_bhardlimit;
236 if (dqg.dqb_bsoftlimit && (!dq->dqb_bsoftlimit ||
237 (dq->dqb_bsoftlimit > dqg.dqb_bsoftlimit)))
238 dq->dqb_bsoftlimit = dqg.dqb_bsoftlimit;
241 if (dqg.dqb_btimelimit && (!dq->dqb_btimelimit ||
242 (dq->dqb_btimelimit > dqg.dqb_btimelimit)))
243 dq->dqb_btimelimit = dqg.dqb_btimelimit;
246 #endif /* __svr4__ */
252 static int getquota( vol, dq, bsize)
255 const u_int32_t bsize;
260 char buf[ MAXPATHLEN + 1];
262 if ( vol->v_qfd == -1 && vol->v_gvs == NULL) {
263 if (( p = mountp( vol->v_path, &vol->v_nfs)) == NULL ) {
264 syslog( LOG_INFO, "getquota: mountp %s fails", vol->v_path );
265 return( AFPERR_PARAM );
269 if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
270 syslog( LOG_ERR, "getquota: malloc: %m" );
273 strcpy( vol->v_gvs, p );
276 sprintf( buf, "%s/quotas", p );
277 if (( vol->v_qfd = open( buf, O_RDONLY, 0 )) < 0 ) {
278 syslog( LOG_INFO, "open %s: %m", buf );
279 return( AFPERR_PARAM );
285 if ( vol->v_gvs == NULL ) {
286 if (( p = special( vol->v_path, &vol->v_nfs )) == NULL ) {
287 syslog( LOG_INFO, "getquota: special %s fails", vol->v_path );
288 return( AFPERR_PARAM );
291 if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
292 syslog( LOG_ERR, "getquota: malloc: %m" );
295 strcpy( vol->v_gvs, p );
299 return vol->v_nfs ? getnfsquota(vol, uuid, bsize, dq) :
300 getfsquota(vol, uuid, dq);
303 static int overquota( dqblk )
308 if ( dqblk->dqb_curblocks < dqblk->dqb_bsoftlimit ) {
312 if ( dqblk->dqb_bwarn ) {
316 if ( gettimeofday( &tv, 0 ) < 0 ) {
317 syslog( LOG_ERR, "overquota: gettimeofday: %m" );
318 return( AFPERR_PARAM );
320 if ( !dqblk->dqb_btimelimit || dqblk->dqb_btimelimit > tv.tv_sec ) {
329 #define dbtob(a) ((a) << 10)
332 /* i do the cast to VolSpace here to make sure that 64-bit shifts
334 #ifdef HAVE_2ARG_DBTOB
335 #define tobytes(a, b) dbtob((VolSpace) (a), (VolSpace) (b))
337 #define tobytes(a, b) dbtob((VolSpace) (a))
339 int uquota_getvolspace( vol, bfree, btotal, bsize)
340 const struct vol *vol;
341 VolSpace *bfree, *btotal;
342 const u_int32_t bsize;
346 if (getquota( vol, &dqblk, bsize) != 0 ) {
347 return( AFPERR_PARAM );
350 /* no limit set for this user. it might be set in the future. */
351 if (dqblk.dqb_bsoftlimit == 0 && dqblk.dqb_bhardlimit == 0) {
352 *btotal = *bfree = ~((VolSpace) 0);
353 } else if ( overquota( &dqblk )) {
354 *btotal = tobytes( dqblk.dqb_bhardlimit, bsize );
355 *bfree = tobytes( dqblk.dqb_bhardlimit, bsize ) -
356 tobytes( dqblk.dqb_curblocks, bsize );
358 *btotal = tobytes( dqblk.dqb_bsoftlimit, bsize );
359 *bfree = tobytes( dqblk.dqb_bsoftlimit, bsize ) -
360 tobytes( dqblk.dqb_curblocks, bsize );