2 * $Id: quota.c,v 1.35 2010-04-03 07:11:35 franklahm 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 #include <sys/param.h>
25 #include <atalk/logger.h>
26 #include <atalk/afp.h>
27 #include <atalk/compat.h>
37 #define WANT_USER_QUOTA 0
38 #define WANT_GROUP_QUOTA 1
40 #ifdef NEED_QUOTACTL_WRAPPER
41 int quotactl(int cmd, const char *special, int id, caddr_t addr)
43 return syscall(__NR_quotactl, cmd, special, id, addr);
45 #endif /* NEED_QUOTACTL_WRAPPER */
47 static int overquota( struct dqblk *);
51 #ifdef HAVE_LINUX_XQM_H
52 #include <linux/xqm.h>
56 #define HAVE_LINUX_XQM_H
58 #ifdef HAVE_LINUX_DQBLK_XFS_H
59 #include <linux/dqblk_xfs.h>
60 #define HAVE_LINUX_XQM_H
61 #endif /* HAVE_LINUX_DQBLK_XFS_H */
62 #endif /* HAVE_XFS_XQM_H */
63 #endif /* HAVE_LINUX_XQM_H */
65 #include <linux/unistd.h>
67 static int is_xfs = 0;
69 static int get_linux_xfs_quota(int, char*, uid_t, struct dqblk *);
70 static int get_linux_fs_quota(int, char*, uid_t, struct dqblk *);
72 /* format supported by current kernel */
73 static int kernel_iface = IFACE_UNSET;
76 ** Check kernel quota version
77 ** Taken from quota-tools 3.08 by Jan Kara <jack@suse.cz>
79 static void linuxquota_get_api( void )
81 #ifndef LINUX_API_VERSION
84 if (stat("/proc/sys/fs/quota", &st) == 0) {
85 kernel_iface = IFACE_GENERIC;
88 struct dqstats_v2 v2_stats;
90 struct sigaction oldsig;
92 /* This signal handling is needed because old kernels send us SIGSEGV as they try to resolve the device */
93 sig.sa_handler = SIG_IGN;
94 sig.sa_sigaction = NULL;
96 sigemptyset(&sig.sa_mask);
97 if (sigaction(SIGSEGV, &sig, &oldsig) < 0) {
98 LOG( log_error, logtype_afpd, "cannot set SEGV signal handler: %s", strerror(errno));
101 if (quotactl(QCMD(Q_V2_GETSTATS, 0), NULL, 0, (void *)&v2_stats) >= 0) {
102 kernel_iface = IFACE_VFSV0;
104 else if (errno != ENOSYS && errno != ENOTSUP) {
105 /* RedHat 7.1 (2.4.2-2) newquota check
106 * Q_V2_GETSTATS in it's old place, Q_GETQUOTA in the new place
107 * (they haven't moved Q_GETSTATS to its new value) */
110 char tmp[1024]; /* Just temporary buffer */
112 if (quotactl(QCMD(Q_V1_GETSTATS, 0), NULL, 0, tmp))
114 if (quotactl(QCMD(Q_V1_GETQUOTA, 0), "/dev/null", 0, tmp))
117 /* On a RedHat 2.4.2-2 we expect 0, EINVAL
118 * On a 2.4.x we expect 0, ENOENT
119 * On a 2.4.x-ac we wont get here */
120 if (err_stat == 0 && err_quota == EINVAL) {
121 kernel_iface = IFACE_VFSV0;
124 kernel_iface = IFACE_VFSOLD;
128 /* This branch is *not* in quota-tools 3.08
129 ** but without it quota version is not correctly
130 ** identified for the original SuSE 8.0 kernel */
131 unsigned int vers_no;
134 if ((qf = fopen("/proc/fs/quota", "r"))) {
135 if (fscanf(qf, "Version %u", &vers_no) == 1) {
136 if ( (vers_no == (6*10000 + 5*100 + 0)) ||
137 (vers_no == (6*10000 + 5*100 + 1)) ) {
138 kernel_iface = IFACE_VFSV0;
144 if (sigaction(SIGSEGV, &oldsig, NULL) < 0) {
145 LOG(log_error, logtype_afpd, "cannot reset signal handler: %s", strerror(errno));
151 if (kernel_iface == IFACE_UNSET)
152 kernel_iface = IFACE_VFSOLD;
154 #else /* defined LINUX_API_VERSION */
155 kernel_iface = LINUX_API_VERSION;
159 /****************************************************************************/
161 static int get_linux_quota(int what, char *path, uid_t euser_id, struct dqblk *dp)
166 r=get_linux_xfs_quota(what, path, euser_id, dp);
168 r=get_linux_fs_quota(what, path, euser_id, dp);
173 /****************************************************************************
174 Abstract out the XFS Quota Manager quota get call.
175 ****************************************************************************/
177 static int get_linux_xfs_quota(int what, char *path, uid_t euser_id, struct dqblk *dqb)
180 #ifdef HAVE_LINUX_XQM_H
181 struct fs_disk_quota D;
183 memset (&D, 0, sizeof(D));
185 if ((ret = quotactl(QCMD(Q_XGETQUOTA,(what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t)&D)))
188 dqb->bsize = (uint64_t)512;
189 dqb->dqb_bsoftlimit = (uint64_t)D.d_blk_softlimit;
190 dqb->dqb_bhardlimit = (uint64_t)D.d_blk_hardlimit;
191 dqb->dqb_ihardlimit = (uint64_t)D.d_ino_hardlimit;
192 dqb->dqb_isoftlimit = (uint64_t)D.d_ino_softlimit;
193 dqb->dqb_curinodes = (uint64_t)D.d_icount;
194 dqb->dqb_curblocks = (uint64_t)D.d_bcount;
200 ** Wrapper for the quotactl(GETQUOTA) call.
201 ** For API v2 the results are copied back into a v1 structure.
202 ** Taken from quota-1.4.8 perl module
204 static int get_linux_fs_quota(int what, char *path, uid_t euser_id, struct dqblk *dqb)
208 if (kernel_iface == IFACE_UNSET)
209 linuxquota_get_api();
211 if (kernel_iface == IFACE_GENERIC)
213 struct dqblk_v3 dqb3;
215 ret = quotactl(QCMD(Q_V3_GETQUOTA, (what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t) &dqb3);
218 dqb->dqb_bhardlimit = dqb3.dqb_bhardlimit;
219 dqb->dqb_bsoftlimit = dqb3.dqb_bsoftlimit;
220 dqb->dqb_curblocks = dqb3.dqb_curspace / DEV_QBSIZE;
221 dqb->dqb_ihardlimit = dqb3.dqb_ihardlimit;
222 dqb->dqb_isoftlimit = dqb3.dqb_isoftlimit;
223 dqb->dqb_curinodes = dqb3.dqb_curinodes;
224 dqb->dqb_btime = dqb3.dqb_btime;
225 dqb->dqb_itime = dqb3.dqb_itime;
226 dqb->bsize = DEV_QBSIZE;
229 else if (kernel_iface == IFACE_VFSV0)
231 struct dqblk_v2 dqb2;
233 ret = quotactl(QCMD(Q_V2_GETQUOTA, (what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t) &dqb2);
236 dqb->dqb_bhardlimit = dqb2.dqb_bhardlimit;
237 dqb->dqb_bsoftlimit = dqb2.dqb_bsoftlimit;
238 dqb->dqb_curblocks = dqb2.dqb_curspace / DEV_QBSIZE;
239 dqb->dqb_ihardlimit = dqb2.dqb_ihardlimit;
240 dqb->dqb_isoftlimit = dqb2.dqb_isoftlimit;
241 dqb->dqb_curinodes = dqb2.dqb_curinodes;
242 dqb->dqb_btime = dqb2.dqb_btime;
243 dqb->dqb_itime = dqb2.dqb_itime;
244 dqb->bsize = DEV_QBSIZE;
247 else /* if (kernel_iface == IFACE_VFSOLD) */
249 struct dqblk_v1 dqb1;
251 ret = quotactl(QCMD(Q_V1_GETQUOTA, (what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t) &dqb1);
254 dqb->dqb_bhardlimit = dqb1.dqb_bhardlimit;
255 dqb->dqb_bsoftlimit = dqb1.dqb_bsoftlimit;
256 dqb->dqb_curblocks = dqb1.dqb_curblocks;
257 dqb->dqb_ihardlimit = dqb1.dqb_ihardlimit;
258 dqb->dqb_isoftlimit = dqb1.dqb_isoftlimit;
259 dqb->dqb_curinodes = dqb1.dqb_curinodes;
260 dqb->dqb_btime = dqb1.dqb_btime;
261 dqb->dqb_itime = dqb1.dqb_itime;
262 dqb->bsize = DEV_QBSIZE;
270 #if defined(HAVE_SYS_MNTTAB_H) || defined(__svr4__)
272 * Return the mount point associated with the filesystem
273 * on which "file" resides. Returns NULL on failure.
276 mountp( char *file, int *nfs)
281 static struct mnttab mnt;
283 if ( lstat( file, &sb ) < 0 ) {
288 if (( mtab = fopen( "/etc/mnttab", "r" )) == NULL ) {
292 while ( getmntent( mtab, &mnt ) == 0 ) {
294 if ( (lstat( mnt.mnt_special, &sb ) == 0) && (devno == sb.st_rdev)) {
296 return mnt.mnt_mountp;
299 /* check for nfs. we probably should use
300 * strcmp(mnt.mnt_fstype, MNTTYPE_NFS), but that's not as fast. */
301 if ((lstat(mnt.mnt_mountp, &sb) == 0) && (devno == sb.st_dev) &&
302 strchr(mnt.mnt_special, ':')) {
305 return mnt.mnt_special;
316 * Return the block-special device name associated with the filesystem
317 * on which "file" resides. Returns NULL on failure.
321 special( char *file, int *nfs)
323 static struct fs_data fsd;
325 if ( getmnt(0, &fsd, 0, STAT_ONE, file ) < 0 ) {
326 LOG(log_info, logtype_afpd, "special: getmnt %s: %s", file, strerror(errno) );
330 /* XXX: does this really detect an nfs mounted fs? */
331 if (strchr(fsd.fd_req.devname, ':'))
333 return( fsd.fd_req.devname );
337 #if (defined(HAVE_SYS_MOUNT_H) && !defined(__linux__)) || defined(BSD4_4) || defined(_IBMR2)
340 special(char *file, int *nfs)
342 static struct statfs sfs;
344 if ( statfs( file, &sfs ) < 0 ) {
349 /* Digital UNIX: The struct sfs contains a field sfs.f_type,
350 * the MOUNT_* constants are defined in <sys/mount.h> */
351 if ((sfs.f_type == MOUNT_NFS)||(sfs.f_type == MOUNT_NFS3))
353 /* XXX: make sure this really detects an nfs mounted fs */
354 if (strchr(sfs.f_mntfromname, ':'))
357 return( sfs.f_mntfromname );
363 special(char *file, int *nfs)
371 if ( lstat( file, &sb ) < 0 ) {
376 if (( mtab = setmntent( "/etc/mtab", "r" )) == NULL ) {
380 while (( mnt = getmntent( mtab )) != NULL ) {
381 /* check for local fs */
382 if ( (lstat( mnt->mnt_fsname, &sb ) == 0) && devno == sb.st_rdev) {
387 /* check for an nfs mount entry. the alternative is to use
388 * strcmp(mnt->mnt_type, MNTTYPE_NFS) instead of the strchr. */
389 if ((lstat(mnt->mnt_dir, &sb) == 0) && (devno == sb.st_dev) &&
390 strchr(mnt->mnt_fsname, ':')) {
402 if (strcmp(mnt->mnt_type, "xfs") == 0)
406 return( mnt->mnt_fsname );
411 #endif /* __svr4__ */
414 static int getfsquota(struct vol *vol, const int uid, struct dqblk *dq)
423 memset(&dqg, 0, sizeof(dqg));
428 qc.addr = (caddr_t)dq;
429 if ( ioctl( vol->v_qfd, Q_QUOTACTL, &qc ) < 0 ) {
430 return( AFPERR_PARAM );
435 if ( quota( Q_GETDLIM, uid, vol->v_gvs, dq ) != 0 ) {
436 return( AFPERR_PARAM );
445 #define QCMD(a,b) (a)
449 /* for group quotas. we only use these if the user belongs
454 if ( seteuid( getuid() ) == 0 ) {
455 if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA,USRQUOTA),
456 uid, (char *)dq ) != 0 ) {
457 /* try group quotas */
459 if ( quotactl(vol->v_path, QCMD(Q_GETQUOTA, GRPQUOTA),
460 groups[0], (char *) &dqg) != 0 ) {
462 return( AFPERR_PARAM );
470 if ( seteuid( getuid() ) == 0 ) {
471 if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA, USRQUOTA),
472 uid, (char *)dq ) != 0 ) {
474 return ( AFPERR_PARAM );
480 if (get_linux_quota (WANT_USER_QUOTA, vol->v_gvs, uid, dq) !=0) {
481 return( AFPERR_PARAM );
484 if (get_linux_quota(WANT_GROUP_QUOTA, vol->v_gvs, getegid(), &dqg) != 0) {
486 LOG(log_debug, logtype_afpd, "group quota did not work!" );
487 #endif /* DEBUG_QUOTA */
489 return AFP_OK; /* no need to check user vs group quota */
495 /* return either the group quota entry or user quota entry,
496 whichever has the least amount of space remaining
499 /* if user space remaining > group space remaining */
501 /* if overquota, free space is 0 otherwise hard-current */
502 ( overquota( dq ) ? 0 : ( dq->dqb_bhardlimit ? dq->dqb_bhardlimit -
503 dq->dqb_curblocks : ~((uint64_t) 0) ) )
507 ( overquota( &dqg ) ? 0 : ( dqg.dqb_bhardlimit ? dqg.dqb_bhardlimit -
508 dqg.dqb_curblocks : ~((uint64_t) 0) ) )
512 /* use group quota limits rather than user limits */
513 dq->dqb_curblocks = dqg.dqb_curblocks;
514 dq->dqb_bhardlimit = dqg.dqb_bhardlimit;
515 dq->dqb_bsoftlimit = dqg.dqb_bsoftlimit;
516 dq->dqb_btimelimit = dqg.dqb_btimelimit;
522 #endif /* __svr4__ */
528 static int getquota( struct vol *vol, struct dqblk *dq, const uint32_t bsize)
533 char buf[ MAXPATHLEN + 1];
535 if ( vol->v_qfd == -1 && vol->v_gvs == NULL) {
536 if (( p = mountp( vol->v_path, &vol->v_nfs)) == NULL ) {
537 LOG(log_info, logtype_afpd, "getquota: mountp %s fails", vol->v_path );
538 return( AFPERR_PARAM );
542 if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
543 LOG(log_error, logtype_afpd, "getquota: malloc: %s", strerror(errno) );
546 strcpy( vol->v_gvs, p );
549 sprintf( buf, "%s/quotas", p );
550 if (( vol->v_qfd = open( buf, O_RDONLY, 0 )) < 0 ) {
551 LOG(log_info, logtype_afpd, "open %s: %s", buf, strerror(errno) );
552 return( AFPERR_PARAM );
558 if ( vol->v_gvs == NULL ) {
559 if (( p = special( vol->v_path, &vol->v_nfs )) == NULL ) {
560 LOG(log_info, logtype_afpd, "getquota: special %s fails", vol->v_path );
561 return( AFPERR_PARAM );
564 if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
565 LOG(log_error, logtype_afpd, "getquota: malloc: %s", strerror(errno) );
568 strcpy( vol->v_gvs, p );
573 /* Digital UNIX: Two forms of specifying an NFS filesystem are possible,
574 either 'hostname:path' or 'path@hostname' (Ultrix heritage) */
577 char pathstring[MNAMELEN];
578 /* MNAMELEN ist defined in <sys/mount.h> */
581 if ((hostpath = strchr(vol->v_gvs,'@')) != NULL ) {
582 /* convert 'path@hostname' to 'hostname:path',
583 * call getnfsquota(),
584 * convert 'hostname:path' back to 'path@hostname' */
586 sprintf(pathstring,"%s:%s",hostpath+1,vol->v_gvs);
587 strcpy(vol->v_gvs,pathstring);
589 result = getnfsquota(vol, uuid, bsize, dq);
591 hostpath = strchr(vol->v_gvs,':');
593 sprintf(pathstring,"%s@%s",hostpath+1,vol->v_gvs);
594 strcpy(vol->v_gvs,pathstring);
599 /* vol->v_gvs is of the form 'hostname:path' */
600 return getnfsquota(vol, uuid, bsize, dq);
602 /* local filesystem */
603 return getfsquota(vol, uuid, dq);
606 return vol->v_nfs ? getnfsquota(vol, uuid, bsize, dq) :
607 getfsquota(vol, uuid, dq);
611 static int overquota( struct dqblk *dqblk)
615 if ( dqblk->dqb_curblocks > dqblk->dqb_bhardlimit &&
616 dqblk->dqb_bhardlimit != 0 ) {
620 if ( dqblk->dqb_curblocks < dqblk->dqb_bsoftlimit ||
621 dqblk->dqb_bsoftlimit == 0 ) {
625 if ( dqblk->dqb_bwarn ) {
629 if ( gettimeofday( &tv, NULL ) < 0 ) {
630 LOG(log_error, logtype_afpd, "overquota: gettimeofday: %s", strerror(errno) );
631 return( AFPERR_PARAM );
633 if ( dqblk->dqb_btimelimit && dqblk->dqb_btimelimit > tv.tv_sec ) {
641 * This next bit is basically for linux -- everything is fine
642 * if you use 1k blocks... but if you try (for example) to mount
643 * a volume via nfs from a netapp (which might use 4k blocks) everything
644 * gets reported improperly. I have no idea about dbtob on other
648 #ifdef HAVE_BROKEN_DBTOB
650 #define dbtob(a, b) ((VolSpace)((VolSpace)(a) * (VolSpace)(b)))
651 #define HAVE_2ARG_DBTOB
655 #define dbtob(a) ((a) << 10)
658 /* i do the cast to VolSpace here to make sure that 64-bit shifts
660 #ifdef HAVE_2ARG_DBTOB
661 #define tobytes(a, b) dbtob((VolSpace) (a), (VolSpace) (b))
663 #define tobytes(a, b) dbtob((VolSpace) (a))
666 int uquota_getvolspace( struct vol *vol, VolSpace *bfree, VolSpace *btotal, const uint32_t bsize)
673 if (getquota( vol, &dqblk, bsize) != 0 ) {
674 return( AFPERR_PARAM );
678 this_bsize = dqblk.bsize;
682 LOG(log_debug, logtype_afpd, "after calling getquota in uquota_getvolspace!" );
683 LOG(log_debug, logtype_afpd, "dqb_ihardlimit: %u", dqblk.dqb_ihardlimit );
684 LOG(log_debug, logtype_afpd, "dqb_isoftlimit: %u", dqblk.dqb_isoftlimit );
685 LOG(log_debug, logtype_afpd, "dqb_curinodes : %u", dqblk.dqb_curinodes );
686 LOG(log_debug, logtype_afpd, "dqb_bhardlimit: %u", dqblk.dqb_bhardlimit );
687 LOG(log_debug, logtype_afpd, "dqb_bsoftlimit: %u", dqblk.dqb_bsoftlimit );
688 LOG(log_debug, logtype_afpd, "dqb_curblocks : %u", dqblk.dqb_curblocks );
689 LOG(log_debug, logtype_afpd, "dqb_btime : %u", dqblk.dqb_btime );
690 LOG(log_debug, logtype_afpd, "dqb_itime : %u", dqblk.dqb_itime );
691 LOG(log_debug, logtype_afpd, "bsize/this_bsize : %u/%u", bsize, this_bsize );
692 LOG(log_debug, logtype_afpd, "dqblk.dqb_bhardlimit size: %u", tobytes( dqblk.dqb_bhardlimit, this_bsize ));
693 LOG(log_debug, logtype_afpd, "dqblk.dqb_bsoftlimit size: %u", tobytes( dqblk.dqb_bsoftlimit, this_bsize ));
694 LOG(log_debug, logtype_afpd, "dqblk.dqb_curblocks size: %u", tobytes( dqblk.dqb_curblocks, this_bsize ));
695 #endif /* DEBUG_QUOTA */
697 /* no limit set for this user. it might be set in the future. */
698 if (dqblk.dqb_bsoftlimit == 0 && dqblk.dqb_bhardlimit == 0) {
699 *btotal = *bfree = ~((VolSpace) 0);
700 } else if ( overquota( &dqblk )) {
701 if ( tobytes( dqblk.dqb_curblocks, this_bsize ) > tobytes( dqblk.dqb_bsoftlimit, this_bsize ) ) {
702 *btotal = tobytes( dqblk.dqb_curblocks, this_bsize );
706 *btotal = tobytes( dqblk.dqb_bsoftlimit, this_bsize );
707 *bfree = tobytes( dqblk.dqb_bsoftlimit, this_bsize ) -
708 tobytes( dqblk.dqb_curblocks, this_bsize );
711 *btotal = tobytes( dqblk.dqb_bhardlimit, this_bsize );
712 *bfree = tobytes( dqblk.dqb_bhardlimit, this_bsize ) -
713 tobytes( dqblk.dqb_curblocks, this_bsize );
717 LOG(log_debug, logtype_afpd, "bfree : %u", *bfree );
718 LOG(log_debug, logtype_afpd, "btotal : %u", *btotal );
719 LOG(log_debug, logtype_afpd, "bfree : %uKB", *bfree/1024 );
720 LOG(log_debug, logtype_afpd, "btotal : %uKB", *btotal/1024 );