1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License, Version 1.0 only
   6  * (the "License").  You may not use this file except in compliance
   7  * with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  * Copyright 1999 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 /*
  27  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
  28  */
  29 
  30 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T     */
  31 /*        All Rights Reserved   */
  32 
  33 /*
  34  * University Copyright- Copyright (c) 1982, 1986, 1988
  35  * The Regents of the University of California
  36  * All Rights Reserved
  37  *
  38  * University Acknowledgment- Portions of this document are derived from
  39  * software developed by the University of California, Berkeley, and its
  40  * contributors.
  41  */
  42 
  43 #ifndef _SYS_FS_UFS_QUOTA_H
  44 #define _SYS_FS_UFS_QUOTA_H
  45 
  46 #ifdef  __cplusplus
  47 extern "C" {
  48 #endif
  49 
  50 /*
  51  * Lock order for the quota sub-system:
  52  *
  53  *  vfs_dqrwlock > ip.i_contents > dq_cachelock > dquot.dq_lock > dq_freelock
  54  *  vfs_dqrwlock > ip.i_contents > dq_cachelock >                 dq_freelock
  55  *  vfs_dqrwlock > ip.i_contents >                dquot.dq_lock > dq_freelock
  56  *  vfs_dqrwlock > ip.i_contents >                                dq_freelock
  57  *  vfs_dqrwlock > ip.i_contents > dq_cachelock > dquot.dq_lock > qip.i_contents
  58  */
  59 
  60 /*
  61  * The following constants define the default amount of time given a user
  62  * before the soft limits are treated as hard limits (usually resulting
  63  * in an allocation failure). These may be  modified by the quotactl
  64  * system call with the Q_SETQLIM or Q_SETQUOTA commands.
  65  */
  66 
  67 #define DQ_FTIMELIMIT   (7 * 24*60*60)          /* 1 week */
  68 #define DQ_BTIMELIMIT   (7 * 24*60*60)          /* 1 week */
  69 
  70 /*
  71  * The dqblk structure defines the format of the disk quota file
  72  * (as it appears on disk) - the file is an array of these structures
  73  * indexed by user number.  The setquota sys call establishes the inode
  74  * for each quota file (a pointer is retained in the mount structure).
  75  */
  76 
  77 struct dqblk {
  78         uint32_t  dqb_bhardlimit; /* absolute limit on disk blks alloc */
  79         uint32_t  dqb_bsoftlimit; /* preferred limit on disk blks */
  80         uint32_t  dqb_curblocks;  /* current block count */
  81         uint32_t  dqb_fhardlimit; /* maximum # allocated files + 1 */
  82         uint32_t  dqb_fsoftlimit; /* preferred file limit */
  83         uint32_t  dqb_curfiles;   /* current # allocated files */
  84         uint32_t  dqb_btimelimit; /* time limit for excessive disk use */
  85         uint32_t  dqb_ftimelimit; /* time limit for excessive files */
  86 };
  87 
  88 #define dqoff(UID)      (((offset_t)(UID) * sizeof (struct dqblk)))
  89 
  90 /*
  91  * The dquot structure records disk usage for a user on a filesystem.
  92  * There is one allocated for each quota that exists on any filesystem
  93  * for the current user. A cache is kept of recently used entries.
  94  * Active inodes have a pointer to the dquot associated with them.
  95  */
  96 struct  dquot {
  97         struct dquot *dq_forw, *dq_back; /* hash list, MUST be first entry */
  98         struct dquot *dq_freef, *dq_freeb; /* free list */
  99         short   dq_flags;
 100 #define DQ_ERROR        0x01            /* An error occurred reading dq */
 101 #define DQ_MOD          0x04            /* this quota modified since read */
 102 #define DQ_BLKS         0x10            /* has been warned about blk limit */
 103 #define DQ_FILES        0x20            /* has been warned about file limit */
 104 #define DQ_TRANS        0x40            /* logging ufs operation started */
 105         ulong_t dq_cnt;                 /* count of active references */
 106         uid_t   dq_uid;                 /* user this applies to */
 107         struct ufsvfs *dq_ufsvfsp;      /* filesystem this relates to */
 108         offset_t dq_mof;                /* master disk offset of quota record */
 109         struct dqblk dq_dqb;            /* actual usage & quotas */
 110 #ifdef _KERNEL
 111         kmutex_t        dq_lock;        /* per dq structure lock */
 112 #endif /* _KERNEL */
 113 };
 114 
 115 #define dq_bhardlimit   dq_dqb.dqb_bhardlimit
 116 #define dq_bsoftlimit   dq_dqb.dqb_bsoftlimit
 117 #define dq_curblocks    dq_dqb.dqb_curblocks
 118 #define dq_fhardlimit   dq_dqb.dqb_fhardlimit
 119 #define dq_fsoftlimit   dq_dqb.dqb_fsoftlimit
 120 #define dq_curfiles     dq_dqb.dqb_curfiles
 121 #define dq_btimelimit   dq_dqb.dqb_btimelimit
 122 #define dq_ftimelimit   dq_dqb.dqb_ftimelimit
 123 
 124 /*
 125  * flags for vfs_qflags in ufsvfs struct
 126  */
 127 #define MQ_ENABLED      0x01            /* quotas are enabled */
 128 
 129 #if defined(_KERNEL)
 130 
 131 /*
 132  * dquot chach hash chain headers
 133  */
 134 #define NDQHASH         64                      /* smallish power of two */
 135 #define DQHASH(uid, mp) \
 136         (((uintptr_t)(mp) + (unsigned)(uid)) & (NDQHASH-1))
 137 
 138 struct  dqhead {
 139         struct  dquot   *dqh_forw;      /* MUST be first */
 140         struct  dquot   *dqh_back;      /* MUST be second */
 141 };
 142 
 143 extern struct dqhead dqhead[NDQHASH];
 144 
 145 extern struct dquot *dquot, *dquotNDQUOT;
 146 extern volatile int ndquot;
 147 extern krwlock_t dq_rwlock;             /* quota sub-system init lock */
 148 extern int quotas_initialized;          /* quota sub-system init flag */
 149 
 150 extern void qtinit();
 151 extern void qtinit2();
 152 extern struct dquot *getinoquota(struct inode *);
 153 extern int chkdq(struct inode *ip, long, int, struct cred *, char **errp,
 154                 size_t *lenp);
 155 extern int chkiq(struct ufsvfs *, int, struct inode *, uid_t, int,
 156                 struct cred *, char **errp, size_t *lenp);
 157 extern void dqrele(struct dquot *);
 158 extern int closedq(struct ufsvfs *, struct cred *);
 159 extern int qsync(struct ufsvfs *);
 160 
 161 extern int getdiskquota(uid_t, struct ufsvfs *, int, struct dquot **);
 162 extern void dqput(struct dquot *);
 163 extern void dqupdate(struct dquot *);
 164 extern void dqinval(struct dquot *);
 165 extern void invalidatedq(struct ufsvfs *);
 166 
 167 extern int quotactl(struct vnode *, intptr_t, int flag, struct cred *);
 168 
 169 #endif  /* _KERNEL */
 170 
 171 /*
 172  * Definitions for the 'quotactl' system call.
 173  */
 174 #define Q_QUOTAON       1       /* turn quotas on */
 175 #define Q_QUOTAOFF      2       /* turn quotas off */
 176 #define Q_SETQUOTA      3       /* set disk limits & usage */
 177 #define Q_GETQUOTA      4       /* get disk limits & usage */
 178 #define Q_SETQLIM       5       /* set disk limits only */
 179 #define Q_SYNC          6       /* update disk copy of quota usages */
 180 #define Q_ALLSYNC       7       /* update disk copy of quota usages for all */
 181 
 182 #ifdef _SYSCALL32
 183 /* ILP32 compatible structure for LP64 kernel. */
 184 struct quotctl32 {
 185         int             op;
 186         uid_t           uid;
 187         uint32_t        addr;
 188 };
 189 #endif /* SYSCALL32 */
 190 
 191 struct quotctl {
 192         int     op;
 193         uid_t   uid;
 194         caddr_t addr;
 195 };
 196 
 197 #define Q_QUOTACTL      0x00030189      /* ioctl command for quotactl */
 198 
 199 #ifdef  __cplusplus
 200 }
 201 #endif
 202 
 203 #endif  /* _SYS_FS_UFS_QUOTA_H */