2 * $Id: quota.c,v 1.12 2001-12-03 05:03:38 jmarcus Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
18 #else /* STDC_HEADERS */
22 #endif /* HAVE_STRCHR */
23 char *strchr (), *strrchr ();
25 #define memcpy(d,s,n) bcopy ((s), (d), (n))
26 #define memmove(d,s,n) bcopy ((s), (d), (n))
27 #endif /* ! HAVE_MEMCPY */
28 #endif /* STDC_HEADERS */
32 #include <sys/param.h>
35 #endif /* HAVE_UNISTD_H */
38 #endif /* HAVE_FCNTL_H */
41 #include <atalk/afp.h>
47 #ifndef NO_QUOTA_SUPPORT
48 #ifdef NEED_QUOTACTL_WRAPPER
49 int quotactl(int cmd, const char *special, int id, caddr_t addr)
51 return syscall(__NR_quotactl, cmd, special, id, addr);
53 #endif /* NEED_QUOTACTL_WRAPPER */
56 #if defined(HAVE_SYS_MNTTAB_H) || defined(__svr4__)
58 * Return the mount point associated with the filesystem
59 * on which "file" resides. Returns NULL on failure.
69 static struct mnttab mnt;
71 if ( stat( file, &sb ) < 0 ) {
76 if (( mtab = fopen( "/etc/mnttab", "r" )) == NULL ) {
80 while ( getmntent( mtab, &mnt ) == 0 ) {
82 if ( (stat( mnt.mnt_special, &sb ) == 0) && (devno == sb.st_rdev)) {
84 return mnt.mnt_mountp;
87 /* check for nfs. we probably should use
88 * strcmp(mnt.mnt_fstype, MNTTYPE_NFS), but that's not as fast. */
89 if ((stat(mnt.mnt_mountp, &sb) == 0) && (devno == sb.st_dev) &&
90 strchr(mnt.mnt_special, ':')) {
93 return mnt.mnt_special;
104 * Return the block-special device name associated with the filesystem
105 * on which "file" resides. Returns NULL on failure.
113 static struct fs_data fsd;
115 if ( getmnt(0, &fsd, 0, STAT_ONE, file ) < 0 ) {
116 syslog(LOG_INFO, "special: getmnt %s: %m", file );
120 /* XXX: does this really detect an nfs mounted fs? */
121 if (strchr(fsd.fd_req.devname, ':'))
123 return( fsd.fd_req.devname );
127 #if (defined(HAVE_SYS_MOUNT_H) && !defined(__linux__)) || defined(BSD4_4) || defined(_IBMR2)
134 static struct statfs sfs;
136 if ( statfs( file, &sfs ) < 0 ) {
140 /* XXX: make sure this really detects an nfs mounted fs */
141 if (strchr(sfs.f_mntfromname, ':'))
143 return( sfs.f_mntfromname );
158 if ( stat( file, &sb ) < 0 ) {
163 if (( mtab = setmntent( "/etc/mtab", "r" )) == NULL ) {
167 while (( mnt = getmntent( mtab )) != NULL ) {
168 /* check for local fs */
169 if ( (stat( mnt->mnt_fsname, &sb ) == 0) && devno == sb.st_rdev) {
171 return( mnt->mnt_fsname );
174 /* check for an nfs mount entry. the alternative is to use
175 * strcmp(mnt->mnt_type, MNTTYPE_NFS) instead of the strchr. */
176 if ((stat(mnt->mnt_dir, &sb) == 0) && (devno == sb.st_dev) &&
177 strchr(mnt->mnt_fsname, ':')) {
180 return( mnt->mnt_fsname );
190 #endif /* __svr4__ */
193 static int getfsquota(vol, uid, dq)
203 qc.addr = (caddr_t)dq;
204 if ( ioctl( vol->v_qfd, Q_QUOTACTL, &qc ) < 0 ) {
205 return( AFPERR_PARAM );
210 if ( quota( Q_GETDLIM, uid, vol->v_gvs, dq ) != 0 ) {
211 return( AFPERR_PARAM );
220 #define QCMD(a,b) (a)
224 /* for group quotas. we only use these if the user belongs
228 memset(&dqg, 0, sizeof(dqg));
232 if ( quotactl( vol->v_gvs, QCMD(Q_GETQUOTA,USRQUOTA),
233 uid, (char *)dq ) != 0 ) {
234 return( AFPERR_PARAM );
238 quotactl(vol->v_gvs, QCMD(Q_GETQUOTA, GRPQUOTA),
239 groups[0], (char *) &dqg);
242 if ( seteuid( getuid() ) == 0 ) {
243 if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA, USRQUOTA),
244 uid, (char *)dq ) != 0 ) {
246 return ( AFPERR_PARAM );
252 if ( quotactl(QCMD(Q_GETQUOTA, USRQUOTA), vol->v_gvs, uid,
253 (caddr_t) dq ) != 0 ) {
254 return( AFPERR_PARAM );
258 quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), vol->v_gvs,
259 groups[0], (char *) &dqg);
263 /* set stuff up for group quotas if necessary */
265 /* max(user blocks, group blocks) */
266 if (dqg.dqb_curblocks && (dq->dqb_curblocks < dqg.dqb_curblocks))
267 dq->dqb_curblocks = dqg.dqb_curblocks;
269 /* min(user limit, group limit) */
270 if (dqg.dqb_bhardlimit && (!dq->dqb_bhardlimit ||
271 (dq->dqb_bhardlimit > dqg.dqb_bhardlimit)))
272 dq->dqb_bhardlimit = dqg.dqb_bhardlimit;
275 if (dqg.dqb_bsoftlimit && (!dq->dqb_bsoftlimit ||
276 (dq->dqb_bsoftlimit > dqg.dqb_bsoftlimit)))
277 dq->dqb_bsoftlimit = dqg.dqb_bsoftlimit;
280 if (dqg.dqb_btimelimit && (!dq->dqb_btimelimit ||
281 (dq->dqb_btimelimit > dqg.dqb_btimelimit)))
282 dq->dqb_btimelimit = dqg.dqb_btimelimit;
287 #endif /* __svr4__ */
293 static int getquota( vol, dq, bsize)
296 const u_int32_t bsize;
301 char buf[ MAXPATHLEN + 1];
303 if ( vol->v_qfd == -1 && vol->v_gvs == NULL) {
304 if (( p = mountp( vol->v_path, &vol->v_nfs)) == NULL ) {
305 syslog( LOG_INFO, "getquota: mountp %s fails", vol->v_path );
306 return( AFPERR_PARAM );
310 if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
311 syslog( LOG_ERR, "getquota: malloc: %m" );
314 strcpy( vol->v_gvs, p );
317 sprintf( buf, "%s/quotas", p );
318 if (( vol->v_qfd = open( buf, O_RDONLY, 0 )) < 0 ) {
319 syslog( LOG_INFO, "open %s: %m", buf );
320 return( AFPERR_PARAM );
326 if ( vol->v_gvs == NULL ) {
327 if (( p = special( vol->v_path, &vol->v_nfs )) == NULL ) {
328 syslog( LOG_INFO, "getquota: special %s fails", vol->v_path );
329 return( AFPERR_PARAM );
332 if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
333 syslog( LOG_ERR, "getquota: malloc: %m" );
336 strcpy( vol->v_gvs, p );
340 return vol->v_nfs ? getnfsquota(vol, uuid, bsize, dq) :
341 getfsquota(vol, uuid, dq);
344 static int overquota( dqblk )
349 if ( dqblk->dqb_curblocks < dqblk->dqb_bsoftlimit ) {
353 if ( dqblk->dqb_bwarn ) {
357 if ( gettimeofday( &tv, 0 ) < 0 ) {
358 syslog( LOG_ERR, "overquota: gettimeofday: %m" );
359 return( AFPERR_PARAM );
361 if ( !dqblk->dqb_btimelimit || dqblk->dqb_btimelimit > tv.tv_sec ) {
369 * This next bit is basically for linux -- everything is fine
370 * if you use 1k blocks... but if you try (for example) to mount
371 * a volume via nfs from a netapp (which might use 4k blocks) everything
372 * gets reported improperly. I have no idea about dbtob on other
376 #ifdef HAVE_BROKEN_DBTOB
378 #define dbtob(a, b) ((VolSpace)((VolSpace)(a) * (VolSpace)(b)))
379 #define HAVE_2ARG_DBTOB
383 #define dbtob(a) ((a) << 10)
386 /* i do the cast to VolSpace here to make sure that 64-bit shifts
388 #ifdef HAVE_2ARG_DBTOB
389 #define tobytes(a, b) dbtob((VolSpace) (a), (VolSpace) (b))
391 #define tobytes(a, b) dbtob((VolSpace) (a))
393 int uquota_getvolspace( vol, bfree, btotal, bsize)
394 const struct vol *vol;
395 VolSpace *bfree, *btotal;
396 const u_int32_t bsize;
400 if (getquota( vol, &dqblk, bsize) != 0 ) {
401 return( AFPERR_PARAM );
404 /* no limit set for this user. it might be set in the future. */
405 if (dqblk.dqb_bsoftlimit == 0 && dqblk.dqb_bhardlimit == 0) {
406 *btotal = *bfree = ~((VolSpace) 0);
407 } else if ( overquota( &dqblk )) {
408 *btotal = tobytes( dqblk.dqb_bhardlimit, bsize );
409 *bfree = tobytes( dqblk.dqb_bhardlimit, bsize ) -
410 tobytes( dqblk.dqb_curblocks, bsize );
412 *btotal = tobytes( dqblk.dqb_bsoftlimit, bsize );
413 *bfree = tobytes( dqblk.dqb_bsoftlimit, bsize ) -
414 tobytes( dqblk.dqb_curblocks, bsize );