Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 4ce3121 | 2005-11-02 14:59:41 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 4ce3121 | 2005-11-02 14:59:41 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 4ce3121 | 2005-11-02 14:59:41 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 4ce3121 | 2005-11-02 14:59:41 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 18 | |
| 19 | #include <linux/capability.h> |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "xfs.h" |
| 22 | #include "xfs_fs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 25 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "xfs_trans.h" |
| 27 | #include "xfs_sb.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 28 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "xfs_dir2.h" |
| 30 | #include "xfs_alloc.h" |
| 31 | #include "xfs_dmapi.h" |
| 32 | #include "xfs_quota.h" |
| 33 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 35 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "xfs_dir2_sf.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 38 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include "xfs_dinode.h" |
| 40 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 41 | #include "xfs_ialloc.h" |
| 42 | #include "xfs_itable.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include "xfs_bmap.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 44 | #include "xfs_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include "xfs_rtalloc.h" |
| 46 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include "xfs_rw.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include "xfs_attr.h" |
| 49 | #include "xfs_buf_item.h" |
| 50 | #include "xfs_utils.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include "xfs_qm.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 52 | #include "xfs_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | #ifdef DEBUG |
| 55 | # define qdprintk(s, args...) cmn_err(CE_DEBUG, s, ## args) |
| 56 | #else |
| 57 | # define qdprintk(s, args...) do { } while (0) |
| 58 | #endif |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | STATIC int xfs_qm_log_quotaoff(xfs_mount_t *, xfs_qoff_logitem_t **, uint); |
| 61 | STATIC int xfs_qm_log_quotaoff_end(xfs_mount_t *, xfs_qoff_logitem_t *, |
| 62 | uint); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | STATIC uint xfs_qm_export_flags(uint); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | STATIC uint xfs_qm_export_qtype_flags(uint); |
| 65 | STATIC void xfs_qm_export_dquot(xfs_mount_t *, xfs_disk_dquot_t *, |
| 66 | fs_disk_quota_t *); |
| 67 | |
| 68 | |
| 69 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | * Turn off quota accounting and/or enforcement for all udquots and/or |
| 71 | * gdquots. Called only at unmount time. |
| 72 | * |
| 73 | * This assumes that there are no dquots of this file system cached |
| 74 | * incore, and modifies the ondisk dquot directly. Therefore, for example, |
| 75 | * it is an error to call this twice, without purging the cache. |
| 76 | */ |
Christoph Hellwig | fcafb71 | 2009-02-09 08:47:34 +0100 | [diff] [blame] | 77 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | xfs_qm_scall_quotaoff( |
| 79 | xfs_mount_t *mp, |
Christoph Hellwig | fcafb71 | 2009-02-09 08:47:34 +0100 | [diff] [blame] | 80 | uint flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
| 82 | uint dqtype; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | int error; |
| 84 | uint inactivate_flags; |
| 85 | xfs_qoff_logitem_t *qoffstart; |
| 86 | int nculprits; |
| 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | /* |
| 89 | * No file system can have quotas enabled on disk but not in core. |
| 90 | * Note that quota utilities (like quotaoff) _expect_ |
| 91 | * errno == EEXIST here. |
| 92 | */ |
| 93 | if ((mp->m_qflags & flags) == 0) |
| 94 | return XFS_ERROR(EEXIST); |
| 95 | error = 0; |
| 96 | |
| 97 | flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD); |
| 98 | |
| 99 | /* |
| 100 | * We don't want to deal with two quotaoffs messing up each other, |
| 101 | * so we're going to serialize it. quotaoff isn't exactly a performance |
| 102 | * critical thing. |
| 103 | * If quotaoff, then we must be dealing with the root filesystem. |
| 104 | */ |
| 105 | ASSERT(mp->m_quotainfo); |
| 106 | if (mp->m_quotainfo) |
Jes Sorensen | 794ee1b | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 107 | mutex_lock(&(XFS_QI_QOFFLOCK(mp))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | ASSERT(mp->m_quotainfo); |
| 110 | |
| 111 | /* |
| 112 | * If we're just turning off quota enforcement, change mp and go. |
| 113 | */ |
| 114 | if ((flags & XFS_ALL_QUOTA_ACCT) == 0) { |
| 115 | mp->m_qflags &= ~(flags); |
| 116 | |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 117 | spin_lock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | mp->m_sb.sb_qflags = mp->m_qflags; |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 119 | spin_unlock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | mutex_unlock(&(XFS_QI_QOFFLOCK(mp))); |
| 121 | |
| 122 | /* XXX what to do if error ? Revert back to old vals incore ? */ |
| 123 | error = xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS); |
| 124 | return (error); |
| 125 | } |
| 126 | |
| 127 | dqtype = 0; |
| 128 | inactivate_flags = 0; |
| 129 | /* |
| 130 | * If accounting is off, we must turn enforcement off, clear the |
| 131 | * quota 'CHKD' certificate to make it known that we have to |
| 132 | * do a quotacheck the next time this quota is turned on. |
| 133 | */ |
| 134 | if (flags & XFS_UQUOTA_ACCT) { |
| 135 | dqtype |= XFS_QMOPT_UQUOTA; |
| 136 | flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD); |
| 137 | inactivate_flags |= XFS_UQUOTA_ACTIVE; |
| 138 | } |
| 139 | if (flags & XFS_GQUOTA_ACCT) { |
| 140 | dqtype |= XFS_QMOPT_GQUOTA; |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 141 | flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | inactivate_flags |= XFS_GQUOTA_ACTIVE; |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 143 | } else if (flags & XFS_PQUOTA_ACCT) { |
| 144 | dqtype |= XFS_QMOPT_PQUOTA; |
| 145 | flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD); |
| 146 | inactivate_flags |= XFS_PQUOTA_ACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Nothing to do? Don't complain. This happens when we're just |
| 151 | * turning off quota enforcement. |
| 152 | */ |
| 153 | if ((mp->m_qflags & flags) == 0) { |
| 154 | mutex_unlock(&(XFS_QI_QOFFLOCK(mp))); |
| 155 | return (0); |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Write the LI_QUOTAOFF log record, and do SB changes atomically, |
David Chinner | cb6edc2 | 2008-04-10 12:20:45 +1000 | [diff] [blame] | 160 | * and synchronously. If we fail to write, we should abort the |
| 161 | * operation as it cannot be recovered safely if we crash. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | */ |
David Chinner | cb6edc2 | 2008-04-10 12:20:45 +1000 | [diff] [blame] | 163 | error = xfs_qm_log_quotaoff(mp, &qoffstart, flags); |
| 164 | if (error) |
| 165 | goto out_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct |
| 169 | * to take care of the race between dqget and quotaoff. We don't take |
| 170 | * any special locks to reset these bits. All processes need to check |
| 171 | * these bits *after* taking inode lock(s) to see if the particular |
| 172 | * quota type is in the process of being turned off. If *ACTIVE, it is |
| 173 | * guaranteed that all dquot structures and all quotainode ptrs will all |
| 174 | * stay valid as long as that inode is kept locked. |
| 175 | * |
| 176 | * There is no turning back after this. |
| 177 | */ |
| 178 | mp->m_qflags &= ~inactivate_flags; |
| 179 | |
| 180 | /* |
| 181 | * Give back all the dquot reference(s) held by inodes. |
| 182 | * Here we go thru every single incore inode in this file system, and |
| 183 | * do a dqrele on the i_udquot/i_gdquot that it may have. |
| 184 | * Essentially, as long as somebody has an inode locked, this guarantees |
| 185 | * that quotas will not be turned off. This is handy because in a |
| 186 | * transaction once we lock the inode(s) and check for quotaon, we can |
| 187 | * depend on the quota inodes (and other things) being valid as long as |
| 188 | * we keep the lock(s). |
| 189 | */ |
| 190 | xfs_qm_dqrele_all_inodes(mp, flags); |
| 191 | |
| 192 | /* |
| 193 | * Next we make the changes in the quota flag in the mount struct. |
| 194 | * This isn't protected by a particular lock directly, because we |
| 195 | * don't want to take a mrlock everytime we depend on quotas being on. |
| 196 | */ |
| 197 | mp->m_qflags &= ~(flags); |
| 198 | |
| 199 | /* |
| 200 | * Go through all the dquots of this file system and purge them, |
| 201 | * according to what was turned off. We may not be able to get rid |
| 202 | * of all dquots, because dquots can have temporary references that |
| 203 | * are not attached to inodes. eg. xfs_setattr, xfs_create. |
| 204 | * So, if we couldn't purge all the dquots from the filesystem, |
| 205 | * we can't get rid of the incore data structures. |
| 206 | */ |
| 207 | while ((nculprits = xfs_qm_dqpurge_all(mp, dqtype|XFS_QMOPT_QUOTAOFF))) |
| 208 | delay(10 * nculprits); |
| 209 | |
| 210 | /* |
| 211 | * Transactions that had started before ACTIVE state bit was cleared |
| 212 | * could have logged many dquots, so they'd have higher LSNs than |
| 213 | * the first QUOTAOFF log record does. If we happen to crash when |
| 214 | * the tail of the log has gone past the QUOTAOFF record, but |
| 215 | * before the last dquot modification, those dquots __will__ |
| 216 | * recover, and that's not good. |
| 217 | * |
| 218 | * So, we have QUOTAOFF start and end logitems; the start |
| 219 | * logitem won't get overwritten until the end logitem appears... |
| 220 | */ |
David Chinner | cb6edc2 | 2008-04-10 12:20:45 +1000 | [diff] [blame] | 221 | error = xfs_qm_log_quotaoff_end(mp, qoffstart, flags); |
| 222 | if (error) { |
| 223 | /* We're screwed now. Shutdown is the only option. */ |
| 224 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
| 225 | goto out_error; |
| 226 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
| 228 | /* |
| 229 | * If quotas is completely disabled, close shop. |
| 230 | */ |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 231 | if (((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET1) || |
| 232 | ((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | mutex_unlock(&(XFS_QI_QOFFLOCK(mp))); |
| 234 | xfs_qm_destroy_quotainfo(mp); |
| 235 | return (0); |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Release our quotainode references, and vn_purge them, |
| 240 | * if we don't need them anymore. |
| 241 | */ |
| 242 | if ((dqtype & XFS_QMOPT_UQUOTA) && XFS_QI_UQIP(mp)) { |
Christoph Hellwig | 26cc002 | 2008-07-18 17:12:43 +1000 | [diff] [blame] | 243 | IRELE(XFS_QI_UQIP(mp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | XFS_QI_UQIP(mp) = NULL; |
| 245 | } |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 246 | if ((dqtype & (XFS_QMOPT_GQUOTA|XFS_QMOPT_PQUOTA)) && XFS_QI_GQIP(mp)) { |
Christoph Hellwig | 26cc002 | 2008-07-18 17:12:43 +1000 | [diff] [blame] | 247 | IRELE(XFS_QI_GQIP(mp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | XFS_QI_GQIP(mp) = NULL; |
| 249 | } |
David Chinner | cb6edc2 | 2008-04-10 12:20:45 +1000 | [diff] [blame] | 250 | out_error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | mutex_unlock(&(XFS_QI_QOFFLOCK(mp))); |
| 252 | |
| 253 | return (error); |
| 254 | } |
| 255 | |
Christoph Hellwig | fcafb71 | 2009-02-09 08:47:34 +0100 | [diff] [blame] | 256 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | xfs_qm_scall_trunc_qfiles( |
| 258 | xfs_mount_t *mp, |
| 259 | uint flags) |
| 260 | { |
David Chinner | 88ab020 | 2008-04-10 12:20:51 +1000 | [diff] [blame] | 261 | int error = 0, error2 = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | xfs_inode_t *qip; |
| 263 | |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 264 | if (!xfs_sb_version_hasquota(&mp->m_sb) || flags == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | qdprintk("qtrunc flags=%x m_qflags=%x\n", flags, mp->m_qflags); |
| 266 | return XFS_ERROR(EINVAL); |
| 267 | } |
| 268 | |
| 269 | if ((flags & XFS_DQ_USER) && mp->m_sb.sb_uquotino != NULLFSINO) { |
| 270 | error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino, 0, 0, &qip, 0); |
David Chinner | 88ab020 | 2008-04-10 12:20:51 +1000 | [diff] [blame] | 271 | if (!error) { |
| 272 | error = xfs_truncate_file(mp, qip); |
Christoph Hellwig | 4335509 | 2008-03-27 18:01:08 +1100 | [diff] [blame] | 273 | IRELE(qip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 277 | if ((flags & (XFS_DQ_GROUP|XFS_DQ_PROJ)) && |
| 278 | mp->m_sb.sb_gquotino != NULLFSINO) { |
David Chinner | 88ab020 | 2008-04-10 12:20:51 +1000 | [diff] [blame] | 279 | error2 = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino, 0, 0, &qip, 0); |
| 280 | if (!error2) { |
| 281 | error2 = xfs_truncate_file(mp, qip); |
Christoph Hellwig | 4335509 | 2008-03-27 18:01:08 +1100 | [diff] [blame] | 282 | IRELE(qip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
David Chinner | 88ab020 | 2008-04-10 12:20:51 +1000 | [diff] [blame] | 286 | return error ? error : error2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | |
| 290 | /* |
| 291 | * Switch on (a given) quota enforcement for a filesystem. This takes |
| 292 | * effect immediately. |
| 293 | * (Switching on quota accounting must be done at mount time.) |
| 294 | */ |
Christoph Hellwig | fcafb71 | 2009-02-09 08:47:34 +0100 | [diff] [blame] | 295 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | xfs_qm_scall_quotaon( |
| 297 | xfs_mount_t *mp, |
| 298 | uint flags) |
| 299 | { |
| 300 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | uint qf; |
| 302 | uint accflags; |
| 303 | __int64_t sbflags; |
| 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD); |
| 306 | /* |
| 307 | * Switching on quota accounting must be done at mount time. |
| 308 | */ |
| 309 | accflags = flags & XFS_ALL_QUOTA_ACCT; |
| 310 | flags &= ~(XFS_ALL_QUOTA_ACCT); |
| 311 | |
| 312 | sbflags = 0; |
| 313 | |
| 314 | if (flags == 0) { |
| 315 | qdprintk("quotaon: zero flags, m_qflags=%x\n", mp->m_qflags); |
| 316 | return XFS_ERROR(EINVAL); |
| 317 | } |
| 318 | |
| 319 | /* No fs can turn on quotas with a delayed effect */ |
| 320 | ASSERT((flags & XFS_ALL_QUOTA_ACCT) == 0); |
| 321 | |
| 322 | /* |
| 323 | * Can't enforce without accounting. We check the superblock |
| 324 | * qflags here instead of m_qflags because rootfs can have |
| 325 | * quota acct on ondisk without m_qflags' knowing. |
| 326 | */ |
| 327 | if (((flags & XFS_UQUOTA_ACCT) == 0 && |
| 328 | (mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 && |
| 329 | (flags & XFS_UQUOTA_ENFD)) |
| 330 | || |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 331 | ((flags & XFS_PQUOTA_ACCT) == 0 && |
| 332 | (mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 && |
Donald Douwsma | 424ea91 | 2007-05-08 13:49:15 +1000 | [diff] [blame] | 333 | (flags & XFS_GQUOTA_ACCT) == 0 && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | (mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 && |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 335 | (flags & XFS_OQUOTA_ENFD))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | qdprintk("Can't enforce without acct, flags=%x sbflags=%x\n", |
| 337 | flags, mp->m_sb.sb_qflags); |
| 338 | return XFS_ERROR(EINVAL); |
| 339 | } |
| 340 | /* |
| 341 | * If everything's upto-date incore, then don't waste time. |
| 342 | */ |
| 343 | if ((mp->m_qflags & flags) == flags) |
| 344 | return XFS_ERROR(EEXIST); |
| 345 | |
| 346 | /* |
| 347 | * Change sb_qflags on disk but not incore mp->qflags |
| 348 | * if this is the root filesystem. |
| 349 | */ |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 350 | spin_lock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | qf = mp->m_sb.sb_qflags; |
| 352 | mp->m_sb.sb_qflags = qf | flags; |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 353 | spin_unlock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
| 355 | /* |
| 356 | * There's nothing to change if it's the same. |
| 357 | */ |
| 358 | if ((qf & flags) == flags && sbflags == 0) |
| 359 | return XFS_ERROR(EEXIST); |
| 360 | sbflags |= XFS_SB_QFLAGS; |
| 361 | |
| 362 | if ((error = xfs_qm_write_sb_changes(mp, sbflags))) |
| 363 | return (error); |
| 364 | /* |
| 365 | * If we aren't trying to switch on quota enforcement, we are done. |
| 366 | */ |
| 367 | if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) != |
| 368 | (mp->m_qflags & XFS_UQUOTA_ACCT)) || |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 369 | ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) != |
| 370 | (mp->m_qflags & XFS_PQUOTA_ACCT)) || |
| 371 | ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) != |
| 372 | (mp->m_qflags & XFS_GQUOTA_ACCT)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | (flags & XFS_ALL_QUOTA_ENFD) == 0) |
| 374 | return (0); |
| 375 | |
| 376 | if (! XFS_IS_QUOTA_RUNNING(mp)) |
| 377 | return XFS_ERROR(ESRCH); |
| 378 | |
| 379 | /* |
| 380 | * Switch on quota enforcement in core. |
| 381 | */ |
Jes Sorensen | 794ee1b | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 382 | mutex_lock(&(XFS_QI_QOFFLOCK(mp))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD); |
| 384 | mutex_unlock(&(XFS_QI_QOFFLOCK(mp))); |
| 385 | |
| 386 | return (0); |
| 387 | } |
| 388 | |
| 389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | /* |
| 391 | * Return quota status information, such as uquota-off, enforcements, etc. |
| 392 | */ |
Christoph Hellwig | fcafb71 | 2009-02-09 08:47:34 +0100 | [diff] [blame] | 393 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | xfs_qm_scall_getqstat( |
| 395 | xfs_mount_t *mp, |
| 396 | fs_quota_stat_t *out) |
| 397 | { |
| 398 | xfs_inode_t *uip, *gip; |
| 399 | boolean_t tempuqip, tempgqip; |
| 400 | |
| 401 | uip = gip = NULL; |
| 402 | tempuqip = tempgqip = B_FALSE; |
| 403 | memset(out, 0, sizeof(fs_quota_stat_t)); |
| 404 | |
| 405 | out->qs_version = FS_QSTAT_VERSION; |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 406 | if (!xfs_sb_version_hasquota(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | out->qs_uquota.qfs_ino = NULLFSINO; |
| 408 | out->qs_gquota.qfs_ino = NULLFSINO; |
| 409 | return (0); |
| 410 | } |
| 411 | out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags & |
| 412 | (XFS_ALL_QUOTA_ACCT| |
| 413 | XFS_ALL_QUOTA_ENFD)); |
| 414 | out->qs_pad = 0; |
| 415 | out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino; |
| 416 | out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino; |
| 417 | |
| 418 | if (mp->m_quotainfo) { |
| 419 | uip = mp->m_quotainfo->qi_uquotaip; |
| 420 | gip = mp->m_quotainfo->qi_gquotaip; |
| 421 | } |
| 422 | if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) { |
| 423 | if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino, |
| 424 | 0, 0, &uip, 0) == 0) |
| 425 | tempuqip = B_TRUE; |
| 426 | } |
| 427 | if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) { |
| 428 | if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino, |
| 429 | 0, 0, &gip, 0) == 0) |
| 430 | tempgqip = B_TRUE; |
| 431 | } |
| 432 | if (uip) { |
| 433 | out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks; |
| 434 | out->qs_uquota.qfs_nextents = uip->i_d.di_nextents; |
| 435 | if (tempuqip) |
Christoph Hellwig | 4335509 | 2008-03-27 18:01:08 +1100 | [diff] [blame] | 436 | IRELE(uip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | } |
| 438 | if (gip) { |
| 439 | out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks; |
| 440 | out->qs_gquota.qfs_nextents = gip->i_d.di_nextents; |
| 441 | if (tempgqip) |
Christoph Hellwig | 4335509 | 2008-03-27 18:01:08 +1100 | [diff] [blame] | 442 | IRELE(gip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
| 444 | if (mp->m_quotainfo) { |
| 445 | out->qs_incoredqs = XFS_QI_MPLNDQUOTS(mp); |
| 446 | out->qs_btimelimit = XFS_QI_BTIMELIMIT(mp); |
| 447 | out->qs_itimelimit = XFS_QI_ITIMELIMIT(mp); |
| 448 | out->qs_rtbtimelimit = XFS_QI_RTBTIMELIMIT(mp); |
| 449 | out->qs_bwarnlimit = XFS_QI_BWARNLIMIT(mp); |
| 450 | out->qs_iwarnlimit = XFS_QI_IWARNLIMIT(mp); |
| 451 | } |
| 452 | return (0); |
| 453 | } |
| 454 | |
| 455 | /* |
| 456 | * Adjust quota limits, and start/stop timers accordingly. |
| 457 | */ |
Christoph Hellwig | fcafb71 | 2009-02-09 08:47:34 +0100 | [diff] [blame] | 458 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | xfs_qm_scall_setqlim( |
| 460 | xfs_mount_t *mp, |
| 461 | xfs_dqid_t id, |
| 462 | uint type, |
| 463 | fs_disk_quota_t *newlim) |
| 464 | { |
| 465 | xfs_disk_dquot_t *ddq; |
| 466 | xfs_dquot_t *dqp; |
| 467 | xfs_trans_t *tp; |
| 468 | int error; |
| 469 | xfs_qcnt_t hard, soft; |
| 470 | |
Nathan Scott | 754002b | 2005-06-21 15:49:06 +1000 | [diff] [blame] | 471 | if ((newlim->d_fieldmask & |
| 472 | (FS_DQ_LIMIT_MASK|FS_DQ_TIMER_MASK|FS_DQ_WARNS_MASK)) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | return (0); |
| 474 | |
| 475 | tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM); |
| 476 | if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_disk_dquot_t) + 128, |
| 477 | 0, 0, XFS_DEFAULT_LOG_COUNT))) { |
| 478 | xfs_trans_cancel(tp, 0); |
| 479 | return (error); |
| 480 | } |
| 481 | |
| 482 | /* |
| 483 | * We don't want to race with a quotaoff so take the quotaoff lock. |
| 484 | * (We don't hold an inode lock, so there's nothing else to stop |
| 485 | * a quotaoff from happening). (XXXThis doesn't currently happen |
| 486 | * because we take the vfslock before calling xfs_qm_sysent). |
| 487 | */ |
Jes Sorensen | 794ee1b | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 488 | mutex_lock(&(XFS_QI_QOFFLOCK(mp))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
| 490 | /* |
| 491 | * Get the dquot (locked), and join it to the transaction. |
| 492 | * Allocate the dquot if this doesn't exist. |
| 493 | */ |
| 494 | if ((error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp))) { |
| 495 | xfs_trans_cancel(tp, XFS_TRANS_ABORT); |
| 496 | mutex_unlock(&(XFS_QI_QOFFLOCK(mp))); |
| 497 | ASSERT(error != ENOENT); |
| 498 | return (error); |
| 499 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | xfs_trans_dqjoin(tp, dqp); |
| 501 | ddq = &dqp->q_core; |
| 502 | |
| 503 | /* |
| 504 | * Make sure that hardlimits are >= soft limits before changing. |
| 505 | */ |
| 506 | hard = (newlim->d_fieldmask & FS_DQ_BHARD) ? |
| 507 | (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) : |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 508 | be64_to_cpu(ddq->d_blk_hardlimit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ? |
| 510 | (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) : |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 511 | be64_to_cpu(ddq->d_blk_softlimit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | if (hard == 0 || hard >= soft) { |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 513 | ddq->d_blk_hardlimit = cpu_to_be64(hard); |
| 514 | ddq->d_blk_softlimit = cpu_to_be64(soft); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | if (id == 0) { |
| 516 | mp->m_quotainfo->qi_bhardlimit = hard; |
| 517 | mp->m_quotainfo->qi_bsoftlimit = soft; |
| 518 | } |
| 519 | } else { |
| 520 | qdprintk("blkhard %Ld < blksoft %Ld\n", hard, soft); |
| 521 | } |
| 522 | hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ? |
| 523 | (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) : |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 524 | be64_to_cpu(ddq->d_rtb_hardlimit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ? |
| 526 | (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) : |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 527 | be64_to_cpu(ddq->d_rtb_softlimit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | if (hard == 0 || hard >= soft) { |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 529 | ddq->d_rtb_hardlimit = cpu_to_be64(hard); |
| 530 | ddq->d_rtb_softlimit = cpu_to_be64(soft); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | if (id == 0) { |
| 532 | mp->m_quotainfo->qi_rtbhardlimit = hard; |
| 533 | mp->m_quotainfo->qi_rtbsoftlimit = soft; |
| 534 | } |
| 535 | } else { |
| 536 | qdprintk("rtbhard %Ld < rtbsoft %Ld\n", hard, soft); |
| 537 | } |
| 538 | |
| 539 | hard = (newlim->d_fieldmask & FS_DQ_IHARD) ? |
| 540 | (xfs_qcnt_t) newlim->d_ino_hardlimit : |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 541 | be64_to_cpu(ddq->d_ino_hardlimit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ? |
| 543 | (xfs_qcnt_t) newlim->d_ino_softlimit : |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 544 | be64_to_cpu(ddq->d_ino_softlimit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | if (hard == 0 || hard >= soft) { |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 546 | ddq->d_ino_hardlimit = cpu_to_be64(hard); |
| 547 | ddq->d_ino_softlimit = cpu_to_be64(soft); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | if (id == 0) { |
| 549 | mp->m_quotainfo->qi_ihardlimit = hard; |
| 550 | mp->m_quotainfo->qi_isoftlimit = soft; |
| 551 | } |
| 552 | } else { |
| 553 | qdprintk("ihard %Ld < isoft %Ld\n", hard, soft); |
| 554 | } |
| 555 | |
Nathan Scott | 754002b | 2005-06-21 15:49:06 +1000 | [diff] [blame] | 556 | /* |
| 557 | * Update warnings counter(s) if requested |
| 558 | */ |
| 559 | if (newlim->d_fieldmask & FS_DQ_BWARNS) |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 560 | ddq->d_bwarns = cpu_to_be16(newlim->d_bwarns); |
Nathan Scott | 754002b | 2005-06-21 15:49:06 +1000 | [diff] [blame] | 561 | if (newlim->d_fieldmask & FS_DQ_IWARNS) |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 562 | ddq->d_iwarns = cpu_to_be16(newlim->d_iwarns); |
Nathan Scott | 754002b | 2005-06-21 15:49:06 +1000 | [diff] [blame] | 563 | if (newlim->d_fieldmask & FS_DQ_RTBWARNS) |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 564 | ddq->d_rtbwarns = cpu_to_be16(newlim->d_rtbwarns); |
Nathan Scott | 754002b | 2005-06-21 15:49:06 +1000 | [diff] [blame] | 565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | if (id == 0) { |
| 567 | /* |
| 568 | * Timelimits for the super user set the relative time |
| 569 | * the other users can be over quota for this file system. |
| 570 | * If it is zero a default is used. Ditto for the default |
Nathan Scott | 754002b | 2005-06-21 15:49:06 +1000 | [diff] [blame] | 571 | * soft and hard limit values (already done, above), and |
| 572 | * for warnings. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | */ |
| 574 | if (newlim->d_fieldmask & FS_DQ_BTIMER) { |
| 575 | mp->m_quotainfo->qi_btimelimit = newlim->d_btimer; |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 576 | ddq->d_btimer = cpu_to_be32(newlim->d_btimer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | } |
| 578 | if (newlim->d_fieldmask & FS_DQ_ITIMER) { |
| 579 | mp->m_quotainfo->qi_itimelimit = newlim->d_itimer; |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 580 | ddq->d_itimer = cpu_to_be32(newlim->d_itimer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | } |
| 582 | if (newlim->d_fieldmask & FS_DQ_RTBTIMER) { |
| 583 | mp->m_quotainfo->qi_rtbtimelimit = newlim->d_rtbtimer; |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 584 | ddq->d_rtbtimer = cpu_to_be32(newlim->d_rtbtimer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | } |
Nathan Scott | 754002b | 2005-06-21 15:49:06 +1000 | [diff] [blame] | 586 | if (newlim->d_fieldmask & FS_DQ_BWARNS) |
| 587 | mp->m_quotainfo->qi_bwarnlimit = newlim->d_bwarns; |
| 588 | if (newlim->d_fieldmask & FS_DQ_IWARNS) |
| 589 | mp->m_quotainfo->qi_iwarnlimit = newlim->d_iwarns; |
| 590 | if (newlim->d_fieldmask & FS_DQ_RTBWARNS) |
| 591 | mp->m_quotainfo->qi_rtbwarnlimit = newlim->d_rtbwarns; |
| 592 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | /* |
| 594 | * If the user is now over quota, start the timelimit. |
| 595 | * The user will not be 'warned'. |
| 596 | * Note that we keep the timers ticking, whether enforcement |
| 597 | * is on or off. We don't really want to bother with iterating |
| 598 | * over all ondisk dquots and turning the timers on/off. |
| 599 | */ |
| 600 | xfs_qm_adjust_dqtimers(mp, ddq); |
| 601 | } |
| 602 | dqp->dq_flags |= XFS_DQ_DIRTY; |
| 603 | xfs_trans_log_dquot(tp, dqp); |
| 604 | |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 605 | error = xfs_trans_commit(tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | xfs_qm_dqprint(dqp); |
| 607 | xfs_qm_dqrele(dqp); |
| 608 | mutex_unlock(&(XFS_QI_QOFFLOCK(mp))); |
| 609 | |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 610 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Christoph Hellwig | fcafb71 | 2009-02-09 08:47:34 +0100 | [diff] [blame] | 613 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | xfs_qm_scall_getquota( |
| 615 | xfs_mount_t *mp, |
| 616 | xfs_dqid_t id, |
| 617 | uint type, |
| 618 | fs_disk_quota_t *out) |
| 619 | { |
| 620 | xfs_dquot_t *dqp; |
| 621 | int error; |
| 622 | |
| 623 | /* |
| 624 | * Try to get the dquot. We don't want it allocated on disk, so |
| 625 | * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't |
| 626 | * exist, we'll get ENOENT back. |
| 627 | */ |
| 628 | if ((error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp))) { |
| 629 | return (error); |
| 630 | } |
| 631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | /* |
| 633 | * If everything's NULL, this dquot doesn't quite exist as far as |
| 634 | * our utility programs are concerned. |
| 635 | */ |
| 636 | if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) { |
| 637 | xfs_qm_dqput(dqp); |
| 638 | return XFS_ERROR(ENOENT); |
| 639 | } |
| 640 | /* xfs_qm_dqprint(dqp); */ |
| 641 | /* |
| 642 | * Convert the disk dquot to the exportable format |
| 643 | */ |
| 644 | xfs_qm_export_dquot(mp, &dqp->q_core, out); |
| 645 | xfs_qm_dqput(dqp); |
| 646 | return (error ? XFS_ERROR(EFAULT) : 0); |
| 647 | } |
| 648 | |
| 649 | |
| 650 | STATIC int |
| 651 | xfs_qm_log_quotaoff_end( |
| 652 | xfs_mount_t *mp, |
| 653 | xfs_qoff_logitem_t *startqoff, |
| 654 | uint flags) |
| 655 | { |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 656 | xfs_trans_t *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | int error; |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 658 | xfs_qoff_logitem_t *qoffi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
| 660 | tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END); |
| 661 | |
| 662 | if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_qoff_logitem_t) * 2, |
| 663 | 0, 0, XFS_DEFAULT_LOG_COUNT))) { |
| 664 | xfs_trans_cancel(tp, 0); |
| 665 | return (error); |
| 666 | } |
| 667 | |
| 668 | qoffi = xfs_trans_get_qoff_item(tp, startqoff, |
| 669 | flags & XFS_ALL_QUOTA_ACCT); |
| 670 | xfs_trans_log_quotaoff_item(tp, qoffi); |
| 671 | |
| 672 | /* |
| 673 | * We have to make sure that the transaction is secure on disk before we |
| 674 | * return and actually stop quota accounting. So, make it synchronous. |
| 675 | * We don't care about quotoff's performance. |
| 676 | */ |
| 677 | xfs_trans_set_sync(tp); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 678 | error = xfs_trans_commit(tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | return (error); |
| 680 | } |
| 681 | |
| 682 | |
| 683 | STATIC int |
| 684 | xfs_qm_log_quotaoff( |
| 685 | xfs_mount_t *mp, |
| 686 | xfs_qoff_logitem_t **qoffstartp, |
| 687 | uint flags) |
| 688 | { |
| 689 | xfs_trans_t *tp; |
| 690 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | xfs_qoff_logitem_t *qoffi=NULL; |
| 692 | uint oldsbqflag=0; |
| 693 | |
| 694 | tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF); |
| 695 | if ((error = xfs_trans_reserve(tp, 0, |
| 696 | sizeof(xfs_qoff_logitem_t) * 2 + |
| 697 | mp->m_sb.sb_sectsize + 128, |
| 698 | 0, |
| 699 | 0, |
| 700 | XFS_DEFAULT_LOG_COUNT))) { |
| 701 | goto error0; |
| 702 | } |
| 703 | |
| 704 | qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT); |
| 705 | xfs_trans_log_quotaoff_item(tp, qoffi); |
| 706 | |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 707 | spin_lock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | oldsbqflag = mp->m_sb.sb_qflags; |
| 709 | mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL; |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 710 | spin_unlock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
| 712 | xfs_mod_sb(tp, XFS_SB_QFLAGS); |
| 713 | |
| 714 | /* |
| 715 | * We have to make sure that the transaction is secure on disk before we |
| 716 | * return and actually stop quota accounting. So, make it synchronous. |
| 717 | * We don't care about quotoff's performance. |
| 718 | */ |
| 719 | xfs_trans_set_sync(tp); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 720 | error = xfs_trans_commit(tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | |
| 722 | error0: |
| 723 | if (error) { |
| 724 | xfs_trans_cancel(tp, 0); |
| 725 | /* |
| 726 | * No one else is modifying sb_qflags, so this is OK. |
| 727 | * We still hold the quotaofflock. |
| 728 | */ |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 729 | spin_lock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | mp->m_sb.sb_qflags = oldsbqflag; |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 731 | spin_unlock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | } |
| 733 | *qoffstartp = qoffi; |
| 734 | return (error); |
| 735 | } |
| 736 | |
| 737 | |
| 738 | /* |
| 739 | * Translate an internal style on-disk-dquot to the exportable format. |
| 740 | * The main differences are that the counters/limits are all in Basic |
| 741 | * Blocks (BBs) instead of the internal FSBs, and all on-disk data has |
| 742 | * to be converted to the native endianness. |
| 743 | */ |
| 744 | STATIC void |
| 745 | xfs_qm_export_dquot( |
| 746 | xfs_mount_t *mp, |
| 747 | xfs_disk_dquot_t *src, |
| 748 | struct fs_disk_quota *dst) |
| 749 | { |
| 750 | memset(dst, 0, sizeof(*dst)); |
| 751 | dst->d_version = FS_DQUOT_VERSION; /* different from src->d_version */ |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 752 | dst->d_flags = xfs_qm_export_qtype_flags(src->d_flags); |
| 753 | dst->d_id = be32_to_cpu(src->d_id); |
| 754 | dst->d_blk_hardlimit = |
| 755 | XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_hardlimit)); |
| 756 | dst->d_blk_softlimit = |
| 757 | XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_softlimit)); |
| 758 | dst->d_ino_hardlimit = be64_to_cpu(src->d_ino_hardlimit); |
| 759 | dst->d_ino_softlimit = be64_to_cpu(src->d_ino_softlimit); |
| 760 | dst->d_bcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_bcount)); |
| 761 | dst->d_icount = be64_to_cpu(src->d_icount); |
| 762 | dst->d_btimer = be32_to_cpu(src->d_btimer); |
| 763 | dst->d_itimer = be32_to_cpu(src->d_itimer); |
| 764 | dst->d_iwarns = be16_to_cpu(src->d_iwarns); |
| 765 | dst->d_bwarns = be16_to_cpu(src->d_bwarns); |
| 766 | dst->d_rtb_hardlimit = |
| 767 | XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_hardlimit)); |
| 768 | dst->d_rtb_softlimit = |
| 769 | XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_softlimit)); |
| 770 | dst->d_rtbcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtbcount)); |
| 771 | dst->d_rtbtimer = be32_to_cpu(src->d_rtbtimer); |
| 772 | dst->d_rtbwarns = be16_to_cpu(src->d_rtbwarns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | |
| 774 | /* |
| 775 | * Internally, we don't reset all the timers when quota enforcement |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 776 | * gets turned off. No need to confuse the user level code, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | * so return zeroes in that case. |
| 778 | */ |
Kouta Ooizumi | e6d2942 | 2007-05-08 13:49:33 +1000 | [diff] [blame] | 779 | if ((!XFS_IS_UQUOTA_ENFORCED(mp) && src->d_flags == XFS_DQ_USER) || |
| 780 | (!XFS_IS_OQUOTA_ENFORCED(mp) && |
| 781 | (src->d_flags & (XFS_DQ_PROJ | XFS_DQ_GROUP)))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | dst->d_btimer = 0; |
| 783 | dst->d_itimer = 0; |
| 784 | dst->d_rtbtimer = 0; |
| 785 | } |
| 786 | |
| 787 | #ifdef DEBUG |
Kouta Ooizumi | e6d2942 | 2007-05-08 13:49:33 +1000 | [diff] [blame] | 788 | if (((XFS_IS_UQUOTA_ENFORCED(mp) && dst->d_flags == XFS_USER_QUOTA) || |
| 789 | (XFS_IS_OQUOTA_ENFORCED(mp) && |
| 790 | (dst->d_flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)))) && |
| 791 | dst->d_id != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | if (((int) dst->d_bcount >= (int) dst->d_blk_softlimit) && |
| 793 | (dst->d_blk_softlimit > 0)) { |
| 794 | ASSERT(dst->d_btimer != 0); |
| 795 | } |
| 796 | if (((int) dst->d_icount >= (int) dst->d_ino_softlimit) && |
| 797 | (dst->d_ino_softlimit > 0)) { |
| 798 | ASSERT(dst->d_itimer != 0); |
| 799 | } |
| 800 | } |
| 801 | #endif |
| 802 | } |
| 803 | |
| 804 | STATIC uint |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | xfs_qm_export_qtype_flags( |
| 806 | uint flags) |
| 807 | { |
| 808 | /* |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 809 | * Can't be more than one, or none. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | */ |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 811 | ASSERT((flags & (XFS_PROJ_QUOTA | XFS_USER_QUOTA)) != |
| 812 | (XFS_PROJ_QUOTA | XFS_USER_QUOTA)); |
| 813 | ASSERT((flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)) != |
| 814 | (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)); |
| 815 | ASSERT((flags & (XFS_USER_QUOTA | XFS_GROUP_QUOTA)) != |
| 816 | (XFS_USER_QUOTA | XFS_GROUP_QUOTA)); |
| 817 | ASSERT((flags & (XFS_PROJ_QUOTA|XFS_USER_QUOTA|XFS_GROUP_QUOTA)) != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | |
| 819 | return (flags & XFS_DQ_USER) ? |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 820 | XFS_USER_QUOTA : (flags & XFS_DQ_PROJ) ? |
| 821 | XFS_PROJ_QUOTA : XFS_GROUP_QUOTA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | STATIC uint |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | xfs_qm_export_flags( |
| 826 | uint flags) |
| 827 | { |
| 828 | uint uflags; |
| 829 | |
| 830 | uflags = 0; |
| 831 | if (flags & XFS_UQUOTA_ACCT) |
| 832 | uflags |= XFS_QUOTA_UDQ_ACCT; |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 833 | if (flags & XFS_PQUOTA_ACCT) |
| 834 | uflags |= XFS_QUOTA_PDQ_ACCT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | if (flags & XFS_GQUOTA_ACCT) |
| 836 | uflags |= XFS_QUOTA_GDQ_ACCT; |
| 837 | if (flags & XFS_UQUOTA_ENFD) |
| 838 | uflags |= XFS_QUOTA_UDQ_ENFD; |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 839 | if (flags & (XFS_OQUOTA_ENFD)) { |
| 840 | uflags |= (flags & XFS_GQUOTA_ACCT) ? |
| 841 | XFS_QUOTA_GDQ_ENFD : XFS_QUOTA_PDQ_ENFD; |
| 842 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | return (uflags); |
| 844 | } |
| 845 | |
| 846 | |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 847 | STATIC int |
| 848 | xfs_dqrele_inode( |
| 849 | struct xfs_inode *ip, |
| 850 | struct xfs_perag *pag, |
| 851 | int flags) |
David Chinner | 5b4d89a | 2008-10-30 17:08:03 +1100 | [diff] [blame] | 852 | { |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 853 | int error; |
David Chinner | 5b4d89a | 2008-10-30 17:08:03 +1100 | [diff] [blame] | 854 | |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 855 | /* skip quota inodes */ |
| 856 | if (ip == XFS_QI_UQIP(ip->i_mount) || ip == XFS_QI_GQIP(ip->i_mount)) { |
| 857 | ASSERT(ip->i_udquot == NULL); |
| 858 | ASSERT(ip->i_gdquot == NULL); |
Dave Chinner | cb4f0d1 | 2008-11-10 17:11:18 +1100 | [diff] [blame] | 859 | read_unlock(&pag->pag_ici_lock); |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 860 | return 0; |
| 861 | } |
Dave Chinner | cb4f0d1 | 2008-11-10 17:11:18 +1100 | [diff] [blame] | 862 | |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 863 | error = xfs_sync_inode_valid(ip, pag); |
| 864 | if (error) |
| 865 | return error; |
Dave Chinner | 6307091 | 2008-11-10 17:13:23 +1100 | [diff] [blame] | 866 | |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 867 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 868 | if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) { |
| 869 | xfs_qm_dqrele(ip->i_udquot); |
| 870 | ip->i_udquot = NULL; |
| 871 | } |
| 872 | if (flags & (XFS_PQUOTA_ACCT|XFS_GQUOTA_ACCT) && ip->i_gdquot) { |
| 873 | xfs_qm_dqrele(ip->i_gdquot); |
| 874 | ip->i_gdquot = NULL; |
| 875 | } |
| 876 | xfs_iput(ip, XFS_ILOCK_EXCL); |
Dave Chinner | cb4f0d1 | 2008-11-10 17:11:18 +1100 | [diff] [blame] | 877 | |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 878 | return 0; |
David Chinner | 5b4d89a | 2008-10-30 17:08:03 +1100 | [diff] [blame] | 879 | } |
| 880 | |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 881 | |
David Chinner | 5b4d89a | 2008-10-30 17:08:03 +1100 | [diff] [blame] | 882 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | * Go thru all the inodes in the file system, releasing their dquots. |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 884 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | * 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] | 886 | * AFTER this, in the case of quotaoff. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | */ |
| 888 | void |
| 889 | xfs_qm_dqrele_all_inodes( |
| 890 | struct xfs_mount *mp, |
| 891 | uint flags) |
| 892 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | ASSERT(mp->m_quotainfo); |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame^] | 894 | xfs_inode_ag_iterator(mp, xfs_dqrele_inode, flags, |
| 895 | XFS_ICI_NO_TAG, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | /*------------------------------------------------------------------------*/ |
| 899 | #ifdef DEBUG |
| 900 | /* |
| 901 | * This contains all the test functions for XFS disk quotas. |
| 902 | * Currently it does a quota accounting check. ie. it walks through |
| 903 | * all inodes in the file system, calculating the dquot accounting fields, |
| 904 | * and prints out any inconsistencies. |
| 905 | */ |
| 906 | xfs_dqhash_t *qmtest_udqtab; |
| 907 | xfs_dqhash_t *qmtest_gdqtab; |
| 908 | int qmtest_hashmask; |
| 909 | int qmtest_nfails; |
Christoph Hellwig | a0b0b8a | 2009-03-29 09:51:00 +0200 | [diff] [blame] | 910 | struct mutex qcheck_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | |
| 912 | #define DQTEST_HASHVAL(mp, id) (((__psunsigned_t)(mp) + \ |
| 913 | (__psunsigned_t)(id)) & \ |
| 914 | (qmtest_hashmask - 1)) |
| 915 | |
| 916 | #define DQTEST_HASH(mp, id, type) ((type & XFS_DQ_USER) ? \ |
| 917 | (qmtest_udqtab + \ |
| 918 | DQTEST_HASHVAL(mp, id)) : \ |
| 919 | (qmtest_gdqtab + \ |
| 920 | DQTEST_HASHVAL(mp, id))) |
| 921 | |
| 922 | #define DQTEST_LIST_PRINT(l, NXT, title) \ |
| 923 | { \ |
| 924 | xfs_dqtest_t *dqp; int i = 0;\ |
| 925 | cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \ |
| 926 | for (dqp = (xfs_dqtest_t *)(l)->qh_next; dqp != NULL; \ |
| 927 | dqp = (xfs_dqtest_t *)dqp->NXT) { \ |
| 928 | cmn_err(CE_DEBUG, " %d. \"%d (%s)\" bcnt = %d, icnt = %d", \ |
| 929 | ++i, dqp->d_id, DQFLAGTO_TYPESTR(dqp), \ |
| 930 | dqp->d_bcount, dqp->d_icount); } \ |
| 931 | } |
| 932 | |
| 933 | typedef struct dqtest { |
| 934 | xfs_dqmarker_t q_lists; |
| 935 | xfs_dqhash_t *q_hash; /* the hashchain header */ |
| 936 | xfs_mount_t *q_mount; /* filesystem this relates to */ |
| 937 | xfs_dqid_t d_id; /* user id or group id */ |
| 938 | xfs_qcnt_t d_bcount; /* # disk blocks owned by the user */ |
| 939 | xfs_qcnt_t d_icount; /* # inodes owned by the user */ |
| 940 | } xfs_dqtest_t; |
| 941 | |
| 942 | STATIC void |
| 943 | xfs_qm_hashinsert(xfs_dqhash_t *h, xfs_dqtest_t *dqp) |
| 944 | { |
| 945 | xfs_dquot_t *d; |
| 946 | if (((d) = (h)->qh_next)) |
| 947 | (d)->HL_PREVP = &((dqp)->HL_NEXT); |
| 948 | (dqp)->HL_NEXT = d; |
| 949 | (dqp)->HL_PREVP = &((h)->qh_next); |
| 950 | (h)->qh_next = (xfs_dquot_t *)dqp; |
| 951 | (h)->qh_version++; |
| 952 | (h)->qh_nelems++; |
| 953 | } |
| 954 | STATIC void |
| 955 | xfs_qm_dqtest_print( |
| 956 | xfs_dqtest_t *d) |
| 957 | { |
| 958 | cmn_err(CE_DEBUG, "-----------DQTEST DQUOT----------------"); |
| 959 | cmn_err(CE_DEBUG, "---- dquot ID = %d", d->d_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | cmn_err(CE_DEBUG, "---- fs = 0x%p", d->q_mount); |
| 961 | cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)", |
| 962 | d->d_bcount, (int)d->d_bcount); |
| 963 | cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)", |
| 964 | d->d_icount, (int)d->d_icount); |
| 965 | cmn_err(CE_DEBUG, "---------------------------"); |
| 966 | } |
| 967 | |
| 968 | STATIC void |
| 969 | xfs_qm_dqtest_failed( |
| 970 | xfs_dqtest_t *d, |
| 971 | xfs_dquot_t *dqp, |
| 972 | char *reason, |
| 973 | xfs_qcnt_t a, |
| 974 | xfs_qcnt_t b, |
| 975 | int error) |
| 976 | { |
| 977 | qmtest_nfails++; |
| 978 | if (error) |
| 979 | cmn_err(CE_DEBUG, "quotacheck failed id=%d, err=%d\nreason: %s", |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 980 | d->d_id, error, reason); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | else |
| 982 | cmn_err(CE_DEBUG, "quotacheck failed id=%d (%s) [%d != %d]", |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 983 | d->d_id, reason, (int)a, (int)b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | xfs_qm_dqtest_print(d); |
| 985 | if (dqp) |
| 986 | xfs_qm_dqprint(dqp); |
| 987 | } |
| 988 | |
| 989 | STATIC int |
| 990 | xfs_dqtest_cmp2( |
| 991 | xfs_dqtest_t *d, |
| 992 | xfs_dquot_t *dqp) |
| 993 | { |
| 994 | int err = 0; |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 995 | if (be64_to_cpu(dqp->q_core.d_icount) != d->d_icount) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | xfs_qm_dqtest_failed(d, dqp, "icount mismatch", |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 997 | be64_to_cpu(dqp->q_core.d_icount), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | d->d_icount, 0); |
| 999 | err++; |
| 1000 | } |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 1001 | if (be64_to_cpu(dqp->q_core.d_bcount) != d->d_bcount) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | xfs_qm_dqtest_failed(d, dqp, "bcount mismatch", |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 1003 | be64_to_cpu(dqp->q_core.d_bcount), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | d->d_bcount, 0); |
| 1005 | err++; |
| 1006 | } |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 1007 | if (dqp->q_core.d_blk_softlimit && |
| 1008 | be64_to_cpu(dqp->q_core.d_bcount) >= |
| 1009 | be64_to_cpu(dqp->q_core.d_blk_softlimit)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | if (!dqp->q_core.d_btimer && dqp->q_core.d_id) { |
| 1011 | cmn_err(CE_DEBUG, |
| 1012 | "%d [%s] [0x%p] BLK TIMER NOT STARTED", |
| 1013 | d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount); |
| 1014 | err++; |
| 1015 | } |
| 1016 | } |
Christoph Hellwig | 1149d96 | 2005-11-02 15:01:12 +1100 | [diff] [blame] | 1017 | if (dqp->q_core.d_ino_softlimit && |
| 1018 | be64_to_cpu(dqp->q_core.d_icount) >= |
| 1019 | be64_to_cpu(dqp->q_core.d_ino_softlimit)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | if (!dqp->q_core.d_itimer && dqp->q_core.d_id) { |
| 1021 | cmn_err(CE_DEBUG, |
| 1022 | "%d [%s] [0x%p] INO TIMER NOT STARTED", |
| 1023 | d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount); |
| 1024 | err++; |
| 1025 | } |
| 1026 | } |
| 1027 | #ifdef QUOTADEBUG |
| 1028 | if (!err) { |
| 1029 | cmn_err(CE_DEBUG, "%d [%s] [0x%p] qchecked", |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1030 | d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | } |
| 1032 | #endif |
| 1033 | return (err); |
| 1034 | } |
| 1035 | |
| 1036 | STATIC void |
| 1037 | xfs_dqtest_cmp( |
| 1038 | xfs_dqtest_t *d) |
| 1039 | { |
| 1040 | xfs_dquot_t *dqp; |
| 1041 | int error; |
| 1042 | |
| 1043 | /* xfs_qm_dqtest_print(d); */ |
| 1044 | if ((error = xfs_qm_dqget(d->q_mount, NULL, d->d_id, d->dq_flags, 0, |
| 1045 | &dqp))) { |
| 1046 | xfs_qm_dqtest_failed(d, NULL, "dqget failed", 0, 0, error); |
| 1047 | return; |
| 1048 | } |
| 1049 | xfs_dqtest_cmp2(d, dqp); |
| 1050 | xfs_qm_dqput(dqp); |
| 1051 | } |
| 1052 | |
| 1053 | STATIC int |
| 1054 | xfs_qm_internalqcheck_dqget( |
| 1055 | xfs_mount_t *mp, |
| 1056 | xfs_dqid_t id, |
| 1057 | uint type, |
| 1058 | xfs_dqtest_t **O_dq) |
| 1059 | { |
| 1060 | xfs_dqtest_t *d; |
| 1061 | xfs_dqhash_t *h; |
| 1062 | |
| 1063 | h = DQTEST_HASH(mp, id, type); |
| 1064 | for (d = (xfs_dqtest_t *) h->qh_next; d != NULL; |
| 1065 | d = (xfs_dqtest_t *) d->HL_NEXT) { |
| 1066 | /* DQTEST_LIST_PRINT(h, HL_NEXT, "@@@@@ dqtestlist @@@@@"); */ |
| 1067 | if (d->d_id == id && mp == d->q_mount) { |
| 1068 | *O_dq = d; |
| 1069 | return (0); |
| 1070 | } |
| 1071 | } |
| 1072 | d = kmem_zalloc(sizeof(xfs_dqtest_t), KM_SLEEP); |
| 1073 | d->dq_flags = type; |
| 1074 | d->d_id = id; |
| 1075 | d->q_mount = mp; |
| 1076 | d->q_hash = h; |
| 1077 | xfs_qm_hashinsert(h, d); |
| 1078 | *O_dq = d; |
| 1079 | return (0); |
| 1080 | } |
| 1081 | |
| 1082 | STATIC void |
| 1083 | xfs_qm_internalqcheck_get_dquots( |
| 1084 | xfs_mount_t *mp, |
| 1085 | xfs_dqid_t uid, |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1086 | xfs_dqid_t projid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | xfs_dqid_t gid, |
| 1088 | xfs_dqtest_t **ud, |
| 1089 | xfs_dqtest_t **gd) |
| 1090 | { |
| 1091 | if (XFS_IS_UQUOTA_ON(mp)) |
| 1092 | xfs_qm_internalqcheck_dqget(mp, uid, XFS_DQ_USER, ud); |
| 1093 | if (XFS_IS_GQUOTA_ON(mp)) |
| 1094 | xfs_qm_internalqcheck_dqget(mp, gid, XFS_DQ_GROUP, gd); |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1095 | else if (XFS_IS_PQUOTA_ON(mp)) |
| 1096 | xfs_qm_internalqcheck_dqget(mp, projid, XFS_DQ_PROJ, gd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | |
| 1100 | STATIC void |
| 1101 | xfs_qm_internalqcheck_dqadjust( |
| 1102 | xfs_inode_t *ip, |
| 1103 | xfs_dqtest_t *d) |
| 1104 | { |
| 1105 | d->d_icount++; |
| 1106 | d->d_bcount += (xfs_qcnt_t)ip->i_d.di_nblocks; |
| 1107 | } |
| 1108 | |
| 1109 | STATIC int |
| 1110 | xfs_qm_internalqcheck_adjust( |
| 1111 | xfs_mount_t *mp, /* mount point for filesystem */ |
| 1112 | xfs_ino_t ino, /* inode number to get data for */ |
| 1113 | void __user *buffer, /* not used */ |
| 1114 | int ubsize, /* not used */ |
| 1115 | void *private_data, /* not used */ |
| 1116 | xfs_daddr_t bno, /* starting block of inode cluster */ |
| 1117 | int *ubused, /* not used */ |
| 1118 | void *dip, /* not used */ |
| 1119 | int *res) /* bulkstat result code */ |
| 1120 | { |
| 1121 | xfs_inode_t *ip; |
| 1122 | xfs_dqtest_t *ud, *gd; |
| 1123 | uint lock_flags; |
| 1124 | boolean_t ipreleased; |
| 1125 | int error; |
| 1126 | |
| 1127 | ASSERT(XFS_IS_QUOTA_RUNNING(mp)); |
| 1128 | |
| 1129 | if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) { |
| 1130 | *res = BULKSTAT_RV_NOTHING; |
| 1131 | qdprintk("internalqcheck: ino=%llu, uqino=%llu, gqino=%llu\n", |
| 1132 | (unsigned long long) ino, |
| 1133 | (unsigned long long) mp->m_sb.sb_uquotino, |
| 1134 | (unsigned long long) mp->m_sb.sb_gquotino); |
| 1135 | return XFS_ERROR(EINVAL); |
| 1136 | } |
| 1137 | ipreleased = B_FALSE; |
| 1138 | again: |
| 1139 | lock_flags = XFS_ILOCK_SHARED; |
| 1140 | if ((error = xfs_iget(mp, NULL, ino, 0, lock_flags, &ip, bno))) { |
| 1141 | *res = BULKSTAT_RV_NOTHING; |
| 1142 | return (error); |
| 1143 | } |
| 1144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | /* |
| 1146 | * This inode can have blocks after eof which can get released |
| 1147 | * when we send it to inactive. Since we don't check the dquot |
| 1148 | * until the after all our calculations are done, we must get rid |
| 1149 | * of those now. |
| 1150 | */ |
| 1151 | if (! ipreleased) { |
| 1152 | xfs_iput(ip, lock_flags); |
| 1153 | ipreleased = B_TRUE; |
| 1154 | goto again; |
| 1155 | } |
| 1156 | xfs_qm_internalqcheck_get_dquots(mp, |
| 1157 | (xfs_dqid_t) ip->i_d.di_uid, |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1158 | (xfs_dqid_t) ip->i_d.di_projid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | (xfs_dqid_t) ip->i_d.di_gid, |
| 1160 | &ud, &gd); |
| 1161 | if (XFS_IS_UQUOTA_ON(mp)) { |
| 1162 | ASSERT(ud); |
| 1163 | xfs_qm_internalqcheck_dqadjust(ip, ud); |
| 1164 | } |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 1165 | if (XFS_IS_OQUOTA_ON(mp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | ASSERT(gd); |
| 1167 | xfs_qm_internalqcheck_dqadjust(ip, gd); |
| 1168 | } |
| 1169 | xfs_iput(ip, lock_flags); |
| 1170 | *res = BULKSTAT_RV_DIDONE; |
| 1171 | return (0); |
| 1172 | } |
| 1173 | |
| 1174 | |
| 1175 | /* PRIVATE, debugging */ |
| 1176 | int |
| 1177 | xfs_qm_internalqcheck( |
| 1178 | xfs_mount_t *mp) |
| 1179 | { |
| 1180 | xfs_ino_t lastino; |
| 1181 | int done, count; |
| 1182 | int i; |
| 1183 | xfs_dqtest_t *d, *e; |
| 1184 | xfs_dqhash_t *h1; |
| 1185 | int error; |
| 1186 | |
| 1187 | lastino = 0; |
| 1188 | qmtest_hashmask = 32; |
| 1189 | count = 5; |
| 1190 | done = 0; |
| 1191 | qmtest_nfails = 0; |
| 1192 | |
| 1193 | if (! XFS_IS_QUOTA_ON(mp)) |
| 1194 | return XFS_ERROR(ESRCH); |
| 1195 | |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 1196 | xfs_log_force(mp, XFS_LOG_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | XFS_bflush(mp->m_ddev_targp); |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 1198 | xfs_log_force(mp, XFS_LOG_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | XFS_bflush(mp->m_ddev_targp); |
| 1200 | |
Jes Sorensen | 794ee1b | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 1201 | mutex_lock(&qcheck_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | /* There should be absolutely no quota activity while this |
| 1203 | is going on. */ |
| 1204 | qmtest_udqtab = kmem_zalloc(qmtest_hashmask * |
| 1205 | sizeof(xfs_dqhash_t), KM_SLEEP); |
| 1206 | qmtest_gdqtab = kmem_zalloc(qmtest_hashmask * |
| 1207 | sizeof(xfs_dqhash_t), KM_SLEEP); |
| 1208 | do { |
| 1209 | /* |
| 1210 | * Iterate thru all the inodes in the file system, |
| 1211 | * adjusting the corresponding dquot counters |
| 1212 | */ |
| 1213 | if ((error = xfs_bulkstat(mp, &lastino, &count, |
| 1214 | xfs_qm_internalqcheck_adjust, NULL, |
| 1215 | 0, NULL, BULKSTAT_FG_IGET, &done))) { |
| 1216 | break; |
| 1217 | } |
| 1218 | } while (! done); |
| 1219 | if (error) { |
| 1220 | cmn_err(CE_DEBUG, "Bulkstat returned error 0x%x", error); |
| 1221 | } |
| 1222 | cmn_err(CE_DEBUG, "Checking results against system dquots"); |
| 1223 | for (i = 0; i < qmtest_hashmask; i++) { |
| 1224 | h1 = &qmtest_udqtab[i]; |
| 1225 | for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) { |
| 1226 | xfs_dqtest_cmp(d); |
| 1227 | e = (xfs_dqtest_t *) d->HL_NEXT; |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1228 | kmem_free(d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | d = e; |
| 1230 | } |
| 1231 | h1 = &qmtest_gdqtab[i]; |
| 1232 | for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) { |
| 1233 | xfs_dqtest_cmp(d); |
| 1234 | e = (xfs_dqtest_t *) d->HL_NEXT; |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1235 | kmem_free(d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | d = e; |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | if (qmtest_nfails) { |
| 1241 | cmn_err(CE_DEBUG, "******** quotacheck failed ********"); |
| 1242 | cmn_err(CE_DEBUG, "failures = %d", qmtest_nfails); |
| 1243 | } else { |
| 1244 | cmn_err(CE_DEBUG, "******** quotacheck successful! ********"); |
| 1245 | } |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1246 | kmem_free(qmtest_udqtab); |
| 1247 | kmem_free(qmtest_gdqtab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | mutex_unlock(&qcheck_lock); |
| 1249 | return (qmtest_nfails); |
| 1250 | } |
| 1251 | |
| 1252 | #endif /* DEBUG */ |