2 * $Id: quota.c,v 1.15 2002-01-13 07:22:02 jmarcus Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
20 #else /* STDC_HEADERS */
24 #endif /* HAVE_STRCHR */
25 char *strchr (), *strrchr ();
27 #define memcpy(d,s,n) bcopy ((s), (d), (n))
28 #define memmove(d,s,n) bcopy ((s), (d), (n))
29 #endif /* ! HAVE_MEMCPY */
30 #endif /* STDC_HEADERS */
34 #include <sys/param.h>
37 #endif /* HAVE_UNISTD_H */
40 #endif /* HAVE_FCNTL_H */
41 #include <atalk/logger.h>
43 #include <atalk/afp.h>
49 #ifndef NO_QUOTA_SUPPORT
50 #ifdef NEED_QUOTACTL_WRAPPER
51 int quotactl(int cmd, const char *special, int id, caddr_t addr)
53 return syscall(__NR_quotactl, cmd, special, id, addr);
55 #endif /* NEED_QUOTACTL_WRAPPER */
58 #if defined(HAVE_SYS_MNTTAB_H) || defined(__svr4__)
60 * Return the mount point associated with the filesystem
61 * on which "file" resides. Returns NULL on failure.
71 static struct mnttab mnt;
73 if ( stat( file, &sb ) < 0 ) {
78 if (( mtab = fopen( "/etc/mnttab", "r" )) == NULL ) {
82 while ( getmntent( mtab, &mnt ) == 0 ) {
84 if ( (stat( mnt.mnt_special, &sb ) == 0) && (devno == sb.st_rdev)) {
86 return mnt.mnt_mountp;
89 /* check for nfs. we probably should use
90 * strcmp(mnt.mnt_fstype, MNTTYPE_NFS), but that's not as fast. */
91 if ((stat(mnt.mnt_mountp, &sb) == 0) && (devno == sb.st_dev) &&
92 strchr(mnt.mnt_special, ':')) {
95 return mnt.mnt_special;
106 * Return the block-special device name associated with the filesystem
107 * on which "file" resides. Returns NULL on failure.
115 static struct fs_data fsd;
117 if ( getmnt(0, &fsd, 0, STAT_ONE, file ) < 0 ) {
118 LOG(log_info, logtype_default, "special: getmnt %s: %m", file );
122 /* XXX: does this really detect an nfs mounted fs? */
123 if (strchr(fsd.fd_req.devname, ':'))
125 return( fsd.fd_req.devname );
129 #if (defined(HAVE_SYS_MOUNT_H) && !defined(__linux__)) || defined(BSD4_4) || defined(_IBMR2)
136 static struct statfs sfs;
138 if ( statfs( file, &sfs ) < 0 ) {
142 /* XXX: make sure this really detects an nfs mounted fs */
143 if (strchr(sfs.f_mntfromname, ':'))
145 return( sfs.f_mntfromname );
160 if ( stat( file, &sb ) < 0 ) {
165 if (( mtab = setmntent( "/etc/mtab", "r" )) == NULL ) {
169 while (( mnt = getmntent( mtab )) != NULL ) {
170 /* check for local fs */
171 if ( (stat( mnt->mnt_fsname, &sb ) == 0) && devno == sb.st_rdev) {
173 return( mnt->mnt_fsname );
176 /* check for an nfs mount entry. the alternative is to use
177 * strcmp(mnt->mnt_type, MNTTYPE_NFS) instead of the strchr. */
178 if ((stat(mnt->mnt_dir, &sb) == 0) && (devno == sb.st_dev) &&
179 strchr(mnt->mnt_fsname, ':')) {
182 return( mnt->mnt_fsname );
192 #endif /* __svr4__ */
195 static int getfsquota(vol, uid, dq)
205 qc.addr = (caddr_t)dq;
206 if ( ioctl( vol->v_qfd, Q_QUOTACTL, &qc ) < 0 ) {
207 return( AFPERR_PARAM );
212 if ( quota( Q_GETDLIM, uid, vol->v_gvs, dq ) != 0 ) {
213 return( AFPERR_PARAM );
222 #define QCMD(a,b) (a)
226 /* for group quotas. we only use these if the user belongs
230 memset(&dqg, 0, sizeof(dqg));
234 if ( seteuid( getuid() ) == 0 ) {
235 if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA,USRQUOTA),
236 uid, (char *)dq ) != 0 ) {
237 /* try group quotas */
239 if ( quotactl(vol->v_path, QCMD(Q_GETQUOTA, GRPQUOTA),
240 groups[0], (char *) &dqg) != 0 ) {
242 return( AFPERR_PARAM );
250 if ( seteuid( getuid() ) == 0 ) {
251 if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA, USRQUOTA),
252 uid, (char *)dq ) != 0 ) {
254 return ( AFPERR_PARAM );
260 if ( quotactl(QCMD(Q_GETQUOTA, USRQUOTA), vol->v_gvs, uid,
261 (caddr_t) dq ) != 0 ) {
262 return( AFPERR_PARAM );
266 quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), vol->v_gvs,
267 groups[0], (char *) &dqg);
271 /* set stuff up for group quotas if necessary */
273 /* max(user blocks, group blocks) */
274 if (dqg.dqb_curblocks && (dq->dqb_curblocks < dqg.dqb_curblocks))
275 dq->dqb_curblocks = dqg.dqb_curblocks;
277 /* min(user limit, group limit) */
278 if (dqg.dqb_bhardlimit && (!dq->dqb_bhardlimit ||
279 (dq->dqb_bhardlimit > dqg.dqb_bhardlimit)))
280 dq->dqb_bhardlimit = dqg.dqb_bhardlimit;
283 if (dqg.dqb_bsoftlimit && (!dq->dqb_bsoftlimit ||
284 (dq->dqb_bsoftlimit > dqg.dqb_bsoftlimit)))
285 dq->dqb_bsoftlimit = dqg.dqb_bsoftlimit;
288 if (dqg.dqb_btimelimit && (!dq->dqb_btimelimit ||
289 (dq->dqb_btimelimit > dqg.dqb_btimelimit)))
290 dq->dqb_btimelimit = dqg.dqb_btimelimit;
295 #endif /* __svr4__ */
301 static int getquota( vol, dq, bsize)
304 const u_int32_t bsize;
309 char buf[ MAXPATHLEN + 1];
311 if ( vol->v_qfd == -1 && vol->v_gvs == NULL) {
312 if (( p = mountp( vol->v_path, &vol->v_nfs)) == NULL ) {
313 LOG(log_info, logtype_default, "getquota: mountp %s fails", vol->v_path );
314 return( AFPERR_PARAM );
318 if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
319 LOG(log_error, logtype_default, "getquota: malloc: %m" );
322 strcpy( vol->v_gvs, p );
325 sprintf( buf, "%s/quotas", p );
326 if (( vol->v_qfd = open( buf, O_RDONLY, 0 )) < 0 ) {
327 LOG(log_info, logtype_default, "open %s: %m", buf );
328 return( AFPERR_PARAM );
334 if ( vol->v_gvs == NULL ) {
335 if (( p = special( vol->v_path, &vol->v_nfs )) == NULL ) {
336 LOG(log_info, logtype_default, "getquota: special %s fails", vol->v_path );
337 return( AFPERR_PARAM );
340 if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
341 LOG(log_error, logtype_default, "getquota: malloc: %s", strerror(errno) );
344 strcpy( vol->v_gvs, p );
348 return vol->v_nfs ? getnfsquota(vol, uuid, bsize, dq) :
349 getfsquota(vol, uuid, dq);
352 static int overquota( dqblk )
357 if ( dqblk->dqb_curblocks < dqblk->dqb_bsoftlimit ) {
361 if ( dqblk->dqb_bwarn ) {
365 if ( gettimeofday( &tv, 0 ) < 0 ) {
366 LOG(log_error, logtype_default, "overquota: gettimeofday: %s", strerror(errno) );
367 return( AFPERR_PARAM );
369 if ( !dqblk->dqb_btimelimit || dqblk->dqb_btimelimit > tv.tv_sec ) {
377 * This next bit is basically for linux -- everything is fine
378 * if you use 1k blocks... but if you try (for example) to mount
379 * a volume via nfs from a netapp (which might use 4k blocks) everything
380 * gets reported improperly. I have no idea about dbtob on other
384 #ifdef HAVE_BROKEN_DBTOB
386 #define dbtob(a, b) ((VolSpace)((VolSpace)(a) * (VolSpace)(b)))
387 #define HAVE_2ARG_DBTOB
391 #define dbtob(a) ((a) << 10)
394 /* i do the cast to VolSpace here to make sure that 64-bit shifts
396 #ifdef HAVE_2ARG_DBTOB
397 #define tobytes(a, b) dbtob((VolSpace) (a), (VolSpace) (b))
399 #define tobytes(a, b) dbtob((VolSpace) (a))
401 int uquota_getvolspace( vol, bfree, btotal, bsize)
402 const struct vol *vol;
403 VolSpace *bfree, *btotal;
404 const u_int32_t bsize;
408 if (getquota( vol, &dqblk, bsize) != 0 ) {
409 return( AFPERR_PARAM );
412 /* no limit set for this user. it might be set in the future. */
413 if (dqblk.dqb_bsoftlimit == 0 && dqblk.dqb_bhardlimit == 0) {
414 *btotal = *bfree = ~((VolSpace) 0);
415 } else if ( overquota( &dqblk )) {
416 *btotal = tobytes( dqblk.dqb_bhardlimit, bsize );
417 *bfree = tobytes( dqblk.dqb_bhardlimit, bsize ) -
418 tobytes( dqblk.dqb_curblocks, bsize );
420 *btotal = tobytes( dqblk.dqb_bsoftlimit, bsize );
421 *bfree = tobytes( dqblk.dqb_bsoftlimit, bsize ) -
422 tobytes( dqblk.dqb_curblocks, bsize );