2 * $Id: quota.c,v 1.22 2002-08-29 17:22:06 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_afpd, "special: getmnt %s: %s", file, strerror(errno) );
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 ) {
143 /* Digital UNIX: The struct sfs contains a field sfs.f_type,
144 * the MOUNT_* constants are defined in <sys/mount.h> */
145 if ((sfs.f_type == MOUNT_NFS)||(sfs.f_type == MOUNT_NFS3))
147 /* XXX: make sure this really detects an nfs mounted fs */
148 if (strchr(sfs.f_mntfromname, ':'))
151 return( sfs.f_mntfromname );
166 if ( stat( file, &sb ) < 0 ) {
171 if (( mtab = setmntent( "/etc/mtab", "r" )) == NULL ) {
175 while (( mnt = getmntent( mtab )) != NULL ) {
176 /* check for local fs */
177 if ( (stat( mnt->mnt_fsname, &sb ) == 0) && devno == sb.st_rdev) {
179 return( mnt->mnt_fsname );
182 /* check for an nfs mount entry. the alternative is to use
183 * strcmp(mnt->mnt_type, MNTTYPE_NFS) instead of the strchr. */
184 if ((stat(mnt->mnt_dir, &sb) == 0) && (devno == sb.st_dev) &&
185 strchr(mnt->mnt_fsname, ':')) {
188 return( mnt->mnt_fsname );
198 #endif /* __svr4__ */
201 static int getfsquota(vol, uid, dq)
211 qc.addr = (caddr_t)dq;
212 if ( ioctl( vol->v_qfd, Q_QUOTACTL, &qc ) < 0 ) {
213 return( AFPERR_PARAM );
218 if ( quota( Q_GETDLIM, uid, vol->v_gvs, dq ) != 0 ) {
219 return( AFPERR_PARAM );
228 #define QCMD(a,b) (a)
232 /* for group quotas. we only use these if the user belongs
236 memset(&dqg, 0, sizeof(dqg));
240 if ( seteuid( getuid() ) == 0 ) {
241 if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA,USRQUOTA),
242 uid, (char *)dq ) != 0 ) {
243 /* try group quotas */
245 if ( quotactl(vol->v_path, QCMD(Q_GETQUOTA, GRPQUOTA),
246 groups[0], (char *) &dqg) != 0 ) {
248 return( AFPERR_PARAM );
256 if ( seteuid( getuid() ) == 0 ) {
257 if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA, USRQUOTA),
258 uid, (char *)dq ) != 0 ) {
260 return ( AFPERR_PARAM );
266 if ( quotactl(QCMD(Q_GETQUOTA, USRQUOTA), vol->v_gvs, uid,
267 (caddr_t) dq ) != 0 ) {
268 return( AFPERR_PARAM );
272 quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), vol->v_gvs,
273 groups[0], (char *) &dqg);
277 /* set stuff up for group quotas if necessary */
279 /* max(user blocks, group blocks) */
280 if (dqg.dqb_curblocks && (dq->dqb_curblocks < dqg.dqb_curblocks))
281 dq->dqb_curblocks = dqg.dqb_curblocks;
283 /* min(user limit, group limit) */
284 if (dqg.dqb_bhardlimit && (!dq->dqb_bhardlimit ||
285 (dq->dqb_bhardlimit > dqg.dqb_bhardlimit)))
286 dq->dqb_bhardlimit = dqg.dqb_bhardlimit;
289 if (dqg.dqb_bsoftlimit && (!dq->dqb_bsoftlimit ||
290 (dq->dqb_bsoftlimit > dqg.dqb_bsoftlimit)))
291 dq->dqb_bsoftlimit = dqg.dqb_bsoftlimit;
294 if (dqg.dqb_btimelimit && (!dq->dqb_btimelimit ||
295 (dq->dqb_btimelimit > dqg.dqb_btimelimit)))
296 dq->dqb_btimelimit = dqg.dqb_btimelimit;
301 #endif /* __svr4__ */
307 static int getquota( vol, dq, bsize)
310 const u_int32_t bsize;
315 char buf[ MAXPATHLEN + 1];
317 if ( vol->v_qfd == -1 && vol->v_gvs == NULL) {
318 if (( p = mountp( vol->v_path, &vol->v_nfs)) == NULL ) {
319 LOG(log_info, logtype_afpd, "getquota: mountp %s fails", vol->v_path );
320 return( AFPERR_PARAM );
324 if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
325 LOG(log_error, logtype_afpd, "getquota: malloc: %s", strerror(errno) );
328 strcpy( vol->v_gvs, p );
331 sprintf( buf, "%s/quotas", p );
332 if (( vol->v_qfd = open( buf, O_RDONLY, 0 )) < 0 ) {
333 LOG(log_info, logtype_afpd, "open %s: %s", buf, strerror(errno) );
334 return( AFPERR_PARAM );
340 if ( vol->v_gvs == NULL ) {
341 if (( p = special( vol->v_path, &vol->v_nfs )) == NULL ) {
342 LOG(log_info, logtype_afpd, "getquota: special %s fails", vol->v_path );
343 return( AFPERR_PARAM );
346 if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
347 LOG(log_error, logtype_afpd, "getquota: malloc: %s", strerror(errno) );
350 strcpy( vol->v_gvs, p );
355 /* Digital UNIX: Two forms of specifying an NFS filesystem are possible,
356 either 'hostname:path' or 'path@hostname' (Ultrix heritage) */
359 char pathstring[MNAMELEN];
360 /* MNAMELEN ist defined in <sys/mount.h> */
363 if ((hostpath = strchr(vol->v_gvs,'@')) != NULL ) {
364 /* convert 'path@hostname' to 'hostname:path',
365 * call getnfsquota(),
366 * convert 'hostname:path' back to 'path@hostname' */
368 sprintf(pathstring,"%s:%s",hostpath+1,vol->v_gvs);
369 strcpy(vol->v_gvs,pathstring);
371 result = getnfsquota(vol, uuid, bsize, dq);
373 hostpath = strchr(vol->v_gvs,':');
375 sprintf(pathstring,"%s@%s",hostpath+1,vol->v_gvs);
376 strcpy(vol->v_gvs,pathstring);
381 /* vol->v_gvs is of the form 'hostname:path' */
382 return getnfsquota(vol, uuid, bsize, dq);
384 /* local filesystem */
385 return getfsquota(vol, uuid, dq);
388 return vol->v_nfs ? getnfsquota(vol, uuid, bsize, dq) :
389 getfsquota(vol, uuid, dq);
393 static int overquota( dqblk )
398 if ( dqblk->dqb_curblocks < dqblk->dqb_bsoftlimit ) {
402 if ( dqblk->dqb_bwarn ) {
406 if ( gettimeofday( &tv, 0 ) < 0 ) {
407 LOG(log_error, logtype_afpd, "overquota: gettimeofday: %s", strerror(errno) );
408 return( AFPERR_PARAM );
410 if ( dqblk->dqb_btimelimit && dqblk->dqb_btimelimit > tv.tv_sec ) {
418 * This next bit is basically for linux -- everything is fine
419 * if you use 1k blocks... but if you try (for example) to mount
420 * a volume via nfs from a netapp (which might use 4k blocks) everything
421 * gets reported improperly. I have no idea about dbtob on other
425 #ifdef HAVE_BROKEN_DBTOB
427 #define dbtob(a, b) ((VolSpace)((VolSpace)(a) * (VolSpace)(b)))
428 #define HAVE_2ARG_DBTOB
432 #define dbtob(a) ((a) << 10)
435 /* i do the cast to VolSpace here to make sure that 64-bit shifts
437 #ifdef HAVE_2ARG_DBTOB
438 #define tobytes(a, b) dbtob((VolSpace) (a), (VolSpace) (b))
440 #define tobytes(a, b) dbtob((VolSpace) (a))
442 int uquota_getvolspace( vol, bfree, btotal, bsize)
443 const struct vol *vol;
444 VolSpace *bfree, *btotal;
445 const u_int32_t bsize;
449 if (getquota( vol, &dqblk, bsize) != 0 ) {
450 return( AFPERR_PARAM );
453 /* no limit set for this user. it might be set in the future. */
454 if (dqblk.dqb_bsoftlimit == 0 && dqblk.dqb_bhardlimit == 0) {
455 *btotal = *bfree = ~((VolSpace) 0);
456 } else if ( overquota( &dqblk )) {
457 if ( tobytes( dqblk.dqb_curblocks, bsize ) > tobytes( dqblk.dqb_bhardlimit, bsize ) ) {
458 *btotal = tobytes( dqblk.dqb_curblocks, bsize );
462 *btotal = tobytes( dqblk.dqb_bhardlimit, bsize );
463 *bfree = tobytes( dqblk.dqb_bhardlimit, bsize ) -
464 tobytes( dqblk.dqb_curblocks, bsize );
467 *btotal = tobytes( dqblk.dqb_bsoftlimit, bsize );
468 *bfree = tobytes( dqblk.dqb_bsoftlimit, bsize ) -
469 tobytes( dqblk.dqb_curblocks, bsize );