2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
15 #include <sys/param.h>
20 #include <atalk/afp.h>
26 #ifndef NO_QUOTA_SUPPORT
27 #ifdef NEED_QUOTACTL_WRAPPER
28 int quotactl(int cmd, const char *special, int id, caddr_t addr)
30 return syscall(__NR_quotactl, cmd, special, id, addr);
35 #if defined(USE_MNTTAB_H) || defined(__svr4__)
37 * Return the mount point associated with the filesystem
38 * on which "file" resides. Returns NULL on failure.
48 static struct mnttab mnt;
50 if ( stat( file, &sb ) < 0 ) {
55 if (( mtab = fopen( "/etc/mnttab", "r" )) == NULL ) {
59 while ( getmntent( mtab, &mnt ) == 0 ) {
61 if ( (stat( mnt.mnt_special, &sb ) == 0) && (devno == sb.st_rdev)) {
63 return mnt.mnt_mountp;
66 /* check for nfs. we probably should use
67 * strcmp(mnt.mnt_fstype, MNTTYPE_NFS), but that's not as fast. */
68 if ((stat(mnt.mnt_mountp, &sb) == 0) && (devno == sb.st_dev) &&
69 strchr(mnt.mnt_special, ':')) {
72 return mnt.mnt_special;
83 * Return the block-special device name associated with the filesystem
84 * on which "file" resides. Returns NULL on failure.
92 static struct fs_data fsd;
94 if ( getmnt(0, &fsd, 0, STAT_ONE, file ) < 0 ) {
95 syslog(LOG_INFO, "special: getmnt %s: %m", file );
99 /* XXX: does this really detect an nfs mounted fs? */
100 if (strchr(fsd.fd_req.devname, ':'))
102 return( fsd.fd_req.devname );
106 #if defined(USE_MOUNT_H) || defined(BSD4_4) || defined(_IBMR2)
113 static struct statfs sfs;
115 if ( statfs( file, &sfs ) < 0 ) {
119 /* XXX: make sure this really detects an nfs mounted fs */
120 if (strchr(sfs.f_mntfromname, ':'))
122 return( sfs.f_mntfromname );
137 if ( stat( file, &sb ) < 0 ) {
142 if (( mtab = setmntent( "/etc/mtab", "r" )) == NULL ) {
146 while (( mnt = getmntent( mtab )) != NULL ) {
147 /* check for local fs */
148 if ( (stat( mnt->mnt_fsname, &sb ) == 0) && devno == sb.st_rdev) {
150 return( mnt->mnt_fsname );
153 /* check for an nfs mount entry. the alternative is to use
154 * strcmp(mnt->mnt_type, MNTTYPE_NFS) instead of the strchr. */
155 if ((stat(mnt->mnt_dir, &sb) == 0) && (devno == sb.st_dev) &&
156 strchr(mnt->mnt_fsname, ':')) {
159 return( mnt->mnt_fsname );
169 #endif /* __svr4__ */
172 static int getfsquota(vol, uid, dq)
182 qc.addr = (caddr_t)dq;
183 if ( ioctl( vol->v_qfd, Q_QUOTACTL, &qc ) < 0 ) {
184 return( AFPERR_PARAM );
189 if ( quota( Q_GETDLIM, uid, vol->v_gvs, dq ) != 0 ) {
190 return( AFPERR_PARAM );
199 #define QCMD(a,b) (a)
202 /* for group quotas. we only use these if the user belongs
206 memset(&dqg, 0, sizeof(dqg));
209 if ( quotactl( vol->v_gvs, QCMD(Q_GETQUOTA,USRQUOTA),
210 uid, (char *)dq ) != 0 ) {
211 return( AFPERR_PARAM );
215 quotactl(vol->v_gvs, QCMD(Q_GETQUOTA, GRPQUOTA),
216 groups[0], (char *) &dqg);
219 if ( quotactl(QCMD(Q_GETQUOTA, USRQUOTA), vol->v_gvs, uid,
220 (caddr_t) dq ) != 0 ) {
221 return( AFPERR_PARAM );
225 quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), vol->v_gvs,
226 groups[0], (char *) &dqg);
229 /* set stuff up for group quotas if necessary */
231 /* max(user blocks, group blocks) */
232 if (dqg.dqb_curblocks && (dq->dqb_curblocks < dqg.dqb_curblocks))
233 dq->dqb_curblocks = dqg.dqb_curblocks;
235 /* min(user limit, group limit) */
236 if (dqg.dqb_bhardlimit && (!dq->dqb_bhardlimit ||
237 (dq->dqb_bhardlimit > dqg.dqb_bhardlimit)))
238 dq->dqb_bhardlimit = dqg.dqb_bhardlimit;
241 if (dqg.dqb_bsoftlimit && (!dq->dqb_bsoftlimit ||
242 (dq->dqb_bsoftlimit > dqg.dqb_bsoftlimit)))
243 dq->dqb_bsoftlimit = dqg.dqb_bsoftlimit;
246 if (dqg.dqb_btimelimit && (!dq->dqb_btimelimit ||
247 (dq->dqb_btimelimit > dqg.dqb_btimelimit)))
248 dq->dqb_btimelimit = dqg.dqb_btimelimit;
251 #endif /* __svr4__ */
257 static int getquota( vol, dq, bsize)
260 const u_int32_t bsize;
265 char buf[ MAXPATHLEN + 1];
267 if ( vol->v_qfd == -1 && vol->v_gvs == NULL) {
268 if (( p = mountp( vol->v_path, &vol->v_nfs)) == NULL ) {
269 syslog( LOG_INFO, "getquota: mountp %s fails", vol->v_path );
270 return( AFPERR_PARAM );
274 if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
275 syslog( LOG_ERR, "getquota: malloc: %m" );
278 strcpy( vol->v_gvs, p );
281 sprintf( buf, "%s/quotas", p );
282 if (( vol->v_qfd = open( buf, O_RDONLY, 0 )) < 0 ) {
283 syslog( LOG_INFO, "open %s: %m", buf );
284 return( AFPERR_PARAM );
290 if ( vol->v_gvs == NULL ) {
291 if (( p = special( vol->v_path, &vol->v_nfs )) == NULL ) {
292 syslog( LOG_INFO, "getquota: special %s fails", vol->v_path );
293 return( AFPERR_PARAM );
296 if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
297 syslog( LOG_ERR, "getquota: malloc: %m" );
300 strcpy( vol->v_gvs, p );
304 return vol->v_nfs ? getnfsquota(vol, uuid, bsize, dq) :
305 getfsquota(vol, uuid, dq);
308 static int overquota( dqblk )
313 if ( dqblk->dqb_curblocks < dqblk->dqb_bsoftlimit ) {
317 if ( dqblk->dqb_bwarn ) {
321 if ( gettimeofday( &tv, 0 ) < 0 ) {
322 syslog( LOG_ERR, "overquota: gettimeofday: %m" );
323 return( AFPERR_PARAM );
325 if ( !dqblk->dqb_btimelimit || dqblk->dqb_btimelimit > tv.tv_sec ) {
334 #define dbtob(a) ((a) << 10)
337 /* i do the cast to VolSpace here to make sure that 64-bit shifts
339 #ifdef HAVE_2ARG_DBTOB
340 #define tobytes(a, b) dbtob((VolSpace) (a), (VolSpace) (b))
342 #define tobytes(a, b) dbtob((VolSpace) (a))
344 int uquota_getvolspace( vol, bfree, btotal, bsize)
345 const struct vol *vol;
346 VolSpace *bfree, *btotal;
347 const u_int32_t bsize;
351 if (getquota( vol, &dqblk, bsize) != 0 ) {
352 return( AFPERR_PARAM );
355 /* no limit set for this user. it might be set in the future. */
356 if (dqblk.dqb_bsoftlimit == 0 && dqblk.dqb_bhardlimit == 0) {
357 *btotal = *bfree = ~((VolSpace) 0);
358 } else if ( overquota( &dqblk )) {
359 *btotal = tobytes( dqblk.dqb_bhardlimit, bsize );
360 *bfree = tobytes( dqblk.dqb_bhardlimit, bsize ) -
361 tobytes( dqblk.dqb_curblocks, bsize );
363 *btotal = tobytes( dqblk.dqb_bsoftlimit, bsize );
364 *bfree = tobytes( dqblk.dqb_bsoftlimit, bsize ) -
365 tobytes( dqblk.dqb_curblocks, bsize );