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