2 * $Id: quota.c,v 1.6 2001-05-22 19:13:36 rufustfirefly Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
17 #include <sys/param.h>
22 #include <atalk/afp.h>
28 #ifndef NO_QUOTA_SUPPORT
29 #ifdef NEED_QUOTACTL_WRAPPER
30 int quotactl(int cmd, const char *special, int id, caddr_t addr)
32 return syscall(__NR_quotactl, cmd, special, id, addr);
37 #if defined(HAVE_SYS_MNTTAB_H) || defined(__svr4__)
39 * Return the mount point associated with the filesystem
40 * on which "file" resides. Returns NULL on failure.
50 static struct mnttab mnt;
52 if ( stat( file, &sb ) < 0 ) {
57 if (( mtab = fopen( "/etc/mnttab", "r" )) == NULL ) {
61 while ( getmntent( mtab, &mnt ) == 0 ) {
63 if ( (stat( mnt.mnt_special, &sb ) == 0) && (devno == sb.st_rdev)) {
65 return mnt.mnt_mountp;
68 /* check for nfs. we probably should use
69 * strcmp(mnt.mnt_fstype, MNTTYPE_NFS), but that's not as fast. */
70 if ((stat(mnt.mnt_mountp, &sb) == 0) && (devno == sb.st_dev) &&
71 strchr(mnt.mnt_special, ':')) {
74 return mnt.mnt_special;
85 * Return the block-special device name associated with the filesystem
86 * on which "file" resides. Returns NULL on failure.
94 static struct fs_data fsd;
96 if ( getmnt(0, &fsd, 0, STAT_ONE, file ) < 0 ) {
97 syslog(LOG_INFO, "special: getmnt %s: %m", file );
101 /* XXX: does this really detect an nfs mounted fs? */
102 if (strchr(fsd.fd_req.devname, ':'))
104 return( fsd.fd_req.devname );
108 #if defined(HAVE_SYS_MOUNT_H) || defined(BSD4_4) || defined(_IBMR2)
115 static struct statfs sfs;
117 if ( statfs( file, &sfs ) < 0 ) {
121 /* XXX: make sure this really detects an nfs mounted fs */
122 if (strchr(sfs.f_mntfromname, ':'))
124 return( sfs.f_mntfromname );
139 if ( stat( file, &sb ) < 0 ) {
144 if (( mtab = setmntent( "/etc/mtab", "r" )) == NULL ) {
148 while (( mnt = getmntent( mtab )) != NULL ) {
149 /* check for local fs */
150 if ( (stat( mnt->mnt_fsname, &sb ) == 0) && devno == sb.st_rdev) {
152 return( mnt->mnt_fsname );
155 /* check for an nfs mount entry. the alternative is to use
156 * strcmp(mnt->mnt_type, MNTTYPE_NFS) instead of the strchr. */
157 if ((stat(mnt->mnt_dir, &sb) == 0) && (devno == sb.st_dev) &&
158 strchr(mnt->mnt_fsname, ':')) {
161 return( mnt->mnt_fsname );
171 #endif /* __svr4__ */
174 static int getfsquota(vol, uid, dq)
184 qc.addr = (caddr_t)dq;
185 if ( ioctl( vol->v_qfd, Q_QUOTACTL, &qc ) < 0 ) {
186 return( AFPERR_PARAM );
191 if ( quota( Q_GETDLIM, uid, vol->v_gvs, dq ) != 0 ) {
192 return( AFPERR_PARAM );
201 #define QCMD(a,b) (a)
205 /* for group quotas. we only use these if the user belongs
209 memset(&dqg, 0, sizeof(dqg));
213 if ( quotactl( vol->v_gvs, QCMD(Q_GETQUOTA,USRQUOTA),
214 uid, (char *)dq ) != 0 ) {
215 return( AFPERR_PARAM );
219 quotactl(vol->v_gvs, QCMD(Q_GETQUOTA, GRPQUOTA),
220 groups[0], (char *) &dqg);
223 if ( seteuid( getuid() ) == 0 ) {
224 if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA, USRQUOTA),
225 uid, (char *)dq ) != 0 ) {
227 return ( AFPERR_PARAM );
233 if ( quotactl(QCMD(Q_GETQUOTA, USRQUOTA), vol->v_gvs, uid,
234 (caddr_t) dq ) != 0 ) {
235 return( AFPERR_PARAM );
239 quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), vol->v_gvs,
240 groups[0], (char *) &dqg);
244 /* set stuff up for group quotas if necessary */
246 /* max(user blocks, group blocks) */
247 if (dqg.dqb_curblocks && (dq->dqb_curblocks < dqg.dqb_curblocks))
248 dq->dqb_curblocks = dqg.dqb_curblocks;
250 /* min(user limit, group limit) */
251 if (dqg.dqb_bhardlimit && (!dq->dqb_bhardlimit ||
252 (dq->dqb_bhardlimit > dqg.dqb_bhardlimit)))
253 dq->dqb_bhardlimit = dqg.dqb_bhardlimit;
256 if (dqg.dqb_bsoftlimit && (!dq->dqb_bsoftlimit ||
257 (dq->dqb_bsoftlimit > dqg.dqb_bsoftlimit)))
258 dq->dqb_bsoftlimit = dqg.dqb_bsoftlimit;
261 if (dqg.dqb_btimelimit && (!dq->dqb_btimelimit ||
262 (dq->dqb_btimelimit > dqg.dqb_btimelimit)))
263 dq->dqb_btimelimit = dqg.dqb_btimelimit;
268 #endif /* __svr4__ */
274 static int getquota( vol, dq, bsize)
277 const u_int32_t bsize;
282 char buf[ MAXPATHLEN + 1];
284 if ( vol->v_qfd == -1 && vol->v_gvs == NULL) {
285 if (( p = mountp( vol->v_path, &vol->v_nfs)) == NULL ) {
286 syslog( LOG_INFO, "getquota: mountp %s fails", vol->v_path );
287 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 );
298 sprintf( buf, "%s/quotas", p );
299 if (( vol->v_qfd = open( buf, O_RDONLY, 0 )) < 0 ) {
300 syslog( LOG_INFO, "open %s: %m", buf );
301 return( AFPERR_PARAM );
307 if ( vol->v_gvs == NULL ) {
308 if (( p = special( vol->v_path, &vol->v_nfs )) == NULL ) {
309 syslog( LOG_INFO, "getquota: special %s fails", vol->v_path );
310 return( AFPERR_PARAM );
313 if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
314 syslog( LOG_ERR, "getquota: malloc: %m" );
317 strcpy( vol->v_gvs, p );
321 return vol->v_nfs ? getnfsquota(vol, uuid, bsize, dq) :
322 getfsquota(vol, uuid, dq);
325 static int overquota( dqblk )
330 if ( dqblk->dqb_curblocks < dqblk->dqb_bsoftlimit ) {
334 if ( dqblk->dqb_bwarn ) {
338 if ( gettimeofday( &tv, 0 ) < 0 ) {
339 syslog( LOG_ERR, "overquota: gettimeofday: %m" );
340 return( AFPERR_PARAM );
342 if ( !dqblk->dqb_btimelimit || dqblk->dqb_btimelimit > tv.tv_sec ) {
350 * This next bit is basically for linux -- everything is fine
351 * if you use 1k blocks... but if you try (for example) to mount
352 * a volume via nfs from a netapp (which might use 4k blocks) everything
353 * gets reported improperly. I have no idea about dbtob on other
357 #ifdef HAVE_BROKEN_DBTOB
359 #define dbtob(a, b) ((VolSpace)((VolSpace)(a) * (VolSpace)(b)))
360 #define HAVE_2ARG_DBTOB
364 #define dbtob(a) ((a) << 10)
367 /* i do the cast to VolSpace here to make sure that 64-bit shifts
369 #ifdef HAVE_2ARG_DBTOB
370 #define tobytes(a, b) dbtob((VolSpace) (a), (VolSpace) (b))
372 #define tobytes(a, b) dbtob((VolSpace) (a))
374 int uquota_getvolspace( vol, bfree, btotal, bsize)
375 const struct vol *vol;
376 VolSpace *bfree, *btotal;
377 const u_int32_t bsize;
381 if (getquota( vol, &dqblk, bsize) != 0 ) {
382 return( AFPERR_PARAM );
385 /* no limit set for this user. it might be set in the future. */
386 if (dqblk.dqb_bsoftlimit == 0 && dqblk.dqb_bhardlimit == 0) {
387 *btotal = *bfree = ~((VolSpace) 0);
388 } else if ( overquota( &dqblk )) {
389 *btotal = tobytes( dqblk.dqb_bhardlimit, bsize );
390 *bfree = tobytes( dqblk.dqb_bhardlimit, bsize ) -
391 tobytes( dqblk.dqb_curblocks, bsize );
393 *btotal = tobytes( dqblk.dqb_bsoftlimit, bsize );
394 *bfree = tobytes( dqblk.dqb_bsoftlimit, bsize ) -
395 tobytes( dqblk.dqb_curblocks, bsize );