2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1992-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * This is one of the most system dependent parts of Samba, and its
25 * done a litle differently. Each system has its own way of doing
31 extern int DEBUGLEVEL;
35 #include <sys/types.h>
36 #include <asm/types.h>
37 #include <sys/quota.h>
40 #include <linux/unistd.h>
42 _syscall4(int, quotactl, int, cmd, const char *, special, int, id, caddr_t, addr);
44 /****************************************************************************
45 try to get the disk space from disk quotas (LINUX version)
46 ****************************************************************************/
48 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
59 /* find the block device file */
61 if ( sys_stat(path, &S) == -1 ) {
67 fp = setmntent(MOUNTED,"r");
70 while ((mnt = getmntent(fp))) {
71 if ( sys_stat(mnt->mnt_dir,&S) == -1 )
73 if (S.st_dev == devno) {
86 r=quotactl(QCMD(Q_GETQUOTA,USRQUOTA), mnt->mnt_fsname, euser_id, (caddr_t)&D);
89 /* Use softlimit to determine disk space, except when it has been exceeded */
96 *dsize =D.dqb_curblocks;
101 /* Use softlimit to determine disk space, except when it has been exceeded */
103 (D.dqb_bsoftlimit && D.dqb_curblocks>=D.dqb_bsoftlimit) ||
104 (D.dqb_bhardlimit && D.dqb_curblocks>=D.dqb_bhardlimit) ||
105 (D.dqb_isoftlimit && D.dqb_curinodes>=D.dqb_isoftlimit) ||
106 (D.dqb_ihardlimit && D.dqb_curinodes>=D.dqb_ihardlimit)
110 *dsize = D.dqb_curblocks;
112 else if (D.dqb_bsoftlimit==0 && D.dqb_bhardlimit==0)
117 if (D.dqb_bsoftlimit == 0)
118 D.dqb_bsoftlimit = D.dqb_bhardlimit;
119 *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
120 *dsize = D.dqb_bsoftlimit;
127 #include <sys/quota.h>
130 /****************************************************************************
131 try to get the disk space from disk quotas (CRAY VERSION)
132 ****************************************************************************/
133 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
137 SMB_STRUCT_STAT sbuf;
139 static SMB_DEV_T devno_cached = 0 ;
141 struct q_request request ;
142 struct qf_header header ;
143 static int quota_default = 0 ;
146 if ( sys_stat(path,&sbuf) == -1 )
149 devno = sbuf.st_dev ;
151 if ( devno != devno_cached ) {
153 devno_cached = devno ;
155 if ((fd = setmntent(KMTAB)) == NULL)
160 while ((mnt = getmntent(fd)) != NULL) {
162 if ( sys_stat(mnt->mnt_dir,&sbuf) == -1 )
165 if (sbuf.st_dev == devno) {
174 pstrcpy(name,mnt->mnt_dir) ;
181 request.qf_magic = QF_MAGIC ;
182 request.qf_entry.id = geteuid() ;
184 if (quotactl(name, Q_GETQUOTA, &request) == -1)
187 if ( ! request.user )
190 if ( request.qf_entry.user_q.f_quota == QFV_DEFAULT ) {
192 if ( ! quota_default ) {
194 if ( quotactl(name, Q_GETHEADER, &header) == -1 )
197 quota_default = header.user_h.def_fq ;
200 *dfree = quota_default ;
202 }else if ( request.qf_entry.user_q.f_quota == QFV_PREVENT ) {
208 *dfree = request.qf_entry.user_q.f_quota ;
212 *dsize = request.qf_entry.user_q.f_use ;
220 *bsize = 4096 ; /* Cray blocksize */
227 #elif defined(SUNOS5) || defined(SUNOS4)
231 #include <sys/fs/ufs_quota.h>
232 #include <sys/mnttab.h>
233 #else /* defined(SUNOS4) */
234 #include <ufs/quota.h>
238 /****************************************************************************
239 try to get the disk space from disk quotas (solaris 2 version)
240 ****************************************************************************/
241 /* Quota code by Peter Urbanec (amiga@cse.unsw.edu.au) */
242 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
244 uid_t user_id, euser_id;
248 struct quotctl command;
257 SMB_STRUCT_STAT sbuf;
259 static SMB_DEV_T devno_cached = 0 ;
262 if ( sys_stat(path,&sbuf) == -1 )
265 devno = sbuf.st_dev ;
266 DEBUG(5,("disk_quotas: looking for path \"%s\" devno=%o\n", path,devno));
267 if ( devno != devno_cached ) {
268 devno_cached = devno ;
270 if ((fd = fopen(MNTTAB, "r")) == NULL)
274 while (getmntent(fd, &mnt) == 0) {
275 if ( sys_stat(mnt.mnt_mountp,&sbuf) == -1 )
277 DEBUG(5,("disk_quotas: testing \"%s\" devno=%o\n",
278 mnt.mnt_mountp,sbuf.st_dev));
279 if (sbuf.st_dev == devno) {
285 pstrcpy(name,mnt.mnt_mountp) ;
286 pstrcat(name,"/quotas") ;
289 if ((fd = setmntent(MOUNTED, "r")) == NULL)
293 while ((mnt = getmntent(fd)) != NULL) {
294 if ( sys_stat(mnt->mnt_dir,&sbuf) == -1 )
296 DEBUG(5,("disk_quotas: testing \"%s\" devno=%o\n",
297 mnt->mnt_dir,sbuf.st_dev));
298 if (sbuf.st_dev == devno) {
304 pstrcpy(name,mnt->mnt_fsname) ;
312 euser_id = geteuid();
315 setuid(0); /* Solaris seems to want to give info only to super-user */
319 DEBUG(5,("disk_quotas: looking for quotas file \"%s\"\n", name));
320 if((file=open(name, O_RDONLY))<0) {
321 setuid(user_id); /* Restore the original UID status */
325 command.op = Q_GETQUOTA;
326 command.uid = euser_id;
327 command.addr = (caddr_t) &D;
328 ret = ioctl(file, Q_QUOTACTL, &command);
331 DEBUG(5,("disk_quotas: trying quotactl on device \"%s\"\n", name));
332 ret = quotactl(Q_GETQUOTA, name, euser_id, &D);
335 setuid(user_id); /* Restore the original uid status. */
339 DEBUG(2,("disk_quotas ioctl (Solaris) failed\n"));
344 /* Use softlimit to determine disk space. A user exceeding the quota is told
345 * that there's no space left. Writes might actually work for a bit if the
346 * hardlimit is set higher than softlimit. Effectively the disk becomes
347 * made of rubber latex and begins to expand to accommodate the user :-)
350 if (D.dqb_bsoftlimit==0)
353 *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
354 *dsize = D.dqb_bsoftlimit;
358 *dsize = D.dqb_curblocks;
361 DEBUG(5,("disk_quotas for path \"%s\" returning bsize %d, dfree %d, dsize %d\n",
362 path,*bsize,*dfree,*dsize));
369 #include <ufs/quota.h>
371 /****************************************************************************
372 try to get the disk space from disk quotas - OFS1 version
373 ****************************************************************************/
374 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
376 uid_t user_id, euser_id;
381 euser_id = geteuid();
384 setreuid(euser_id, -1);
385 r= quotactl(path,QCMD(Q_GETQUOTA, USRQUOTA),euser_id,(char *) &D);
389 if (setreuid(user_id, -1) == -1)
390 DEBUG(5,("Unable to reset uid to %d\n", user_id));
396 if (save_errno == EDQUOT) // disk quota exceeded
399 *dsize = D.dqb_curblocks;
406 /* Use softlimit to determine disk space, except when it has been exceeded */
408 if (D.dqb_bsoftlimit==0)
411 if ((D.dqb_curblocks>D.dqb_bsoftlimit)) {
413 *dsize = D.dqb_curblocks;
415 *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
416 *dsize = D.dqb_bsoftlimit;
422 /****************************************************************************
423 try to get the disk space from disk quotas (IRIX 6.2 version)
424 ****************************************************************************/
426 #include <sys/quota.h>
429 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
434 struct fs_disk_quota F;
441 /* find the block device file */
443 if ( sys_stat(path, &S) == -1 ) {
449 fp = setmntent(MOUNTED,"r");
452 while ((mnt = getmntent(fp))) {
453 if ( sys_stat(mnt->mnt_dir,&S) == -1 )
455 if (S.st_dev == devno) {
469 /* Use softlimit to determine disk space, except when it has been exceeded */
473 if ( 0 == strcmp ( mnt->mnt_type, "efs" ))
475 r=quotactl (Q_GETQUOTA, mnt->mnt_fsname, euser_id, (caddr_t) &D);
477 seteuid(euser_id); /* Restore the original uid status. */
482 /* Use softlimit to determine disk space, except when it has been exceeded */
484 (D.dqb_bsoftlimit && D.dqb_curblocks>=D.dqb_bsoftlimit) ||
485 (D.dqb_bhardlimit && D.dqb_curblocks>=D.dqb_bhardlimit) ||
486 (D.dqb_fsoftlimit && D.dqb_curfiles>=D.dqb_fsoftlimit) ||
487 (D.dqb_fhardlimit && D.dqb_curfiles>=D.dqb_fhardlimit)
491 *dsize = D.dqb_curblocks;
493 else if (D.dqb_bsoftlimit==0 && D.dqb_bhardlimit==0)
499 *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
500 *dsize = D.dqb_bsoftlimit;
504 else if ( 0 == strcmp ( mnt->mnt_type, "xfs" ))
506 r=quotactl (Q_XGETQUOTA, mnt->mnt_fsname, euser_id, (caddr_t) &F);
508 seteuid(euser_id); /* Restore the original uid status. */
513 /* Use softlimit to determine disk space, except when it has been exceeded */
515 (F.d_blk_softlimit && F.d_bcount>=F.d_blk_softlimit) ||
516 (F.d_blk_hardlimit && F.d_bcount>=F.d_blk_hardlimit) ||
517 (F.d_ino_softlimit && F.d_icount>=F.d_ino_softlimit) ||
518 (F.d_ino_hardlimit && F.d_icount>=F.d_ino_hardlimit)
522 * Fixme!: these are __uint64_t, this may truncate values
525 *dsize = (int) F.d_bcount;
527 else if (F.d_blk_softlimit==0 && F.d_blk_hardlimit==0)
533 *dfree = (int)(F.d_blk_softlimit - F.d_bcount);
534 *dsize = (int)F.d_blk_softlimit;
540 seteuid(euser_id); /* Restore the original uid status. */
550 #if defined(__FreeBSD__) || defined(__OpenBSD__)
551 #include <ufs/ufs/quota.h>
552 #include <machine/param.h>
554 /* AIX quota patch from Ole Holm Nielsen <ohnielse@fysik.dtu.dk> */
555 #include <jfs/quota.h>
556 /* AIX 4.X: Rename members of the dqblk structure (ohnielse@fysik.dtu.dk) */
557 #define dqb_curfiles dqb_curinodes
558 #define dqb_fhardlimit dqb_ihardlimit
559 #define dqb_fsoftlimit dqb_isoftlimit
560 #else /* !__FreeBSD__ && !AIX && !__OpenBSD__ */
561 #include <sys/quota.h>
565 /****************************************************************************
566 try to get the disk space from disk quotas - default version
567 ****************************************************************************/
568 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
573 #if !defined(__FreeBSD__) && !defined(AIX) && !defined(__OpenBSD__)
576 /* find the block device file */
577 if ((sys_stat(path, &S)<0) ||
578 (devnm(S_IFBLK, S.st_dev, dev_disk, 256, 0)<0)) return (False);
581 euser_id = geteuid();
587 /* for HPUX, real uid must be same as euid to execute quotactl for euid */
589 setresuid(euser_id,-1,-1);
590 r=quotactl(Q_GETQUOTA, dev_disk, euser_id, &D);
591 if (setresuid(user_id,-1,-1))
592 DEBUG(5,("Unable to reset uid to %d\n", user_id));
595 #if defined(__FreeBSD__) || defined(__OpenBSD__)
597 /* FreeBSD patches from Marty Moll <martym@arbor.edu> */
601 /* Need to be root to get quotas in FreeBSD */
606 r= quotactl(path,QCMD(Q_GETQUOTA,USRQUOTA),euser_id,(char *) &D);
608 /* As FreeBSD has group quotas, if getting the user
609 quota fails, try getting the group instead. */
611 r= quotactl(path,QCMD(Q_GETQUOTA,GRPQUOTA),egrp_id,(char *) &D);
616 /* AIX has both USER and GROUP quotas:
617 Get the USER quota (ohnielse@fysik.dtu.dk) */
618 r= quotactl(path,QCMD(Q_GETQUOTA,USRQUOTA),euser_id,(char *) &D);
619 #else /* !__FreeBSD__ && !AIX && !__OpenBSD__ */
620 r=quotactl(Q_GETQUOTA, dev_disk, euser_id, &D);
621 #endif /* !__FreeBSD__ && !AIX && !__OpenBSD__ */
622 #endif /* HAVE_SETRES */
624 /* Use softlimit to determine disk space, except when it has been exceeded */
625 #if defined(__FreeBSD__) || defined(__OpenBSD__)
627 #else /* !__FreeBSD__ && !__OpenBSD__ */
629 #endif /*!__FreeBSD__ && !__OpenBSD__ */
636 *dsize =D.dqb_curblocks;
641 if (D.dqb_bsoftlimit==0)
643 /* Use softlimit to determine disk space, except when it has been exceeded */
644 if ((D.dqb_curblocks>D.dqb_bsoftlimit)
645 #if !defined(__FreeBSD__) && !defined(__OpenBSD__)
646 ||((D.dqb_curfiles>D.dqb_fsoftlimit) && (D.dqb_fsoftlimit != 0))
650 *dsize = D.dqb_curblocks;
653 *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
654 *dsize = D.dqb_bsoftlimit;