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, SMB_BIG_UINT *bsize, SMB_BIG_UINT *dfree, SMB_BIG_UINT *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 ****************************************************************************/
134 BOOL disk_quotas(char *path, SMB_BIG_UINT *bsize, SMB_BIG_UINT *dfree, SMB_BIG_UINT *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)
231 #include <sys/param.h>
233 #include <sys/fs/ufs_quota.h>
234 #include <sys/mnttab.h>
235 #else /* defined(SUNOS4) */
236 #include <ufs/quota.h>
240 /****************************************************************************
241 try to get the disk space from disk quotas (SunOS & Solaris2 version)
242 Quota code by Peter Urbanec (amiga@cse.unsw.edu.au).
243 ****************************************************************************/
245 BOOL disk_quotas(char *path, SMB_BIG_UINT *bsize, SMB_BIG_UINT *dfree, SMB_BIG_UINT *dsize)
247 uid_t user_id, euser_id;
251 struct quotctl command;
260 SMB_STRUCT_STAT sbuf;
262 static SMB_DEV_T devno_cached = 0 ;
265 if ( sys_stat(path,&sbuf) == -1 )
268 devno = sbuf.st_dev ;
269 DEBUG(5,("disk_quotas: looking for path \"%s\" devno=%o\n", path,devno));
270 if ( devno != devno_cached ) {
271 devno_cached = devno ;
273 if ((fd = fopen(MNTTAB, "r")) == NULL)
277 while (getmntent(fd, &mnt) == 0) {
278 if ( sys_stat(mnt.mnt_mountp,&sbuf) == -1 )
280 DEBUG(5,("disk_quotas: testing \"%s\" devno=%o\n",
281 mnt.mnt_mountp,sbuf.st_dev));
282 if (sbuf.st_dev == devno) {
288 pstrcpy(name,mnt.mnt_mountp) ;
289 pstrcat(name,"/quotas") ;
292 if ((fd = setmntent(MOUNTED, "r")) == NULL)
296 while ((mnt = getmntent(fd)) != NULL) {
297 if ( sys_stat(mnt->mnt_dir,&sbuf) == -1 )
299 DEBUG(5,("disk_quotas: testing \"%s\" devno=%o\n",
300 mnt->mnt_dir,sbuf.st_dev));
301 if (sbuf.st_dev == devno) {
307 pstrcpy(name,mnt->mnt_fsname) ;
315 euser_id = geteuid();
318 setuid(0); /* Solaris seems to want to give info only to super-user */
322 DEBUG(5,("disk_quotas: looking for quotas file \"%s\"\n", name));
323 if((file=open(name, O_RDONLY))<0) {
324 setuid(user_id); /* Restore the original UID status */
328 command.op = Q_GETQUOTA;
329 command.uid = euser_id;
330 command.addr = (caddr_t) &D;
331 ret = ioctl(file, Q_QUOTACTL, &command);
334 DEBUG(5,("disk_quotas: trying quotactl on device \"%s\"\n", name));
335 ret = quotactl(Q_GETQUOTA, name, euser_id, &D);
338 setuid(user_id); /* Restore the original uid status. */
342 DEBUG(5,("disk_quotas ioctl (Solaris) failed. Error = %s\n", strerror(errno) ));
347 /* Use softlimit to determine disk space. A user exceeding the quota is told
348 * that there's no space left. Writes might actually work for a bit if the
349 * hardlimit is set higher than softlimit. Effectively the disk becomes
350 * made of rubber latex and begins to expand to accommodate the user :-)
353 if (D.dqb_bsoftlimit==0)
356 *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
357 *dsize = D.dqb_bsoftlimit;
362 *dsize = D.dqb_curblocks;
365 DEBUG(5,("disk_quotas for path \"%s\" returning bsize %.0f, dfree %.0f, dsize %.0f\n",
366 path,(double)*bsize,(double)*dfree,(double)*dsize));
373 #include <ufs/quota.h>
375 /****************************************************************************
376 try to get the disk space from disk quotas - OFS1 version
377 ****************************************************************************/
379 BOOL disk_quotas(char *path, SMB_BIG_UINT *bsize, SMB_BIG_UINT *dfree, SMB_BIG_UINT *dsize)
381 uid_t user_id, euser_id;
386 euser_id = geteuid();
389 setreuid(euser_id, -1);
390 r= quotactl(path,QCMD(Q_GETQUOTA, USRQUOTA),euser_id,(char *) &D);
394 if (setreuid(user_id, -1) == -1)
395 DEBUG(5,("Unable to reset uid to %d\n", user_id));
401 if (save_errno == EDQUOT) // disk quota exceeded
404 *dsize = D.dqb_curblocks;
411 /* Use softlimit to determine disk space, except when it has been exceeded */
413 if (D.dqb_bsoftlimit==0)
416 if ((D.dqb_curblocks>D.dqb_bsoftlimit)) {
418 *dsize = D.dqb_curblocks;
420 *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
421 *dsize = D.dqb_bsoftlimit;
427 /****************************************************************************
428 try to get the disk space from disk quotas (IRIX 6.2 version)
429 ****************************************************************************/
431 #include <sys/quota.h>
434 BOOL disk_quotas(char *path, SMB_BIG_UINT *bsize, SMB_BIG_UINT *dfree, SMB_BIG_UINT *dsize)
439 struct fs_disk_quota F;
446 /* find the block device file */
448 if ( sys_stat(path, &S) == -1 ) {
454 fp = setmntent(MOUNTED,"r");
457 while ((mnt = getmntent(fp))) {
458 if ( sys_stat(mnt->mnt_dir,&S) == -1 )
460 if (S.st_dev == devno) {
474 /* Use softlimit to determine disk space, except when it has been exceeded */
478 if ( 0 == strcmp ( mnt->mnt_type, "efs" ))
480 r=quotactl (Q_GETQUOTA, mnt->mnt_fsname, euser_id, (caddr_t) &D);
482 seteuid(euser_id); /* Restore the original uid status. */
487 /* Use softlimit to determine disk space, except when it has been exceeded */
489 (D.dqb_bsoftlimit && D.dqb_curblocks>=D.dqb_bsoftlimit) ||
490 (D.dqb_bhardlimit && D.dqb_curblocks>=D.dqb_bhardlimit) ||
491 (D.dqb_fsoftlimit && D.dqb_curfiles>=D.dqb_fsoftlimit) ||
492 (D.dqb_fhardlimit && D.dqb_curfiles>=D.dqb_fhardlimit)
496 *dsize = D.dqb_curblocks;
498 else if (D.dqb_bsoftlimit==0 && D.dqb_bhardlimit==0)
504 *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
505 *dsize = D.dqb_bsoftlimit;
509 else if ( 0 == strcmp ( mnt->mnt_type, "xfs" ))
511 r=quotactl (Q_XGETQUOTA, mnt->mnt_fsname, euser_id, (caddr_t) &F);
513 seteuid(euser_id); /* Restore the original uid status. */
518 /* Use softlimit to determine disk space, except when it has been exceeded */
520 (F.d_blk_softlimit && F.d_bcount>=F.d_blk_softlimit) ||
521 (F.d_blk_hardlimit && F.d_bcount>=F.d_blk_hardlimit) ||
522 (F.d_ino_softlimit && F.d_icount>=F.d_ino_softlimit) ||
523 (F.d_ino_hardlimit && F.d_icount>=F.d_ino_hardlimit)
529 else if (F.d_blk_softlimit==0 && F.d_blk_hardlimit==0)
535 *dfree = (F.d_blk_softlimit - F.d_bcount);
536 *dsize = F.d_blk_softlimit;
542 seteuid(euser_id); /* Restore the original uid status. */
552 #if defined(__FreeBSD__) || defined(__OpenBSD__)
553 #include <ufs/ufs/quota.h>
554 #include <machine/param.h>
556 /* AIX quota patch from Ole Holm Nielsen <ohnielse@fysik.dtu.dk> */
557 #include <jfs/quota.h>
558 /* AIX 4.X: Rename members of the dqblk structure (ohnielse@fysik.dtu.dk) */
559 #define dqb_curfiles dqb_curinodes
560 #define dqb_fhardlimit dqb_ihardlimit
561 #define dqb_fsoftlimit dqb_isoftlimit
562 #else /* !__FreeBSD__ && !AIX && !__OpenBSD__ */
563 #include <sys/quota.h>
567 /****************************************************************************
568 try to get the disk space from disk quotas - default version
569 ****************************************************************************/
571 BOOL disk_quotas(char *path, SMB_BIG_UINT *bsize, SMB_BIG_UINT *dfree, SMB_BIG_UINT *dsize)
576 #if !defined(__FreeBSD__) && !defined(AIX) && !defined(__OpenBSD__)
579 /* find the block device file */
580 if ((sys_stat(path, &S)<0) ||
581 (devnm(S_IFBLK, S.st_dev, dev_disk, 256, 0)<0)) return (False);
584 euser_id = geteuid();
590 /* for HPUX, real uid must be same as euid to execute quotactl for euid */
592 setresuid(euser_id,-1,-1);
593 r=quotactl(Q_GETQUOTA, dev_disk, euser_id, &D);
594 if (setresuid(user_id,-1,-1))
595 DEBUG(5,("Unable to reset uid to %d\n", user_id));
598 #if defined(__FreeBSD__) || defined(__OpenBSD__)
600 /* FreeBSD patches from Marty Moll <martym@arbor.edu> */
604 /* Need to be root to get quotas in FreeBSD */
609 r= quotactl(path,QCMD(Q_GETQUOTA,USRQUOTA),euser_id,(char *) &D);
611 /* As FreeBSD has group quotas, if getting the user
612 quota fails, try getting the group instead. */
614 r= quotactl(path,QCMD(Q_GETQUOTA,GRPQUOTA),egrp_id,(char *) &D);
619 /* AIX has both USER and GROUP quotas:
620 Get the USER quota (ohnielse@fysik.dtu.dk) */
621 r= quotactl(path,QCMD(Q_GETQUOTA,USRQUOTA),euser_id,(char *) &D);
622 #else /* !__FreeBSD__ && !AIX && !__OpenBSD__ */
623 r=quotactl(Q_GETQUOTA, dev_disk, euser_id, &D);
624 #endif /* !__FreeBSD__ && !AIX && !__OpenBSD__ */
625 #endif /* HAVE_SETRES */
627 /* Use softlimit to determine disk space, except when it has been exceeded */
628 #if defined(__FreeBSD__) || defined(__OpenBSD__)
630 #else /* !__FreeBSD__ && !__OpenBSD__ */
632 #endif /*!__FreeBSD__ && !__OpenBSD__ */
639 *dsize =D.dqb_curblocks;
644 if (D.dqb_bsoftlimit==0)
646 /* Use softlimit to determine disk space, except when it has been exceeded */
647 if ((D.dqb_curblocks>D.dqb_bsoftlimit)
648 #if !defined(__FreeBSD__) && !defined(__OpenBSD__)
649 ||((D.dqb_curfiles>D.dqb_fsoftlimit) && (D.dqb_fsoftlimit != 0))
653 *dsize = D.dqb_curblocks;
656 *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
657 *dsize = D.dqb_bsoftlimit;