2 * $Id: quota.c,v 1.24 2003-12-28 13:51:12 srittau 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
52 #if defined(HAVE_SYS_MNTTAB_H) || defined(__svr4__)
54 * Return the mount point associated with the filesystem
55 * on which "file" resides. Returns NULL on failure.
65 static struct mnttab mnt;
67 if ( stat( file, &sb ) < 0 ) {
72 if (( mtab = fopen( "/etc/mnttab", "r" )) == NULL ) {
76 while ( getmntent( mtab, &mnt ) == 0 ) {
78 if ( (stat( mnt.mnt_special, &sb ) == 0) && (devno == sb.st_rdev)) {
80 return mnt.mnt_mountp;
83 /* check for nfs. we probably should use
84 * strcmp(mnt.mnt_fstype, MNTTYPE_NFS), but that's not as fast. */
85 if ((stat(mnt.mnt_mountp, &sb) == 0) && (devno == sb.st_dev) &&
86 strchr(mnt.mnt_special, ':')) {
89 return mnt.mnt_special;
98 #if (defined(HAVE_SYS_MOUNT_H) && !defined(__linux__)) || defined(BSD4_4) || defined(_IBMR2)
105 static struct statfs sfs;
107 if ( statfs( file, &sfs ) < 0 ) {
112 /* Digital UNIX: The struct sfs contains a field sfs.f_type,
113 * the MOUNT_* constants are defined in <sys/mount.h> */
114 if ((sfs.f_type == MOUNT_NFS)||(sfs.f_type == MOUNT_NFS3))
116 /* XXX: make sure this really detects an nfs mounted fs */
117 if (strchr(sfs.f_mntfromname, ':'))
120 return( sfs.f_mntfromname );
135 if ( stat( file, &sb ) < 0 ) {
140 if (( mtab = setmntent( "/etc/mtab", "r" )) == NULL ) {
144 while (( mnt = getmntent( mtab )) != NULL ) {
145 /* check for local fs */
146 if ( (stat( mnt->mnt_fsname, &sb ) == 0) && devno == sb.st_rdev) {
148 return( mnt->mnt_fsname );
151 /* check for an nfs mount entry. the alternative is to use
152 * strcmp(mnt->mnt_type, MNTTYPE_NFS) instead of the strchr. */
153 if ((stat(mnt->mnt_dir, &sb) == 0) && (devno == sb.st_dev) &&
154 strchr(mnt->mnt_fsname, ':')) {
157 return( mnt->mnt_fsname );
166 #endif /* __svr4__ */
169 static int getfsquota(vol, uid, dq)
179 qc.addr = (caddr_t)dq;
180 if ( ioctl( vol->v_qfd, Q_QUOTACTL, &qc ) < 0 ) {
181 return( AFPERR_PARAM );
191 #define QCMD(a,b) (a)
195 /* for group quotas. we only use these if the user belongs
199 memset(&dqg, 0, sizeof(dqg));
203 if ( seteuid( getuid() ) == 0 ) {
204 if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA,USRQUOTA),
205 uid, (char *)dq ) != 0 ) {
206 /* try group quotas */
208 if ( quotactl(vol->v_path, QCMD(Q_GETQUOTA, GRPQUOTA),
209 groups[0], (char *) &dqg) != 0 ) {
211 return( AFPERR_PARAM );
219 if ( seteuid( getuid() ) == 0 ) {
220 if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA, USRQUOTA),
221 uid, (char *)dq ) != 0 ) {
223 return ( AFPERR_PARAM );
229 if ( quotactl(QCMD(Q_GETQUOTA, USRQUOTA), vol->v_gvs, uid,
230 (caddr_t) dq ) != 0 ) {
231 return( AFPERR_PARAM );
235 quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), vol->v_gvs,
236 groups[0], (char *) &dqg);
240 /* set stuff up for group quotas if necessary */
242 /* max(user blocks, group blocks) */
243 #ifdef HAVE_STRUCT_IF_DQBLK
244 if (dqg.dqb_curspace && (dq->dqb_curspace < dqg.dqb_curspace))
245 dq->dqb_curspace = dqg.dqb_curspace;
247 if (dqg.dqb_curblocks && (dq->dqb_curblocks < dqg.dqb_curblocks))
248 dq->dqb_curblocks = dqg.dqb_curblocks;
251 /* min(user limit, group limit) */
252 if (dqg.dqb_bhardlimit && (!dq->dqb_bhardlimit ||
253 (dq->dqb_bhardlimit > dqg.dqb_bhardlimit)))
254 dq->dqb_bhardlimit = dqg.dqb_bhardlimit;
257 if (dqg.dqb_bsoftlimit && (!dq->dqb_bsoftlimit ||
258 (dq->dqb_bsoftlimit > dqg.dqb_bsoftlimit)))
259 dq->dqb_bsoftlimit = dqg.dqb_bsoftlimit;
262 if (dqg.dqb_btimelimit && (!dq->dqb_btimelimit ||
263 (dq->dqb_btimelimit > dqg.dqb_btimelimit)))
264 dq->dqb_btimelimit = dqg.dqb_btimelimit;
268 #endif /* __svr4__ */
274 static int getquota( vol, dq, bsize)
277 const u_int32_t bsize;
282 char buf[ MAXPATHLEN + 1];
284 if ( vol->v_qfd == -1 && vol->v_gvs == NULL) {
285 if (( p = mountp( vol->v_path, &vol->v_nfs)) == NULL ) {
286 LOG(log_info, logtype_afpd, "getquota: mountp %s fails", vol->v_path );
287 return( AFPERR_PARAM );
291 if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
292 LOG(log_error, logtype_afpd, "getquota: malloc: %s", strerror(errno) );
295 strcpy( vol->v_gvs, p );
298 sprintf( buf, "%s/quotas", p );
299 if (( vol->v_qfd = open( buf, O_RDONLY, 0 )) < 0 ) {
300 LOG(log_info, logtype_afpd, "open %s: %s", buf, strerror(errno) );
301 return( AFPERR_PARAM );
307 if ( vol->v_gvs == NULL ) {
308 if (( p = special( vol->v_path, &vol->v_nfs )) == NULL ) {
309 LOG(log_info, logtype_afpd, "getquota: special %s fails", vol->v_path );
310 return( AFPERR_PARAM );
313 if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
314 LOG(log_error, logtype_afpd, "getquota: malloc: %s", strerror(errno) );
317 strcpy( vol->v_gvs, p );
322 /* Digital UNIX: Two forms of specifying an NFS filesystem are possible,
323 either 'hostname:path' or 'path@hostname' (Ultrix heritage) */
326 char pathstring[MNAMELEN];
327 /* MNAMELEN ist defined in <sys/mount.h> */
330 if ((hostpath = strchr(vol->v_gvs,'@')) != NULL ) {
331 /* convert 'path@hostname' to 'hostname:path',
332 * call getnfsquota(),
333 * convert 'hostname:path' back to 'path@hostname' */
335 sprintf(pathstring,"%s:%s",hostpath+1,vol->v_gvs);
336 strcpy(vol->v_gvs,pathstring);
338 result = getnfsquota(vol, uuid, bsize, dq);
340 hostpath = strchr(vol->v_gvs,':');
342 sprintf(pathstring,"%s@%s",hostpath+1,vol->v_gvs);
343 strcpy(vol->v_gvs,pathstring);
348 /* vol->v_gvs is of the form 'hostname:path' */
349 return getnfsquota(vol, uuid, bsize, dq);
351 /* local filesystem */
352 return getfsquota(vol, uuid, dq);
355 return vol->v_nfs ? getnfsquota(vol, uuid, bsize, dq) :
356 getfsquota(vol, uuid, dq);
360 static int overquota( dqblk )
365 #ifdef HAVE_STRUCT_IF_DQBLK
366 if ( dqblk->dqb_curspace < dqblk->dqb_bsoftlimit ) {
368 if ( dqblk->dqb_curblocks < dqblk->dqb_bsoftlimit ) {
372 if ( gettimeofday( &tv, 0 ) < 0 ) {
373 LOG(log_error, logtype_afpd, "overquota: gettimeofday: %s", strerror(errno) );
374 return( AFPERR_PARAM );
376 if ( dqblk->dqb_btimelimit && dqblk->dqb_btimelimit > tv.tv_sec ) {
383 * This next bit is basically for linux -- everything is fine
384 * if you use 1k blocks... but if you try (for example) to mount
385 * a volume via nfs from a netapp (which might use 4k blocks) everything
386 * gets reported improperly. I have no idea about dbtob on other
390 #ifdef HAVE_BROKEN_DBTOB
392 #define dbtob(a, b) ((VolSpace)((VolSpace)(a) * (VolSpace)(b)))
393 #define HAVE_2ARG_DBTOB
397 #define dbtob(a) ((a) << 10)
400 /* i do the cast to VolSpace here to make sure that 64-bit shifts
402 #ifdef HAVE_2ARG_DBTOB
403 #define tobytes(a, b) dbtob((VolSpace) (a), (VolSpace) (b))
405 #define tobytes(a, b) dbtob((VolSpace) (a))
407 int uquota_getvolspace( vol, bfree, btotal, bsize)
408 const struct vol *vol;
409 VolSpace *bfree, *btotal;
410 const u_int32_t bsize;
414 if (getquota( vol, &dqblk, bsize) != 0 ) {
415 return( AFPERR_PARAM );
418 /* no limit set for this user. it might be set in the future. */
419 if (dqblk.dqb_bsoftlimit == 0 && dqblk.dqb_bhardlimit == 0) {
420 *btotal = *bfree = ~((VolSpace) 0);
421 } else if ( overquota( &dqblk )) {
422 #ifdef HAVE_STRUCT_IF_DQBLK
423 if ( tobytes( dqblk.dqb_curspace, bsize ) > tobytes( dqblk.dqb_bhardlimit, bsize ) ) {
424 *btotal = tobytes( dqblk.dqb_curspace, bsize );
426 if ( tobytes( dqblk.dqb_curblocks, bsize ) > tobytes( dqblk.dqb_bhardlimit, bsize ) ) {
427 *btotal = tobytes( dqblk.dqb_curblocks, bsize );
432 *btotal = tobytes( dqblk.dqb_bhardlimit, bsize );
433 *bfree = tobytes( dqblk.dqb_bhardlimit, bsize ) -
434 #ifdef HAVE_STRUCT_IF_DQBLK
435 tobytes( dqblk.dqb_curspace, bsize );
437 tobytes( dqblk.dqb_curblocks, bsize );
441 *btotal = tobytes( dqblk.dqb_bsoftlimit, bsize );
442 *bfree = tobytes( dqblk.dqb_bsoftlimit, bsize ) -
443 tobytes( dqblk.dqb_curblocks, bsize );