1 /* 2 * Copyright (c) 2000-2005 Silicon Graphics, Inc. All Rights Reserved. 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms of version 2 of the GNU General Public License as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it would be useful, but 9 * WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 11 * 12 * Further, this software is distributed without any warranty that it is 13 * free of the rightful claim of any third person regarding infringement 14 * or the like. Any license provided herein, whether implied or 15 * otherwise, applies only to this software file. Patent licenses, if 16 * any, provided herein do not apply to combinations of this program with 17 * other software, or any other product whatsoever. 18 * 19 * You should have received a copy of the GNU General Public License along 20 * with this program; if not, write the Free Software Foundation, Inc., 59 21 * Temple Place - Suite 330, Boston MA 02111-1307, USA. 22 * 23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, 24 * Mountain View, CA 94043, or: 25 * 26 * http://www.sgi.com 27 * 28 * For further information regarding this notice, see: 29 * 30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/ 31 */ 32 #ifndef __XFS_QUOTA_H__ 33 #define __XFS_QUOTA_H__ 34 35 /* 36 * The ondisk form of a dquot structure. 37 */ 38 #define XFS_DQUOT_MAGIC 0x4451 /* 'DQ' */ 39 #define XFS_DQUOT_VERSION (u_int8_t)0x01 /* latest version number */ 40 41 /* 42 * uid_t and gid_t are hard-coded to 32 bits in the inode. 43 * Hence, an 'id' in a dquot is 32 bits.. 44 */ 45 typedef __int32_t xfs_dqid_t; 46 47 /* 48 * Eventhough users may not have quota limits occupying all 64-bits, 49 * they may need 64-bit accounting. Hence, 64-bit quota-counters, 50 * and quota-limits. This is a waste in the common case, but hey ... 51 */ 52 typedef __uint64_t xfs_qcnt_t; 53 typedef __uint16_t xfs_qwarncnt_t; 54 55 /* 56 * This is the main portion of the on-disk representation of quota 57 * information for a user. This is the q_core of the xfs_dquot_t that 58 * is kept in kernel memory. We pad this with some more expansion room 59 * to construct the on disk structure. 60 */ 61 typedef struct xfs_disk_dquot { 62 /*16*/ u_int16_t d_magic; /* dquot magic = XFS_DQUOT_MAGIC */ 63 /*8 */ u_int8_t d_version; /* dquot version */ 64 /*8 */ u_int8_t d_flags; /* XFS_DQ_USER/PROJ/GROUP */ 65 /*32*/ xfs_dqid_t d_id; /* user,project,group id */ 66 /*64*/ xfs_qcnt_t d_blk_hardlimit;/* absolute limit on disk blks */ 67 /*64*/ xfs_qcnt_t d_blk_softlimit;/* preferred limit on disk blks */ 68 /*64*/ xfs_qcnt_t d_ino_hardlimit;/* maximum # allocated inodes */ 69 /*64*/ xfs_qcnt_t d_ino_softlimit;/* preferred inode limit */ 70 /*64*/ xfs_qcnt_t d_bcount; /* disk blocks owned by the user */ 71 /*64*/ xfs_qcnt_t d_icount; /* inodes owned by the user */ 72 /*32*/ __int32_t d_itimer; /* zero if within inode limits if not, 73 this is when we refuse service */ 74 /*32*/ __int32_t d_btimer; /* similar to above; for disk blocks */ 75 /*16*/ xfs_qwarncnt_t d_iwarns; /* warnings issued wrt num inodes */ 76 /*16*/ xfs_qwarncnt_t d_bwarns; /* warnings issued wrt disk blocks */ 77 /*32*/ __int32_t d_pad0; /* 64 bit align */ 78 /*64*/ xfs_qcnt_t d_rtb_hardlimit;/* absolute limit on realtime blks */ 79 /*64*/ xfs_qcnt_t d_rtb_softlimit;/* preferred limit on RT disk blks */ 80 /*64*/ xfs_qcnt_t d_rtbcount; /* realtime blocks owned */ 81 /*32*/ __int32_t d_rtbtimer; /* similar to above; for RT disk blocks */ 82 /*16*/ xfs_qwarncnt_t d_rtbwarns; /* warnings issued wrt RT disk blocks */ 83 /*16*/ __uint16_t d_pad; 84 } xfs_disk_dquot_t; 85 86 /* 87 * This is what goes on disk. This is separated from the xfs_disk_dquot because 88 * carrying the unnecessary padding would be a waste of memory. 89 */ 90 typedef struct xfs_dqblk { 91 xfs_disk_dquot_t dd_diskdq; /* portion that lives incore as well */ 92 char dd_fill[32]; /* filling for posterity */ 93 } xfs_dqblk_t; 94 95 /* 96 * flags for q_flags field in the dquot. 97 */ 98 #define XFS_DQ_USER 0x0001 /* a user quota */ 99 #define XFS_DQ_PROJ 0x0002 /* project quota */ 100 #define XFS_DQ_GROUP 0x0004 /* a group quota */ 101 #define XFS_DQ_FLOCKED 0x0008 /* flush lock taken */ 102 #define XFS_DQ_DIRTY 0x0010 /* dquot is dirty */ 103 #define XFS_DQ_WANT 0x0020 /* for lookup/reclaim race */ 104 #define XFS_DQ_INACTIVE 0x0040 /* dq off mplist & hashlist */ 105 #define XFS_DQ_MARKER 0x0080 /* sentinel */ 106 107 #define XFS_DQ_ALLTYPES (XFS_DQ_USER|XFS_DQ_PROJ|XFS_DQ_GROUP) 108 109 /* 110 * In the worst case, when both user and group quotas are on, 111 * we can have a max of three dquots changing in a single transaction. 112 */ 113 #define XFS_DQUOT_LOGRES(mp) (sizeof(xfs_disk_dquot_t) * 3) 114 115 116 /* 117 * These are the structures used to lay out dquots and quotaoff 118 * records on the log. Quite similar to those of inodes. 119 */ 120 121 /* 122 * log format struct for dquots. 123 * The first two fields must be the type and size fitting into 124 * 32 bits : log_recovery code assumes that. 125 */ 126 typedef struct xfs_dq_logformat { 127 __uint16_t qlf_type; /* dquot log item type */ 128 __uint16_t qlf_size; /* size of this item */ 129 xfs_dqid_t qlf_id; /* usr/grp/proj id : 32 bits */ 130 __int64_t qlf_blkno; /* blkno of dquot buffer */ 131 __int32_t qlf_len; /* len of dquot buffer */ 132 __uint32_t qlf_boffset; /* off of dquot in buffer */ 133 } xfs_dq_logformat_t; 134 135 /* 136 * log format struct for QUOTAOFF records. 137 * The first two fields must be the type and size fitting into 138 * 32 bits : log_recovery code assumes that. 139 * We write two LI_QUOTAOFF logitems per quotaoff, the last one keeps a pointer 140 * to the first and ensures that the first logitem is taken out of the AIL 141 * only when the last one is securely committed. 142 */ 143 typedef struct xfs_qoff_logformat { 144 unsigned short qf_type; /* quotaoff log item type */ 145 unsigned short qf_size; /* size of this item */ 146 unsigned int qf_flags; /* USR and/or GRP */ 147 char qf_pad[12]; /* padding for future */ 148 } xfs_qoff_logformat_t; 149 150 151 /* 152 * Disk quotas status in m_qflags, and also sb_qflags. 16 bits. 153 */ 154 #define XFS_UQUOTA_ACCT 0x0001 /* user quota accounting ON */ 155 #define XFS_UQUOTA_ENFD 0x0002 /* user quota limits enforced */ 156 #define XFS_UQUOTA_CHKD 0x0004 /* quotacheck run on usr quotas */ 157 #define XFS_PQUOTA_ACCT 0x0008 /* project quota accounting ON */ 158 #define XFS_OQUOTA_ENFD 0x0010 /* other (grp/prj) quota limits enforced */ 159 #define XFS_OQUOTA_CHKD 0x0020 /* quotacheck run on other (grp/prj) quotas */ 160 #define XFS_GQUOTA_ACCT 0x0040 /* group quota accounting ON */ 161 162 /* 163 * Quota Accounting/Enforcement flags 164 */ 165 #define XFS_ALL_QUOTA_ACCT \ 166 (XFS_UQUOTA_ACCT | XFS_GQUOTA_ACCT | XFS_PQUOTA_ACCT) 167 #define XFS_ALL_QUOTA_ENFD (XFS_UQUOTA_ENFD | XFS_OQUOTA_ENFD) 168 #define XFS_ALL_QUOTA_CHKD (XFS_UQUOTA_CHKD | XFS_OQUOTA_CHKD) 169 170 #define XFS_IS_QUOTA_RUNNING(mp) ((mp)->m_qflags & XFS_ALL_QUOTA_ACCT) 171 #define XFS_IS_QUOTA_ENFORCED(mp) ((mp)->m_qflags & XFS_ALL_QUOTA_ENFD) 172 #define XFS_IS_UQUOTA_RUNNING(mp) ((mp)->m_qflags & XFS_UQUOTA_ACCT) 173 #define XFS_IS_PQUOTA_RUNNING(mp) ((mp)->m_qflags & XFS_PQUOTA_ACCT) 174 #define XFS_IS_GQUOTA_RUNNING(mp) ((mp)->m_qflags & XFS_GQUOTA_ACCT) 175 176 /* 177 * Incore only flags for quotaoff - these bits get cleared when quota(s) 178 * are in the process of getting turned off. These flags are in m_qflags but 179 * never in sb_qflags. 180 */ 181 #define XFS_UQUOTA_ACTIVE 0x0100 /* uquotas are being turned off */ 182 #define XFS_PQUOTA_ACTIVE 0x0200 /* pquotas are being turned off */ 183 #define XFS_GQUOTA_ACTIVE 0x0400 /* gquotas are being turned off */ 184 185 /* 186 * Checking XFS_IS_*QUOTA_ON() while holding any inode lock guarantees 187 * quota will be not be switched off as long as that inode lock is held. 188 */ 189 #define XFS_IS_QUOTA_ON(mp) ((mp)->m_qflags & (XFS_UQUOTA_ACTIVE | \ 190 XFS_GQUOTA_ACTIVE | \ 191 XFS_PQUOTA_ACTIVE)) 192 #define XFS_IS_OQUOTA_ON(mp) ((mp)->m_qflags & (XFS_GQUOTA_ACTIVE | \ 193 XFS_PQUOTA_ACTIVE)) 194 #define XFS_IS_UQUOTA_ON(mp) ((mp)->m_qflags & XFS_UQUOTA_ACTIVE) 195 #define XFS_IS_GQUOTA_ON(mp) ((mp)->m_qflags & XFS_GQUOTA_ACTIVE) 196 #define XFS_IS_PQUOTA_ON(mp) ((mp)->m_qflags & XFS_PQUOTA_ACTIVE) 197 198 /* 199 * Flags to tell various functions what to do. Not all of these are meaningful 200 * to a single function. None of these XFS_QMOPT_* flags are meant to have 201 * persistent values (ie. their values can and will change between versions) 202 */ 203 #define XFS_QMOPT_DQLOCK 0x0000001 /* dqlock */ 204 #define XFS_QMOPT_DQALLOC 0x0000002 /* alloc dquot ondisk if needed */ 205 #define XFS_QMOPT_UQUOTA 0x0000004 /* user dquot requested */ 206 #define XFS_QMOPT_PQUOTA 0x0000008 /* project dquot requested */ 207 #define XFS_QMOPT_FORCE_RES 0x0000010 /* ignore quota limits */ 208 #define XFS_QMOPT_DQSUSER 0x0000020 /* don't cache super users dquot */ 209 #define XFS_QMOPT_SBVERSION 0x0000040 /* change superblock version num */ 210 #define XFS_QMOPT_QUOTAOFF 0x0000080 /* quotas are being turned off */ 211 #define XFS_QMOPT_UMOUNTING 0x0000100 /* filesys is being unmounted */ 212 #define XFS_QMOPT_DOLOG 0x0000200 /* log buf changes (in quotacheck) */ 213 #define XFS_QMOPT_DOWARN 0x0000400 /* increase warning cnt if necessary */ 214 #define XFS_QMOPT_ILOCKED 0x0000800 /* inode is already locked (excl) */ 215 #define XFS_QMOPT_DQREPAIR 0x0001000 /* repair dquot, if damaged. */ 216 #define XFS_QMOPT_GQUOTA 0x0002000 /* group dquot requested */ 217 218 /* 219 * flags to xfs_trans_mod_dquot to indicate which field needs to be 220 * modified. 221 */ 222 #define XFS_QMOPT_RES_REGBLKS 0x0010000 223 #define XFS_QMOPT_RES_RTBLKS 0x0020000 224 #define XFS_QMOPT_BCOUNT 0x0040000 225 #define XFS_QMOPT_ICOUNT 0x0080000 226 #define XFS_QMOPT_RTBCOUNT 0x0100000 227 #define XFS_QMOPT_DELBCOUNT 0x0200000 228 #define XFS_QMOPT_DELRTBCOUNT 0x0400000 229 #define XFS_QMOPT_RES_INOS 0x0800000 230 231 /* 232 * flags for dqflush and dqflush_all. 233 */ 234 #define XFS_QMOPT_SYNC 0x1000000 235 #define XFS_QMOPT_ASYNC 0x2000000 236 #define XFS_QMOPT_DELWRI 0x4000000 237 238 /* 239 * flags for dqalloc. 240 */ 241 #define XFS_QMOPT_INHERIT 0x8000000 242 243 /* 244 * flags to xfs_trans_mod_dquot. 245 */ 246 #define XFS_TRANS_DQ_RES_BLKS XFS_QMOPT_RES_REGBLKS 247 #define XFS_TRANS_DQ_RES_RTBLKS XFS_QMOPT_RES_RTBLKS 248 #define XFS_TRANS_DQ_RES_INOS XFS_QMOPT_RES_INOS 249 #define XFS_TRANS_DQ_BCOUNT XFS_QMOPT_BCOUNT 250 #define XFS_TRANS_DQ_DELBCOUNT XFS_QMOPT_DELBCOUNT 251 #define XFS_TRANS_DQ_ICOUNT XFS_QMOPT_ICOUNT 252 #define XFS_TRANS_DQ_RTBCOUNT XFS_QMOPT_RTBCOUNT 253 #define XFS_TRANS_DQ_DELRTBCOUNT XFS_QMOPT_DELRTBCOUNT 254 255 256 #define XFS_QMOPT_QUOTALL \ 257 (XFS_QMOPT_UQUOTA | XFS_QMOPT_PQUOTA | XFS_QMOPT_GQUOTA) 258 #define XFS_QMOPT_RESBLK_MASK (XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_RES_RTBLKS) 259 260 #ifdef __KERNEL__ 261 /* 262 * This check is done typically without holding the inode lock; 263 * that may seem racey, but it is harmless in the context that it is used. 264 * The inode cannot go inactive as long a reference is kept, and 265 * therefore if dquot(s) were attached, they'll stay consistent. 266 * If, for example, the ownership of the inode changes while 267 * we didn't have the inode locked, the appropriate dquot(s) will be 268 * attached atomically. 269 */ 270 #define XFS_NOT_DQATTACHED(mp, ip) ((XFS_IS_UQUOTA_ON(mp) &&\ 271 (ip)->i_udquot == NULL) || \ 272 (XFS_IS_OQUOTA_ON(mp) && \ 273 (ip)->i_gdquot == NULL)) 274 275 #define XFS_QM_NEED_QUOTACHECK(mp) \ 276 ((XFS_IS_UQUOTA_ON(mp) && \ 277 (mp->m_sb.sb_qflags & XFS_UQUOTA_CHKD) == 0) || \ 278 (XFS_IS_GQUOTA_ON(mp) && \ 279 ((mp->m_sb.sb_qflags & XFS_OQUOTA_CHKD) == 0 || \ 280 (mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT))) || \ 281 (XFS_IS_PQUOTA_ON(mp) && \ 282 ((mp->m_sb.sb_qflags & XFS_OQUOTA_CHKD) == 0 || \ 283 (mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT)))) 284 285 #define XFS_MOUNT_QUOTA_SET1 (XFS_UQUOTA_ACCT|XFS_UQUOTA_ENFD|\ 286 XFS_UQUOTA_CHKD|XFS_PQUOTA_ACCT|\ 287 XFS_OQUOTA_ENFD|XFS_OQUOTA_CHKD) 288 289 #define XFS_MOUNT_QUOTA_SET2 (XFS_UQUOTA_ACCT|XFS_UQUOTA_ENFD|\ 290 XFS_UQUOTA_CHKD|XFS_GQUOTA_ACCT|\ 291 XFS_OQUOTA_ENFD|XFS_OQUOTA_CHKD) 292 293 #define XFS_MOUNT_QUOTA_ALL (XFS_UQUOTA_ACCT|XFS_UQUOTA_ENFD|\ 294 XFS_UQUOTA_CHKD|XFS_PQUOTA_ACCT|\ 295 XFS_OQUOTA_ENFD|XFS_OQUOTA_CHKD|\ 296 XFS_GQUOTA_ACCT) 297 #define XFS_MOUNT_QUOTA_MASK (XFS_MOUNT_QUOTA_ALL | XFS_UQUOTA_ACTIVE | \ 298 XFS_GQUOTA_ACTIVE | XFS_PQUOTA_ACTIVE) 299 300 301 /* 302 * The structure kept inside the xfs_trans_t keep track of dquot changes 303 * within a transaction and apply them later. 304 */ 305 typedef struct xfs_dqtrx { 306 struct xfs_dquot *qt_dquot; /* the dquot this refers to */ 307 ulong qt_blk_res; /* blks reserved on a dquot */ 308 ulong qt_blk_res_used; /* blks used from the reservation */ 309 ulong qt_ino_res; /* inode reserved on a dquot */ 310 ulong qt_ino_res_used; /* inodes used from the reservation */ 311 long qt_bcount_delta; /* dquot blk count changes */ 312 long qt_delbcnt_delta; /* delayed dquot blk count changes */ 313 long qt_icount_delta; /* dquot inode count changes */ 314 ulong qt_rtblk_res; /* # blks reserved on a dquot */ 315 ulong qt_rtblk_res_used;/* # blks used from reservation */ 316 long qt_rtbcount_delta;/* dquot realtime blk changes */ 317 long qt_delrtb_delta; /* delayed RT blk count changes */ 318 } xfs_dqtrx_t; 319 320 /* 321 * Dquot transaction functions, used if quota is enabled. 322 */ 323 typedef void (*qo_dup_dqinfo_t)(struct xfs_trans *, struct xfs_trans *); 324 typedef void (*qo_mod_dquot_byino_t)(struct xfs_trans *, 325 struct xfs_inode *, uint, long); 326 typedef void (*qo_free_dqinfo_t)(struct xfs_trans *); 327 typedef void (*qo_apply_dquot_deltas_t)(struct xfs_trans *); 328 typedef void (*qo_unreserve_and_mod_dquots_t)(struct xfs_trans *); 329 typedef int (*qo_reserve_quota_nblks_t)( 330 struct xfs_trans *, struct xfs_mount *, 331 struct xfs_inode *, long, long, uint); 332 typedef int (*qo_reserve_quota_bydquots_t)( 333 struct xfs_trans *, struct xfs_mount *, 334 struct xfs_dquot *, struct xfs_dquot *, 335 long, long, uint); 336 typedef struct xfs_dqtrxops { 337 qo_dup_dqinfo_t qo_dup_dqinfo; 338 qo_free_dqinfo_t qo_free_dqinfo; 339 qo_mod_dquot_byino_t qo_mod_dquot_byino; 340 qo_apply_dquot_deltas_t qo_apply_dquot_deltas; 341 qo_reserve_quota_nblks_t qo_reserve_quota_nblks; 342 qo_reserve_quota_bydquots_t qo_reserve_quota_bydquots; 343 qo_unreserve_and_mod_dquots_t qo_unreserve_and_mod_dquots; 344 } xfs_dqtrxops_t; 345 346 #define XFS_DQTRXOP(mp, tp, op, args...) \ 347 ((mp)->m_qm_ops.xfs_dqtrxops ? \ 348 ((mp)->m_qm_ops.xfs_dqtrxops->op)(tp, ## args) : 0) 349 350 #define XFS_DQTRXOP_VOID(mp, tp, op, args...) \ 351 ((mp)->m_qm_ops.xfs_dqtrxops ? \ 352 ((mp)->m_qm_ops.xfs_dqtrxops->op)(tp, ## args) : (void)0) 353 354 #define XFS_TRANS_DUP_DQINFO(mp, otp, ntp) \ 355 XFS_DQTRXOP_VOID(mp, otp, qo_dup_dqinfo, ntp) 356 #define XFS_TRANS_FREE_DQINFO(mp, tp) \ 357 XFS_DQTRXOP_VOID(mp, tp, qo_free_dqinfo) 358 #define XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, field, delta) \ 359 XFS_DQTRXOP_VOID(mp, tp, qo_mod_dquot_byino, ip, field, delta) 360 #define XFS_TRANS_APPLY_DQUOT_DELTAS(mp, tp) \ 361 XFS_DQTRXOP_VOID(mp, tp, qo_apply_dquot_deltas) 362 #define XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, nblks, ninos, fl) \ 363 XFS_DQTRXOP(mp, tp, qo_reserve_quota_nblks, mp, ip, nblks, ninos, fl) 364 #define XFS_TRANS_RESERVE_QUOTA_BYDQUOTS(mp, tp, ud, gd, nb, ni, fl) \ 365 XFS_DQTRXOP(mp, tp, qo_reserve_quota_bydquots, mp, ud, gd, nb, ni, fl) 366 #define XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(mp, tp) \ 367 XFS_DQTRXOP_VOID(mp, tp, qo_unreserve_and_mod_dquots) 368 369 #define XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, nblks, ninos, flags) \ 370 XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, -(nblks), -(ninos), flags) 371 #define XFS_TRANS_RESERVE_QUOTA(mp, tp, ud, gd, nb, ni, f) \ 372 XFS_TRANS_RESERVE_QUOTA_BYDQUOTS(mp, tp, ud, gd, nb, ni, \ 373 f | XFS_QMOPT_RES_REGBLKS) 374 #define XFS_TRANS_UNRESERVE_QUOTA(mp, tp, ud, gd, nb, ni, f) \ 375 XFS_TRANS_RESERVE_QUOTA_BYDQUOTS(mp, tp, ud, gd, -(nb), -(ni), \ 376 f | XFS_QMOPT_RES_REGBLKS) 377 378 extern int xfs_qm_dqcheck(xfs_disk_dquot_t *, xfs_dqid_t, uint, uint, char *); 379 extern int xfs_mount_reset_sbqflags(struct xfs_mount *); 380 381 extern struct bhv_vfsops xfs_qmops; 382 383 #endif /* __KERNEL__ */ 384 385 #endif /* __XFS_QUOTA_H__ */ 386