/*
- * $Id: quota.c,v 1.22 2002-08-29 17:22:06 jmarcus Exp $
+ * $Id: quota.c,v 1.22.8.1 2003-09-09 18:18:54 bfernhomberg Exp $
*
* Copyright (c) 1990,1993 Regents of The University of Michigan.
* All Rights Reserved. See COPYRIGHT.
#include <stdlib.h>
#include <string.h>
#include <errno.h>
-
+#include <sys/types.h>
/* STDC check */
#if STDC_HEADERS
#include <string.h>
#include "volume.h"
#include "unix.h"
+#define DEBUG_QUOTA 1
+#define WANT_USER_QUOTA 0
+#define WANT_GROUP_QUOTA 1
+
#ifndef NO_QUOTA_SUPPORT
#ifdef NEED_QUOTACTL_WRAPPER
int quotactl(int cmd, const char *special, int id, caddr_t addr)
}
#endif /* NEED_QUOTACTL_WRAPPER */
+#ifdef linux
+
+#ifdef HAVE_LINUX_XQM_H
+#include <linux/xqm.h>
+#else
+#ifdef HAVE_XFS_XQM_H
+#include <xfs/xqm.h>
+#define HAVE_LINUX_XQM_H
+#endif /* HAVE_XFS_XQM_H */
+#endif /* HAVE_LINUX_XQM_H */
+
+#include <linux/unistd.h>
+
+static int is_xfs = 0;
+
+static int get_linux_xfs_quota(int, char*, uid_t, struct dqblk *);
+static int get_linux_fs_quota(int, char*, uid_t, struct dqblk *);
+
+/* format supported by current kernel */
+static int kernel_iface = IFACE_UNSET;
+
+/*
+** Check kernel quota version
+** Taken from quota-tools 3.08 by Jan Kara <jack@suse.cz>
+*/
+static void linuxquota_get_api( void )
+{
+#ifndef LINUX_API_VERSION
+ struct stat st;
+
+ if (stat("/proc/sys/fs/quota", &st) == 0) {
+ kernel_iface = IFACE_GENERIC;
+ }
+ else {
+ struct dqstats_v2 v2_stats;
+ struct sigaction sig;
+ struct sigaction oldsig;
+
+ /* This signal handling is needed because old kernels send us SIGSEGV as they try to resolve the device */
+ sig.sa_handler = SIG_IGN;
+ sig.sa_sigaction = NULL;
+ sig.sa_flags = 0;
+ sigemptyset(&sig.sa_mask);
+ if (sigaction(SIGSEGV, &sig, &oldsig) < 0) {
+ LOG( log_error, logtype_afpd, "cannot set SEGV signal handler: %s\n", strerror(errno));
+ goto failure;
+ }
+ if (quotactl(QCMD(Q_V2_GETSTATS, 0), NULL, 0, (void *)&v2_stats) >= 0) {
+ kernel_iface = IFACE_VFSV0;
+ }
+ else if (errno != ENOSYS && errno != ENOTSUP) {
+ /* RedHat 7.1 (2.4.2-2) newquota check
+ * Q_V2_GETSTATS in it's old place, Q_GETQUOTA in the new place
+ * (they haven't moved Q_GETSTATS to its new value) */
+ int err_stat = 0;
+ int err_quota = 0;
+ char tmp[1024]; /* Just temporary buffer */
+
+ if (quotactl(QCMD(Q_V1_GETSTATS, 0), NULL, 0, tmp))
+ err_stat = errno;
+ if (quotactl(QCMD(Q_V1_GETQUOTA, 0), "/dev/null", 0, tmp))
+ err_quota = errno;
+
+ /* On a RedHat 2.4.2-2 we expect 0, EINVAL
+ * On a 2.4.x we expect 0, ENOENT
+ * On a 2.4.x-ac we wont get here */
+ if (err_stat == 0 && err_quota == EINVAL) {
+ kernel_iface = IFACE_VFSV0;
+ }
+ else {
+ kernel_iface = IFACE_VFSOLD;
+ }
+ }
+ else {
+ /* This branch is *not* in quota-tools 3.08
+ ** but without it quota version is not correctly
+ ** identified for the original SuSE 8.0 kernel */
+ unsigned int vers_no;
+ FILE * qf;
+
+ if ((qf = fopen("/proc/fs/quota", "r"))) {
+ if (fscanf(qf, "Version %u", &vers_no) == 1) {
+ if ( (vers_no == (6*10000 + 5*100 + 0)) ||
+ (vers_no == (6*10000 + 5*100 + 1)) ) {
+ kernel_iface = IFACE_VFSV0;
+ }
+ }
+ fclose(qf);
+ }
+ }
+ if (sigaction(SIGSEGV, &oldsig, NULL) < 0) {
+ LOG(log_error, logtype_afpd, "cannot reset signal handler: %s\n", strerror(errno));
+ goto failure;
+ }
+ }
+
+failure:
+ if (kernel_iface == IFACE_UNSET)
+ kernel_iface = IFACE_VFSOLD;
+
+#else /* defined LINUX_API_VERSION */
+ kernel_iface = LINUX_API_VERSION;
+#endif
+}
+
+/****************************************************************************/
+
+static int get_linux_quota(int what, char *path, uid_t euser_id, struct dqblk *dp)
+{
+ int r; /* result */
+
+ if ( is_xfs )
+ r=get_linux_xfs_quota(what, path, euser_id, dp);
+ else
+ r=get_linux_fs_quota(what, path, euser_id, dp);
+
+ return r;
+}
+
+/****************************************************************************
+ Abstract out the XFS Quota Manager quota get call.
+****************************************************************************/
+
+static int get_linux_xfs_quota(what, path, euser_id, dp)
+int what;
+char *path;
+uid_t euser_id;
+struct dqblk *dp;
+{
+ int ret = -1;
+#ifdef HAVE_LINUX_XQM_H
+ struct fs_disk_quota D;
+
+ memset (&D, 0, sizeof(fs_disk_quota));
+
+ if ((ret = quotactl(QCMD(Q_XGETQUOTA,USRQUOTA), path, euser_id, (caddr_t)&D)))
+ return ret;
+
+ dp->bsize = (u_int64_t)512;
+ dp->softlimit = (u_int64_t)D.d_blk_softlimit;
+ dp->hardlimit = (u_int64_t)D.d_blk_hardlimit;
+ dp->ihardlimit = (u_int64_t)D.d_ino_hardlimit;
+ dp->isoftlimit = (u_int64_t)D.d_ino_softlimit;
+ dp->curinodes = (u_int64_t)D.d_icount;
+ dp->curblocks = (u_int64_t)D.d_bcount;
+#endif
+ return ret;
+}
+
+/*
+** Wrapper for the quotactl(GETQUOTA) call.
+** For API v2 the results are copied back into a v1 structure.
+** Taken from quota-1.4.8 perl module
+*/
+static int get_linux_fs_quota( what, path, euser_id, dqb)
+int what;
+char *path;
+uid_t euser_id;
+struct dqblk *dqb;
+{
+ int ret;
+
+ if (kernel_iface == IFACE_UNSET)
+ linuxquota_get_api();
+
+ if (kernel_iface == IFACE_GENERIC)
+ {
+ struct dqblk_v3 dqb3;
+
+ ret = quotactl(QCMD(Q_V3_GETQUOTA, (what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t) &dqb3);
+ if (ret == 0)
+ {
+ dqb->dqb_bhardlimit = dqb3.dqb_bhardlimit;
+ dqb->dqb_bsoftlimit = dqb3.dqb_bsoftlimit;
+ dqb->dqb_curblocks = dqb3.dqb_curspace / DEV_QBSIZE;
+ dqb->dqb_ihardlimit = dqb3.dqb_ihardlimit;
+ dqb->dqb_isoftlimit = dqb3.dqb_isoftlimit;
+ dqb->dqb_curinodes = dqb3.dqb_curinodes;
+ dqb->dqb_btime = dqb3.dqb_btime;
+ dqb->dqb_itime = dqb3.dqb_itime;
+ dqb->bsize = DEV_QBSIZE;
+ }
+ }
+ else if (kernel_iface == IFACE_VFSV0)
+ {
+ struct dqblk_v2 dqb2;
+
+ ret = quotactl(QCMD(Q_V2_GETQUOTA, (what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t) &dqb2);
+ if (ret == 0)
+ {
+ dqb->dqb_bhardlimit = dqb2.dqb_bhardlimit;
+ dqb->dqb_bsoftlimit = dqb2.dqb_bsoftlimit;
+ dqb->dqb_curblocks = dqb2.dqb_curspace / DEV_QBSIZE;
+ dqb->dqb_ihardlimit = dqb2.dqb_ihardlimit;
+ dqb->dqb_isoftlimit = dqb2.dqb_isoftlimit;
+ dqb->dqb_curinodes = dqb2.dqb_curinodes;
+ dqb->dqb_btime = dqb2.dqb_btime;
+ dqb->dqb_itime = dqb2.dqb_itime;
+ dqb->bsize = DEV_QBSIZE;
+ }
+ }
+ else /* if (kernel_iface == IFACE_VFSOLD) */
+ {
+ struct dqblk_v1 dqb1;
+
+ ret = quotactl(QCMD(Q_V1_GETQUOTA, (what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t) &dqb1);
+ if (ret == 0)
+ {
+ dqb->dqb_bhardlimit = dqb1.dqb_bhardlimit;
+ dqb->dqb_bsoftlimit = dqb1.dqb_bsoftlimit;
+ dqb->dqb_curblocks = dqb1.dqb_curblocks;
+ dqb->dqb_ihardlimit = dqb1.dqb_ihardlimit;
+ dqb->dqb_isoftlimit = dqb1.dqb_isoftlimit;
+ dqb->dqb_curinodes = dqb1.dqb_curinodes;
+ dqb->dqb_btime = dqb1.dqb_btime;
+ dqb->dqb_itime = dqb1.dqb_itime;
+ dqb->bsize = DEV_QBSIZE;
+ }
+ }
+ return ret;
+}
+
+#endif /* linux */
#if defined(HAVE_SYS_MNTTAB_H) || defined(__svr4__)
/*
FILE *mtab;
dev_t devno;
struct mntent *mnt;
+ int found=0;
if ( stat( file, &sb ) < 0 ) {
return( NULL );
while (( mnt = getmntent( mtab )) != NULL ) {
/* check for local fs */
if ( (stat( mnt->mnt_fsname, &sb ) == 0) && devno == sb.st_rdev) {
- endmntent( mtab );
- return( mnt->mnt_fsname );
+ found = 1;
+ break;
}
/* check for an nfs mount entry. the alternative is to use
if ((stat(mnt->mnt_dir, &sb) == 0) && (devno == sb.st_dev) &&
strchr(mnt->mnt_fsname, ':')) {
*nfs = 1;
- endmntent( mtab );
- return( mnt->mnt_fsname );
+ found = 1;
+ break;
}
}
endmntent( mtab );
- return( NULL );
+
+ if (!found)
+ return (NULL);
+#ifdef linux
+ if (strcmp(mnt->mnt_type, "xfs") == 0)
+ is_xfs = 1;
+#endif
+
+ return( mnt->mnt_fsname );
}
#endif /* BSD4_4 */
static int getfsquota(vol, uid, dq)
-struct vol *vol;
-const int uid;
-struct dqblk *dq;
+struct vol *vol;
+const int uid;
+struct dqblk *dq;
+
{
+ struct dqblk dqg;
+
+ memset(&dqg, 0, sizeof(dqg));
+
#ifdef __svr4__
struct quotctl qc;
#ifndef TRU64
/* for group quotas. we only use these if the user belongs
* to one group. */
- struct dqblk dqg;
-
- memset(&dqg, 0, sizeof(dqg));
#endif /* TRU64 */
#ifdef BSD4_4
}
#else /* BSD4_4 */
- if ( quotactl(QCMD(Q_GETQUOTA, USRQUOTA), vol->v_gvs, uid,
- (caddr_t) dq ) != 0 ) {
- return( AFPERR_PARAM );
- }
-
- if (ngroups >= 1)
- quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), vol->v_gvs,
- groups[0], (char *) &dqg);
+ if (get_linux_quota (WANT_USER_QUOTA, vol->v_gvs, uid, dq) !=0) {
+ return( AFPERR_PARAM );
+ }
+
+ if (ngroups >= 1)
+ {
+ if (get_linux_quota(WANT_GROUP_QUOTA, vol->v_gvs, groups[0], &dqg) != 0)
+ LOG(log_info, logtype_afpd, "group quota did not work!" );
+ }
#endif /* BSD4_4 */
#ifndef TRU64
/* max(user blocks, group blocks) */
if (dqg.dqb_curblocks && (dq->dqb_curblocks < dqg.dqb_curblocks))
- dq->dqb_curblocks = dqg.dqb_curblocks;
-
+ dq->dqb_curblocks = dqg.dqb_curblocks;
/* min(user limit, group limit) */
if (dqg.dqb_bhardlimit && (!dq->dqb_bhardlimit ||
(dq->dqb_bhardlimit > dqg.dqb_bhardlimit)))
static int getquota( vol, dq, bsize)
struct vol *vol;
-struct dqblk *dq;
-const u_int32_t bsize;
+struct dqblk *dq;
+const u_int32_t bsize;
{
char *p;
}
static int overquota( dqblk )
-struct dqblk *dqblk;
+struct dqblk *dqblk;
{
struct timeval tv;
#else
#define tobytes(a, b) dbtob((VolSpace) (a))
#endif
+
int uquota_getvolspace( vol, bfree, btotal, bsize)
const struct vol *vol;
VolSpace *bfree, *btotal;
const u_int32_t bsize;
{
- struct dqblk dqblk;
+ u_int64_t this_bsize;
+ struct dqblk dqblk;
- if (getquota( vol, &dqblk, bsize) != 0 ) {
- return( AFPERR_PARAM );
- }
+ this_bsize = bsize;
+
+ if (getquota( vol, &dqblk, bsize) != 0 ) {
+ return( AFPERR_PARAM );
+ }
- /* no limit set for this user. it might be set in the future. */
- if (dqblk.dqb_bsoftlimit == 0 && dqblk.dqb_bhardlimit == 0) {
- *btotal = *bfree = ~((VolSpace) 0);
- } else if ( overquota( &dqblk )) {
- if ( tobytes( dqblk.dqb_curblocks, bsize ) > tobytes( dqblk.dqb_bhardlimit, bsize ) ) {
- *btotal = tobytes( dqblk.dqb_curblocks, bsize );
- *bfree = 0;
- }
- else {
- *btotal = tobytes( dqblk.dqb_bhardlimit, bsize );
- *bfree = tobytes( dqblk.dqb_bhardlimit, bsize ) -
- tobytes( dqblk.dqb_curblocks, bsize );
- }
- } else {
- *btotal = tobytes( dqblk.dqb_bsoftlimit, bsize );
- *bfree = tobytes( dqblk.dqb_bsoftlimit, bsize ) -
- tobytes( dqblk.dqb_curblocks, bsize );
- }
+#ifdef linux
+ this_bsize = dqblk.bsize;
+#endif
+
+#if DEBUG_QUOTA
+ LOG(log_info, logtype_afpd, "after calling getquota in uquota_getvolspace!" );
+ LOG(log_info, logtype_afpd, "dqb_ihardlimit: %u", dqblk.dqb_ihardlimit );
+ LOG(log_info, logtype_afpd, "dqb_isoftlimit: %u", dqblk.dqb_isoftlimit );
+ LOG(log_info, logtype_afpd, "dqb_curinodes : %u", dqblk.dqb_curinodes );
+ LOG(log_info, logtype_afpd, "dqb_bhardlimit: %u", dqblk.dqb_bhardlimit );
+ LOG(log_info, logtype_afpd, "dqb_bsoftlimit: %u", dqblk.dqb_bsoftlimit );
+ LOG(log_info, logtype_afpd, "dqb_curblocks : %u", dqblk.dqb_curblocks );
+ LOG(log_info, logtype_afpd, "dqb_btime : %u", dqblk.dqb_btime );
+ LOG(log_info, logtype_afpd, "dqb_itime : %u", dqblk.dqb_itime );
+ LOG(log_info, logtype_afpd, "bsize/this_bsize : %u/%u", bsize, this_bsize );
+ LOG(log_info, logtype_afpd, "dqblk.dqb_bhardlimit size: %u", tobytes( dqblk.dqb_bhardlimit, this_bsize ));
+ LOG(log_info, logtype_afpd, "dqblk.dqb_bsoftlimit size: %u", tobytes( dqblk.dqb_bsoftlimit, this_bsize ));
+ LOG(log_info, logtype_afpd, "dqblk.dqb_curblocks size: %u", tobytes( dqblk.dqb_curblocks, this_bsize ));
+#endif /* DEBUG_QUOTA */
+
+ /* no limit set for this user. it might be set in the future. */
+ if (dqblk.dqb_bsoftlimit == 0 && dqblk.dqb_bhardlimit == 0) {
+ *btotal = *bfree = ~((VolSpace) 0);
+ } else if ( overquota( &dqblk )) {
+ if ( tobytes( dqblk.dqb_curblocks, this_bsize ) > tobytes( dqblk.dqb_bhardlimit, this_bsize ) ) {
+ *btotal = tobytes( dqblk.dqb_curblocks, this_bsize );
+ *bfree = 0;
+ }
+ else {
+ *btotal = tobytes( dqblk.dqb_bhardlimit, this_bsize );
+ *bfree = tobytes( dqblk.dqb_bhardlimit, this_bsize ) -
+ tobytes( dqblk.dqb_curblocks, this_bsize );
+ }
+ } else {
+ *btotal = tobytes( dqblk.dqb_bsoftlimit, this_bsize );
+ *bfree = tobytes( dqblk.dqb_bsoftlimit, this_bsize ) -
+ tobytes( dqblk.dqb_curblocks, this_bsize );
+ }
+
+#if DEBUG_QUOTA
+ LOG(log_info, logtype_afpd, "bfree : %u", *bfree );
+ LOG(log_info, logtype_afpd, "btotal : %u", *btotal );
+ LOG(log_info, logtype_afpd, "bfree : %uKB", *bfree/1024 );
+ LOG(log_info, logtype_afpd, "btotal : %uKB", *btotal/1024 );
+#endif
- return( AFP_OK );
+ return( AFP_OK );
}
#endif
/*
- * $Id: unix.h,v 1.12.2.1 2003-07-21 05:50:54 didg Exp $
+ * $Id: unix.h,v 1.12.2.1.2.1 2003-09-09 18:18:54 bfernhomberg Exp $
*/
#ifndef AFPD_UNIX_H
#include <sys/cdefs.h>
#endif /* HAVE_SYS_CDEFS_H */
#include <netatalk/endian.h>
+#include "config.h"
#include "volume.h"
#if defined( sun ) && !defined( __svr4__ )
#include <sys/mnttab.h>
#endif /* __svr4__ || HAVE_SYS_MNTTAB_H */
+
+
#if defined(HAVE_SYS_MOUNT_H) || defined(BSD4_4) || \
defined(linux) || defined(ultrix)
#include <sys/mount.h>
#if defined(linux) || defined(ultrix) || defined(HAVE_QUOTA_H)
#ifndef NEED_QUOTACTL_WRAPPER
-#include <sys/quota.h>
+/*#include <sys/quota.h>*/
+/*long quotactl __P((int, const char *, unsigned int, caddr_t)); */
+/* extern long quotactl __P((int, const char *, long, caddr_t)); */
+
#else /* ! NEED_QUOTACTL_WRAPPER */
#include <asm/types.h>
#include <asm/unistd.h>
#include <sys/stat.h>
#include "directory.h"
+
+#if defined (linux)
+
+#define MAXQUOTAS 2
+
+/* definitions from sys/quota.h */
+#define USRQUOTA 0 /* element used for user quotas */
+#define GRPQUOTA 1 /* element used for group quotas */
+
+/*
+ * Command definitions for the 'quotactl' system call.
+ * The commands are broken into a main command defined below
+ * and a subcommand that is used to convey the type of
+ * quota that is being manipulated (see above).
+ */
+#define SUBCMDMASK 0x00ff
+#define SUBCMDSHIFT 8
+#define QCMD(cmd, type) (((cmd) << SUBCMDSHIFT) | ((type) & SUBCMDMASK))
+
+/* declare an internal version of the quota block struct */
+typedef u_int64_t qsize_t; /* Type in which we store size limitations */
+typedef u_int32_t qid_t; /* Type in which we store ids in memory */
+
+struct dqblk {
+ qsize_t bsize;
+ qsize_t dqb_ihardlimit; /* absolute limit on allocated inodes */
+ qsize_t dqb_isoftlimit; /* preferred inode limit */
+ qsize_t dqb_curinodes; /* current # allocated inodes */
+ qsize_t dqb_bhardlimit; /* absolute limit on disk blks alloc */
+ qsize_t dqb_bsoftlimit; /* preferred limit on disk blks */
+ qsize_t dqb_curblocks; /* current block count */
+ time_t dqb_btime; /* time limit for excessive disk use */
+ time_t dqb_itime; /* time limit for excessive inode use */
+};
+
+/* API v1 command definitions */
+#define Q_V1_GETQUOTA 0x0300
+#define Q_V1_SYNC 0x0600
+#define Q_V1_SETQLIM 0x0700
+#define Q_V1_GETSTATS 0x0800
+/* API v2 command definitions */
+#define Q_V2_SYNC 0x0600
+#define Q_V2_SETQLIM 0x0700
+#define Q_V2_GETQUOTA 0x0D00
+#define Q_V2_GETSTATS 0x1100
+/* proc API command definitions */
+#define Q_V3_SYNC 0x800001
+#define Q_V3_GETQUOTA 0x800007
+#define Q_V3_SETQUOTA 0x800008
+
+/* Interface versions */
+#define IFACE_UNSET 0
+#define IFACE_VFSOLD 1
+#define IFACE_VFSV0 2
+#define IFACE_GENERIC 3
+
+#define DEV_QBSIZE 1024
+
+struct dqblk_v3 {
+ u_int64_t dqb_bhardlimit;
+ u_int64_t dqb_bsoftlimit;
+ u_int64_t dqb_curspace;
+ u_int64_t dqb_ihardlimit;
+ u_int64_t dqb_isoftlimit;
+ u_int64_t dqb_curinodes;
+ u_int64_t dqb_btime;
+ u_int64_t dqb_itime;
+ u_int32_t dqb_valid;
+};
+
+struct dqblk_v2 {
+ unsigned int dqb_ihardlimit;
+ unsigned int dqb_isoftlimit;
+ unsigned int dqb_curinodes;
+ unsigned int dqb_bhardlimit;
+ unsigned int dqb_bsoftlimit;
+ qsize_t dqb_curspace;
+ time_t dqb_btime;
+ time_t dqb_itime;
+};
+
+struct dqstats_v2 {
+ u_int32_t lookups;
+ u_int32_t drops;
+ u_int32_t reads;
+ u_int32_t writes;
+ u_int32_t cache_hits;
+ u_int32_t allocated_dquots;
+ u_int32_t free_dquots;
+ u_int32_t syncs;
+ u_int32_t version;
+};
+
+struct dqblk_v1 {
+ u_int32_t dqb_bhardlimit;
+ u_int32_t dqb_bsoftlimit;
+ u_int32_t dqb_curblocks;
+ u_int32_t dqb_ihardlimit;
+ u_int32_t dqb_isoftlimit;
+ u_int32_t dqb_curinodes;
+ time_t dqb_btime;
+ time_t dqb_itime;
+};
+
+extern long quotactl __P ((unsigned int, const char *, int, caddr_t));
+
+
+
+#endif /* linux */
+
extern int getnfsquota __P((const struct vol *, const int, const u_int32_t,
struct dqblk *));