2 * $Id: quota.c,v 1.22.8.11.2.3 2006-09-11 08:05:02 didg Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
12 #ifndef NO_QUOTA_SUPPORT
17 #include <sys/types.h>
21 #else /* STDC_HEADERS */
25 #endif /* HAVE_STRCHR */
26 char *strchr (), *strrchr ();
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 */
34 #include <sys/param.h>
37 #endif /* HAVE_UNISTD_H */
40 #endif /* HAVE_FCNTL_H */
42 #include <atalk/logger.h>
43 #include <atalk/afp.h>
49 /* if you want to debug quota
52 #define WANT_USER_QUOTA 0
53 #define WANT_GROUP_QUOTA 1
55 #ifdef NEED_QUOTACTL_WRAPPER
56 int quotactl(int cmd, const char *special, int id, caddr_t addr)
58 return syscall(__NR_quotactl, cmd, special, id, addr);
60 #endif /* NEED_QUOTACTL_WRAPPER */
62 static int overquota( struct dqblk *);
66 #ifdef HAVE_LINUX_XQM_H
67 #include <linux/xqm.h>
71 #define HAVE_LINUX_XQM_H
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 */
80 #include <linux/unistd.h>
82 static int is_xfs = 0;
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 *);
87 /* format supported by current kernel */
88 static int kernel_iface = IFACE_UNSET;
91 ** Check kernel quota version
92 ** Taken from quota-tools 3.08 by Jan Kara <jack@suse.cz>
94 static void linuxquota_get_api( void )
96 #ifndef LINUX_API_VERSION
99 if (stat("/proc/sys/fs/quota", &st) == 0) {
100 kernel_iface = IFACE_GENERIC;
103 struct dqstats_v2 v2_stats;
104 struct sigaction sig;
105 struct sigaction oldsig;
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;
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));
116 if (quotactl(QCMD(Q_V2_GETSTATS, 0), NULL, 0, (void *)&v2_stats) >= 0) {
117 kernel_iface = IFACE_VFSV0;
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) */
125 char tmp[1024]; /* Just temporary buffer */
127 if (quotactl(QCMD(Q_V1_GETSTATS, 0), NULL, 0, tmp))
129 if (quotactl(QCMD(Q_V1_GETQUOTA, 0), "/dev/null", 0, tmp))
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;
139 kernel_iface = IFACE_VFSOLD;
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;
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;
159 if (sigaction(SIGSEGV, &oldsig, NULL) < 0) {
160 LOG(log_error, logtype_afpd, "cannot reset signal handler: %s", strerror(errno));
166 if (kernel_iface == IFACE_UNSET)
167 kernel_iface = IFACE_VFSOLD;
169 #else /* defined LINUX_API_VERSION */
170 kernel_iface = LINUX_API_VERSION;
174 /****************************************************************************/
176 static int get_linux_quota(int what, char *path, uid_t euser_id, struct dqblk *dp)
181 r=get_linux_xfs_quota(what, path, euser_id, dp);
183 r=get_linux_fs_quota(what, path, euser_id, dp);
188 /****************************************************************************
189 Abstract out the XFS Quota Manager quota get call.
190 ****************************************************************************/
192 static int get_linux_xfs_quota(what, path, euser_id, dqb)
199 #ifdef HAVE_LINUX_XQM_H
200 struct fs_disk_quota D;
202 memset (&D, 0, sizeof(D));
204 if ((ret = quotactl(QCMD(Q_XGETQUOTA,(what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t)&D)))
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;
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
223 static int get_linux_fs_quota( what, path, euser_id, dqb)
231 if (kernel_iface == IFACE_UNSET)
232 linuxquota_get_api();
234 if (kernel_iface == IFACE_GENERIC)
236 struct dqblk_v3 dqb3;
238 ret = quotactl(QCMD(Q_V3_GETQUOTA, (what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t) &dqb3);
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;
252 else if (kernel_iface == IFACE_VFSV0)
254 struct dqblk_v2 dqb2;
256 ret = quotactl(QCMD(Q_V2_GETQUOTA, (what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t) &dqb2);
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;
270 else /* if (kernel_iface == IFACE_VFSOLD) */
272 struct dqblk_v1 dqb1;
274 ret = quotactl(QCMD(Q_V1_GETQUOTA, (what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t) &dqb1);
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;
293 #if defined(HAVE_SYS_MNTTAB_H) || defined(__svr4__)
295 * Return the mount point associated with the filesystem
296 * on which "file" resides. Returns NULL on failure.
306 static struct mnttab mnt;
308 if ( stat( file, &sb ) < 0 ) {
313 if (( mtab = fopen( "/etc/mnttab", "r" )) == NULL ) {
317 while ( getmntent( mtab, &mnt ) == 0 ) {
319 if ( (stat( mnt.mnt_special, &sb ) == 0) && (devno == sb.st_rdev)) {
321 return mnt.mnt_mountp;
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, ':')) {
330 return mnt.mnt_special;
341 * Return the block-special device name associated with the filesystem
342 * on which "file" resides. Returns NULL on failure.
350 static struct fs_data fsd;
352 if ( getmnt(0, &fsd, 0, STAT_ONE, file ) < 0 ) {
353 LOG(log_info, logtype_afpd, "special: getmnt %s: %s", file, strerror(errno) );
357 /* XXX: does this really detect an nfs mounted fs? */
358 if (strchr(fsd.fd_req.devname, ':'))
360 return( fsd.fd_req.devname );
364 #if (defined(HAVE_SYS_MOUNT_H) && !defined(__linux__)) || defined(BSD4_4) || defined(_IBMR2)
371 static struct statfs sfs;
373 if ( statfs( file, &sfs ) < 0 ) {
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))
382 /* XXX: make sure this really detects an nfs mounted fs */
383 if (strchr(sfs.f_mntfromname, ':'))
386 return( sfs.f_mntfromname );
402 if ( stat( file, &sb ) < 0 ) {
407 if (( mtab = setmntent( "/etc/mtab", "r" )) == NULL ) {
411 while (( mnt = getmntent( mtab )) != NULL ) {
412 /* check for local fs */
413 if ( (stat( mnt->mnt_fsname, &sb ) == 0) && devno == sb.st_rdev) {
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, ':')) {
433 if (strcmp(mnt->mnt_type, "xfs") == 0)
437 return( mnt->mnt_fsname );
442 #endif /* __svr4__ */
445 static int getfsquota(vol, uid, dq)
457 memset(&dqg, 0, sizeof(dqg));
462 qc.addr = (caddr_t)dq;
463 if ( ioctl( vol->v_qfd, Q_QUOTACTL, &qc ) < 0 ) {
464 return( AFPERR_PARAM );
469 if ( quota( Q_GETDLIM, uid, vol->v_gvs, dq ) != 0 ) {
470 return( AFPERR_PARAM );
479 #define QCMD(a,b) (a)
483 /* for group quotas. we only use these if the user belongs
488 if ( seteuid( getuid() ) == 0 ) {
489 if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA,USRQUOTA),
490 uid, (char *)dq ) != 0 ) {
491 /* try group quotas */
493 if ( quotactl(vol->v_path, QCMD(Q_GETQUOTA, GRPQUOTA),
494 groups[0], (char *) &dqg) != 0 ) {
496 return( AFPERR_PARAM );
504 if ( seteuid( getuid() ) == 0 ) {
505 if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA, USRQUOTA),
506 uid, (char *)dq ) != 0 ) {
508 return ( AFPERR_PARAM );
514 if (get_linux_quota (WANT_USER_QUOTA, vol->v_gvs, uid, dq) !=0) {
515 return( AFPERR_PARAM );
518 if (get_linux_quota(WANT_GROUP_QUOTA, vol->v_gvs, getegid(), &dqg) != 0) {
520 LOG(log_debug, logtype_afpd, "group quota did not work!" );
521 #endif /* DEBUG_QUOTA */
523 return AFP_OK; /* no need to check user vs group quota */
529 /* return either the group quota entry or user quota entry,
530 whichever has the least amount of space remaining
533 /* if user space remaining > group space remaining */
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) ) )
541 ( overquota( &dqg ) ? 0 : ( dqg.dqb_bhardlimit ? dqg.dqb_bhardlimit -
542 dqg.dqb_curblocks : ~((u_int64_t) 0) ) )
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;
556 #endif /* __svr4__ */
562 static int getquota( vol, dq, bsize)
565 const u_int32_t bsize;
570 char buf[ MAXPATHLEN + 1];
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 );
579 if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
580 LOG(log_error, logtype_afpd, "getquota: malloc: %s", strerror(errno) );
583 strcpy( vol->v_gvs, p );
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 );
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 );
601 if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
602 LOG(log_error, logtype_afpd, "getquota: malloc: %s", strerror(errno) );
605 strcpy( vol->v_gvs, p );
610 /* Digital UNIX: Two forms of specifying an NFS filesystem are possible,
611 either 'hostname:path' or 'path@hostname' (Ultrix heritage) */
614 char pathstring[MNAMELEN];
615 /* MNAMELEN ist defined in <sys/mount.h> */
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' */
623 sprintf(pathstring,"%s:%s",hostpath+1,vol->v_gvs);
624 strcpy(vol->v_gvs,pathstring);
626 result = getnfsquota(vol, uuid, bsize, dq);
628 hostpath = strchr(vol->v_gvs,':');
630 sprintf(pathstring,"%s@%s",hostpath+1,vol->v_gvs);
631 strcpy(vol->v_gvs,pathstring);
636 /* vol->v_gvs is of the form 'hostname:path' */
637 return getnfsquota(vol, uuid, bsize, dq);
639 /* local filesystem */
640 return getfsquota(vol, uuid, dq);
643 return vol->v_nfs ? getnfsquota(vol, uuid, bsize, dq) :
644 getfsquota(vol, uuid, dq);
648 static int overquota( dqblk )
653 if ( dqblk->dqb_curblocks > dqblk->dqb_bhardlimit &&
654 dqblk->dqb_bhardlimit != 0 ) {
658 if ( dqblk->dqb_curblocks < dqblk->dqb_bsoftlimit ||
659 dqblk->dqb_bsoftlimit == 0 ) {
663 if ( dqblk->dqb_bwarn ) {
667 if ( gettimeofday( &tv, 0 ) < 0 ) {
668 LOG(log_error, logtype_afpd, "overquota: gettimeofday: %s", strerror(errno) );
669 return( AFPERR_PARAM );
671 if ( dqblk->dqb_btimelimit && dqblk->dqb_btimelimit > tv.tv_sec ) {
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
686 #ifdef HAVE_BROKEN_DBTOB
688 #define dbtob(a, b) ((VolSpace)((VolSpace)(a) * (VolSpace)(b)))
689 #define HAVE_2ARG_DBTOB
693 #define dbtob(a) ((a) << 10)
696 /* i do the cast to VolSpace here to make sure that 64-bit shifts
698 #ifdef HAVE_2ARG_DBTOB
699 #define tobytes(a, b) dbtob((VolSpace) (a), (VolSpace) (b))
701 #define tobytes(a, b) dbtob((VolSpace) (a))
704 int uquota_getvolspace( vol, bfree, btotal, bsize)
706 VolSpace *bfree, *btotal;
707 const u_int32_t bsize;
709 u_int64_t this_bsize;
714 if (getquota( vol, &dqblk, bsize) != 0 ) {
715 return( AFPERR_PARAM );
719 this_bsize = dqblk.bsize;
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 */
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 );
747 *btotal = tobytes( dqblk.dqb_bsoftlimit, this_bsize );
748 *bfree = tobytes( dqblk.dqb_bsoftlimit, this_bsize ) -
749 tobytes( dqblk.dqb_curblocks, this_bsize );
752 *btotal = tobytes( dqblk.dqb_bhardlimit, this_bsize );
753 *bfree = tobytes( dqblk.dqb_bhardlimit, this_bsize ) -
754 tobytes( dqblk.dqb_curblocks, this_bsize );
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 );