Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Nathan Scott | 4ce3121 | 2005-11-02 14:59:41 +1100 | [diff] [blame] | 3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 4 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 6 | |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include "xfs.h" |
| 9 | #include "xfs_fs.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 10 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 11 | #include "xfs_format.h" |
| 12 | #include "xfs_log_format.h" |
| 13 | #include "xfs_trans_resv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include "xfs_sb.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include "xfs_inode.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 17 | #include "xfs_trans.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 18 | #include "xfs_quota.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "xfs_qm.h" |
Dave Chinner | 6d8b79c | 2012-10-08 21:56:09 +1100 | [diff] [blame] | 20 | #include "xfs_icache.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Pavel Reichl | 1cc95e6 | 2019-11-12 17:04:27 -0800 | [diff] [blame] | 22 | STATIC int |
| 23 | xfs_qm_log_quotaoff( |
| 24 | struct xfs_mount *mp, |
| 25 | struct xfs_qoff_logitem **qoffstartp, |
| 26 | uint flags) |
| 27 | { |
| 28 | struct xfs_trans *tp; |
| 29 | int error; |
| 30 | struct xfs_qoff_logitem *qoffi; |
| 31 | |
Pavel Reichl | 1cc95e6 | 2019-11-12 17:04:27 -0800 | [diff] [blame] | 32 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_quotaoff, 0, 0, 0, &tp); |
| 33 | if (error) |
| 34 | goto out; |
| 35 | |
| 36 | qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT); |
| 37 | xfs_trans_log_quotaoff_item(tp, qoffi); |
| 38 | |
| 39 | spin_lock(&mp->m_sb_lock); |
| 40 | mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL; |
| 41 | spin_unlock(&mp->m_sb_lock); |
| 42 | |
| 43 | xfs_log_sb(tp); |
| 44 | |
| 45 | /* |
| 46 | * We have to make sure that the transaction is secure on disk before we |
| 47 | * return and actually stop quota accounting. So, make it synchronous. |
| 48 | * We don't care about quotoff's performance. |
| 49 | */ |
| 50 | xfs_trans_set_sync(tp); |
| 51 | error = xfs_trans_commit(tp); |
| 52 | if (error) |
| 53 | goto out; |
| 54 | |
| 55 | *qoffstartp = qoffi; |
| 56 | out: |
| 57 | return error; |
| 58 | } |
| 59 | |
| 60 | STATIC int |
| 61 | xfs_qm_log_quotaoff_end( |
| 62 | struct xfs_mount *mp, |
Brian Foster | 8a62714 | 2020-03-16 14:26:09 -0700 | [diff] [blame] | 63 | struct xfs_qoff_logitem **startqoff, |
Pavel Reichl | 1cc95e6 | 2019-11-12 17:04:27 -0800 | [diff] [blame] | 64 | uint flags) |
| 65 | { |
| 66 | struct xfs_trans *tp; |
| 67 | int error; |
| 68 | struct xfs_qoff_logitem *qoffi; |
| 69 | |
| 70 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_equotaoff, 0, 0, 0, &tp); |
| 71 | if (error) |
| 72 | return error; |
| 73 | |
Brian Foster | 8a62714 | 2020-03-16 14:26:09 -0700 | [diff] [blame] | 74 | qoffi = xfs_trans_get_qoff_item(tp, *startqoff, |
Pavel Reichl | 1cc95e6 | 2019-11-12 17:04:27 -0800 | [diff] [blame] | 75 | flags & XFS_ALL_QUOTA_ACCT); |
| 76 | xfs_trans_log_quotaoff_item(tp, qoffi); |
Brian Foster | 8a62714 | 2020-03-16 14:26:09 -0700 | [diff] [blame] | 77 | *startqoff = NULL; |
Pavel Reichl | 1cc95e6 | 2019-11-12 17:04:27 -0800 | [diff] [blame] | 78 | |
| 79 | /* |
| 80 | * We have to make sure that the transaction is secure on disk before we |
| 81 | * return and actually stop quota accounting. So, make it synchronous. |
| 82 | * We don't care about quotoff's performance. |
| 83 | */ |
| 84 | xfs_trans_set_sync(tp); |
| 85 | return xfs_trans_commit(tp); |
| 86 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | * Turn off quota accounting and/or enforcement for all udquots and/or |
| 90 | * gdquots. Called only at unmount time. |
| 91 | * |
| 92 | * This assumes that there are no dquots of this file system cached |
| 93 | * incore, and modifies the ondisk dquot directly. Therefore, for example, |
| 94 | * it is an error to call this twice, without purging the cache. |
| 95 | */ |
Christoph Hellwig | fcafb71 | 2009-02-09 08:47:34 +0100 | [diff] [blame] | 96 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | xfs_qm_scall_quotaoff( |
| 98 | xfs_mount_t *mp, |
Christoph Hellwig | fcafb71 | 2009-02-09 08:47:34 +0100 | [diff] [blame] | 99 | uint flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
Christoph Hellwig | 8a7b8a8 | 2010-04-20 17:01:30 +1000 | [diff] [blame] | 101 | struct xfs_quotainfo *q = mp->m_quotainfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | uint dqtype; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | int error; |
| 104 | uint inactivate_flags; |
Brian Foster | 8a62714 | 2020-03-16 14:26:09 -0700 | [diff] [blame] | 105 | struct xfs_qoff_logitem *qoffstart = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | /* |
| 108 | * No file system can have quotas enabled on disk but not in core. |
| 109 | * Note that quota utilities (like quotaoff) _expect_ |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 110 | * errno == -EEXIST here. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | */ |
| 112 | if ((mp->m_qflags & flags) == 0) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 113 | return -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | error = 0; |
| 115 | |
| 116 | flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD); |
| 117 | |
| 118 | /* |
| 119 | * We don't want to deal with two quotaoffs messing up each other, |
| 120 | * so we're going to serialize it. quotaoff isn't exactly a performance |
| 121 | * critical thing. |
| 122 | * If quotaoff, then we must be dealing with the root filesystem. |
| 123 | */ |
Christoph Hellwig | 8a7b8a8 | 2010-04-20 17:01:30 +1000 | [diff] [blame] | 124 | ASSERT(q); |
| 125 | mutex_lock(&q->qi_quotaofflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | /* |
| 128 | * If we're just turning off quota enforcement, change mp and go. |
| 129 | */ |
| 130 | if ((flags & XFS_ALL_QUOTA_ACCT) == 0) { |
| 131 | mp->m_qflags &= ~(flags); |
| 132 | |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 133 | spin_lock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | mp->m_sb.sb_qflags = mp->m_qflags; |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 135 | spin_unlock(&mp->m_sb_lock); |
Christoph Hellwig | 8a7b8a8 | 2010-04-20 17:01:30 +1000 | [diff] [blame] | 136 | mutex_unlock(&q->qi_quotaofflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
| 138 | /* XXX what to do if error ? Revert back to old vals incore ? */ |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 139 | return xfs_sync_sb(mp, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | dqtype = 0; |
| 143 | inactivate_flags = 0; |
| 144 | /* |
| 145 | * If accounting is off, we must turn enforcement off, clear the |
| 146 | * quota 'CHKD' certificate to make it known that we have to |
| 147 | * do a quotacheck the next time this quota is turned on. |
| 148 | */ |
| 149 | if (flags & XFS_UQUOTA_ACCT) { |
| 150 | dqtype |= XFS_QMOPT_UQUOTA; |
| 151 | flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD); |
| 152 | inactivate_flags |= XFS_UQUOTA_ACTIVE; |
| 153 | } |
| 154 | if (flags & XFS_GQUOTA_ACCT) { |
| 155 | dqtype |= XFS_QMOPT_GQUOTA; |
Chandra Seetharaman | 83e782e | 2013-06-27 17:25:10 -0500 | [diff] [blame] | 156 | flags |= (XFS_GQUOTA_CHKD | XFS_GQUOTA_ENFD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | inactivate_flags |= XFS_GQUOTA_ACTIVE; |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 158 | } |
| 159 | if (flags & XFS_PQUOTA_ACCT) { |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 160 | dqtype |= XFS_QMOPT_PQUOTA; |
Chandra Seetharaman | 83e782e | 2013-06-27 17:25:10 -0500 | [diff] [blame] | 161 | flags |= (XFS_PQUOTA_CHKD | XFS_PQUOTA_ENFD); |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 162 | inactivate_flags |= XFS_PQUOTA_ACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Nothing to do? Don't complain. This happens when we're just |
| 167 | * turning off quota enforcement. |
| 168 | */ |
Christoph Hellwig | 8a7b8a8 | 2010-04-20 17:01:30 +1000 | [diff] [blame] | 169 | if ((mp->m_qflags & flags) == 0) |
| 170 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
| 172 | /* |
| 173 | * Write the LI_QUOTAOFF log record, and do SB changes atomically, |
David Chinner | cb6edc2 | 2008-04-10 12:20:45 +1000 | [diff] [blame] | 174 | * and synchronously. If we fail to write, we should abort the |
| 175 | * operation as it cannot be recovered safely if we crash. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | */ |
David Chinner | cb6edc2 | 2008-04-10 12:20:45 +1000 | [diff] [blame] | 177 | error = xfs_qm_log_quotaoff(mp, &qoffstart, flags); |
| 178 | if (error) |
Christoph Hellwig | 8a7b8a8 | 2010-04-20 17:01:30 +1000 | [diff] [blame] | 179 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
| 181 | /* |
| 182 | * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct |
| 183 | * to take care of the race between dqget and quotaoff. We don't take |
| 184 | * any special locks to reset these bits. All processes need to check |
| 185 | * these bits *after* taking inode lock(s) to see if the particular |
| 186 | * quota type is in the process of being turned off. If *ACTIVE, it is |
| 187 | * guaranteed that all dquot structures and all quotainode ptrs will all |
| 188 | * stay valid as long as that inode is kept locked. |
| 189 | * |
| 190 | * There is no turning back after this. |
| 191 | */ |
| 192 | mp->m_qflags &= ~inactivate_flags; |
| 193 | |
| 194 | /* |
| 195 | * Give back all the dquot reference(s) held by inodes. |
| 196 | * Here we go thru every single incore inode in this file system, and |
| 197 | * do a dqrele on the i_udquot/i_gdquot that it may have. |
| 198 | * Essentially, as long as somebody has an inode locked, this guarantees |
| 199 | * that quotas will not be turned off. This is handy because in a |
| 200 | * transaction once we lock the inode(s) and check for quotaon, we can |
| 201 | * depend on the quota inodes (and other things) being valid as long as |
| 202 | * we keep the lock(s). |
| 203 | */ |
| 204 | xfs_qm_dqrele_all_inodes(mp, flags); |
| 205 | |
| 206 | /* |
| 207 | * Next we make the changes in the quota flag in the mount struct. |
| 208 | * This isn't protected by a particular lock directly, because we |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 209 | * don't want to take a mrlock every time we depend on quotas being on. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | */ |
Christoph Hellwig | b84a3a9 | 2012-03-14 11:53:34 -0500 | [diff] [blame] | 211 | mp->m_qflags &= ~flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | * Go through all the dquots of this file system and purge them, |
Christoph Hellwig | b84a3a9 | 2012-03-14 11:53:34 -0500 | [diff] [blame] | 215 | * according to what was turned off. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | */ |
Christoph Hellwig | b84a3a9 | 2012-03-14 11:53:34 -0500 | [diff] [blame] | 217 | xfs_qm_dqpurge_all(mp, dqtype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
| 219 | /* |
| 220 | * Transactions that had started before ACTIVE state bit was cleared |
| 221 | * could have logged many dquots, so they'd have higher LSNs than |
| 222 | * the first QUOTAOFF log record does. If we happen to crash when |
| 223 | * the tail of the log has gone past the QUOTAOFF record, but |
| 224 | * before the last dquot modification, those dquots __will__ |
| 225 | * recover, and that's not good. |
| 226 | * |
| 227 | * So, we have QUOTAOFF start and end logitems; the start |
| 228 | * logitem won't get overwritten until the end logitem appears... |
| 229 | */ |
Brian Foster | 8a62714 | 2020-03-16 14:26:09 -0700 | [diff] [blame] | 230 | error = xfs_qm_log_quotaoff_end(mp, &qoffstart, flags); |
David Chinner | cb6edc2 | 2008-04-10 12:20:45 +1000 | [diff] [blame] | 231 | if (error) { |
| 232 | /* We're screwed now. Shutdown is the only option. */ |
| 233 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
Christoph Hellwig | 8a7b8a8 | 2010-04-20 17:01:30 +1000 | [diff] [blame] | 234 | goto out_unlock; |
David Chinner | cb6edc2 | 2008-04-10 12:20:45 +1000 | [diff] [blame] | 235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
| 237 | /* |
Chandra Seetharaman | c31ad43 | 2013-07-10 18:00:36 -0500 | [diff] [blame] | 238 | * If all quotas are completely turned off, close shop. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | */ |
Chandra Seetharaman | c31ad43 | 2013-07-10 18:00:36 -0500 | [diff] [blame] | 240 | if (mp->m_qflags == 0) { |
Christoph Hellwig | 8a7b8a8 | 2010-04-20 17:01:30 +1000 | [diff] [blame] | 241 | mutex_unlock(&q->qi_quotaofflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | xfs_qm_destroy_quotainfo(mp); |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 243 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /* |
Christoph Hellwig | 8a7b8a8 | 2010-04-20 17:01:30 +1000 | [diff] [blame] | 247 | * Release our quotainode references if we don't need them anymore. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | */ |
Christoph Hellwig | 8a7b8a8 | 2010-04-20 17:01:30 +1000 | [diff] [blame] | 249 | if ((dqtype & XFS_QMOPT_UQUOTA) && q->qi_uquotaip) { |
Darrick J. Wong | 44a8736 | 2018-07-25 12:52:32 -0700 | [diff] [blame] | 250 | xfs_irele(q->qi_uquotaip); |
Christoph Hellwig | 8a7b8a8 | 2010-04-20 17:01:30 +1000 | [diff] [blame] | 251 | q->qi_uquotaip = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 253 | if ((dqtype & XFS_QMOPT_GQUOTA) && q->qi_gquotaip) { |
Darrick J. Wong | 44a8736 | 2018-07-25 12:52:32 -0700 | [diff] [blame] | 254 | xfs_irele(q->qi_gquotaip); |
Christoph Hellwig | 8a7b8a8 | 2010-04-20 17:01:30 +1000 | [diff] [blame] | 255 | q->qi_gquotaip = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 257 | if ((dqtype & XFS_QMOPT_PQUOTA) && q->qi_pquotaip) { |
Darrick J. Wong | 44a8736 | 2018-07-25 12:52:32 -0700 | [diff] [blame] | 258 | xfs_irele(q->qi_pquotaip); |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 259 | q->qi_pquotaip = NULL; |
| 260 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Christoph Hellwig | 8a7b8a8 | 2010-04-20 17:01:30 +1000 | [diff] [blame] | 262 | out_unlock: |
Brian Foster | 8a62714 | 2020-03-16 14:26:09 -0700 | [diff] [blame] | 263 | if (error && qoffstart) |
| 264 | xfs_qm_qoff_logitem_relse(qoffstart); |
Christoph Hellwig | 8a7b8a8 | 2010-04-20 17:01:30 +1000 | [diff] [blame] | 265 | mutex_unlock(&q->qi_quotaofflock); |
| 266 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Christoph Hellwig | 5d18898 | 2010-07-20 17:51:31 +1000 | [diff] [blame] | 269 | STATIC int |
| 270 | xfs_qm_scall_trunc_qfile( |
| 271 | struct xfs_mount *mp, |
| 272 | xfs_ino_t ino) |
| 273 | { |
| 274 | struct xfs_inode *ip; |
| 275 | struct xfs_trans *tp; |
| 276 | int error; |
| 277 | |
| 278 | if (ino == NULLFSINO) |
| 279 | return 0; |
| 280 | |
| 281 | error = xfs_iget(mp, NULL, ino, 0, 0, &ip); |
| 282 | if (error) |
| 283 | return error; |
| 284 | |
| 285 | xfs_ilock(ip, XFS_IOLOCK_EXCL); |
| 286 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 287 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp); |
Christoph Hellwig | 5d18898 | 2010-07-20 17:51:31 +1000 | [diff] [blame] | 288 | if (error) { |
Christoph Hellwig | 5d18898 | 2010-07-20 17:51:31 +1000 | [diff] [blame] | 289 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
| 290 | goto out_put; |
| 291 | } |
| 292 | |
| 293 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 294 | xfs_trans_ijoin(tp, ip, 0); |
Christoph Hellwig | 5d18898 | 2010-07-20 17:51:31 +1000 | [diff] [blame] | 295 | |
Christoph Hellwig | 673e8e5 | 2011-12-18 20:00:04 +0000 | [diff] [blame] | 296 | ip->i_d.di_size = 0; |
Christoph Hellwig | 673e8e5 | 2011-12-18 20:00:04 +0000 | [diff] [blame] | 297 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 298 | |
| 299 | error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0); |
Christoph Hellwig | 5d18898 | 2010-07-20 17:51:31 +1000 | [diff] [blame] | 300 | if (error) { |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 301 | xfs_trans_cancel(tp); |
Christoph Hellwig | 5d18898 | 2010-07-20 17:51:31 +1000 | [diff] [blame] | 302 | goto out_unlock; |
| 303 | } |
| 304 | |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 305 | ASSERT(ip->i_df.if_nextents == 0); |
Christoph Hellwig | 673e8e5 | 2011-12-18 20:00:04 +0000 | [diff] [blame] | 306 | |
Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 307 | xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 308 | error = xfs_trans_commit(tp); |
Christoph Hellwig | 5d18898 | 2010-07-20 17:51:31 +1000 | [diff] [blame] | 309 | |
| 310 | out_unlock: |
| 311 | xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
| 312 | out_put: |
Darrick J. Wong | 44a8736 | 2018-07-25 12:52:32 -0700 | [diff] [blame] | 313 | xfs_irele(ip); |
Christoph Hellwig | 5d18898 | 2010-07-20 17:51:31 +1000 | [diff] [blame] | 314 | return error; |
| 315 | } |
| 316 | |
Christoph Hellwig | fcafb71 | 2009-02-09 08:47:34 +0100 | [diff] [blame] | 317 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | xfs_qm_scall_trunc_qfiles( |
| 319 | xfs_mount_t *mp, |
| 320 | uint flags) |
| 321 | { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 322 | int error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
Eric Sandeen | f58522c | 2014-05-05 17:27:06 +1000 | [diff] [blame] | 324 | if (!xfs_sb_version_hasquota(&mp->m_sb) || flags == 0 || |
Darrick J. Wong | 41ed4a5 | 2020-07-14 10:36:09 -0700 | [diff] [blame] | 325 | (flags & ~XFS_QMOPT_QUOTALL)) { |
Eric Sandeen | 08e96e1 | 2013-10-11 20:59:05 -0500 | [diff] [blame] | 326 | xfs_debug(mp, "%s: flags=%x m_qflags=%x", |
Dave Chinner | 8221112 | 2011-03-07 10:07:35 +1100 | [diff] [blame] | 327 | __func__, flags, mp->m_qflags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 328 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Darrick J. Wong | 41ed4a5 | 2020-07-14 10:36:09 -0700 | [diff] [blame] | 331 | if (flags & XFS_QMOPT_UQUOTA) { |
Christoph Hellwig | 5d18898 | 2010-07-20 17:51:31 +1000 | [diff] [blame] | 332 | error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_uquotino); |
Jie Liu | c61a9e3 | 2013-11-22 14:04:00 +0800 | [diff] [blame] | 333 | if (error) |
| 334 | return error; |
| 335 | } |
Darrick J. Wong | 41ed4a5 | 2020-07-14 10:36:09 -0700 | [diff] [blame] | 336 | if (flags & XFS_QMOPT_GQUOTA) { |
Jie Liu | c61a9e3 | 2013-11-22 14:04:00 +0800 | [diff] [blame] | 337 | error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_gquotino); |
| 338 | if (error) |
| 339 | return error; |
| 340 | } |
Darrick J. Wong | 41ed4a5 | 2020-07-14 10:36:09 -0700 | [diff] [blame] | 341 | if (flags & XFS_QMOPT_PQUOTA) |
Jie Liu | c61a9e3 | 2013-11-22 14:04:00 +0800 | [diff] [blame] | 342 | error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_pquotino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
Jie Liu | c61a9e3 | 2013-11-22 14:04:00 +0800 | [diff] [blame] | 344 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | /* |
| 348 | * Switch on (a given) quota enforcement for a filesystem. This takes |
| 349 | * effect immediately. |
| 350 | * (Switching on quota accounting must be done at mount time.) |
| 351 | */ |
Christoph Hellwig | fcafb71 | 2009-02-09 08:47:34 +0100 | [diff] [blame] | 352 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | xfs_qm_scall_quotaon( |
| 354 | xfs_mount_t *mp, |
| 355 | uint flags) |
| 356 | { |
| 357 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | uint qf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | /* |
Kaixu Xia | cd59455 | 2020-04-22 21:54:30 -0700 | [diff] [blame] | 361 | * Switching on quota accounting must be done at mount time, |
| 362 | * only consider quota enforcement stuff here. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | */ |
Kaixu Xia | cd59455 | 2020-04-22 21:54:30 -0700 | [diff] [blame] | 364 | flags &= XFS_ALL_QUOTA_ENFD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | if (flags == 0) { |
Eric Sandeen | 08e96e1 | 2013-10-11 20:59:05 -0500 | [diff] [blame] | 367 | xfs_debug(mp, "%s: zero flags, m_qflags=%x", |
Dave Chinner | 8221112 | 2011-03-07 10:07:35 +1100 | [diff] [blame] | 368 | __func__, mp->m_qflags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 369 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | /* |
| 373 | * Can't enforce without accounting. We check the superblock |
| 374 | * qflags here instead of m_qflags because rootfs can have |
| 375 | * quota acct on ondisk without m_qflags' knowing. |
| 376 | */ |
Jan Kara | fbf64b3 | 2014-10-08 11:52:52 +0200 | [diff] [blame] | 377 | if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 && |
Chandra Seetharaman | 83e782e | 2013-06-27 17:25:10 -0500 | [diff] [blame] | 378 | (flags & XFS_UQUOTA_ENFD)) || |
Jan Kara | fbf64b3 | 2014-10-08 11:52:52 +0200 | [diff] [blame] | 379 | ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 && |
Chandra Seetharaman | 83e782e | 2013-06-27 17:25:10 -0500 | [diff] [blame] | 380 | (flags & XFS_GQUOTA_ENFD)) || |
Jan Kara | fbf64b3 | 2014-10-08 11:52:52 +0200 | [diff] [blame] | 381 | ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 && |
Chandra Seetharaman | 83e782e | 2013-06-27 17:25:10 -0500 | [diff] [blame] | 382 | (flags & XFS_PQUOTA_ENFD))) { |
Dave Chinner | 8221112 | 2011-03-07 10:07:35 +1100 | [diff] [blame] | 383 | xfs_debug(mp, |
Eric Sandeen | 08e96e1 | 2013-10-11 20:59:05 -0500 | [diff] [blame] | 384 | "%s: Can't enforce without acct, flags=%x sbflags=%x", |
Dave Chinner | 8221112 | 2011-03-07 10:07:35 +1100 | [diff] [blame] | 385 | __func__, flags, mp->m_sb.sb_qflags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 386 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
| 388 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 389 | * If everything's up to-date incore, then don't waste time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | */ |
| 391 | if ((mp->m_qflags & flags) == flags) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 392 | return -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
| 394 | /* |
| 395 | * Change sb_qflags on disk but not incore mp->qflags |
| 396 | * if this is the root filesystem. |
| 397 | */ |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 398 | spin_lock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | qf = mp->m_sb.sb_qflags; |
| 400 | mp->m_sb.sb_qflags = qf | flags; |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 401 | spin_unlock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
| 403 | /* |
| 404 | * There's nothing to change if it's the same. |
| 405 | */ |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 406 | if ((qf & flags) == flags) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 407 | return -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 409 | error = xfs_sync_sb(mp, false); |
| 410 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 411 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | /* |
| 413 | * If we aren't trying to switch on quota enforcement, we are done. |
| 414 | */ |
| 415 | if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) != |
| 416 | (mp->m_qflags & XFS_UQUOTA_ACCT)) || |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 417 | ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) != |
| 418 | (mp->m_qflags & XFS_PQUOTA_ACCT)) || |
| 419 | ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) != |
Jan Kara | fbf64b3 | 2014-10-08 11:52:52 +0200 | [diff] [blame] | 420 | (mp->m_qflags & XFS_GQUOTA_ACCT))) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 421 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
| 423 | if (! XFS_IS_QUOTA_RUNNING(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 424 | return -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
| 426 | /* |
| 427 | * Switch on quota enforcement in core. |
| 428 | */ |
Christoph Hellwig | 8a7b8a8 | 2010-04-20 17:01:30 +1000 | [diff] [blame] | 429 | mutex_lock(&mp->m_quotainfo->qi_quotaofflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD); |
Christoph Hellwig | 8a7b8a8 | 2010-04-20 17:01:30 +1000 | [diff] [blame] | 431 | mutex_unlock(&mp->m_quotainfo->qi_quotaofflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 433 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 436 | #define XFS_QC_MASK \ |
| 437 | (QC_LIMIT_MASK | QC_TIMER_MASK | QC_WARNS_MASK) |
Christoph Hellwig | c472b43 | 2010-05-06 17:05:17 -0400 | [diff] [blame] | 438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | /* |
Darrick J. Wong | d1520de | 2020-07-14 10:37:33 -0700 | [diff] [blame] | 440 | * Adjust limits of this quota, and the defaults if passed in. Returns true |
| 441 | * if the new limits made sense and were applied, false otherwise. |
| 442 | */ |
| 443 | static inline bool |
| 444 | xfs_setqlim_limits( |
| 445 | struct xfs_mount *mp, |
| 446 | struct xfs_dquot_res *res, |
| 447 | struct xfs_quota_limits *qlim, |
| 448 | xfs_qcnt_t hard, |
| 449 | xfs_qcnt_t soft, |
| 450 | const char *tag) |
| 451 | { |
| 452 | /* The hard limit can't be less than the soft limit. */ |
| 453 | if (hard != 0 && hard < soft) { |
| 454 | xfs_debug(mp, "%shard %lld < %ssoft %lld", tag, hard, tag, |
| 455 | soft); |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | res->hardlimit = hard; |
| 460 | res->softlimit = soft; |
| 461 | if (qlim) { |
| 462 | qlim->hard = hard; |
| 463 | qlim->soft = soft; |
| 464 | } |
| 465 | |
| 466 | return true; |
| 467 | } |
| 468 | |
| 469 | static inline void |
| 470 | xfs_setqlim_warns( |
| 471 | struct xfs_dquot_res *res, |
| 472 | struct xfs_quota_limits *qlim, |
| 473 | int warns) |
| 474 | { |
| 475 | res->warnings = warns; |
| 476 | if (qlim) |
| 477 | qlim->warn = warns; |
| 478 | } |
| 479 | |
| 480 | static inline void |
| 481 | xfs_setqlim_timer( |
Darrick J. Wong | 11d8a91 | 2020-08-17 09:58:36 -0700 | [diff] [blame] | 482 | struct xfs_mount *mp, |
Darrick J. Wong | d1520de | 2020-07-14 10:37:33 -0700 | [diff] [blame] | 483 | struct xfs_dquot_res *res, |
| 484 | struct xfs_quota_limits *qlim, |
| 485 | s64 timer) |
| 486 | { |
Darrick J. Wong | 11d8a91 | 2020-08-17 09:58:36 -0700 | [diff] [blame] | 487 | if (qlim) { |
| 488 | /* Set the length of the default grace period. */ |
Darrick J. Wong | ccc8e77 | 2020-08-17 09:58:42 -0700 | [diff] [blame] | 489 | res->timer = xfs_dquot_set_grace_period(timer); |
| 490 | qlim->time = res->timer; |
Darrick J. Wong | 11d8a91 | 2020-08-17 09:58:36 -0700 | [diff] [blame] | 491 | } else { |
| 492 | /* Set the grace period expiration on a quota. */ |
| 493 | res->timer = xfs_dquot_set_timeout(mp, timer); |
| 494 | } |
Darrick J. Wong | d1520de | 2020-07-14 10:37:33 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | * Adjust quota limits, and start/stop timers accordingly. |
| 499 | */ |
Christoph Hellwig | fcafb71 | 2009-02-09 08:47:34 +0100 | [diff] [blame] | 500 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | xfs_qm_scall_setqlim( |
Brian Foster | b136645 | 2013-03-18 10:51:46 -0400 | [diff] [blame] | 502 | struct xfs_mount *mp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | xfs_dqid_t id, |
Darrick J. Wong | 1a7ed27 | 2020-07-15 17:53:43 -0700 | [diff] [blame] | 504 | xfs_dqtype_t type, |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 505 | struct qc_dqblk *newlim) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | { |
Christoph Hellwig | 8a7b8a8 | 2010-04-20 17:01:30 +1000 | [diff] [blame] | 507 | struct xfs_quotainfo *q = mp->m_quotainfo; |
Brian Foster | b136645 | 2013-03-18 10:51:46 -0400 | [diff] [blame] | 508 | struct xfs_dquot *dqp; |
| 509 | struct xfs_trans *tp; |
Carlos Maiolino | be60794 | 2016-02-08 11:27:55 +1100 | [diff] [blame] | 510 | struct xfs_def_quota *defq; |
Darrick J. Wong | d1520de | 2020-07-14 10:37:33 -0700 | [diff] [blame] | 511 | struct xfs_dquot_res *res; |
| 512 | struct xfs_quota_limits *qlim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | int error; |
| 514 | xfs_qcnt_t hard, soft; |
| 515 | |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 516 | if (newlim->d_fieldmask & ~XFS_QC_MASK) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 517 | return -EINVAL; |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 518 | if ((newlim->d_fieldmask & XFS_QC_MASK) == 0) |
Christoph Hellwig | c472b43 | 2010-05-06 17:05:17 -0400 | [diff] [blame] | 519 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
Dave Chinner | f648167 | 2013-05-21 18:02:00 +1000 | [diff] [blame] | 521 | /* |
| 522 | * We don't want to race with a quotaoff so take the quotaoff lock. |
| 523 | * We don't hold an inode lock, so there's nothing else to stop |
| 524 | * a quotaoff from happening. |
| 525 | */ |
| 526 | mutex_lock(&q->qi_quotaofflock); |
| 527 | |
| 528 | /* |
| 529 | * Get the dquot (locked) before we start, as we need to do a |
| 530 | * transaction to allocate it if it doesn't exist. Once we have the |
| 531 | * dquot, unlock it so we can start the next transaction safely. We hold |
| 532 | * a reference to the dquot, so it's safe to do this unlock/lock without |
| 533 | * it being reclaimed in the mean time. |
| 534 | */ |
Darrick J. Wong | 30ab2dc | 2018-05-04 15:30:24 -0700 | [diff] [blame] | 535 | error = xfs_qm_dqget(mp, id, type, true, &dqp); |
Dave Chinner | f648167 | 2013-05-21 18:02:00 +1000 | [diff] [blame] | 536 | if (error) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 537 | ASSERT(error != -ENOENT); |
Dave Chinner | f648167 | 2013-05-21 18:02:00 +1000 | [diff] [blame] | 538 | goto out_unlock; |
| 539 | } |
Carlos Maiolino | be60794 | 2016-02-08 11:27:55 +1100 | [diff] [blame] | 540 | |
Eric Sandeen | ce6e7e79c | 2020-05-21 13:07:00 -0700 | [diff] [blame] | 541 | defq = xfs_get_defquota(q, xfs_dquot_type(dqp)); |
Dave Chinner | f648167 | 2013-05-21 18:02:00 +1000 | [diff] [blame] | 542 | xfs_dqunlock(dqp); |
| 543 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 544 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_setqlim, 0, 0, 0, &tp); |
| 545 | if (error) |
Dave Chinner | f648167 | 2013-05-21 18:02:00 +1000 | [diff] [blame] | 546 | goto out_rele; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
Dave Chinner | f648167 | 2013-05-21 18:02:00 +1000 | [diff] [blame] | 548 | xfs_dqlock(dqp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | xfs_trans_dqjoin(tp, dqp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
| 551 | /* |
Darrick J. Wong | d1520de | 2020-07-14 10:37:33 -0700 | [diff] [blame] | 552 | * Update quota limits, warnings, and timers, and the defaults |
| 553 | * if we're touching id == 0. |
| 554 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | * Make sure that hardlimits are >= soft limits before changing. |
Darrick J. Wong | d1520de | 2020-07-14 10:37:33 -0700 | [diff] [blame] | 556 | * |
| 557 | * Update warnings counter(s) if requested. |
| 558 | * |
Eric Sandeen | df42ce64 | 2020-05-21 13:07:01 -0700 | [diff] [blame] | 559 | * Timelimits for the super user set the relative time the other users |
| 560 | * can be over quota for this file system. If it is zero a default is |
| 561 | * used. Ditto for the default soft and hard limit values (already |
| 562 | * done, above), and for warnings. |
| 563 | * |
| 564 | * For other IDs, userspace can bump out the grace period if over |
| 565 | * the soft limit. |
| 566 | */ |
Eric Sandeen | df42ce64 | 2020-05-21 13:07:01 -0700 | [diff] [blame] | 567 | |
Darrick J. Wong | d1520de | 2020-07-14 10:37:33 -0700 | [diff] [blame] | 568 | /* Blocks on the data device. */ |
| 569 | hard = (newlim->d_fieldmask & QC_SPC_HARD) ? |
| 570 | (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_spc_hardlimit) : |
| 571 | dqp->q_blk.hardlimit; |
| 572 | soft = (newlim->d_fieldmask & QC_SPC_SOFT) ? |
| 573 | (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_spc_softlimit) : |
| 574 | dqp->q_blk.softlimit; |
| 575 | res = &dqp->q_blk; |
| 576 | qlim = id == 0 ? &defq->blk : NULL; |
| 577 | |
| 578 | if (xfs_setqlim_limits(mp, res, qlim, hard, soft, "blk")) |
| 579 | xfs_dquot_set_prealloc_limits(dqp); |
| 580 | if (newlim->d_fieldmask & QC_SPC_WARNS) |
| 581 | xfs_setqlim_warns(res, qlim, newlim->d_spc_warns); |
| 582 | if (newlim->d_fieldmask & QC_SPC_TIMER) |
Darrick J. Wong | 11d8a91 | 2020-08-17 09:58:36 -0700 | [diff] [blame] | 583 | xfs_setqlim_timer(mp, res, qlim, newlim->d_spc_timer); |
Darrick J. Wong | d1520de | 2020-07-14 10:37:33 -0700 | [diff] [blame] | 584 | |
| 585 | /* Blocks on the realtime device. */ |
| 586 | hard = (newlim->d_fieldmask & QC_RT_SPC_HARD) ? |
| 587 | (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_rt_spc_hardlimit) : |
| 588 | dqp->q_rtb.hardlimit; |
| 589 | soft = (newlim->d_fieldmask & QC_RT_SPC_SOFT) ? |
| 590 | (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_rt_spc_softlimit) : |
| 591 | dqp->q_rtb.softlimit; |
| 592 | res = &dqp->q_rtb; |
| 593 | qlim = id == 0 ? &defq->rtb : NULL; |
| 594 | |
| 595 | xfs_setqlim_limits(mp, res, qlim, hard, soft, "rtb"); |
| 596 | if (newlim->d_fieldmask & QC_RT_SPC_WARNS) |
| 597 | xfs_setqlim_warns(res, qlim, newlim->d_rt_spc_warns); |
| 598 | if (newlim->d_fieldmask & QC_RT_SPC_TIMER) |
Darrick J. Wong | 11d8a91 | 2020-08-17 09:58:36 -0700 | [diff] [blame] | 599 | xfs_setqlim_timer(mp, res, qlim, newlim->d_rt_spc_timer); |
Darrick J. Wong | d1520de | 2020-07-14 10:37:33 -0700 | [diff] [blame] | 600 | |
| 601 | /* Inodes */ |
| 602 | hard = (newlim->d_fieldmask & QC_INO_HARD) ? |
| 603 | (xfs_qcnt_t) newlim->d_ino_hardlimit : |
| 604 | dqp->q_ino.hardlimit; |
| 605 | soft = (newlim->d_fieldmask & QC_INO_SOFT) ? |
| 606 | (xfs_qcnt_t) newlim->d_ino_softlimit : |
| 607 | dqp->q_ino.softlimit; |
| 608 | res = &dqp->q_ino; |
| 609 | qlim = id == 0 ? &defq->ino : NULL; |
| 610 | |
| 611 | xfs_setqlim_limits(mp, res, qlim, hard, soft, "ino"); |
| 612 | if (newlim->d_fieldmask & QC_INO_WARNS) |
| 613 | xfs_setqlim_warns(res, qlim, newlim->d_ino_warns); |
| 614 | if (newlim->d_fieldmask & QC_INO_TIMER) |
Darrick J. Wong | 11d8a91 | 2020-08-17 09:58:36 -0700 | [diff] [blame] | 615 | xfs_setqlim_timer(mp, res, qlim, newlim->d_ino_timer); |
Eric Sandeen | df42ce64 | 2020-05-21 13:07:01 -0700 | [diff] [blame] | 616 | |
| 617 | if (id != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | /* |
| 619 | * If the user is now over quota, start the timelimit. |
| 620 | * The user will not be 'warned'. |
| 621 | * Note that we keep the timers ticking, whether enforcement |
| 622 | * is on or off. We don't really want to bother with iterating |
| 623 | * over all ondisk dquots and turning the timers on/off. |
| 624 | */ |
Darrick J. Wong | c8c753e | 2020-07-14 10:37:33 -0700 | [diff] [blame] | 625 | xfs_qm_adjust_dqtimers(dqp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | } |
Darrick J. Wong | 985a78f | 2020-07-14 10:37:13 -0700 | [diff] [blame] | 627 | dqp->q_flags |= XFS_DQFLAG_DIRTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | xfs_trans_log_dquot(tp, dqp); |
| 629 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 630 | error = xfs_trans_commit(tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
Dave Chinner | f648167 | 2013-05-21 18:02:00 +1000 | [diff] [blame] | 632 | out_rele: |
| 633 | xfs_qm_dqrele(dqp); |
| 634 | out_unlock: |
Christoph Hellwig | 8a7b8a8 | 2010-04-20 17:01:30 +1000 | [diff] [blame] | 635 | mutex_unlock(&q->qi_quotaofflock); |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 636 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Darrick J. Wong | 2e330e7 | 2018-05-04 15:30:20 -0700 | [diff] [blame] | 639 | /* Fill out the quota context. */ |
| 640 | static void |
| 641 | xfs_qm_scall_getquota_fill_qc( |
Christoph Hellwig | 18535a7 | 2012-02-20 02:28:16 +0000 | [diff] [blame] | 642 | struct xfs_mount *mp, |
Darrick J. Wong | 1a7ed27 | 2020-07-15 17:53:43 -0700 | [diff] [blame] | 643 | xfs_dqtype_t type, |
Darrick J. Wong | 2e330e7 | 2018-05-04 15:30:20 -0700 | [diff] [blame] | 644 | const struct xfs_dquot *dqp, |
| 645 | struct qc_dqblk *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | { |
| 647 | memset(dst, 0, sizeof(*dst)); |
Darrick J. Wong | d3537cf | 2020-07-14 10:37:31 -0700 | [diff] [blame] | 648 | dst->d_spc_hardlimit = XFS_FSB_TO_B(mp, dqp->q_blk.hardlimit); |
| 649 | dst->d_spc_softlimit = XFS_FSB_TO_B(mp, dqp->q_blk.softlimit); |
Darrick J. Wong | 19dce7e | 2020-07-14 10:37:32 -0700 | [diff] [blame] | 650 | dst->d_ino_hardlimit = dqp->q_ino.hardlimit; |
| 651 | dst->d_ino_softlimit = dqp->q_ino.softlimit; |
Darrick J. Wong | 784e80f | 2020-07-14 10:37:30 -0700 | [diff] [blame] | 652 | dst->d_space = XFS_FSB_TO_B(mp, dqp->q_blk.reserved); |
| 653 | dst->d_ino_count = dqp->q_ino.reserved; |
Darrick J. Wong | 19dce7e | 2020-07-14 10:37:32 -0700 | [diff] [blame] | 654 | dst->d_spc_timer = dqp->q_blk.timer; |
| 655 | dst->d_ino_timer = dqp->q_ino.timer; |
Darrick J. Wong | c8c45fb | 2020-07-14 10:37:31 -0700 | [diff] [blame] | 656 | dst->d_ino_warns = dqp->q_ino.warnings; |
| 657 | dst->d_spc_warns = dqp->q_blk.warnings; |
Darrick J. Wong | d3537cf | 2020-07-14 10:37:31 -0700 | [diff] [blame] | 658 | dst->d_rt_spc_hardlimit = XFS_FSB_TO_B(mp, dqp->q_rtb.hardlimit); |
| 659 | dst->d_rt_spc_softlimit = XFS_FSB_TO_B(mp, dqp->q_rtb.softlimit); |
Darrick J. Wong | 784e80f | 2020-07-14 10:37:30 -0700 | [diff] [blame] | 660 | dst->d_rt_space = XFS_FSB_TO_B(mp, dqp->q_rtb.reserved); |
Darrick J. Wong | 19dce7e | 2020-07-14 10:37:32 -0700 | [diff] [blame] | 661 | dst->d_rt_spc_timer = dqp->q_rtb.timer; |
Darrick J. Wong | c8c45fb | 2020-07-14 10:37:31 -0700 | [diff] [blame] | 662 | dst->d_rt_spc_warns = dqp->q_rtb.warnings; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | |
| 664 | /* |
| 665 | * Internally, we don't reset all the timers when quota enforcement |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 666 | * gets turned off. No need to confuse the user level code, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | * so return zeroes in that case. |
| 668 | */ |
Darrick J. Wong | dbcbc7b | 2020-07-15 17:48:31 -0700 | [diff] [blame] | 669 | if (!xfs_dquot_is_enforced(dqp)) { |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 670 | dst->d_spc_timer = 0; |
| 671 | dst->d_ino_timer = 0; |
| 672 | dst->d_rt_spc_timer = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | #ifdef DEBUG |
Darrick J. Wong | dbcbc7b | 2020-07-15 17:48:31 -0700 | [diff] [blame] | 676 | if (xfs_dquot_is_enforced(dqp) && dqp->q_id != 0) { |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 677 | if ((dst->d_space > dst->d_spc_softlimit) && |
| 678 | (dst->d_spc_softlimit > 0)) { |
| 679 | ASSERT(dst->d_spc_timer != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | } |
Darrick J. Wong | d3537cf | 2020-07-14 10:37:31 -0700 | [diff] [blame] | 681 | if ((dst->d_ino_count > dqp->q_ino.softlimit) && |
| 682 | (dqp->q_ino.softlimit > 0)) { |
Jan Kara | 14bf61f | 2014-10-09 16:03:13 +0200 | [diff] [blame] | 683 | ASSERT(dst->d_ino_timer != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | #endif |
Darrick J. Wong | 2e330e7 | 2018-05-04 15:30:20 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /* Return the quota information for the dquot matching id. */ |
| 690 | int |
| 691 | xfs_qm_scall_getquota( |
| 692 | struct xfs_mount *mp, |
| 693 | xfs_dqid_t id, |
Darrick J. Wong | 1a7ed27 | 2020-07-15 17:53:43 -0700 | [diff] [blame] | 694 | xfs_dqtype_t type, |
Darrick J. Wong | 2e330e7 | 2018-05-04 15:30:20 -0700 | [diff] [blame] | 695 | struct qc_dqblk *dst) |
| 696 | { |
| 697 | struct xfs_dquot *dqp; |
| 698 | int error; |
| 699 | |
| 700 | /* |
Darrick J. Wong | 30ab2dc | 2018-05-04 15:30:24 -0700 | [diff] [blame] | 701 | * Try to get the dquot. We don't want it allocated on disk, so don't |
| 702 | * set doalloc. If it doesn't exist, we'll get ENOENT back. |
Darrick J. Wong | 2e330e7 | 2018-05-04 15:30:20 -0700 | [diff] [blame] | 703 | */ |
Darrick J. Wong | 30ab2dc | 2018-05-04 15:30:24 -0700 | [diff] [blame] | 704 | error = xfs_qm_dqget(mp, id, type, false, &dqp); |
Darrick J. Wong | 2e330e7 | 2018-05-04 15:30:20 -0700 | [diff] [blame] | 705 | if (error) |
| 706 | return error; |
| 707 | |
| 708 | /* |
| 709 | * If everything's NULL, this dquot doesn't quite exist as far as |
| 710 | * our utility programs are concerned. |
| 711 | */ |
| 712 | if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) { |
| 713 | error = -ENOENT; |
| 714 | goto out_put; |
| 715 | } |
| 716 | |
| 717 | xfs_qm_scall_getquota_fill_qc(mp, type, dqp, dst); |
| 718 | |
Christoph Hellwig | 18535a7 | 2012-02-20 02:28:16 +0000 | [diff] [blame] | 719 | out_put: |
| 720 | xfs_qm_dqput(dqp); |
| 721 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Darrick J. Wong | 2e330e7 | 2018-05-04 15:30:20 -0700 | [diff] [blame] | 724 | /* |
| 725 | * Return the quota information for the first initialized dquot whose id |
| 726 | * is at least as high as id. |
| 727 | */ |
| 728 | int |
| 729 | xfs_qm_scall_getquota_next( |
| 730 | struct xfs_mount *mp, |
| 731 | xfs_dqid_t *id, |
Darrick J. Wong | 1a7ed27 | 2020-07-15 17:53:43 -0700 | [diff] [blame] | 732 | xfs_dqtype_t type, |
Darrick J. Wong | 2e330e7 | 2018-05-04 15:30:20 -0700 | [diff] [blame] | 733 | struct qc_dqblk *dst) |
| 734 | { |
| 735 | struct xfs_dquot *dqp; |
| 736 | int error; |
| 737 | |
| 738 | error = xfs_qm_dqget_next(mp, *id, type, &dqp); |
| 739 | if (error) |
| 740 | return error; |
| 741 | |
| 742 | /* Fill in the ID we actually read from disk */ |
Darrick J. Wong | c51df73 | 2020-07-14 10:37:30 -0700 | [diff] [blame] | 743 | *id = dqp->q_id; |
Darrick J. Wong | 2e330e7 | 2018-05-04 15:30:20 -0700 | [diff] [blame] | 744 | |
| 745 | xfs_qm_scall_getquota_fill_qc(mp, type, dqp, dst); |
| 746 | |
| 747 | xfs_qm_dqput(dqp); |
| 748 | return error; |
| 749 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 751 | STATIC int |
| 752 | xfs_dqrele_inode( |
| 753 | struct xfs_inode *ip, |
Brian Foster | a454f74 | 2012-11-06 09:50:39 -0500 | [diff] [blame] | 754 | void *args) |
David Chinner | 5b4d89a | 2008-10-30 17:08:03 +1100 | [diff] [blame] | 755 | { |
Darrick J. Wong | 390600f | 2020-05-21 13:08:48 -0700 | [diff] [blame] | 756 | uint *flags = args; |
| 757 | |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 758 | /* skip quota inodes */ |
Christoph Hellwig | 8a7b8a8 | 2010-04-20 17:01:30 +1000 | [diff] [blame] | 759 | if (ip == ip->i_mount->m_quotainfo->qi_uquotaip || |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 760 | ip == ip->i_mount->m_quotainfo->qi_gquotaip || |
| 761 | ip == ip->i_mount->m_quotainfo->qi_pquotaip) { |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 762 | ASSERT(ip->i_udquot == NULL); |
| 763 | ASSERT(ip->i_gdquot == NULL); |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 764 | ASSERT(ip->i_pdquot == NULL); |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 765 | return 0; |
| 766 | } |
Dave Chinner | cb4f0d1 | 2008-11-10 17:11:18 +1100 | [diff] [blame] | 767 | |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 768 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Darrick J. Wong | 390600f | 2020-05-21 13:08:48 -0700 | [diff] [blame] | 769 | if ((*flags & XFS_UQUOTA_ACCT) && ip->i_udquot) { |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 770 | xfs_qm_dqrele(ip->i_udquot); |
| 771 | ip->i_udquot = NULL; |
| 772 | } |
Darrick J. Wong | 390600f | 2020-05-21 13:08:48 -0700 | [diff] [blame] | 773 | if ((*flags & XFS_GQUOTA_ACCT) && ip->i_gdquot) { |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 774 | xfs_qm_dqrele(ip->i_gdquot); |
| 775 | ip->i_gdquot = NULL; |
| 776 | } |
Darrick J. Wong | 390600f | 2020-05-21 13:08:48 -0700 | [diff] [blame] | 777 | if ((*flags & XFS_PQUOTA_ACCT) && ip->i_pdquot) { |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 778 | xfs_qm_dqrele(ip->i_pdquot); |
| 779 | ip->i_pdquot = NULL; |
| 780 | } |
Christoph Hellwig | f2d6761 | 2010-06-24 11:52:50 +1000 | [diff] [blame] | 781 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 782 | return 0; |
David Chinner | 5b4d89a | 2008-10-30 17:08:03 +1100 | [diff] [blame] | 783 | } |
| 784 | |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 785 | |
David Chinner | 5b4d89a | 2008-10-30 17:08:03 +1100 | [diff] [blame] | 786 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | * Go thru all the inodes in the file system, releasing their dquots. |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 788 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | * Note that the mount structure gets modified to indicate that quotas are off |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 790 | * AFTER this, in the case of quotaoff. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | */ |
| 792 | void |
| 793 | xfs_qm_dqrele_all_inodes( |
Darrick J. Wong | 390600f | 2020-05-21 13:08:48 -0700 | [diff] [blame] | 794 | struct xfs_mount *mp, |
| 795 | uint flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | ASSERT(mp->m_quotainfo); |
Darrick J. Wong | 042f65f | 2020-05-21 13:08:50 -0700 | [diff] [blame] | 798 | xfs_inode_walk(mp, XFS_INODE_WALK_INEW_WAIT, xfs_dqrele_inode, |
Darrick J. Wong | 390600f | 2020-05-21 13:08:48 -0700 | [diff] [blame] | 799 | &flags, XFS_ICI_NO_TAG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | } |