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