3 Unix SMB/Netbios implementation.
6 Copyright (C) Andrew Tridgell 1992-1997
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * This is one of the most system dependent parts of Samba, and its
26 * done a litle differently. Each system has its own way of doing
32 extern int DEBUGLEVEL;
36 #include <sys/types.h>
37 #include <asm/types.h>
38 #include <sys/quota.h>
41 #include <linux/unistd.h>
43 _syscall4(int, quotactl, int, cmd, const char *, special, int, id, caddr_t, addr);
45 /****************************************************************************
46 try to get the disk space from disk quotas (LINUX version)
47 ****************************************************************************/
49 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
60 /* find the block device file */
62 if ( stat(path, &S) == -1 ) {
68 fp = setmntent(MOUNTED,"r");
71 while ((mnt = getmntent(fp))) {
72 if ( stat(mnt->mnt_dir,&S) == -1 )
74 if (S.st_dev == devno) {
87 r=quotactl(QCMD(Q_GETQUOTA,USRQUOTA), mnt->mnt_fsname, euser_id, (caddr_t)&D);
90 /* Use softlimit to determine disk space, except when it has been exceeded */
97 *dsize =D.dqb_curblocks;
102 /* Use softlimit to determine disk space, except when it has been exceeded */
104 (D.dqb_bsoftlimit && D.dqb_curblocks>=D.dqb_bsoftlimit) ||
105 (D.dqb_bhardlimit && D.dqb_curblocks>=D.dqb_bhardlimit) ||
106 (D.dqb_isoftlimit && D.dqb_curinodes>=D.dqb_isoftlimit) ||
107 (D.dqb_ihardlimit && D.dqb_curinodes>=D.dqb_ihardlimit)
111 *dsize = D.dqb_curblocks;
113 else if (D.dqb_bsoftlimit==0 && D.dqb_bhardlimit==0)
118 *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
119 *dsize = D.dqb_bsoftlimit;
126 #include <sys/quota.h>
129 /****************************************************************************
130 try to get the disk space from disk quotas (CRAY VERSION)
131 ****************************************************************************/
132 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
138 static dev_t devno_cached = 0 ;
139 static char name[MNTMAXSTR] ;
140 struct q_request request ;
141 struct qf_header header ;
142 static int quota_default = 0 ;
145 if ( stat(path,&sbuf) == -1 )
148 devno = sbuf.st_dev ;
150 if ( devno != devno_cached ) {
152 devno_cached = devno ;
154 if ((fd = setmntent(KMTAB)) == NULL)
159 while ((mnt = getmntent(fd)) != NULL) {
161 if ( stat(mnt->mnt_dir,&sbuf) == -1 )
164 if (sbuf.st_dev == devno) {
173 strcpy(name,mnt->mnt_dir) ;
180 request.qf_magic = QF_MAGIC ;
181 request.qf_entry.id = geteuid() ;
183 if (quotactl(name, Q_GETQUOTA, &request) == -1)
186 if ( ! request.user )
189 if ( request.qf_entry.user_q.f_quota == QFV_DEFAULT ) {
191 if ( ! quota_default ) {
193 if ( quotactl(name, Q_GETHEADER, &header) == -1 )
196 quota_default = header.user_h.def_fq ;
199 *dfree = quota_default ;
201 }else if ( request.qf_entry.user_q.f_quota == QFV_PREVENT ) {
207 *dfree = request.qf_entry.user_q.f_quota ;
211 *dsize = request.qf_entry.user_q.f_use ;
219 *bsize = 4096 ; /* Cray blocksize */
226 #elif defined(SUNOS5) || defined(SUNOS4)
230 #include <sys/fs/ufs_quota.h>
231 #include <sys/mnttab.h>
232 #else /* defined(SUNOS4) */
233 #include <ufs/quota.h>
237 /****************************************************************************
238 try to get the disk space from disk quotas (solaris 2 version)
239 ****************************************************************************/
240 /* Quota code by Peter Urbanec (amiga@cse.unsw.edu.au) */
241 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
243 uid_t user_id, euser_id;
247 struct quotctl command;
250 static char name[MNT_LINE_MAX] ;
253 static char name[MNTMAXSTR] ;
258 static dev_t devno_cached = 0 ;
261 if ( stat(path,&sbuf) == -1 )
264 devno = sbuf.st_dev ;
265 DEBUG(5,("disk_quotas: looking for path \"%s\" devno=%o\n", path,devno));
266 if ( devno != devno_cached ) {
267 devno_cached = devno ;
269 if ((fd = fopen(MNTTAB, "r")) == NULL)
273 while (getmntent(fd, &mnt) == 0) {
274 if ( stat(mnt.mnt_mountp,&sbuf) == -1 )
276 DEBUG(5,("disk_quotas: testing \"%s\" devno=%o\n",
277 mnt.mnt_mountp,sbuf.st_dev));
278 if (sbuf.st_dev == devno) {
284 strcpy(name,mnt.mnt_mountp) ;
285 strcat(name,"/quotas") ;
288 if ((fd = setmntent(MOUNTED, "r")) == NULL)
292 while ((mnt = getmntent(fd)) != NULL) {
293 if ( stat(mnt->mnt_dir,&sbuf) == -1 )
295 DEBUG(5,("disk_quotas: testing \"%s\" devno=%o\n",
296 mnt->mnt_dir,sbuf.st_dev));
297 if (sbuf.st_dev == devno) {
303 strcpy(name,mnt->mnt_fsname) ;
311 euser_id = geteuid();
314 setuid(0); /* Solaris seems to want to give info only to super-user */
318 DEBUG(5,("disk_quotas: looking for quotas file \"%s\"\n", name));
319 if((file=open(name, O_RDONLY))<0) {
320 setuid(user_id); /* Restore the original UID status */
324 command.op = Q_GETQUOTA;
325 command.uid = euser_id;
326 command.addr = (caddr_t) &D;
327 ret = ioctl(file, Q_QUOTACTL, &command);
330 DEBUG(5,("disk_quotas: trying quotactl on device \"%s\"\n", name));
331 ret = quotactl(Q_GETQUOTA, name, euser_id, &D);
334 setuid(user_id); /* Restore the original uid status. */
338 DEBUG(2,("disk_quotas ioctl (Solaris) failed\n"));
343 /* Use softlimit to determine disk space. A user exceeding the quota is told
344 * that there's no space left. Writes might actually work for a bit if the
345 * hardlimit is set higher than softlimit. Effectively the disk becomes
346 * made of rubber latex and begins to expand to accommodate the user :-)
349 if (D.dqb_bsoftlimit==0)
352 *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
353 *dsize = D.dqb_bsoftlimit;
357 *dsize = D.dqb_curblocks;
360 DEBUG(5,("disk_quotas for path \"%s\" returning bsize %d, dfree %d, dsize %d\n",
361 path,*bsize,*dfree,*dsize));
368 #include <ufs/quota.h>
370 /****************************************************************************
371 try to get the disk space from disk quotas - OFS1 version
372 ****************************************************************************/
373 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
375 uid_t user_id, euser_id;
380 euser_id = geteuid();
383 setreuid(euser_id, -1);
384 r= quotactl(path,QCMD(Q_GETQUOTA, USRQUOTA),euser_id,(char *) &D);
388 if (setreuid(user_id, -1) == -1)
389 DEBUG(5,("Unable to reset uid to %d\n", user_id));
395 if (save_errno == EDQUOT) // disk quota exceeded
398 *dsize = D.dqb_curblocks;
405 /* Use softlimit to determine disk space, except when it has been exceeded */
407 if (D.dqb_bsoftlimit==0)
410 if ((D.dqb_curblocks>D.dqb_bsoftlimit)) {
412 *dsize = D.dqb_curblocks;
414 *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
415 *dsize = D.dqb_bsoftlimit;
421 /****************************************************************************
422 try to get the disk space from disk quotas (IRIX 6.2 version)
423 ****************************************************************************/
425 #include <sys/quota.h>
428 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
433 struct fs_disk_quota F;
440 /* find the block device file */
442 if ( stat(path, &S) == -1 ) {
448 fp = setmntent(MOUNTED,"r");
451 while ((mnt = getmntent(fp))) {
452 if ( stat(mnt->mnt_dir,&S) == -1 )
454 if (S.st_dev == devno) {
468 /* Use softlimit to determine disk space, except when it has been exceeded */
472 if ( 0 == strcmp ( mnt->mnt_type, "efs" ))
474 r=quotactl (Q_GETQUOTA, mnt->mnt_fsname, euser_id, (caddr_t) &D);
476 seteuid(euser_id); /* Restore the original uid status. */
481 /* Use softlimit to determine disk space, except when it has been exceeded */
483 (D.dqb_bsoftlimit && D.dqb_curblocks>=D.dqb_bsoftlimit) ||
484 (D.dqb_bhardlimit && D.dqb_curblocks>=D.dqb_bhardlimit) ||
485 (D.dqb_fsoftlimit && D.dqb_curfiles>=D.dqb_fsoftlimit) ||
486 (D.dqb_fhardlimit && D.dqb_curfiles>=D.dqb_fhardlimit)
490 *dsize = D.dqb_curblocks;
492 else if (D.dqb_bsoftlimit==0 && D.dqb_bhardlimit==0)
498 *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
499 *dsize = D.dqb_bsoftlimit;
503 else if ( 0 == strcmp ( mnt->mnt_type, "xfs" ))
505 r=quotactl (Q_XGETQUOTA, mnt->mnt_fsname, euser_id, (caddr_t) &F);
507 seteuid(euser_id); /* Restore the original uid status. */
512 /* Use softlimit to determine disk space, except when it has been exceeded */
514 (F.d_blk_softlimit && F.d_bcount>=F.d_blk_softlimit) ||
515 (F.d_blk_hardlimit && F.d_bcount>=F.d_blk_hardlimit) ||
516 (F.d_ino_softlimit && F.d_icount>=F.d_ino_softlimit) ||
517 (F.d_ino_hardlimit && F.d_icount>=F.d_ino_hardlimit)
521 * Fixme!: these are __uint64_t, this may truncate values
524 *dsize = (int) F.d_bcount;
526 else if (F.d_blk_softlimit==0 && F.d_blk_hardlimit==0)
532 *dfree = (int)(F.d_blk_softlimit - F.d_bcount);
533 *dsize = (int)F.d_blk_softlimit;
539 seteuid(euser_id); /* Restore the original uid status. */
550 #include <ufs/ufs/quota.h>
551 #include <machine/param.h>
553 /* AIX quota patch from Ole Holm Nielsen <ohnielse@fysik.dtu.dk> */
554 #include <jfs/quota.h>
555 /* AIX 4.X: Rename members of the dqblk structure (ohnielse@fysik.dtu.dk) */
556 #define dqb_curfiles dqb_curinodes
557 #define dqb_fhardlimit dqb_ihardlimit
558 #define dqb_fsoftlimit dqb_isoftlimit
559 #else /* !__FreeBSD__ && !AIX */
560 #include <sys/quota.h>
564 /****************************************************************************
565 try to get the disk space from disk quotas - default version
566 ****************************************************************************/
567 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
572 #if !defined(__FreeBSD__) && !defined(AIX)
575 /* find the block device file */
576 if ((stat(path, &S)<0) ||
577 (devnm(S_IFBLK, S.st_dev, dev_disk, 256, 0)<0)) return (False);
580 euser_id = geteuid();
586 /* for HPUX, real uid must be same as euid to execute quotactl for euid */
588 setresuid(euser_id,-1,-1);
589 r=quotactl(Q_GETQUOTA, dev_disk, euser_id, &D);
590 if (setresuid(user_id,-1,-1))
591 DEBUG(5,("Unable to reset uid to %d\n", user_id));
593 #else /* USE_SETRES */
594 #if defined(__FreeBSD__)
596 /* FreeBSD patches from Marty Moll <martym@arbor.edu> */
600 /* Need to be root to get quotas in FreeBSD */
605 r= quotactl(path,QCMD(Q_GETQUOTA,USRQUOTA),euser_id,(char *) &D);
607 /* As FreeBSD has group quotas, if getting the user
608 quota fails, try getting the group instead. */
610 r= quotactl(path,QCMD(Q_GETQUOTA,GRPQUOTA),egrp_id,(char *) &D);
615 /* AIX has both USER and GROUP quotas:
616 Get the USER quota (ohnielse@fysik.dtu.dk) */
617 r= quotactl(path,QCMD(Q_GETQUOTA,USRQUOTA),euser_id,(char *) &D);
618 #else /* !__FreeBSD__ && !AIX */
619 r=quotactl(Q_GETQUOTA, dev_disk, euser_id, &D);
620 #endif /* !__FreeBSD__ && !AIX */
621 #endif /* USE_SETRES */
623 /* Use softlimit to determine disk space, except when it has been exceeded */
624 #if defined(__FreeBSD__)
626 #else /* !__FreeBSD__ */
628 #endif /*!__FreeBSD__ */
635 *dsize =D.dqb_curblocks;
640 if (D.dqb_bsoftlimit==0)
642 /* Use softlimit to determine disk space, except when it has been exceeded */
643 if ((D.dqb_curblocks>D.dqb_bsoftlimit)
644 #if !defined(__FreeBSD__)
645 ||((D.dqb_curfiles>D.dqb_fsoftlimit) && (D.dqb_fsoftlimit != 0))
649 *dsize = D.dqb_curblocks;
652 *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
653 *dsize = D.dqb_bsoftlimit;
661 /* this keeps fussy compilers happy */
662 void quotas_dummy(void) {}