]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/quota.c
remove a debug quota log output
[netatalk.git] / etc / afpd / quota.c
1 /*
2  * $Id: quota.c,v 1.27 2006-09-15 00:07:43 didg Exp $
3  *
4  * Copyright (c) 1990,1993 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #ifndef NO_QUOTA_SUPPORT
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <sys/types.h>
18 /* STDC check */
19 #if STDC_HEADERS
20 #include <string.h>
21 #else /* STDC_HEADERS */
22 #ifndef HAVE_STRCHR
23 #define strchr index
24 #define strrchr index
25 #endif /* HAVE_STRCHR */
26 char *strchr (), *strrchr ();
27 #ifndef HAVE_MEMCPY
28 #define memcpy(d,s,n) bcopy ((s), (d), (n))
29 #define memmove(d,s,n) bcopy ((s), (d), (n))
30 #endif /* ! HAVE_MEMCPY */
31 #endif /* STDC_HEADERS */
32 #include <sys/stat.h>
33 #include <sys/time.h>
34 #include <sys/param.h>
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif /* HAVE_UNISTD_H */
38 #ifdef HAVE_FCNTL_H
39 #include <fcntl.h>
40 #endif /* HAVE_FCNTL_H */
41
42 #include <atalk/logger.h>
43 #include <atalk/afp.h>
44
45 #include "auth.h"
46 #include "volume.h"
47 #include "unix.h"
48
49 #define DEBUG_QUOTA 0
50 #define WANT_USER_QUOTA 0
51 #define WANT_GROUP_QUOTA 1
52
53 #ifdef NEED_QUOTACTL_WRAPPER
54 int quotactl(int cmd, const char *special, int id, caddr_t addr)
55 {
56     return syscall(__NR_quotactl, cmd, special, id, addr);
57 }
58 #endif /* NEED_QUOTACTL_WRAPPER */
59
60 static int overquota( struct dqblk *);
61
62 #ifdef linux
63
64 #ifdef HAVE_LINUX_XQM_H
65 #include <linux/xqm.h>
66 #else
67 #ifdef HAVE_XFS_XQM_H
68 #include <xfs/xqm.h>
69 #define HAVE_LINUX_XQM_H
70 #else
71 #ifdef  HAVE_LINUX_DQBLK_XFS_H
72 #include <linux/dqblk_xfs.h>
73 #define HAVE_LINUX_XQM_H
74 #endif /* HAVE_LINUX_DQBLK_XFS_H */
75 #endif /* HAVE_XFS_XQM_H */
76 #endif /* HAVE_LINUX_XQM_H */
77
78 #include <linux/unistd.h>
79
80 static int is_xfs = 0;
81
82 static int get_linux_xfs_quota(int, char*, uid_t, struct dqblk *);
83 static int get_linux_fs_quota(int, char*, uid_t, struct dqblk *);
84
85 /* format supported by current kernel */
86 static int kernel_iface = IFACE_UNSET;
87
88 /*
89 **  Check kernel quota version
90 **  Taken from quota-tools 3.08 by Jan Kara <jack@suse.cz>
91 */
92 static void linuxquota_get_api( void )
93 {
94 #ifndef LINUX_API_VERSION
95     struct stat st;
96
97     if (stat("/proc/sys/fs/quota", &st) == 0) {
98         kernel_iface = IFACE_GENERIC;
99     }
100     else {
101         struct dqstats_v2 v2_stats;
102         struct sigaction  sig;
103         struct sigaction  oldsig;
104
105         /* This signal handling is needed because old kernels send us SIGSEGV as they try to resolve the device */
106         sig.sa_handler   = SIG_IGN;
107         sig.sa_sigaction = NULL;
108         sig.sa_flags     = 0;
109         sigemptyset(&sig.sa_mask);
110         if (sigaction(SIGSEGV, &sig, &oldsig) < 0) {
111             LOG( log_error, logtype_afpd, "cannot set SEGV signal handler: %s", strerror(errno));
112             goto failure;
113         }
114         if (quotactl(QCMD(Q_V2_GETSTATS, 0), NULL, 0, (void *)&v2_stats) >= 0) {
115             kernel_iface = IFACE_VFSV0;
116         }
117         else if (errno != ENOSYS && errno != ENOTSUP) {
118             /* RedHat 7.1 (2.4.2-2) newquota check 
119              * Q_V2_GETSTATS in it's old place, Q_GETQUOTA in the new place
120              * (they haven't moved Q_GETSTATS to its new value) */
121             int err_stat = 0;
122             int err_quota = 0;
123             char tmp[1024];         /* Just temporary buffer */
124
125             if (quotactl(QCMD(Q_V1_GETSTATS, 0), NULL, 0, tmp))
126                 err_stat = errno;
127             if (quotactl(QCMD(Q_V1_GETQUOTA, 0), "/dev/null", 0, tmp))
128                 err_quota = errno;
129
130             /* On a RedHat 2.4.2-2      we expect 0, EINVAL
131              * On a 2.4.x               we expect 0, ENOENT
132              * On a 2.4.x-ac    we wont get here */
133             if (err_stat == 0 && err_quota == EINVAL) {
134                 kernel_iface = IFACE_VFSV0;
135             }
136             else {
137                 kernel_iface = IFACE_VFSOLD;
138             }
139         }
140         else {
141             /* This branch is *not* in quota-tools 3.08
142             ** but without it quota version is not correctly
143             ** identified for the original SuSE 8.0 kernel */
144             unsigned int vers_no;
145             FILE * qf;
146
147             if ((qf = fopen("/proc/fs/quota", "r"))) {
148                 if (fscanf(qf, "Version %u", &vers_no) == 1) {
149                     if ( (vers_no == (6*10000 + 5*100 + 0)) ||
150                          (vers_no == (6*10000 + 5*100 + 1)) ) {
151                         kernel_iface = IFACE_VFSV0;
152                     }
153                 }
154                 fclose(qf);
155             }
156         }
157         if (sigaction(SIGSEGV, &oldsig, NULL) < 0) {
158             LOG(log_error, logtype_afpd, "cannot reset signal handler: %s", strerror(errno));
159             goto failure;
160         }
161     }
162
163 failure:
164     if (kernel_iface == IFACE_UNSET)
165        kernel_iface = IFACE_VFSOLD;
166
167 #else /* defined LINUX_API_VERSION */
168     kernel_iface = LINUX_API_VERSION;
169 #endif
170 }
171
172 /****************************************************************************/
173
174 static int get_linux_quota(int what, char *path, uid_t euser_id, struct dqblk *dp)
175 {
176         int r; /* result */
177
178         if ( is_xfs )
179                 r=get_linux_xfs_quota(what, path, euser_id, dp);
180         else
181                 r=get_linux_fs_quota(what, path, euser_id, dp);
182     
183         return r;
184 }
185
186 /****************************************************************************
187  Abstract out the XFS Quota Manager quota get call.
188 ****************************************************************************/
189
190 static int get_linux_xfs_quota(what, path, euser_id, dqb)
191 int what;
192 char *path;
193 uid_t euser_id;
194 struct dqblk *dqb;
195 {
196         int ret = -1;
197 #ifdef HAVE_LINUX_XQM_H
198         struct fs_disk_quota D;
199         
200         memset (&D, 0, sizeof(D));
201
202         if ((ret = quotactl(QCMD(Q_XGETQUOTA,(what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t)&D)))
203                return ret;
204
205         dqb->bsize = (u_int64_t)512;
206         dqb->dqb_bsoftlimit  = (u_int64_t)D.d_blk_softlimit;
207         dqb->dqb_bhardlimit  = (u_int64_t)D.d_blk_hardlimit;
208         dqb->dqb_ihardlimit  = (u_int64_t)D.d_ino_hardlimit;
209         dqb->dqb_isoftlimit  = (u_int64_t)D.d_ino_softlimit;
210         dqb->dqb_curinodes   = (u_int64_t)D.d_icount;
211         dqb->dqb_curblocks   = (u_int64_t)D.d_bcount; 
212 #endif
213        return ret;
214 }
215
216 /*
217 ** Wrapper for the quotactl(GETQUOTA) call.
218 ** For API v2 the results are copied back into a v1 structure.
219 ** Taken from quota-1.4.8 perl module
220 */
221 static int get_linux_fs_quota( what, path, euser_id, dqb)
222 int what;
223 char *path;
224 uid_t euser_id;
225 struct dqblk *dqb;
226 {
227         int ret;
228
229         if (kernel_iface == IFACE_UNSET)
230                 linuxquota_get_api();
231
232         if (kernel_iface == IFACE_GENERIC)
233         {
234                 struct dqblk_v3 dqb3;
235
236                 ret = quotactl(QCMD(Q_V3_GETQUOTA, (what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t) &dqb3);
237                 if (ret == 0)
238                 {
239                         dqb->dqb_bhardlimit = dqb3.dqb_bhardlimit;
240                         dqb->dqb_bsoftlimit = dqb3.dqb_bsoftlimit;
241                         dqb->dqb_curblocks  = dqb3.dqb_curspace / DEV_QBSIZE;
242                         dqb->dqb_ihardlimit = dqb3.dqb_ihardlimit;
243                         dqb->dqb_isoftlimit = dqb3.dqb_isoftlimit;
244                         dqb->dqb_curinodes  = dqb3.dqb_curinodes;
245                         dqb->dqb_btime      = dqb3.dqb_btime;
246                         dqb->dqb_itime      = dqb3.dqb_itime;
247                         dqb->bsize          = DEV_QBSIZE;
248                 }
249         }
250         else if (kernel_iface == IFACE_VFSV0)
251         {
252                 struct dqblk_v2 dqb2;
253
254                 ret = quotactl(QCMD(Q_V2_GETQUOTA, (what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t) &dqb2);
255                 if (ret == 0)
256                 {
257                         dqb->dqb_bhardlimit = dqb2.dqb_bhardlimit;
258                         dqb->dqb_bsoftlimit = dqb2.dqb_bsoftlimit;
259                         dqb->dqb_curblocks  = dqb2.dqb_curspace / DEV_QBSIZE;
260                         dqb->dqb_ihardlimit = dqb2.dqb_ihardlimit;
261                         dqb->dqb_isoftlimit = dqb2.dqb_isoftlimit;
262                         dqb->dqb_curinodes  = dqb2.dqb_curinodes;
263                         dqb->dqb_btime      = dqb2.dqb_btime;
264                         dqb->dqb_itime      = dqb2.dqb_itime;
265                         dqb->bsize          = DEV_QBSIZE;
266                 }
267         }
268         else /* if (kernel_iface == IFACE_VFSOLD) */
269         {
270                 struct dqblk_v1 dqb1;
271
272                 ret = quotactl(QCMD(Q_V1_GETQUOTA, (what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t) &dqb1);
273                 if (ret == 0)
274                 {
275                         dqb->dqb_bhardlimit = dqb1.dqb_bhardlimit;
276                         dqb->dqb_bsoftlimit = dqb1.dqb_bsoftlimit;
277                         dqb->dqb_curblocks  = dqb1.dqb_curblocks;
278                         dqb->dqb_ihardlimit = dqb1.dqb_ihardlimit;
279                         dqb->dqb_isoftlimit = dqb1.dqb_isoftlimit;
280                         dqb->dqb_curinodes  = dqb1.dqb_curinodes;
281                         dqb->dqb_btime      = dqb1.dqb_btime;
282                         dqb->dqb_itime      = dqb1.dqb_itime;
283                         dqb->bsize          = DEV_QBSIZE;
284                 }
285         }
286         return ret;
287 }
288
289 #endif /* linux */
290
291 #if defined(HAVE_SYS_MNTTAB_H) || defined(__svr4__)
292 /*
293  * Return the mount point associated with the filesystem
294  * on which "file" resides.  Returns NULL on failure.
295  */
296 static char *
297 mountp( file, nfs)
298 char    *file;
299 int         *nfs;
300 {
301     struct stat                 sb;
302     FILE                        *mtab;
303     dev_t                       devno;
304     static struct mnttab        mnt;
305
306     if ( stat( file, &sb ) < 0 ) {
307         return( NULL );
308     }
309     devno = sb.st_dev;
310
311     if (( mtab = fopen( "/etc/mnttab", "r" )) == NULL ) {
312         return( NULL );
313     }
314
315     while ( getmntent( mtab, &mnt ) == 0 ) {
316         /* local fs */
317         if ( (stat( mnt.mnt_special, &sb ) == 0) && (devno == sb.st_rdev)) {
318             fclose( mtab );
319             return mnt.mnt_mountp;
320         }
321
322         /* check for nfs. we probably should use
323          * strcmp(mnt.mnt_fstype, MNTTYPE_NFS), but that's not as fast. */
324         if ((stat(mnt.mnt_mountp, &sb) == 0) && (devno == sb.st_dev) &&
325                 strchr(mnt.mnt_special, ':')) {
326             *nfs = 1;
327             fclose( mtab );
328             return mnt.mnt_special;
329         }
330     }
331
332     fclose( mtab );
333     return( NULL );
334 }
335
336 #else /* __svr4__ */
337 #ifdef ultrix
338 /*
339 * Return the block-special device name associated with the filesystem
340 * on which "file" resides.  Returns NULL on failure.
341 */
342
343 static char *
344 special( file, nfs )
345 char *file;
346 int  *nfs;
347 {
348     static struct fs_data       fsd;
349
350     if ( getmnt(0, &fsd, 0, STAT_ONE, file ) < 0 ) {
351         LOG(log_info, logtype_afpd, "special: getmnt %s: %s", file, strerror(errno) );
352         return( NULL );
353     }
354
355     /* XXX: does this really detect an nfs mounted fs? */
356     if (strchr(fsd.fd_req.devname, ':'))
357         *nfs = 1;
358     return( fsd.fd_req.devname );
359 }
360
361 #else /* ultrix */
362 #if (defined(HAVE_SYS_MOUNT_H) && !defined(__linux__)) || defined(BSD4_4) || defined(_IBMR2)
363
364 static char *
365 special( file, nfs )
366 char    *file;
367 int         *nfs;
368 {
369     static struct statfs        sfs;
370
371     if ( statfs( file, &sfs ) < 0 ) {
372         return( NULL );
373     }
374
375 #ifdef TRU64
376     /* Digital UNIX: The struct sfs contains a field sfs.f_type,
377      * the MOUNT_* constants are defined in <sys/mount.h> */
378     if ((sfs.f_type == MOUNT_NFS)||(sfs.f_type == MOUNT_NFS3))
379 #else /* TRU64 */
380     /* XXX: make sure this really detects an nfs mounted fs */
381     if (strchr(sfs.f_mntfromname, ':'))
382 #endif /* TRU64 */
383         *nfs = 1;
384     return( sfs.f_mntfromname );
385 }
386
387 #else /* BSD4_4 */
388
389 static char *
390 special( file, nfs )
391 char *file;
392 int *nfs;
393 {
394     struct stat         sb;
395     FILE                *mtab;
396     dev_t               devno;
397     struct mntent       *mnt;
398     int                 found=0;
399
400     if ( stat( file, &sb ) < 0 ) {
401         return( NULL );
402     }
403     devno = sb.st_dev;
404
405     if (( mtab = setmntent( "/etc/mtab", "r" )) == NULL ) {
406         return( NULL );
407     }
408
409     while (( mnt = getmntent( mtab )) != NULL ) {
410         /* check for local fs */
411         if ( (stat( mnt->mnt_fsname, &sb ) == 0) && devno == sb.st_rdev) {
412             found = 1;
413             break;
414         }
415
416         /* check for an nfs mount entry. the alternative is to use
417         * strcmp(mnt->mnt_type, MNTTYPE_NFS) instead of the strchr. */
418         if ((stat(mnt->mnt_dir, &sb) == 0) && (devno == sb.st_dev) &&
419                 strchr(mnt->mnt_fsname, ':')) {
420             *nfs = 1;
421             found = 1;
422             break;
423         }
424     }
425
426     endmntent( mtab );
427
428     if (!found)
429         return (NULL);
430 #ifdef linux
431     if (strcmp(mnt->mnt_type, "xfs") == 0)
432         is_xfs = 1;
433 #endif
434         
435     return( mnt->mnt_fsname );
436 }
437
438 #endif /* BSD4_4 */
439 #endif /* ultrix */
440 #endif /* __svr4__ */
441
442
443 static int getfsquota(vol, uid, dq)
444 struct vol              *vol;
445 const int               uid;
446 struct dqblk            *dq;
447
448 {
449         struct dqblk dqg;
450
451 #ifdef __svr4__
452     struct quotctl      qc;
453 #endif
454
455     memset(&dqg, 0, sizeof(dqg));
456         
457 #ifdef __svr4__
458     qc.op = Q_GETQUOTA;
459     qc.uid = uid;
460     qc.addr = (caddr_t)dq;
461     if ( ioctl( vol->v_qfd, Q_QUOTACTL, &qc ) < 0 ) {
462         return( AFPERR_PARAM );
463     }
464
465 #else /* __svr4__ */
466 #ifdef ultrix
467     if ( quota( Q_GETDLIM, uid, vol->v_gvs, dq ) != 0 ) {
468         return( AFPERR_PARAM );
469     }
470 #else /* ultrix */
471
472 #ifndef USRQUOTA
473 #define USRQUOTA   0
474 #endif
475
476 #ifndef QCMD
477 #define QCMD(a,b)  (a)
478 #endif
479
480 #ifndef TRU64
481     /* for group quotas. we only use these if the user belongs
482     * to one group. */
483 #endif /* TRU64 */
484
485 #ifdef BSD4_4
486     if ( seteuid( getuid() ) == 0 ) {
487         if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA,USRQUOTA),
488                        uid, (char *)dq ) != 0 ) {
489             /* try group quotas */
490             if (ngroups >= 1) {
491                 if ( quotactl(vol->v_path, QCMD(Q_GETQUOTA, GRPQUOTA),
492                               groups[0], (char *) &dqg) != 0 ) {
493                     seteuid( uid );
494                     return( AFPERR_PARAM );
495                 }
496             }
497         }
498         seteuid( uid );
499     }
500
501 #elif defined(TRU64)
502     if ( seteuid( getuid() ) == 0 ) {
503         if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA, USRQUOTA),
504                        uid, (char *)dq ) != 0 ) {
505             seteuid( uid );
506             return ( AFPERR_PARAM );
507         }
508         seteuid( uid );
509     }
510
511 #else /* BSD4_4 */
512     if (get_linux_quota (WANT_USER_QUOTA, vol->v_gvs, uid, dq) !=0) {
513         return( AFPERR_PARAM );
514     }
515
516     if (get_linux_quota(WANT_GROUP_QUOTA, vol->v_gvs, getegid(),  &dqg) != 0) {
517 #ifdef DEBUG_QUOTA
518         LOG(log_debug, logtype_afpd, "group quota did not work!" );
519 #endif /* DEBUG_QUOTA */
520
521         return AFP_OK; /* no need to check user vs group quota */
522     }
523 #endif  /* BSD4_4 */
524
525
526 #ifndef TRU64
527     /* return either the group quota entry or user quota entry,
528        whichever has the least amount of space remaining
529     */
530
531     /* if user space remaining > group space remaining */
532     if( 
533         /* if overquota, free space is 0 otherwise hard-current */
534         ( overquota( dq ) ? 0 : ( dq->dqb_bhardlimit ? dq->dqb_bhardlimit - 
535                                   dq->dqb_curblocks : ~((u_int64_t) 0) ) )
536
537       >
538         
539         ( overquota( &dqg ) ? 0 : ( dqg.dqb_bhardlimit ? dqg.dqb_bhardlimit - 
540                                     dqg.dqb_curblocks : ~((u_int64_t) 0) ) )
541
542       ) /* if */
543     {
544         /* use group quota limits rather than user limits */
545         dq->dqb_curblocks = dqg.dqb_curblocks;
546         dq->dqb_bhardlimit = dqg.dqb_bhardlimit;
547         dq->dqb_bsoftlimit = dqg.dqb_bsoftlimit;
548         dq->dqb_btimelimit = dqg.dqb_btimelimit;
549     } /* if */
550
551 #endif /* TRU64 */
552
553 #endif /* ultrix */
554 #endif /* __svr4__ */
555
556     return AFP_OK;
557 }
558
559
560 static int getquota( vol, dq, bsize)
561 struct vol              *vol;
562 struct dqblk            *dq;
563 const u_int32_t         bsize;
564 {
565     char *p;
566
567 #ifdef __svr4__
568     char                buf[ MAXPATHLEN + 1];
569
570     if ( vol->v_qfd == -1 && vol->v_gvs == NULL) {
571         if (( p = mountp( vol->v_path, &vol->v_nfs)) == NULL ) {
572             LOG(log_info, logtype_afpd, "getquota: mountp %s fails", vol->v_path );
573             return( AFPERR_PARAM );
574         }
575
576         if (vol->v_nfs) {
577             if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
578                 LOG(log_error, logtype_afpd, "getquota: malloc: %s", strerror(errno) );
579                 return AFPERR_MISC;
580             }
581             strcpy( vol->v_gvs, p );
582
583         } else {
584             sprintf( buf, "%s/quotas", p );
585             if (( vol->v_qfd = open( buf, O_RDONLY, 0 )) < 0 ) {
586                 LOG(log_info, logtype_afpd, "open %s: %s", buf, strerror(errno) );
587                 return( AFPERR_PARAM );
588             }
589         }
590
591     }
592 #else
593     if ( vol->v_gvs == NULL ) {
594         if (( p = special( vol->v_path, &vol->v_nfs )) == NULL ) {
595             LOG(log_info, logtype_afpd, "getquota: special %s fails", vol->v_path );
596             return( AFPERR_PARAM );
597         }
598
599         if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
600             LOG(log_error, logtype_afpd, "getquota: malloc: %s", strerror(errno) );
601             return AFPERR_MISC;
602         }
603         strcpy( vol->v_gvs, p );
604     }
605 #endif
606
607 #ifdef TRU64
608     /* Digital UNIX: Two forms of specifying an NFS filesystem are possible,
609        either 'hostname:path' or 'path@hostname' (Ultrix heritage) */
610     if (vol->v_nfs) {
611         char *hostpath;
612         char pathstring[MNAMELEN];
613         /* MNAMELEN ist defined in <sys/mount.h> */
614         int result;
615         
616         if ((hostpath = strchr(vol->v_gvs,'@')) != NULL ) {
617             /* convert 'path@hostname' to 'hostname:path',
618              * call getnfsquota(),
619              * convert 'hostname:path' back to 'path@hostname' */
620             *hostpath = '\0';
621             sprintf(pathstring,"%s:%s",hostpath+1,vol->v_gvs);
622             strcpy(vol->v_gvs,pathstring);
623             
624             result = getnfsquota(vol, uuid, bsize, dq);
625             
626             hostpath = strchr(vol->v_gvs,':');
627             *hostpath = '\0';
628             sprintf(pathstring,"%s@%s",hostpath+1,vol->v_gvs);
629             strcpy(vol->v_gvs,pathstring);
630             
631             return result;
632         }
633         else
634             /* vol->v_gvs is of the form 'hostname:path' */
635             return getnfsquota(vol, uuid, bsize, dq);
636     } else
637         /* local filesystem */
638         return getfsquota(vol, uuid, dq);
639            
640 #else /* TRU64 */
641     return vol->v_nfs ? getnfsquota(vol, uuid, bsize, dq) :
642            getfsquota(vol, uuid, dq);
643 #endif /* TRU64 */
644 }
645
646 static int overquota( dqblk )
647 struct dqblk      *dqblk;
648 {
649     struct timeval      tv;
650
651     if ( dqblk->dqb_curblocks > dqblk->dqb_bhardlimit &&
652          dqblk->dqb_bhardlimit != 0 ) {
653         return( 1 );
654     }
655
656     if ( dqblk->dqb_curblocks < dqblk->dqb_bsoftlimit ||
657          dqblk->dqb_bsoftlimit == 0 ) {
658         return( 0 );
659     }
660 #ifdef ultrix
661     if ( dqblk->dqb_bwarn ) {
662         return( 0 );
663     }
664 #else /* ultrix */
665     if ( gettimeofday( &tv, 0 ) < 0 ) {
666         LOG(log_error, logtype_afpd, "overquota: gettimeofday: %s", strerror(errno) );
667         return( AFPERR_PARAM );
668     }
669     if ( dqblk->dqb_btimelimit && dqblk->dqb_btimelimit > tv.tv_sec ) {
670         return( 0 );
671     }
672 #endif /* ultrix */
673     return( 1 );
674 }
675
676 /*
677  * This next bit is basically for linux -- everything is fine
678  * if you use 1k blocks... but if you try (for example) to mount
679  * a volume via nfs from a netapp (which might use 4k blocks) everything
680  * gets reported improperly.  I have no idea about dbtob on other
681  * platforms.
682  */
683
684 #ifdef HAVE_BROKEN_DBTOB
685 #undef dbtob
686 #define dbtob(a, b)     ((VolSpace)((VolSpace)(a) * (VolSpace)(b)))
687 #define HAVE_2ARG_DBTOB
688 #endif
689
690 #ifndef dbtob
691 #define dbtob(a)       ((a) << 10)
692 #endif
693
694 /* i do the cast to VolSpace here to make sure that 64-bit shifts
695    work */
696 #ifdef HAVE_2ARG_DBTOB
697 #define tobytes(a, b)  dbtob((VolSpace) (a), (VolSpace) (b))
698 #else 
699 #define tobytes(a, b)  dbtob((VolSpace) (a))
700 #endif
701
702 int uquota_getvolspace( vol, bfree, btotal, bsize)
703 struct vol      *vol;
704 VolSpace        *bfree, *btotal;
705 const u_int32_t bsize;
706 {
707         u_int64_t this_bsize;
708         struct dqblk dqblk;
709
710         this_bsize = bsize;
711                         
712         if (getquota( vol, &dqblk, bsize) != 0 ) {
713                 return( AFPERR_PARAM );
714         }
715
716 #ifdef linux
717         this_bsize = dqblk.bsize;
718 #endif
719
720 #ifdef DEBUG_QUOTA
721         LOG(log_info, logtype_afpd, "after calling getquota in uquota_getvolspace!" );
722         LOG(log_info, logtype_afpd, "dqb_ihardlimit: %u", dqblk.dqb_ihardlimit );
723         LOG(log_info, logtype_afpd, "dqb_isoftlimit: %u", dqblk.dqb_isoftlimit );
724         LOG(log_info, logtype_afpd, "dqb_curinodes : %u", dqblk.dqb_curinodes );
725         LOG(log_info, logtype_afpd, "dqb_bhardlimit: %u", dqblk.dqb_bhardlimit );
726         LOG(log_info, logtype_afpd, "dqb_bsoftlimit: %u", dqblk.dqb_bsoftlimit );
727         LOG(log_info, logtype_afpd, "dqb_curblocks : %u", dqblk.dqb_curblocks );
728         LOG(log_info, logtype_afpd, "dqb_btime     : %u", dqblk.dqb_btime );
729         LOG(log_info, logtype_afpd, "dqb_itime     : %u", dqblk.dqb_itime );
730         LOG(log_info, logtype_afpd, "bsize/this_bsize : %u/%u", bsize, this_bsize );
731         LOG(log_info, logtype_afpd, "dqblk.dqb_bhardlimit size: %u", tobytes( dqblk.dqb_bhardlimit, this_bsize ));
732         LOG(log_info, logtype_afpd, "dqblk.dqb_bsoftlimit size: %u", tobytes( dqblk.dqb_bsoftlimit, this_bsize ));
733         LOG(log_info, logtype_afpd, "dqblk.dqb_curblocks  size: %u", tobytes( dqblk.dqb_curblocks, this_bsize ));
734 #endif /* DEBUG_QUOTA */ 
735
736         /* no limit set for this user. it might be set in the future. */
737         if (dqblk.dqb_bsoftlimit == 0 && dqblk.dqb_bhardlimit == 0) {
738                 *btotal = *bfree = ~((VolSpace) 0);
739         } else if ( overquota( &dqblk )) {
740                 if ( tobytes( dqblk.dqb_curblocks, this_bsize ) > tobytes( dqblk.dqb_bsoftlimit, this_bsize ) ) {
741                         *btotal = tobytes( dqblk.dqb_curblocks, this_bsize );
742                         *bfree = 0;
743                 }
744                 else {
745                         *btotal = tobytes( dqblk.dqb_bsoftlimit, this_bsize );
746                         *bfree  = tobytes( dqblk.dqb_bsoftlimit, this_bsize ) -
747                                   tobytes( dqblk.dqb_curblocks, this_bsize );
748                 }
749         } else {
750                 *btotal = tobytes( dqblk.dqb_bhardlimit, this_bsize );
751                 *bfree  = tobytes( dqblk.dqb_bhardlimit, this_bsize  ) -
752                           tobytes( dqblk.dqb_curblocks, this_bsize );
753         }
754
755 #ifdef DEBUG_QUOTA
756         LOG(log_info, logtype_afpd, "bfree          : %u", *bfree );
757         LOG(log_info, logtype_afpd, "btotal         : %u", *btotal );
758         LOG(log_info, logtype_afpd, "bfree          : %uKB", *bfree/1024 );
759         LOG(log_info, logtype_afpd, "btotal         : %uKB", *btotal/1024 );
760 #endif
761
762         return( AFP_OK );
763 }
764 #endif