2 * $Id: quota.c,v 1.10 2001-07-31 19:50:14 srittau Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
17 #include <sys/param.h>
20 #endif /* HAVE_UNISTD_H */
23 #endif /* HAVE_FCNTL_H */
26 #include <atalk/afp.h>
32 #ifndef NO_QUOTA_SUPPORT
33 #ifdef NEED_QUOTACTL_WRAPPER
34 int quotactl(int cmd, const char *special, int id, caddr_t addr)
36 return syscall(__NR_quotactl, cmd, special, id, addr);
38 #endif /* NEED_QUOTACTL_WRAPPER */
41 #if defined(HAVE_SYS_MNTTAB_H) || defined(__svr4__)
43 * Return the mount point associated with the filesystem
44 * on which "file" resides. Returns NULL on failure.
54 static struct mnttab mnt;
56 if ( stat( file, &sb ) < 0 ) {
61 if (( mtab = fopen( "/etc/mnttab", "r" )) == NULL ) {
65 while ( getmntent( mtab, &mnt ) == 0 ) {
67 if ( (stat( mnt.mnt_special, &sb ) == 0) && (devno == sb.st_rdev)) {
69 return mnt.mnt_mountp;
72 /* check for nfs. we probably should use
73 * strcmp(mnt.mnt_fstype, MNTTYPE_NFS), but that's not as fast. */
74 if ((stat(mnt.mnt_mountp, &sb) == 0) && (devno == sb.st_dev) &&
75 strchr(mnt.mnt_special, ':')) {
78 return mnt.mnt_special;
89 * Return the block-special device name associated with the filesystem
90 * on which "file" resides. Returns NULL on failure.
98 static struct fs_data fsd;
100 if ( getmnt(0, &fsd, 0, STAT_ONE, file ) < 0 ) {
101 syslog(LOG_INFO, "special: getmnt %s: %m", file );
105 /* XXX: does this really detect an nfs mounted fs? */
106 if (strchr(fsd.fd_req.devname, ':'))
108 return( fsd.fd_req.devname );
112 #if (defined(HAVE_SYS_MOUNT_H) && !defined(__linux__)) || defined(BSD4_4) || defined(_IBMR2)
119 static struct statfs sfs;
121 if ( statfs( file, &sfs ) < 0 ) {
125 /* XXX: make sure this really detects an nfs mounted fs */
126 if (strchr(sfs.f_mntfromname, ':'))
128 return( sfs.f_mntfromname );
143 if ( stat( file, &sb ) < 0 ) {
148 if (( mtab = setmntent( "/etc/mtab", "r" )) == NULL ) {
152 while (( mnt = getmntent( mtab )) != NULL ) {
153 /* check for local fs */
154 if ( (stat( mnt->mnt_fsname, &sb ) == 0) && devno == sb.st_rdev) {
156 return( mnt->mnt_fsname );
159 /* check for an nfs mount entry. the alternative is to use
160 * strcmp(mnt->mnt_type, MNTTYPE_NFS) instead of the strchr. */
161 if ((stat(mnt->mnt_dir, &sb) == 0) && (devno == sb.st_dev) &&
162 strchr(mnt->mnt_fsname, ':')) {
165 return( mnt->mnt_fsname );
175 #endif /* __svr4__ */
178 static int getfsquota(vol, uid, dq)
188 qc.addr = (caddr_t)dq;
189 if ( ioctl( vol->v_qfd, Q_QUOTACTL, &qc ) < 0 ) {
190 return( AFPERR_PARAM );
195 if ( quota( Q_GETDLIM, uid, vol->v_gvs, dq ) != 0 ) {
196 return( AFPERR_PARAM );
205 #define QCMD(a,b) (a)
209 /* for group quotas. we only use these if the user belongs
213 memset(&dqg, 0, sizeof(dqg));
217 if ( quotactl( vol->v_gvs, QCMD(Q_GETQUOTA,USRQUOTA),
218 uid, (char *)dq ) != 0 ) {
219 return( AFPERR_PARAM );
223 quotactl(vol->v_gvs, QCMD(Q_GETQUOTA, GRPQUOTA),
224 groups[0], (char *) &dqg);
227 if ( seteuid( getuid() ) == 0 ) {
228 if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA, USRQUOTA),
229 uid, (char *)dq ) != 0 ) {
231 return ( AFPERR_PARAM );
237 if ( quotactl(QCMD(Q_GETQUOTA, USRQUOTA), vol->v_gvs, uid,
238 (caddr_t) dq ) != 0 ) {
239 return( AFPERR_PARAM );
243 quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), vol->v_gvs,
244 groups[0], (char *) &dqg);
248 /* set stuff up for group quotas if necessary */
250 /* max(user blocks, group blocks) */
251 if (dqg.dqb_curblocks && (dq->dqb_curblocks < dqg.dqb_curblocks))
252 dq->dqb_curblocks = dqg.dqb_curblocks;
254 /* min(user limit, group limit) */
255 if (dqg.dqb_bhardlimit && (!dq->dqb_bhardlimit ||
256 (dq->dqb_bhardlimit > dqg.dqb_bhardlimit)))
257 dq->dqb_bhardlimit = dqg.dqb_bhardlimit;
260 if (dqg.dqb_bsoftlimit && (!dq->dqb_bsoftlimit ||
261 (dq->dqb_bsoftlimit > dqg.dqb_bsoftlimit)))
262 dq->dqb_bsoftlimit = dqg.dqb_bsoftlimit;
265 if (dqg.dqb_btimelimit && (!dq->dqb_btimelimit ||
266 (dq->dqb_btimelimit > dqg.dqb_btimelimit)))
267 dq->dqb_btimelimit = dqg.dqb_btimelimit;
272 #endif /* __svr4__ */
278 static int getquota( vol, dq, bsize)
281 const u_int32_t bsize;
286 char buf[ MAXPATHLEN + 1];
288 if ( vol->v_qfd == -1 && vol->v_gvs == NULL) {
289 if (( p = mountp( vol->v_path, &vol->v_nfs)) == NULL ) {
290 syslog( LOG_INFO, "getquota: mountp %s fails", vol->v_path );
291 return( AFPERR_PARAM );
295 if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
296 syslog( LOG_ERR, "getquota: malloc: %m" );
299 strcpy( vol->v_gvs, p );
302 sprintf( buf, "%s/quotas", p );
303 if (( vol->v_qfd = open( buf, O_RDONLY, 0 )) < 0 ) {
304 syslog( LOG_INFO, "open %s: %m", buf );
305 return( AFPERR_PARAM );
311 if ( vol->v_gvs == NULL ) {
312 if (( p = special( vol->v_path, &vol->v_nfs )) == NULL ) {
313 syslog( LOG_INFO, "getquota: special %s fails", vol->v_path );
314 return( AFPERR_PARAM );
317 if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
318 syslog( LOG_ERR, "getquota: malloc: %m" );
321 strcpy( vol->v_gvs, p );
325 return vol->v_nfs ? getnfsquota(vol, uuid, bsize, dq) :
326 getfsquota(vol, uuid, dq);
329 static int overquota( dqblk )
334 if ( dqblk->dqb_curblocks < dqblk->dqb_bsoftlimit ) {
338 if ( dqblk->dqb_bwarn ) {
342 if ( gettimeofday( &tv, 0 ) < 0 ) {
343 syslog( LOG_ERR, "overquota: gettimeofday: %m" );
344 return( AFPERR_PARAM );
346 if ( !dqblk->dqb_btimelimit || dqblk->dqb_btimelimit > tv.tv_sec ) {
354 * This next bit is basically for linux -- everything is fine
355 * if you use 1k blocks... but if you try (for example) to mount
356 * a volume via nfs from a netapp (which might use 4k blocks) everything
357 * gets reported improperly. I have no idea about dbtob on other
361 #ifdef HAVE_BROKEN_DBTOB
363 #define dbtob(a, b) ((VolSpace)((VolSpace)(a) * (VolSpace)(b)))
364 #define HAVE_2ARG_DBTOB
368 #define dbtob(a) ((a) << 10)
371 /* i do the cast to VolSpace here to make sure that 64-bit shifts
373 #ifdef HAVE_2ARG_DBTOB
374 #define tobytes(a, b) dbtob((VolSpace) (a), (VolSpace) (b))
376 #define tobytes(a, b) dbtob((VolSpace) (a))
378 int uquota_getvolspace( vol, bfree, btotal, bsize)
379 const struct vol *vol;
380 VolSpace *bfree, *btotal;
381 const u_int32_t bsize;
385 if (getquota( vol, &dqblk, bsize) != 0 ) {
386 return( AFPERR_PARAM );
389 /* no limit set for this user. it might be set in the future. */
390 if (dqblk.dqb_bsoftlimit == 0 && dqblk.dqb_bhardlimit == 0) {
391 *btotal = *bfree = ~((VolSpace) 0);
392 } else if ( overquota( &dqblk )) {
393 *btotal = tobytes( dqblk.dqb_bhardlimit, bsize );
394 *bfree = tobytes( dqblk.dqb_bhardlimit, bsize ) -
395 tobytes( dqblk.dqb_curblocks, bsize );
397 *btotal = tobytes( dqblk.dqb_bsoftlimit, bsize );
398 *bfree = tobytes( dqblk.dqb_bsoftlimit, bsize ) -
399 tobytes( dqblk.dqb_curblocks, bsize );