2 * $Id: quota.c,v 1.22.8.8 2004-03-22 22:46:29 bfernhomberg 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>
50 #define WANT_USER_QUOTA 0
51 #define WANT_GROUP_QUOTA 1
53 #ifdef NEED_QUOTACTL_WRAPPER
54 int quotactl(int cmd, const char *special, int id, caddr_t addr)
56 return syscall(__NR_quotactl, cmd, special, id, addr);
58 #endif /* NEED_QUOTACTL_WRAPPER */
62 #ifdef HAVE_LINUX_XQM_H
63 #include <linux/xqm.h>
67 #define HAVE_LINUX_XQM_H
68 #endif /* HAVE_XFS_XQM_H */
69 #endif /* HAVE_LINUX_XQM_H */
71 #include <linux/unistd.h>
73 static int is_xfs = 0;
75 static int get_linux_xfs_quota(int, char*, uid_t, struct dqblk *);
76 static int get_linux_fs_quota(int, char*, uid_t, struct dqblk *);
77 static int overquota( struct dqblk *);
79 /* format supported by current kernel */
80 static int kernel_iface = IFACE_UNSET;
83 ** Check kernel quota version
84 ** Taken from quota-tools 3.08 by Jan Kara <jack@suse.cz>
86 static void linuxquota_get_api( void )
88 #ifndef LINUX_API_VERSION
91 if (stat("/proc/sys/fs/quota", &st) == 0) {
92 kernel_iface = IFACE_GENERIC;
95 struct dqstats_v2 v2_stats;
97 struct sigaction oldsig;
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;
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));
108 if (quotactl(QCMD(Q_V2_GETSTATS, 0), NULL, 0, (void *)&v2_stats) >= 0) {
109 kernel_iface = IFACE_VFSV0;
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) */
117 char tmp[1024]; /* Just temporary buffer */
119 if (quotactl(QCMD(Q_V1_GETSTATS, 0), NULL, 0, tmp))
121 if (quotactl(QCMD(Q_V1_GETQUOTA, 0), "/dev/null", 0, tmp))
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;
131 kernel_iface = IFACE_VFSOLD;
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;
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;
151 if (sigaction(SIGSEGV, &oldsig, NULL) < 0) {
152 LOG(log_error, logtype_afpd, "cannot reset signal handler: %s\n", strerror(errno));
158 if (kernel_iface == IFACE_UNSET)
159 kernel_iface = IFACE_VFSOLD;
161 #else /* defined LINUX_API_VERSION */
162 kernel_iface = LINUX_API_VERSION;
166 /****************************************************************************/
168 static int get_linux_quota(int what, char *path, uid_t euser_id, struct dqblk *dp)
173 r=get_linux_xfs_quota(what, path, euser_id, dp);
175 r=get_linux_fs_quota(what, path, euser_id, dp);
180 /****************************************************************************
181 Abstract out the XFS Quota Manager quota get call.
182 ****************************************************************************/
184 static int get_linux_xfs_quota(what, path, euser_id, dqb)
191 #ifdef HAVE_LINUX_XQM_H
192 struct fs_disk_quota D;
194 memset (&D, 0, sizeof(D));
196 if ((ret = quotactl(QCMD(Q_XGETQUOTA,(what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t)&D)))
199 dqb->bsize = (u_int64_t)512;
200 dqb->dqb_bsoftlimit = (u_int64_t)D.d_blk_softlimit;
201 dqb->dqb_bhardlimit = (u_int64_t)D.d_blk_hardlimit;
202 dqb->dqb_ihardlimit = (u_int64_t)D.d_ino_hardlimit;
203 dqb->dqb_isoftlimit = (u_int64_t)D.d_ino_softlimit;
204 dqb->dqb_curinodes = (u_int64_t)D.d_icount;
205 dqb->dqb_curblocks = (u_int64_t)D.d_bcount;
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
215 static int get_linux_fs_quota( what, path, euser_id, dqb)
223 if (kernel_iface == IFACE_UNSET)
224 linuxquota_get_api();
226 if (kernel_iface == IFACE_GENERIC)
228 struct dqblk_v3 dqb3;
230 ret = quotactl(QCMD(Q_V3_GETQUOTA, (what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t) &dqb3);
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;
244 else if (kernel_iface == IFACE_VFSV0)
246 struct dqblk_v2 dqb2;
248 ret = quotactl(QCMD(Q_V2_GETQUOTA, (what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t) &dqb2);
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;
262 else /* if (kernel_iface == IFACE_VFSOLD) */
264 struct dqblk_v1 dqb1;
266 ret = quotactl(QCMD(Q_V1_GETQUOTA, (what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t) &dqb1);
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;
285 #if defined(HAVE_SYS_MNTTAB_H) || defined(__svr4__)
287 * Return the mount point associated with the filesystem
288 * on which "file" resides. Returns NULL on failure.
298 static struct mnttab mnt;
300 if ( stat( file, &sb ) < 0 ) {
305 if (( mtab = fopen( "/etc/mnttab", "r" )) == NULL ) {
309 while ( getmntent( mtab, &mnt ) == 0 ) {
311 if ( (stat( mnt.mnt_special, &sb ) == 0) && (devno == sb.st_rdev)) {
313 return mnt.mnt_mountp;
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, ':')) {
322 return mnt.mnt_special;
333 * Return the block-special device name associated with the filesystem
334 * on which "file" resides. Returns NULL on failure.
342 static struct fs_data fsd;
344 if ( getmnt(0, &fsd, 0, STAT_ONE, file ) < 0 ) {
345 LOG(log_info, logtype_afpd, "special: getmnt %s: %s", file, strerror(errno) );
349 /* XXX: does this really detect an nfs mounted fs? */
350 if (strchr(fsd.fd_req.devname, ':'))
352 return( fsd.fd_req.devname );
356 #if (defined(HAVE_SYS_MOUNT_H) && !defined(__linux__)) || defined(BSD4_4) || defined(_IBMR2)
363 static struct statfs sfs;
365 if ( statfs( file, &sfs ) < 0 ) {
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))
374 /* XXX: make sure this really detects an nfs mounted fs */
375 if (strchr(sfs.f_mntfromname, ':'))
378 return( sfs.f_mntfromname );
394 if ( stat( file, &sb ) < 0 ) {
399 if (( mtab = setmntent( "/etc/mtab", "r" )) == NULL ) {
403 while (( mnt = getmntent( mtab )) != NULL ) {
404 /* check for local fs */
405 if ( (stat( mnt->mnt_fsname, &sb ) == 0) && devno == sb.st_rdev) {
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, ':')) {
425 if (strcmp(mnt->mnt_type, "xfs") == 0)
429 return( mnt->mnt_fsname );
434 #endif /* __svr4__ */
437 static int getfsquota(vol, uid, dq)
445 memset(&dqg, 0, sizeof(dqg));
452 qc.addr = (caddr_t)dq;
453 if ( ioctl( vol->v_qfd, Q_QUOTACTL, &qc ) < 0 ) {
454 return( AFPERR_PARAM );
459 if ( quota( Q_GETDLIM, uid, vol->v_gvs, dq ) != 0 ) {
460 return( AFPERR_PARAM );
469 #define QCMD(a,b) (a)
473 /* for group quotas. we only use these if the user belongs
478 if ( seteuid( getuid() ) == 0 ) {
479 if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA,USRQUOTA),
480 uid, (char *)dq ) != 0 ) {
481 /* try group quotas */
483 if ( quotactl(vol->v_path, QCMD(Q_GETQUOTA, GRPQUOTA),
484 groups[0], (char *) &dqg) != 0 ) {
486 return( AFPERR_PARAM );
494 if ( seteuid( getuid() ) == 0 ) {
495 if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA, USRQUOTA),
496 uid, (char *)dq ) != 0 ) {
498 return ( AFPERR_PARAM );
504 if (get_linux_quota (WANT_USER_QUOTA, vol->v_gvs, uid, dq) !=0) {
505 return( AFPERR_PARAM );
508 if (get_linux_quota(WANT_GROUP_QUOTA, vol->v_gvs, getegid(), &dqg) != 0) {
510 LOG(log_debug, logtype_afpd, "group quota did not work!" );
511 #endif /* DEBUG_QUOTA */
513 return AFP_OK; /* no need to check user vs group quota */
519 /* return either the group quota entry or user quota entry,
520 whichever has the least amount of space remaining
523 /* if user space remaining > group space remaining */
525 /* if overquota, free space is 0 otherwise hard-current */
526 ( overquota( dq ) ? 0 : ( dq->dqb_bhardlimit ? dq->dqb_bhardlimit -
527 dq->dqb_curblocks : ~((qsize_t) 0) ) )
531 ( overquota( &dqg ) ? 0 : ( dqg.dqb_bhardlimit ? dqg.dqb_bhardlimit -
532 dqg.dqb_curblocks : ~((qsize_t) 0) ) )
536 /* use group quota limits rather than user limits */
537 dq->dqb_curblocks = dqg.dqb_curblocks;
538 dq->dqb_bhardlimit = dqg.dqb_bhardlimit;
539 dq->dqb_bsoftlimit = dqg.dqb_bsoftlimit;
540 dq->dqb_btimelimit = dqg.dqb_btimelimit;
546 #endif /* __svr4__ */
552 static int getquota( vol, dq, bsize)
555 const u_int32_t bsize;
560 char buf[ MAXPATHLEN + 1];
562 if ( vol->v_qfd == -1 && vol->v_gvs == NULL) {
563 if (( p = mountp( vol->v_path, &vol->v_nfs)) == NULL ) {
564 LOG(log_info, logtype_afpd, "getquota: mountp %s fails", vol->v_path );
565 return( AFPERR_PARAM );
569 if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
570 LOG(log_error, logtype_afpd, "getquota: malloc: %s", strerror(errno) );
573 strcpy( vol->v_gvs, p );
576 sprintf( buf, "%s/quotas", p );
577 if (( vol->v_qfd = open( buf, O_RDONLY, 0 )) < 0 ) {
578 LOG(log_info, logtype_afpd, "open %s: %s", buf, strerror(errno) );
579 return( AFPERR_PARAM );
585 if ( vol->v_gvs == NULL ) {
586 if (( p = special( vol->v_path, &vol->v_nfs )) == NULL ) {
587 LOG(log_info, logtype_afpd, "getquota: special %s fails", vol->v_path );
588 return( AFPERR_PARAM );
591 if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
592 LOG(log_error, logtype_afpd, "getquota: malloc: %s", strerror(errno) );
595 strcpy( vol->v_gvs, p );
600 /* Digital UNIX: Two forms of specifying an NFS filesystem are possible,
601 either 'hostname:path' or 'path@hostname' (Ultrix heritage) */
604 char pathstring[MNAMELEN];
605 /* MNAMELEN ist defined in <sys/mount.h> */
608 if ((hostpath = strchr(vol->v_gvs,'@')) != NULL ) {
609 /* convert 'path@hostname' to 'hostname:path',
610 * call getnfsquota(),
611 * convert 'hostname:path' back to 'path@hostname' */
613 sprintf(pathstring,"%s:%s",hostpath+1,vol->v_gvs);
614 strcpy(vol->v_gvs,pathstring);
616 result = getnfsquota(vol, uuid, bsize, dq);
618 hostpath = strchr(vol->v_gvs,':');
620 sprintf(pathstring,"%s@%s",hostpath+1,vol->v_gvs);
621 strcpy(vol->v_gvs,pathstring);
626 /* vol->v_gvs is of the form 'hostname:path' */
627 return getnfsquota(vol, uuid, bsize, dq);
629 /* local filesystem */
630 return getfsquota(vol, uuid, dq);
633 return vol->v_nfs ? getnfsquota(vol, uuid, bsize, dq) :
634 getfsquota(vol, uuid, dq);
638 static int overquota( dqblk )
643 if ( dqblk->dqb_curblocks > dqblk->dqb_bhardlimit &&
644 dqblk->dqb_bhardlimit != 0 ) {
648 if ( dqblk->dqb_curblocks < dqblk->dqb_bsoftlimit ||
649 dqblk->dqb_bsoftlimit == 0 ) {
653 if ( dqblk->dqb_bwarn ) {
657 if ( gettimeofday( &tv, 0 ) < 0 ) {
658 LOG(log_error, logtype_afpd, "overquota: gettimeofday: %s", strerror(errno) );
659 return( AFPERR_PARAM );
661 if ( dqblk->dqb_btimelimit && dqblk->dqb_btimelimit > tv.tv_sec ) {
669 * This next bit is basically for linux -- everything is fine
670 * if you use 1k blocks... but if you try (for example) to mount
671 * a volume via nfs from a netapp (which might use 4k blocks) everything
672 * gets reported improperly. I have no idea about dbtob on other
676 #ifdef HAVE_BROKEN_DBTOB
678 #define dbtob(a, b) ((VolSpace)((VolSpace)(a) * (VolSpace)(b)))
679 #define HAVE_2ARG_DBTOB
683 #define dbtob(a) ((a) << 10)
686 /* i do the cast to VolSpace here to make sure that 64-bit shifts
688 #ifdef HAVE_2ARG_DBTOB
689 #define tobytes(a, b) dbtob((VolSpace) (a), (VolSpace) (b))
691 #define tobytes(a, b) dbtob((VolSpace) (a))
694 int uquota_getvolspace( vol, bfree, btotal, bsize)
695 const struct vol *vol;
696 VolSpace *bfree, *btotal;
697 const u_int32_t bsize;
699 u_int64_t this_bsize;
704 if (getquota( vol, &dqblk, bsize) != 0 ) {
705 return( AFPERR_PARAM );
709 this_bsize = dqblk.bsize;
713 LOG(log_info, logtype_afpd, "after calling getquota in uquota_getvolspace!" );
714 LOG(log_info, logtype_afpd, "dqb_ihardlimit: %u", dqblk.dqb_ihardlimit );
715 LOG(log_info, logtype_afpd, "dqb_isoftlimit: %u", dqblk.dqb_isoftlimit );
716 LOG(log_info, logtype_afpd, "dqb_curinodes : %u", dqblk.dqb_curinodes );
717 LOG(log_info, logtype_afpd, "dqb_bhardlimit: %u", dqblk.dqb_bhardlimit );
718 LOG(log_info, logtype_afpd, "dqb_bsoftlimit: %u", dqblk.dqb_bsoftlimit );
719 LOG(log_info, logtype_afpd, "dqb_curblocks : %u", dqblk.dqb_curblocks );
720 LOG(log_info, logtype_afpd, "dqb_btime : %u", dqblk.dqb_btime );
721 LOG(log_info, logtype_afpd, "dqb_itime : %u", dqblk.dqb_itime );
722 LOG(log_info, logtype_afpd, "bsize/this_bsize : %u/%u", bsize, this_bsize );
723 LOG(log_info, logtype_afpd, "dqblk.dqb_bhardlimit size: %u", tobytes( dqblk.dqb_bhardlimit, this_bsize ));
724 LOG(log_info, logtype_afpd, "dqblk.dqb_bsoftlimit size: %u", tobytes( dqblk.dqb_bsoftlimit, this_bsize ));
725 LOG(log_info, logtype_afpd, "dqblk.dqb_curblocks size: %u", tobytes( dqblk.dqb_curblocks, this_bsize ));
726 #endif /* DEBUG_QUOTA */
728 /* no limit set for this user. it might be set in the future. */
729 if (dqblk.dqb_bsoftlimit == 0 && dqblk.dqb_bhardlimit == 0) {
730 *btotal = *bfree = ~((VolSpace) 0);
731 } else if ( overquota( &dqblk )) {
732 if ( tobytes( dqblk.dqb_curblocks, this_bsize ) > tobytes( dqblk.dqb_bsoftlimit, this_bsize ) ) {
733 *btotal = tobytes( dqblk.dqb_curblocks, this_bsize );
737 *btotal = tobytes( dqblk.dqb_bsoftlimit, this_bsize );
738 *bfree = tobytes( dqblk.dqb_bsoftlimit, this_bsize ) -
739 tobytes( dqblk.dqb_curblocks, this_bsize );
742 *btotal = tobytes( dqblk.dqb_bhardlimit, this_bsize );
743 *bfree = tobytes( dqblk.dqb_bhardlimit, this_bsize ) -
744 tobytes( dqblk.dqb_curblocks, this_bsize );
748 LOG(log_info, logtype_afpd, "bfree : %u", *bfree );
749 LOG(log_info, logtype_afpd, "btotal : %u", *btotal );
750 LOG(log_info, logtype_afpd, "bfree : %uKB", *bfree/1024 );
751 LOG(log_info, logtype_afpd, "btotal : %uKB", *btotal/1024 );