3 Unix SMB/Netbios implementation.
6 Copyright (C) Andrew Tridgell 1992-1998
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 ( sys_stat(path, &S) == -1 ) {
68 fp = setmntent(MOUNTED,"r");
71 while ((mnt = getmntent(fp))) {
72 if ( sys_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 if (D.dqb_bsoftlimit == 0)
119 D.dqb_bsoftlimit = D.dqb_bhardlimit;
120 *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
121 *dsize = D.dqb_bsoftlimit;
128 #include <sys/quota.h>
131 /****************************************************************************
132 try to get the disk space from disk quotas (CRAY VERSION)
133 ****************************************************************************/
134 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
138 SMB_STRUCT_STAT sbuf;
140 static SMB_DEV_T devno_cached = 0 ;
142 struct q_request request ;
143 struct qf_header header ;
144 static int quota_default = 0 ;
147 if ( sys_stat(path,&sbuf) == -1 )
150 devno = sbuf.st_dev ;
152 if ( devno != devno_cached ) {
154 devno_cached = devno ;
156 if ((fd = setmntent(KMTAB)) == NULL)
161 while ((mnt = getmntent(fd)) != NULL) {
163 if ( sys_stat(mnt->mnt_dir,&sbuf) == -1 )
166 if (sbuf.st_dev == devno) {
175 pstrcpy(name,mnt->mnt_dir) ;
182 request.qf_magic = QF_MAGIC ;
183 request.qf_entry.id = geteuid() ;
185 if (quotactl(name, Q_GETQUOTA, &request) == -1)
188 if ( ! request.user )
191 if ( request.qf_entry.user_q.f_quota == QFV_DEFAULT ) {
193 if ( ! quota_default ) {
195 if ( quotactl(name, Q_GETHEADER, &header) == -1 )
198 quota_default = header.user_h.def_fq ;
201 *dfree = quota_default ;
203 }else if ( request.qf_entry.user_q.f_quota == QFV_PREVENT ) {
209 *dfree = request.qf_entry.user_q.f_quota ;
213 *dsize = request.qf_entry.user_q.f_use ;
221 *bsize = 4096 ; /* Cray blocksize */
228 #elif defined(SUNOS5) || defined(SUNOS4)
232 #include <sys/fs/ufs_quota.h>
233 #include <sys/mnttab.h>
234 #else /* defined(SUNOS4) */
235 #include <ufs/quota.h>
239 /****************************************************************************
240 try to get the disk space from disk quotas (solaris 2 version)
241 ****************************************************************************/
242 /* Quota code by Peter Urbanec (amiga@cse.unsw.edu.au) */
243 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
245 uid_t user_id, euser_id;
249 struct quotctl command;
258 SMB_STRUCT_STAT sbuf;
260 static SMB_DEV_T devno_cached = 0 ;
263 if ( sys_stat(path,&sbuf) == -1 )
266 devno = sbuf.st_dev ;
267 DEBUG(5,("disk_quotas: looking for path \"%s\" devno=%o\n", path,devno));
268 if ( devno != devno_cached ) {
269 devno_cached = devno ;
271 if ((fd = fopen(MNTTAB, "r")) == NULL)
275 while (getmntent(fd, &mnt) == 0) {
276 if ( sys_stat(mnt.mnt_mountp,&sbuf) == -1 )
278 DEBUG(5,("disk_quotas: testing \"%s\" devno=%o\n",
279 mnt.mnt_mountp,sbuf.st_dev));
280 if (sbuf.st_dev == devno) {
286 pstrcpy(name,mnt.mnt_mountp) ;
287 pstrcat(name,"/quotas") ;
290 if ((fd = setmntent(MOUNTED, "r")) == NULL)
294 while ((mnt = getmntent(fd)) != NULL) {
295 if ( sys_stat(mnt->mnt_dir,&sbuf) == -1 )
297 DEBUG(5,("disk_quotas: testing \"%s\" devno=%o\n",
298 mnt->mnt_dir,sbuf.st_dev));
299 if (sbuf.st_dev == devno) {
305 pstrcpy(name,mnt->mnt_fsname) ;
313 euser_id = geteuid();
316 setuid(0); /* Solaris seems to want to give info only to super-user */
320 DEBUG(5,("disk_quotas: looking for quotas file \"%s\"\n", name));
321 if((file=open(name, O_RDONLY))<0) {
322 setuid(user_id); /* Restore the original UID status */
326 command.op = Q_GETQUOTA;
327 command.uid = euser_id;
328 command.addr = (caddr_t) &D;
329 ret = ioctl(file, Q_QUOTACTL, &command);
332 DEBUG(5,("disk_quotas: trying quotactl on device \"%s\"\n", name));
333 ret = quotactl(Q_GETQUOTA, name, euser_id, &D);
336 setuid(user_id); /* Restore the original uid status. */
340 DEBUG(2,("disk_quotas ioctl (Solaris) failed\n"));
345 /* Use softlimit to determine disk space. A user exceeding the quota is told
346 * that there's no space left. Writes might actually work for a bit if the
347 * hardlimit is set higher than softlimit. Effectively the disk becomes
348 * made of rubber latex and begins to expand to accommodate the user :-)
351 if (D.dqb_bsoftlimit==0)
354 *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
355 *dsize = D.dqb_bsoftlimit;
359 *dsize = D.dqb_curblocks;
362 DEBUG(5,("disk_quotas for path \"%s\" returning bsize %d, dfree %d, dsize %d\n",
363 path,*bsize,*dfree,*dsize));
370 #include <ufs/quota.h>
372 /****************************************************************************
373 try to get the disk space from disk quotas - OFS1 version
374 ****************************************************************************/
375 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
377 uid_t user_id, euser_id;
382 euser_id = geteuid();
385 setreuid(euser_id, -1);
386 r= quotactl(path,QCMD(Q_GETQUOTA, USRQUOTA),euser_id,(char *) &D);
390 if (setreuid(user_id, -1) == -1)
391 DEBUG(5,("Unable to reset uid to %d\n", user_id));
397 if (save_errno == EDQUOT) // disk quota exceeded
400 *dsize = D.dqb_curblocks;
407 /* Use softlimit to determine disk space, except when it has been exceeded */
409 if (D.dqb_bsoftlimit==0)
412 if ((D.dqb_curblocks>D.dqb_bsoftlimit)) {
414 *dsize = D.dqb_curblocks;
416 *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
417 *dsize = D.dqb_bsoftlimit;
423 /****************************************************************************
424 try to get the disk space from disk quotas (IRIX 6.2 version)
425 ****************************************************************************/
427 #include <sys/quota.h>
430 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
435 struct fs_disk_quota F;
442 /* find the block device file */
444 if ( sys_stat(path, &S) == -1 ) {
450 fp = setmntent(MOUNTED,"r");
453 while ((mnt = getmntent(fp))) {
454 if ( sys_stat(mnt->mnt_dir,&S) == -1 )
456 if (S.st_dev == devno) {
470 /* Use softlimit to determine disk space, except when it has been exceeded */
474 if ( 0 == strcmp ( mnt->mnt_type, "efs" ))
476 r=quotactl (Q_GETQUOTA, mnt->mnt_fsname, euser_id, (caddr_t) &D);
478 seteuid(euser_id); /* Restore the original uid status. */
483 /* Use softlimit to determine disk space, except when it has been exceeded */
485 (D.dqb_bsoftlimit && D.dqb_curblocks>=D.dqb_bsoftlimit) ||
486 (D.dqb_bhardlimit && D.dqb_curblocks>=D.dqb_bhardlimit) ||
487 (D.dqb_fsoftlimit && D.dqb_curfiles>=D.dqb_fsoftlimit) ||
488 (D.dqb_fhardlimit && D.dqb_curfiles>=D.dqb_fhardlimit)
492 *dsize = D.dqb_curblocks;
494 else if (D.dqb_bsoftlimit==0 && D.dqb_bhardlimit==0)
500 *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
501 *dsize = D.dqb_bsoftlimit;
505 else if ( 0 == strcmp ( mnt->mnt_type, "xfs" ))
507 r=quotactl (Q_XGETQUOTA, mnt->mnt_fsname, euser_id, (caddr_t) &F);
509 seteuid(euser_id); /* Restore the original uid status. */
514 /* Use softlimit to determine disk space, except when it has been exceeded */
516 (F.d_blk_softlimit && F.d_bcount>=F.d_blk_softlimit) ||
517 (F.d_blk_hardlimit && F.d_bcount>=F.d_blk_hardlimit) ||
518 (F.d_ino_softlimit && F.d_icount>=F.d_ino_softlimit) ||
519 (F.d_ino_hardlimit && F.d_icount>=F.d_ino_hardlimit)
523 * Fixme!: these are __uint64_t, this may truncate values
526 *dsize = (int) F.d_bcount;
528 else if (F.d_blk_softlimit==0 && F.d_blk_hardlimit==0)
534 *dfree = (int)(F.d_blk_softlimit - F.d_bcount);
535 *dsize = (int)F.d_blk_softlimit;
541 seteuid(euser_id); /* Restore the original uid status. */
551 #if defined(__FreeBSD__) || defined(__OpenBSD__)
552 #include <ufs/ufs/quota.h>
553 #include <machine/param.h>
555 /* AIX quota patch from Ole Holm Nielsen <ohnielse@fysik.dtu.dk> */
556 #include <jfs/quota.h>
557 /* AIX 4.X: Rename members of the dqblk structure (ohnielse@fysik.dtu.dk) */
558 #define dqb_curfiles dqb_curinodes
559 #define dqb_fhardlimit dqb_ihardlimit
560 #define dqb_fsoftlimit dqb_isoftlimit
561 #else /* !__FreeBSD__ && !AIX && !__OpenBSD__ */
562 #include <sys/quota.h>
566 /****************************************************************************
567 try to get the disk space from disk quotas - default version
568 ****************************************************************************/
569 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
574 #if !defined(__FreeBSD__) && !defined(AIX) && !defined(__OpenBSD__)
577 /* find the block device file */
578 if ((sys_stat(path, &S)<0) ||
579 (devnm(S_IFBLK, S.st_dev, dev_disk, 256, 0)<0)) return (False);
582 euser_id = geteuid();
588 /* for HPUX, real uid must be same as euid to execute quotactl for euid */
590 setresuid(euser_id,-1,-1);
591 r=quotactl(Q_GETQUOTA, dev_disk, euser_id, &D);
592 if (setresuid(user_id,-1,-1))
593 DEBUG(5,("Unable to reset uid to %d\n", user_id));
596 #if defined(__FreeBSD__) || defined(__OpenBSD__)
598 /* FreeBSD patches from Marty Moll <martym@arbor.edu> */
602 /* Need to be root to get quotas in FreeBSD */
607 r= quotactl(path,QCMD(Q_GETQUOTA,USRQUOTA),euser_id,(char *) &D);
609 /* As FreeBSD has group quotas, if getting the user
610 quota fails, try getting the group instead. */
612 r= quotactl(path,QCMD(Q_GETQUOTA,GRPQUOTA),egrp_id,(char *) &D);
617 /* AIX has both USER and GROUP quotas:
618 Get the USER quota (ohnielse@fysik.dtu.dk) */
619 r= quotactl(path,QCMD(Q_GETQUOTA,USRQUOTA),euser_id,(char *) &D);
620 #else /* !__FreeBSD__ && !AIX && !__OpenBSD__ */
621 r=quotactl(Q_GETQUOTA, dev_disk, euser_id, &D);
622 #endif /* !__FreeBSD__ && !AIX && !__OpenBSD__ */
623 #endif /* HAVE_SETRES */
625 /* Use softlimit to determine disk space, except when it has been exceeded */
626 #if defined(__FreeBSD__) || defined(__OpenBSD__)
628 #else /* !__FreeBSD__ && !__OpenBSD__ */
630 #endif /*!__FreeBSD__ && !__OpenBSD__ */
637 *dsize =D.dqb_curblocks;
642 if (D.dqb_bsoftlimit==0)
644 /* Use softlimit to determine disk space, except when it has been exceeded */
645 if ((D.dqb_curblocks>D.dqb_bsoftlimit)
646 #if !defined(__FreeBSD__) && !defined(__OpenBSD__)
647 ||((D.dqb_curfiles>D.dqb_fsoftlimit) && (D.dqb_fsoftlimit != 0))
651 *dsize = D.dqb_curblocks;
654 *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
655 *dsize = D.dqb_bsoftlimit;
663 /* this keeps fussy compilers happy */
664 void quotas_dummy(void) {}