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