1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2000-2006 Silicon Graphics, Inc. 4 * All Rights Reserved. 5 */ 6 #include "xfs_platform.h" 7 #include "xfs_fs.h" 8 #include "xfs_shared.h" 9 #include "xfs_format.h" 10 #include "xfs_log_format.h" 11 #include "xfs_trans_resv.h" 12 #include "xfs_mount.h" 13 #include "xfs_quota.h" 14 #include "xfs_inode.h" 15 #include "xfs_trans.h" 16 #include "xfs_qm.h" 17 18 19 STATIC void 20 xfs_fill_statvfs_from_dquot( 21 struct kstatfs *statp, 22 struct xfs_inode *ip, 23 struct xfs_dquot *dqp) 24 { 25 struct xfs_dquot_res *blkres = &dqp->q_blk; 26 uint64_t limit; 27 28 if (XFS_IS_REALTIME_MOUNT(ip->i_mount) && 29 (ip->i_diflags & (XFS_DIFLAG_RTINHERIT | XFS_DIFLAG_REALTIME))) 30 blkres = &dqp->q_rtb; 31 32 limit = blkres->softlimit ? 33 blkres->softlimit : 34 blkres->hardlimit; 35 if (limit) { 36 uint64_t remaining = 0; 37 38 if (limit > blkres->reserved) 39 remaining = limit - blkres->reserved; 40 41 statp->f_blocks = min(statp->f_blocks, limit); 42 statp->f_bfree = min(statp->f_bfree, remaining); 43 } 44 45 limit = dqp->q_ino.softlimit ? 46 dqp->q_ino.softlimit : 47 dqp->q_ino.hardlimit; 48 if (limit) { 49 uint64_t remaining = 0; 50 51 if (limit > dqp->q_ino.reserved) 52 remaining = limit - dqp->q_ino.reserved; 53 54 statp->f_files = min(statp->f_files, limit); 55 statp->f_ffree = min(statp->f_ffree, remaining); 56 } 57 } 58 59 60 /* 61 * Directory tree accounting is implemented using project quotas, where 62 * the project identifier is inherited from parent directories. 63 * A statvfs (df, etc.) of a directory that is using project quota should 64 * return a statvfs of the project, not the entire filesystem. 65 * This makes such trees appear as if they are filesystems in themselves. 66 */ 67 void 68 xfs_qm_statvfs( 69 struct xfs_inode *ip, 70 struct kstatfs *statp) 71 { 72 struct xfs_mount *mp = ip->i_mount; 73 struct xfs_dquot *dqp; 74 75 if (!xfs_qm_dqget(mp, ip->i_projid, XFS_DQTYPE_PROJ, false, &dqp)) { 76 mutex_lock(&dqp->q_qlock); 77 xfs_fill_statvfs_from_dquot(statp, ip, dqp); 78 mutex_unlock(&dqp->q_qlock); 79 xfs_qm_dqrele(dqp); 80 } 81 } 82 83 STATIC int 84 xfs_qm_validate_state_change( 85 struct xfs_mount *mp, 86 uint uqd, 87 uint gqd, 88 uint pqd) 89 { 90 int state; 91 92 /* Is quota state changing? */ 93 state = ((uqd && !XFS_IS_UQUOTA_ON(mp)) || 94 (!uqd && XFS_IS_UQUOTA_ON(mp)) || 95 (gqd && !XFS_IS_GQUOTA_ON(mp)) || 96 (!gqd && XFS_IS_GQUOTA_ON(mp)) || 97 (pqd && !XFS_IS_PQUOTA_ON(mp)) || 98 (!pqd && XFS_IS_PQUOTA_ON(mp))); 99 100 return state && 101 (xfs_dev_is_read_only(mp, "changing quota state") || 102 xfs_has_norecovery(mp)); 103 } 104 105 int 106 xfs_qm_newmount( 107 xfs_mount_t *mp, 108 uint *needquotamount, 109 uint *quotaflags) 110 { 111 uint quotaondisk; 112 uint uquotaondisk = 0, gquotaondisk = 0, pquotaondisk = 0; 113 114 quotaondisk = xfs_has_quota(mp) && 115 (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT); 116 117 if (quotaondisk) { 118 uquotaondisk = mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT; 119 pquotaondisk = mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT; 120 gquotaondisk = mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT; 121 } 122 123 /* 124 * If the device itself is read-only and/or in norecovery 125 * mode, we can't allow the user to change the state of 126 * quota on the mount - this would generate a transaction 127 * on the ro device, which would lead to an I/O error and 128 * shutdown. 129 */ 130 131 if (xfs_qm_validate_state_change(mp, uquotaondisk, 132 gquotaondisk, pquotaondisk)) { 133 134 if (xfs_has_metadir(mp)) 135 xfs_warn(mp, 136 "metadir enabled, please mount without any quota mount options"); 137 else 138 xfs_warn(mp, "please mount with%s%s%s%s.", 139 (!quotaondisk ? "out quota" : ""), 140 (uquotaondisk ? " usrquota" : ""), 141 (gquotaondisk ? " grpquota" : ""), 142 (pquotaondisk ? " prjquota" : "")); 143 return -EPERM; 144 } 145 146 if (XFS_IS_QUOTA_ON(mp) || quotaondisk) { 147 /* 148 * Call mount_quotas at this point only if we won't have to do 149 * a quotacheck. 150 */ 151 if (quotaondisk && !XFS_QM_NEED_QUOTACHECK(mp)) { 152 /* 153 * If an error occurred, qm_mount_quotas code 154 * has already disabled quotas. So, just finish 155 * mounting, and get on with the boring life 156 * without disk quotas. 157 */ 158 xfs_qm_mount_quotas(mp); 159 } else { 160 /* 161 * Clear the quota flags, but remember them. This 162 * is so that the quota code doesn't get invoked 163 * before we're ready. This can happen when an 164 * inode goes inactive and wants to free blocks, 165 * or via xfs_log_mount_finish. 166 */ 167 *needquotamount = true; 168 *quotaflags = mp->m_qflags; 169 mp->m_qflags = 0; 170 } 171 } 172 173 return 0; 174 } 175 176 /* 177 * If the sysadmin didn't provide any quota mount options, restore the quota 178 * accounting and enforcement state from the ondisk superblock. Only do this 179 * for metadir filesystems because this is a behavior change. 180 */ 181 void 182 xfs_qm_resume_quotaon( 183 struct xfs_mount *mp) 184 { 185 if (!xfs_has_metadir(mp)) 186 return; 187 if (xfs_has_norecovery(mp)) 188 return; 189 190 mp->m_qflags = mp->m_sb.sb_qflags & (XFS_ALL_QUOTA_ACCT | 191 XFS_ALL_QUOTA_ENFD); 192 } 193